How Do You Convert RGB to Hex (and Check the Result Is Right)?
Updated
Convert each of the three RGB numbers (0–255) into a two-digit hexadecimal value, then join them in R, G, B order as #RRGGBB. For example, rgb(255, 99, 71) becomes #FF6347. This works for any CSS or HTML color value, and you can verify it by converting the hex back to RGB.
The conversion rule
Each channel is a number from 0 to 255. Hexadecimal is base 16, so two hex digits represent exactly 0–255 (16 × 16 = 256 values). That's why every channel maps to exactly two characters.
The mapping:
| Decimal | Hex | Decimal | Hex |
|---|---|---|---|
| 0 | 00 | 10 | 0A |
| 1 | 01 | 11 | 0B |
| ... | ... | 12 | 0C |
| 9 | 09 | 13 | 0D |
| 15 | 0F | 14 | 0E |
| 16 | 10 | 15 | 0F |
| 255 | FF | 16 | 10 |
Two anchors worth memorizing: 0 → 00 and 255 → FF. Values 10–15 use the letters A–F.
Step-by-step: converting by hand
Take rgb(102, 51, 153) (a medium purple).
Step 1 — Convert R = 102.
Divide by 16: 102 ÷ 16 = 6 remainder 6. So the first digit is 6, the second is 6 → 66.
Step 2 — Convert G = 51.
51 ÷ 16 = 3 remainder 3 → 33.
Step 3 — Convert B = 153.
153 ÷ 16 = 9 remainder 9 → 99.
Step 4 — Join in order.
#663399.
The division method: the quotient is the first hex digit, the remainder is the second. If the remainder is 10–15, write A–F.
The leading-zero trap
This is the most common mistake. If a channel value is below 16, the hex result is a single digit — but you must pad it to two digits.
rgb(0, 255, 0)→ R = 0 →00, G = 255 →FF, B = 0 →00→#00FF00✓rgb(5, 10, 15)→ R = 5 →05, G = 10 →0A, B = 15 →0F→#050A0F✓
Writing #5AF instead of #050A0F produces a different color, because three-digit hex is shorthand that expands each digit (#5AF → #55AAFF).
Handling out-of-range and decimal inputs
RGB values must be integers from 0 to 255. If your input breaks that:
- Above 255 (e.g., from a calculation): clamp to 255, or the hex will overflow past two digits.
- Below 0: clamp to 0.
- Decimals (e.g.,
rgb(102.6, 51.2, 153.9)): round or truncate to an integer first. Rounding is the more common convention; either way, decide and apply it consistently.
Verify the result
Two quick checks catch almost every error:
- Convert back. Take your hex and convert it to RGB. If you get the original three numbers, the conversion is correct.
#663399→ R = 6×16 + 6 = 102, G = 3×16 + 3 = 51, B = 9×16 + 9 = 153. Matches. - Paste it into code. Drop the hex into a CSS rule like
color: #663399;and confirm the browser renders the color you expect. A typo in a hex digit usually shows up immediately as the wrong shade.
Using a converter instead of hand math
For one-off conversions, an online tool is faster and removes arithmetic risk. HexColor.co's lookup accepts a hex code, an RGB value, or a browser color name and returns the conversions — including RGB, HSL, HSV, CMYK, and the CIE spaces — along with harmonies, shades, tints, and contrast figures against white and black. You can enter a value like rgb(102, 51, 153) directly and read off the hex.
Browser DevTools also works: open the color picker in the Styles panel, switch to the RGB or hex field, and the value updates live. This is useful when you're already editing a page and want to confirm a color in context.
When you need more than one color
If you're converting several RGB values to build a palette, a single converter gets tedious. HexColor.co groups its 22 tools by task — palette and scheme generators, a color mixer, gradient generators, and an image-to-colors extractor — so you can pull colors out of an image or website and get the hex values in one pass rather than converting each channel by hand.