Key takeaways
- Six digits, three channels. RR is red, GG is green, and BB is blue, each from 00 to FF.
- Two hex digits equal one byte. 00 is 0 and FF is 255, so each channel has 256 steps.
- #RGB is shorthand. It is not a different color; each digit is doubled.
- Alpha is a separate channel. It runs from 00 to FF in CSS hex, or 0 to 1 in rgba().
- Hex is an sRGB value. It is not a CMYK print recipe and not a physical measurement of light.
What a hex code actually encodes
A hex color code starts with # and then six hexadecimal digits. Hexadecimal is base 16, so the allowed characters are 0–9 and A–F. The digits are read in pairs: the first pair is red, the second green, the third blue, which is why the format is written #RRGGBB.
Each pair represents one byte, and one byte holds 256 values. In decimal that is 0–255, exactly the range CSS uses for rgb(). A pair is not a percentage and not a 0–1 fraction: 80 in hex is 128, not 80% and not 0.8. Six digits give 256 × 256 × 256 = 16,777,216 combinations.
The hash is part of CSS syntax, not part of the number. Some APIs accept either form, but keep the hash in CSS or the declaration is invalid.
Decoding a color by hand
To convert a pair, multiply the first digit by 16 and add the second. In hex, A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15. The formula is value = 16 × high digit + low digit.
For FF: 15 × 16 + 15 = 255. For 88: 8 × 16 + 8 = 136. For 0F: 0 × 16 + 15 = 15. For A3: 10 × 16 + 3 = 163. Those four results decode a large share of real-world colors.
| Pair | Arithmetic | Decimal |
|---|---|---|
| FF | 15 × 16 + 15 | 255 |
| 88 | 8 × 16 + 8 | 136 |
| 0F | 0 × 16 + 15 | 15 |
| A3 | 10 × 16 + 3 | 163 |
| 80 | 8 × 16 + 0 | 128 |
The reverse is repeated division by 16: 136 ÷ 16 = 8 remainder 8, so 136 is 88. Most people use a converter, but the arithmetic makes a bad result obvious. A channel above 255 or a string of the wrong length is a decoding error, not a color choice.
Short form, long form, and alpha
#RGB is a compressed form. Each digit is doubled, not padded: #F80 becomes #FF8800, and #0AF becomes #00AAFF. The expansion is exact, so #F80 and #FF8800 are the same color, not a lighter or darker variant.
Alpha adds a fourth pair. In CSS Color 4, #RGBA and #RRGGBBAA are valid: the final pair is opacity, where 00 is fully transparent and FF is fully opaque. #FF880080 is orange at about 50% opacity because 80 hex is 128, and 128 ÷ 255 = 0.502. The alpha byte is not a percentage; 80 does not mean 80%.
The ordering trap is Android and Java, which use #AARRGGBB — alpha first. A value copied from an Android color resource into CSS is interpreted with red and alpha swapped, producing a different color. Check the source before pasting, and do not assume that an eight-digit hex string is portable.
Older browsers do not support four- and eight-digit hex. If you need broad support, use rgba(255, 136, 0, 0.5) instead of #FF880080. The functional form is more verbose but works wherever rgb() works.
Worked examples
| Hex | R | G | B | Result |
|---|---|---|---|---|
| #FF8800 | 255 | 136 | 0 | rgb(255, 136, 0), orange |
| #0FA3A3 | 15 | 163 | 163 | rgb(15, 163, 163), teal |
| #FFFFFF | 255 | 255 | 255 | rgb(255, 255, 255), white |
| #000000 | 0 | 0 | 0 | rgb(0, 0, 0), black |
| #336699 | 51 | 102 | 153 | rgb(51, 102, 153), steel blue |
| #FF000080 | 255 | 0 | 0 | rgba(255, 0, 0, 0.5) |
Take #336699. 33 hex is 3 × 16 + 3 = 51. 66 is 6 × 16 + 6 = 102. 99 is 9 × 16 + 9 = 153. The result is rgb(51, 102, 153), a muted blue. In HSL it is hsl(210, 50%, 40%). The three channel values are independent levels, not percentages of one another.
From RGB to HSL
HSL re-expresses the same RGB cube as hue, saturation, and lightness. Hue is an angle from 0 to 360, saturation and lightness are percentages, and the conversion is reversible except for rounding. It is often easier to adjust a color in HSL, but HSL carries no more color information than RGB.
The formulas use the largest and smallest channel values, normalized to 0–1. Lightness L = (max + min) ÷ 2. Saturation is 0 when max = min; otherwise S = (max − min) ÷ (1 − |2L − 1|). Hue depends on which channel is largest: for red, H = 60 × ((G − B) ÷ (max − min)); for green, H = 60 × (2 + (B − R) ÷ (max − min)); for blue, H = 60 × (4 + (R − G) ÷ (max − min)).
For #FF8800, max is 1 and min is 0, so L = 50% and S = 100%. Red is largest, and (G − B) ÷ (max − min) = 0.533, so H = 32°. The result is hsl(32, 100%, 50%). For #0FA3A3, max is 0.639 and min is 0.059, L ≈ 35%, S ≈ 83%, and green is largest with hue 180°: hsl(180, 83%, 35%).
Lightness is not perceived brightness. A saturated yellow and a saturated blue can share L = 50% while one looks far brighter. For contrast work, use relative luminance, which linearizes each channel before weighting it.
Alpha and compositing
Alpha is composited, not subtracted. The displayed color is α × foreground + (1 − α) × background, channel by channel. Half-transparent red over white gives rgb(255, 128, 128); the same red over black gives rgb(128, 0, 0). The hex value of the foreground never changes, but what you see does.
This matters for design systems. A translucent overlay looks different on every background, and stacked overlays multiply the effect rather than adding opacity. Two 50% layers do not make 100%; they leave about 25% of the original showing through. If a color must look identical on several backgrounds, use an opaque value instead of alpha.
Opacity also affects text contrast. A gray that passes a contrast check at full opacity can fail once it is placed on a tinted card at 70% opacity. Check the composited result, not the declared value.
Common mistakes
Dropping a digit. #12345 is invalid. Six digits are required for #RRGGBB, and three for #RGB.
Treating shorthand as a different color. #F80 and #FF8800 are identical; a tool that returns different values is padding incorrectly.
Mixing alpha order. CSS uses #RRGGBBAA; Android uses #AARRGGBB. The same eight characters can mean two different colors.
Assuming hex is CMYK. A hex code describes an RGB color in sRGB. Converting it to CMYK depends on the press, ink, paper, and profile, so there is no single correct CMYK equivalent.
Rounding HSL and converting back. hsl(32, 100%, 50%) rounds to whole numbers; converting back can produce rgb(255, 136, 0) or a neighbor. Keep the original hex if exactness matters.
Averaging channels for brightness. (R + G + B) ÷ 3 is not luminance. The eye is more sensitive to green, and gamma matters, which is why accessibility formulas use weighted, linearized channels.
Practical caveats: gamut, gamma, and print
Hex values are device values in the sRGB color space, not measurements of light. A wide-gamut display can show more saturated colors, but an untagged hex value is still interpreted as sRGB by browsers. If color accuracy matters, work with tagged profiles and calibrated displays rather than trusting what one screen shows.
Eight-bit channels also quantize. A smooth gradient across 256 steps can band on a large display, and converting through HSL and back can shift a channel by one. For print, do not hand-convert hex to CMYK: the same sRGB red can land on different ink mixes depending on the profile and substrate.
Frequently asked questions
How do I convert hex to RGB by hand?
Split the six digits into three pairs, then compute 16 × first digit + second digit for each pair. #FF8800 becomes 255, 136, and 0.
What does #FFF mean?
It is shorthand for #FFFFFF, pure white. Each digit is doubled: F becomes FF, so all three channels are 255.
Is #FF8800 the same as rgb(255, 136, 0)?
Yes. They are two notations for the same sRGB color, and neither changes the other.
How do I write 50% transparent red in hex?
In CSS, #FF000080. The 80 alpha byte is 128, and 128 ÷ 255 is about 0.502. The rgba() form is rgba(255, 0, 0, 0.5).
Why is my converted color slightly different?
Check for a dropped digit, the Android alpha order, or a rounded HSL value converted back to RGB. Also confirm that both tools assume sRGB; a color-managed workflow can display the same value differently.
Is hex the same as CMYK?
No. Hex is an RGB notation for screens. CMYK is an ink model whose values depend on the printing process, so any conversion needs a color profile.
Further reading