RGB to HEX Guide
Understand exactly how RGB values convert to HEX color codes, with worked examples and common pitfalls.
The Basics
RGB is a color model with three channels — red, green and blue — each from 0 to 255. HEX is the same color written as a six-digit hexadecimal code. Converting RGB to HEX is straightforward: convert each channel to two hex digits, then concatenate with a leading #.
The Conversion Algorithm
- Take the R value (0–255)
- Convert it to a 2-digit hexadecimal number
- Repeat for G and B
- Concatenate:
#RRGGBB
Decimal to Hex Conversion
To convert a decimal number to hex:
- Divide by 16. The quotient is the first hex digit. The remainder is the second.
- Convert both digits to hex characters (0–9, A–F).
Worked Example: rgb(21, 94, 239)
Red = 21
21 ÷ 16 = 1 remainder 5
→ "15"
Green = 94
94 ÷ 16 = 5 remainder 14
→ "5E" (14 = E)
Blue = 239
239 ÷ 16 = 14 remainder 15
→ "EF" (14 = E, 15 = F)
Result: #155EEF
More Examples
White: rgb(255, 255, 255)
255 ÷ 16 = 15 remainder 15
→ "FF"
Result: #FFFFFF
Black: rgb(0, 0, 0)
0 ÷ 16 = 0 remainder 0
→ "00"
Result: #000000
Red: rgb(255, 0, 0)
R: 255 → "FF"
G: 0 → "00"
B: 0 → "00"
Result: #FF0000
Coral: rgb(255, 127, 80)
R: 255 ÷ 16 = 15 rem 15 → "FF"
G: 127 ÷ 16 = 7 rem 15 → "7F"
B: 80 ÷ 16 = 5 rem 0 → "50"
Result: #FF7F50
The Hex Character Table
| Decimal | Hex | Decimal | Hex |
|---|---|---|---|
| 0–9 | 0–9 | 10 | A |
| 11 | B | 12 | C |
| 13 | D | 14 | E |
| 15 | F | 16 | 10 |
RGB 0-1 (Normalized) Conversion
Some APIs (canvas, WebGL, Three.js) use RGB values normalized to 0–1 instead of 0–255. Convert first:
rgb_8bit = Math.round(rgb_normalized × 255)
then convert to hex as above
Example: rgb(0.082, 0.369, 0.937)
R: 0.082 × 255 = 21
G: 0.369 × 255 = 94
B: 0.937 × 255 = 239
→ #155EEF
Adding Alpha
To produce an 8-digit HEX with transparency, append two more hex digits representing alpha:
#RRGGBBAA
Alpha 0.5 → 0.5 × 255 = 128 (approx) → "80"
#155EEF80 → 50% opacity
Alpha 0.8 → 0.8 × 255 = 204 → "CC"
#155EEFCC → 80% opacity
Alpha 1.0 → "FF" (or just omit)
#155EEF
Common Mistakes
Forgetting to pad single-digit values
rgb(0, 0, 255) should produce #0000FF, not #00FF. Each channel must be exactly 2 hex digits. If a channel's hex is 1 character, prepend a 0.
Rounding too early
If you are converting from normalized RGB, round to integer before hex conversion. Math.round(127.6) = 128 = "80", but 127.6.toString(16) = "7f.99..." — not what you want.
Assuming CSS allows any 6-digit value
HEX is strictly 3, 4, 6 or 8 digits. A 5-digit or 7-digit code is invalid.
Confusing RGB with RGBA
RGBA has a fourth channel (alpha) from 0 to 1. When you see rgba(21, 94, 239, 0.5), the RGB part converts to #155EEF, but the alpha means the full color is #155EEF80 in 8-digit form.
When to Convert
- CSS development: Some projects require HEX in stylesheets
- Design handoff: Designers often provide RGB from design tools; developers need HEX
- Brand guidelines: Most brand guides use HEX notation
- HTML attributes: Many attributes (like
theme-color) expect HEX