To convert RGB color to hex, change the red, green, and blue channel values from decimal numbers into two-digit hexadecimal values. Then place the three pairs after a number sign: #RRGGBB.
For example, RGB(255, 99, 71) becomes #FF6347. The process is consistent for every color because each RGB channel ranges from 0 to 255, while each hexadecimal pair ranges from 00 to FF.
RGB color to hex: read the #RRGGBB format
An RGB color contains three channels in this order:
- R: red
- G: green
- B: blue
Each channel uses a decimal value from 0 through 255. A hex color uses two hexadecimal digits for each channel:
#RR GG BB
The first pair controls red, the second controls green, and the third controls blue. Hexadecimal uses 16 symbols: 0–9 and A–F, where A equals 10, B equals 11, C equals 12, D equals 13, E equals 14, and F equals 15.
Every channel must occupy exactly two hex digits. Without a leading zero, a value such as decimal 5 would produce 5 instead of 05, shifting the remaining channel pairs and changing the color code.
Convert RGB to hexadecimal in two digits per channel
To perform an RGB to hexadecimal conversion, handle each channel separately:
- Divide the decimal channel value by 16.
- Use the whole-number result as the first hex digit.
- Use the remainder as the second hex digit.
- If the result has only one digit, add a leading zero.
For instance, convert decimal 99:
99 ÷ 16 = 6 remainder 3
The quotient 6 and remainder 3 produce 63 in hexadecimal. For decimal 255, the calculation is 15 remainder 15. Both 15s become F, so 255 converts to FF.
For values from 10 through 15, use A through F for the second digit. For example, decimal 12 becomes 0C: 12 has a quotient of 0 and a remainder of 12, which is C.
Worked RGB code to hex examples, including leading zeros
Example 1: RGB(255, 99, 71)
- Red: 255 → FF
- Green: 99 → 63
- Blue: 71 → 47
Join the pairs in RGB order: RGB(255, 99, 71) = #FF6347.
Example 2: RGB(12, 160, 5)
- Red: 12 → 0C
- Green: 160 → A0
- Blue: 5 → 05
The final code is RGB(12, 160, 5) = #0CA005. The red and blue channels need leading zeros because both values are below 16.
Check a hex color by reversing the conversion
To verify a result, split the hex code into its three pairs and convert each pair back to decimal. For #0CA005:
- 0C = (0 × 16) + 12 = 12
- A0 = (10 × 16) + 0 = 160
- 05 = (0 × 16) + 5 = 5
The reverse check returns RGB(12, 160, 5), confirming that the two-digit pairs and their order are correct.
