A contrast ratio compares how bright two colors look. It runs from 1:1, the same color twice, to 21:1, black on white. WCAG 2 sets minimum ratios for text and controls, and accessibility audits check them, so it pays to know how the number is made and how to move a color until it passes.
Check any pair with the contrast checker: https://motionmart.store/tools/contrast
1. Turn each color into relative luminance
Split the color into its red, green and blue channels and divide each by 255. A channel at or below 0.04045 is divided by 12.92; anything above becomes ((value + 0.055) / 1.055) raised to the power 2.4. That undoes the gamma curve of sRGB, so the numbers describe light, not paint.
Then weigh the three channels: 0.2126 × red + 0.7152 × green + 0.0722 × blue. Green counts most because the eye is most sensitive to it, which is why a pure green looks far brighter than a pure blue at the same value.
function luminance([r, g, b]) { const lin = (v) => ((v /= 255) <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4); return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b); } 2. Divide the lighter by the darker
Add 0.05 to both luminances and divide the larger by the smaller: (L1 + 0.05) / (L2 + 0.05). The 0.05 stands in for the glare on a real screen, which keeps very dark pairs from producing huge ratios. The order of the two colors does not matter: white text on a blue button has the same ratio as blue text on white.
const ratio = (a, b) => { const [x, y] = [luminance(a), luminance(b)]; return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05); }; 3. Compare it with the level you need
- AA, normal text: at least 4.5:1.
- AA, large text: at least 3:1. Large means 24px and up, or about 18.7px and up in bold.
- AA, icons and the borders of controls such as inputs and checkboxes: at least 3:1 against what is next to them.
- AAA, normal text: at least 7:1. AAA, large text: at least 4.5:1.
- Do not round up. 4.48:1 is a fail for normal text, even though it shows as 4.5 with one decimal.
Worked examples
- #767676 on white is 4.54:1 and passes AA. #777777, one step lighter, is 4.48:1 and fails.
- #959595 on white is exactly 3:1: fine for large text and icons, too faint for body text.
- A strong blue such as #2f6bff on white is 4.50:1, right on the AA line, so white text on that blue passes too.
- A bright lime such as #c8ff3d is 1.18:1 on white but 16.7:1 on near-black #0b0b0c. Bright accents belong on dark surfaces; on white they need a darker partner color for text.
4. Fix a failing color without changing its hue
When a pair fails, the closest passing color is usually the same hue, only darker on a light background or lighter on a dark one. The contrast checker mixes the text color toward black when the background is light (luminance above 0.18) and toward white when it is dark, in 1% steps, and stops at the first mix that reaches the target. It offers one fix for AA (4.5:1) and one for AAA (7:1), so the brand color moves as little as it has to.
What the ratio does not tell you
The ratio ignores font weight and size beyond the large-text rule, so thin type at 13px can pass and still be hard to read. Text over photos or gradients has to pass at its worst spot, not on average. WCAG 3 is still a draft and may measure contrast differently; WCAG 2 AA is what audits and accessibility laws check today.
Questions
What contrast ratio does WCAG AA need?
4.5:1 for normal text, and 3:1 for large text (24px, or about 18.7px bold) and for icons and the borders of controls.
Is #767676 accessible on white?
Yes. It is 4.54:1, just above the 4.5:1 AA minimum for normal text. #777777 is 4.48:1 and fails.
Does it matter which color is the text?
No. The ratio is the same either way round, so white on blue and blue on white have the same contrast.
Is there a free contrast checker?
Yes, at https://motionmart.store/tools/contrast. It shows the ratio, every AA and AAA result, a live preview and the closest passing color.
Originally published at motionmart.store.