RGB to HEX Conversion Explained
What's actually being converted
A color on screen is three numbers — how much red, green and blue light to mix — each ranging from 0 to 255. RGB writes those three numbers in base 10, separated by commas: rgb(255, 87, 51). HEX writes the same three numbers in base 16, two digits each, glued together after a #: #FF5733. Nothing about the color changes between the two — it's a notation difference, the way '15' and 'F' are the same quantity in decimal and hexadecimal.
Converting one channel by hand
Take a single 0–255 value, say 87. Divide by 16: 87 ÷ 16 = 5 remainder 7. The quotient (5) becomes the first hex digit, the remainder (7) becomes the second, giving '57'. For remainders or quotients of 10 or above, hexadecimal uses letters: 10=A, 11=B, 12=C, 13=D, 14=E, 15=F. So 255 ÷ 16 = 15 remainder 15, giving 'FF' — the maximum value in either notation.
Repeat that for all three channels and concatenate them in order: red, then green, then blue. rgb(255, 87, 51) becomes FF (255), 57 (87), 33 (51) — #FF5733.
Reading a HEX code back into RGB
Split the six digits after the # into three pairs. Convert each pair from base 16 back to base 10: the first digit is multiplied by 16, the second is added directly. For '57': 5 × 16 + 7 = 87. Do the same for the other two pairs to recover the full rgb() triplet.
Frequently asked questions
Why do some HEX codes only have three digits, like #FFF?
Does HEX support transparency the way RGBA does?
Is one format better for CSS than the other?
Try the RGB to HEX Converter
Turn red, green and blue channel values into a HEX colour code you can paste into CSS.