One colour, three notations
HEX, RGB and HSL describe the same underlying colour. Converting between them changes nothing visually; it only changes which aspects are easy to adjust.
- HEX —
#4F46E5: two digits each for red, green and blue, in base 16. An eight-digit form adds alpha, e.g.#4F46E580. - RGB —
rgb(79 70 229): the same channels written in decimal 0–255. With alpha it becomesrgb(79 70 229 / 0.5). - HSL —
hsl(245 75% 59%): hue in degrees (0–360), saturation and lightness in percent. Best when you want to derive shades or a matching palette by tweaking one value.
Alpha is not the same as a lighter colour
Reducing alpha makes a colour transparent: it blends with whatever is behind it. Reducing lightness makes it darker. A 50%-alpha white on a dark background looks grey; a 50%-lightness white looks mid-grey too — but on a different background the two diverge. When approximating a translucent colour for a solid fallback, blend the two colours explicitly rather than lowering lightness.
Contrast ratio and WCAG
WCAG measures contrast as a ratio between 1:1 (identical) and 21:1 (black on white), computed from relative luminance.
- 4.5:1 — minimum for normal body text (AA)
- 3:1 — minimum for large text (18pt, or 14pt bold) and for UI component boundaries (AA)
- 7:1 — AAA for normal text
A ratio around 4.5:1 is the practical target: light grey body text on white is the single most common accessibility failure on otherwise well-designed sites.
Checking a palette quickly
Convert both colours to RGB, compute their relative luminance (linearise each channel, then weight red 0.2126, green 0.7152, blue 0.0722), and apply (L1 + 0.05) / (L2 + 0.05) with the lighter colour as L1. A converter that shows the ratio for two colours saves doing this by hand.
Remember that colour alone should never carry meaning — a red error state also needs an icon or text label for colour-blind users.
How the conversions actually work
Converting hex to RGB means reading each pair of digits as a base-16 number: #4F46E5 becomes 79, 70 and 229. The reverse pads each channel to two digits, which is why rgb(0 128 255) is #0080FF and not #080FF. HSL is a different model: hue is an angle on a colour wheel, saturation says how far the colour sits from grey, and lightness says how close it is to black or white. Converting HSL to RGB takes a small amount of arithmetic; it is not a per-channel translation.
Gamut, gamma and why the ratios matter
CSS colours live in sRGB, whose channels are gamma-encoded: the value 128 is not half the light of 255. WCAG contrast therefore converts each channel to linear light before computing relative luminance, which is why a ratio cannot be estimated by averaging channel values. Two colours can look very different in brightness while sitting at the same numeric distance in RGB space.
Alpha, compositing and layered backgrounds
A translucent colour has no defined appearance until it is placed over something. The result is the source blended with the backdrop, per channel: result = alpha × foreground + (1 - alpha) × background. That means the same rgba(0, 0, 0, 0.4) text is readable on white and nearly invisible on a dark hero image. A contrast checker that accepts only two solid colours cannot evaluate this case, so either test the composited colour or give the element a solid backdrop.
Common format mistakes
- Three-digit hex is expanded, not truncated.
#F80means#FF8800: each digit is duplicated, not treated as a low value. - Eight-digit hex order is RRGGBBAA. CSS puts alpha last, while some native APIs and image formats put it first. Copying a colour between systems without checking the order produces a wrong-looking colour that is still perfectly valid.
- Legacy and modern syntax differ.
rgba(0, 0, 0, 0.5)uses commas and a fourth argument;rgb(0 0 0 / 0.5)uses spaces and a slash. Mixing the two forms is a parse error in strict tooling. - Hue wraps around.
hsl(350 ...)andhsl(-10 ...)describe the same colour, so arithmetic on hue needs a modulo 360 step. - Percentages are not channel values. In HSL,
100%saturation means full colour; in RGB,100%means 255. The units are not interchangeable. - Alpha is not opacity. A container with
opacity: 0.5fades its text and borders as well; an alpha background only affects the background colour.
Contrast edge cases a checker will not catch
- Thin or light-weight text. A ratio of exactly 4.5:1 passes the formula, but hairline fonts at small sizes still look faint; leave margin.
- Text over images. A ratio measured against the average colour of a photo is meaningless; add a solid overlay or scrim.
- Hover and focus states. Focus indicators are non-text UI and need 3:1 against adjacent colours, not just the resting state.
- Disabled controls. WCAG exempts inactive components, but users still need to recognise them; do not let a disabled style become invisible.
- Large-text thresholds. 3:1 applies from 18pt regular or 14pt bold; a 17pt heading is still held to 4.5:1.
- Gradients and video. Contrast changes across the element, so check the lightest and darkest points rather than one sample.
Choosing a notation
| Format | Best for | Watch out for |
|---|---|---|
| HEX | Design handoff, compact tokens, CSS variables | Hard to read or tweak by hand |
| RGB | Computed colours and channel arithmetic | Repeating three numbers |
| HSL | Generating shades, themes and hover states | Uneven perceived lightness across hues |
Practical checklist
- Pick one notation per project and convert only where a library requires another.
- Derive hover and active states in HSL by changing lightness, then verify the resulting contrast.
- Check body text against 4.5:1, large text and UI edges against 3:1, and aim for 7:1 where readability matters most.
- Never rely on colour alone; pair it with text, an icon or an underline.
- Test composites over every real background, not just white.
Convert between notations and check ratios with our Colour Converter and Contrast Checker. The underlying arithmetic is covered in binary, hex and decimal conversion.