2 min read
One of the most common ways to represent colors is with RGB where you have three (hexadecimal) numbers that represent the red, green, and blue values respectively. Here's an example using my brand color.
If you separate this out, you get three values of
54. This might seem odd to have letters represent a number, but that's how hexadecimal numbers work. Instead of using
1-10 digits it uses
1-f (1-9,a,b,c,d,e,f) as digits.
Alright, let's dive in.
Convert Decimal to Hexidecimal
If you have a decimal number that you want to convert to hexidecimal, you can call the
toString() function with the number and pass in a target base (binary, decimal, hexedecimal, etc.). This parameter is known as the
radix. Here's a few examples.
let num = 15; num.toString(16) //f num = 10 num.toString(16) //a num = 1 num.toString(16) //1
Convert Hexadecimal to Decimal
What about the reverse? If you're converting hexadecimal back to decimal you can use the
parseInt() function and pass the the original number (as a string) in the first parameter and the
radix in the second.
parseInt("f", 16) //15 parseInt("a", 16) //10 parseInt("1", 16) //1