RGB to HEX
What is RGB?
RGB (red, green, blue) is an additive color model used in digital displays, where colors are created by combining different intensities of red, green, and blue light. Each color channel typically ranges from 0 to 255, allowing for over 16 million possible colors. Example: RGB(99, 91, 255).
What is HEX?
HEX is a hexadecimal notation system for representing RGB colors in web design and digital applications. It consists of a "#" symbol followed by six characters, where each pair of characters represents the intensity of red, green, and blue respectively, using values from 00 to FF. Example hex: #635BFF.
How to convert RGB to HEX
RGB and HEX color codes are virtually the same thing: it’s the same logic, but different encoding. The RGB color model represents colors as a combination of red, green, and blue values, each ranging from 0 to 255.
For example, RGB(255, 0, 0) is a red color (we have max red, no green, no blue), while RGB (255, 0, 255) would be purple max red, no green, no blue).
On the other hand, the HEX format represents the same logic using a six-digit alphanumeric code, where “00” (double zero) means no color, and “FF” equals 255 in RGB.
Here are a few examples:
Color | RGB | HEX |
---|---|---|
White | RGB(255,255,255) | #FFFFFF |
Black | RGB(0,0,0) | #000000 |
Red | RGB(255,0,0) | #FF0000 |
Green | RGB(0,255,0) | #00FF00 |
Blue | RGB(0,0,255) | #0000FF |
Yellow | RGB(255,255,0) | #FFFF00 |
Magenta | RGB(255,0,255) | #FF00FF |
Cyan | RGB(0,255,255) | #00FFFF |
Turquoise | RGB(64,224,208) | #40E0D0 |
Orange | RGB(255,165,0) | #FFA500 |
Purple | RGB(128,0,128) | #800080 |
Gray | RGB(128,128,128) | #808080 |
RGB to HEX formula
To convert an RGB color to its HEX equivalent, we follow a series of mathematical steps. Let’s break down this process using the example color RGB(255, 128, 64).
Step 1: Divide by 16
The first step involves dividing each RGB component by 16. This gives us:
- Red: 255 ÷ 16 = 15.9375
- Green: 128 ÷ 16 = 8
- Blue: 64 ÷ 16 = 4
Step 2: Extract the first HEX digit
We take the integer part of each result as the first digit of the corresponding HEX pair. In our example:
- Red: 15 -> F
- Green: 8
- Blue: 4
Since hexadecimal digits range from 0 to 9 and A to F (where A=10, B=11, C=12, D=13, E=14, F=15), the value 15 translates to ‘F’. Therefore, the HEX pair for the red component is ‘FF’.
Step 3: Calculating the second HEX digit
To find the second digit, we multiply the fractional part of our division results by 16:
- Red: 0.9375 × 16 = 15 -> F
- Green: 0 × 16 = 0
- Blue: 0 × 16 = 0
Step 4: Combine digits
We now have two digits for each color component. Let’s put them together:
- Red: 15 becomes FF
- Green: 80 remains 80
- Blue: 40 remains 40
Combining these pairs in the order of red, green, and blue gives us the final HEX code: #FF8040.
This conversion method works because of the relationship between the decimal system (base 10) used in RGB and the hexadecimal system (base 16) used in HEX. The maximum value in RGB (255) corresponds to FF in HEX, as 16² – 1 = 255. Each HEX digit represents a power of 16, allowing us to encode the full range of RGB values (0–255) using just two hexadecimal digits.