Sub topic 12
Contrast ratios and readable color pairs
This chapter assumes you know hue, saturation, and value, and that you have read about value contrast and perceived lightness. It covers how to measure whether two colors are readable together.
Why value alone is not enough
Value contrast tells you that a pair differs in lightness. It does not tell you whether the pair is readable. HSV value is a simple number computed from RGB, so two colors with the same value can look very different in brightness. Accessibility rules therefore use a measure based on how bright a color appears to the eye. That measure is relative luminance.
How the ratio is calculated
The Web Content Accessibility Guidelines (WCAG) define contrast ratio in three steps:
- Convert each sRGB channel from 0–255 to 0–1.
- Linearize each channel. If c ≤ 0.04045, use c / 12.92. Otherwise use ((c + 0.055) / 1.055) raised to 2.4.
- Compute luminance as L = 0.2126 R + 0.7152 G + 0.0722 B.
The ratio is (Llighter + 0.05) / (Ldarker + 0.05). It ranges from 1:1 (identical colors) to 21:1 (black on white). The green weight is the largest, which matches the eye's greater sensitivity to green.
def channel(c8):
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(v) for v in rgb)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
def contrast(rgb1, rgb2):
l1, l2 = luminance(rgb1), luminance(rgb2)
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((0x76,) * 3, white), 2)) # 4.54
print(round(contrast((0x77,) * 3, white), 2)) # 4.48The last two lines show how sharp the threshold is. Gray #767676 passes on white. Gray #777777 is one step lighter and fails.
Thresholds to remember
| Use | AA minimum | AAA minimum |
|---|---|---|
| Normal body text | 4.5:1 | 7:1 |
| Large text (18 pt, or 14 pt bold) | 3:1 | 4.5:1 |
| Icons, input borders, UI components | 3:1 | none |
AA is the usual target for products. Use AAA where you can, especially for long reading.
Fixing a failing pair
When a pair fails, change value first, because value drives luminance most directly. Keep the hue so the color keeps its identity, and move lightness in HSL until the ratio passes.
A mid-saturation yellow such as hsl(50 90% 50%) on white gives a poor ratio. Hue-wise it is fine, but its luminance is high. Dropping lightness to about 25% gives a dark golden brown that passes, though it looks less like yellow. This is why yellow text on white is a classic failure, and why yellow works better as a background with dark text.
Common errors
- Judging by eye on a bright, well-calibrated monitor. Check the numbers instead.
- Assuming a hue change fixes contrast. Two hues with equal luminance, such as a red and a green, can reach a ratio near 1:1 even though they look different. This is also a problem for color-blind users.
- Forgetting states. Check hover, disabled, and focus colors as well as the default.
- Using color as the only signal. Add an icon, label, or underline.
Exercise
A designer sets light gray text, #999999, on a white background for body copy. The ratio is about 2.8:1. Name the WCAG AA target for body text and one way to fix the pair while keeping the gray hue.
Show answer
Normal body text needs at least 4.5:1 for AA. Keep the hue and saturation, which are zero for a pure gray, and lower the lightness until the ratio reaches 4.5:1. For example, #767676 gives about 4.54:1 on white.