Sub topic 12
Contrast ratios and readable color pairs
This chapter assumes you know hue, saturation, and value, and that HSL separates lightness from hue. It also assumes you can read short Python and CSS. Here you turn "this looks readable" into a number you can measure, test, and fix.
Why hue alone does not make text readable
Two colors can differ strongly in hue and still be hard to tell apart in brightness. A saturated red text on a saturated green background is a classic case. Both have similar lightness, so the edge between them shimmers and the letters blur. Readers with color vision deficiency may see almost no difference at all.
Legibility depends mostly on the difference in lightness between foreground and background. This is the value contrast you used for hierarchy in the previous chapter, now measured precisely. Hue and saturation still matter for mood and brand, but lightness does the work of making text legible.
The WCAG contrast ratio
The Web Content Accessibility Guidelines (WCAG) 2.x define contrast as a ratio between 1:1 and 21:1.
- 1:1 means identical colors, so the text is invisible.
- 21:1 is pure black on pure white, the maximum.
The ratio is computed from each color's relative luminance. This is a physically motivated measure of how much light a color emits, scaled from 0 (black) to 1 (white). The calculation has three steps.
- Convert each 8-bit sRGB channel to a 0 to 1 value, then undo the gamma curve ("linearize" it).
- Combine the channels with weights that reflect how sensitive the eye is to each:
L = 0.2126 R + 0.7152 G + 0.0722 B. - Compare the lighter luminance
L1and darker luminanceL2:ratio = (L1 + 0.05) / (L2 + 0.05).
The weights show why green dominates perceived brightness and blue contributes very little. A pure blue (#0000ff) has luminance 0.0722, so it is dark despite being fully "on" numerically. This is the gap between numeric value and perceived lightness that an earlier chapter covered.
Thresholds you need to know
WCAG sets minimums by what the color is used for.
| Use | Level AA | Level AAA |
|---|---|---|
| Body text (under about 24 px regular) | 4.5:1 | 7:1 |
| Large text (about 24 px regular, or 18.66 px bold and up) | 3:1 | 4.5:1 |
| UI components and meaningful graphics (borders of inputs, icons, focus rings) | 3:1 | No separate AAA level |
| Decorative elements, disabled controls, logos | No requirement | No requirement |
Most projects target AA. Treat these numbers as floors. Thin fonts, small sizes, glare on a phone outdoors, and aging eyes all push the real need higher.
Computing the ratio in Python
This code implements the formula directly. It is complete and runs with only the standard library.
def channel(c8):
"""Linearize one 0-255 sRGB channel."""
c = c8 / 255
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
def luminance(rgb):
r, g, b = (channel(c) for c in rgb)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
def contrast(fg, bg):
l1, l2 = luminance(fg), luminance(bg)
lighter, darker = max(l1, l2), min(l1, l2)
return (lighter + 0.05) / (darker + 0.05)
white = (255, 255, 255)
print(round(contrast((0, 0, 0), white), 2)) # 21.0
print(round(contrast((118, 118, 118), white), 2)) # 4.54 (#767676)
print(round(contrast((119, 119, 119), white), 2)) # 4.48 (#777777)The last two lines show how sharp the boundary is. #767676 is the lightest neutral gray that passes AA for body text on white, and #777777 fails by a hair. The two grays look identical, which is why you measure instead of judging by eye.
Fixing a failing pair by changing lightness
When a pair fails, change lightness and leave hue alone. Moving the value keeps the color in the same family, so the brand identity survives. HSL makes this convenient because lightness is its own axis. Note that Python's colorsys uses the order hue, lightness, saturation (hls), not HSL.
import colorsys
def to_rgb255(hue_deg, sat, light):
r, g, b = colorsys.hls_to_rgb(hue_deg / 360, light, sat)
return tuple(round(x * 255) for x in (r, g, b))
def darken_until(hue_deg, sat, light, bg, target=4.5):
"""Lower lightness in 1% steps until the color reaches the target ratio."""
while light > 0:
rgb = to_rgb255(hue_deg, sat, light)
if contrast(rgb, bg) >= target:
return rgb, round(light, 2)
light -= 0.01
return None
# A bright blue link color on a white page
result = darken_until(210, 0.80, 0.60, (255, 255, 255))
print(result) # first (rgb, lightness) that reaches 4.5:1This code reuses contrast from the previous example. The search is deliberately simple. It stops at the lightest passing color, so you keep as much of the original brightness as the rule allows. For dark backgrounds, do the opposite and raise lightness.
Building contrast into your CSS tokens
Define pairs together, and test each pair, not each color alone. A color has no contrast by itself. Only a pair does.
:root {
--hue: 210;
/* Surfaces */
--surface: hsl(var(--hue) 20% 98%);
--surface-raised: hsl(var(--hue) 20% 93%);
/* Text on surfaces */
--text: hsl(var(--hue) 30% 15%);
--text-muted: hsl(var(--hue) 15% 35%);
/* Interactive */
--link: hsl(var(--hue) 85% 33%);
--focus-ring: hsl(var(--hue) 90% 40%);
}
body {
background: var(--surface);
color: var(--text);
}
.caption {
color: var(--text-muted);
}
a {
color: var(--link);
}
:focus-visible {
outline: 3px solid var(--focus-ring);
outline-offset: 2px;
}The lightness values follow a pattern. Text sits near 15 to 35 percent lightness, and surfaces sit above 90 percent. Because the hue is shared, the page stays cohesive, and the gap in lightness carries the readability. These values are a starting point. Run each pair through a checker before you ship.
Common errors
- Checking only the main text. Placeholder text, captions, disabled-looking but active buttons, and chart labels often fail while the headline passes.
- Forgetting states. Hover, focus, visited, and selected states change the colors. Each state forms a new pair.
- Text over images or gradients. The ratio varies across the background. Test the worst spot, or add a solid or semi-opaque panel behind the text.
- Using opacity to make muted text.
opacity: 0.6blends the text toward the background, and the resulting color is not the one you tested. Use an explicit muted color instead. - Relying on hue to carry meaning. A red error and a green success of equal lightness look the same to many viewers. Add an icon, a label, or a lightness difference.
- Trusting saturation to rescue contrast. Raising saturation changes the color but usually does little for the ratio. Lightness is the lever that moves it most.
Limits of the WCAG 2 ratio
The ratio is a useful standard, but it is a simplified model.
- It treats light-on-dark and dark-on-light symmetrically. In practice, dark mode text can look harsher or thinner than the numbers suggest.
- It ignores font weight and size beyond the single large-text cutoff. A 4.5:1 pair in a hairline font can still be tiring to read.
- Mid-tone colors can score the same ratio on both sides, such as a mid-gray on white versus on black, but they do not read equally well.
Newer models, such as APCA (the candidate method for WCAG 3), try to correct these problems by accounting for polarity, size, and weight. For now, WCAG 2 AA is the measurable requirement in most laws and procurement rules, so meet it first. Then use your own eyes on real devices for the cases the ratio misses.
Summary of the workflow
- List every foreground and background pair, including states.
- Identify the use of each pair: body text, large text, or UI component.
- Compute the ratio and compare it to 4.5:1 or 3:1.
- Adjust lightness, keeping hue fixed, until the pair passes.
- Add a non-color cue anywhere meaning depends on hue.
Exercise
A designer uses light gray #999999 text (about 2.8:1 on white) for 16 px body copy on a white page. It fails AA. Which HSL property should change first to fix it while keeping the same family of color, and which direction?
Show answer
Lightness (value) should change, and it should go down so the text becomes darker. Hue stays the same to preserve the color identity. Neutral gray has no saturation, so saturation is not the lever. The text must reach at least 4.5:1, and #767676 is the lightest neutral gray that does so on white.
Exercise
Using the formula (lighter + 0.05) / (darker + 0.05), what is the contrast ratio of white on white, and why is it not 0?
Show answer
Both luminances are 1, so the ratio is (1 + 0.05) / (1 + 0.05) = 1. The 0.05 offset in the formula keeps the ratio from dividing by zero for pure black, and the scale starts at 1:1 (identical colors) rather than 0. The maximum is 21:1, which is black on white: (1 + 0.05) / (0 + 0.05) = 21.
Exercise
A success message is green text and an error message is red text, both at similar lightness, with no other differences. Name the problem and give two fixes.
Show answer
Meaning is carried by hue alone, and the two colors have similar lightness, so viewers with color vision deficiency may not distinguish them. Fixes: add an icon or a text label such as "Error:", and give the two colors clearly different lightness. Also check that each one meets 4.5:1 against its background.