Skip to content
Back to blog
Field notes · image

Three Shortcuts That Break the WCAG Contrast Ratio Formula

#767676 on white is 4.54:1. Skip linearization and it drops to 2.05:1; use video luma weights and forestgreen jumps from 4.39 to 5.04. Worked by hand.

5 min readSimon Wong

Gray #767676 on white has a WCAG contrast ratio of 4.54:1, which clears the 4.5:1 that level AA requires for normal text. Compute the same pair without linearizing the sRGB channels and you get 2.05:1, a clear fail. Keep the linearization but borrow the brightness weights from video, and CSS forestgreen on white moves from 4.39:1 (fail) to 5.04:1 (pass). The formula has two steps and three places where a shortcut quietly changes the verdict.

How to Calculate the Contrast Ratio

WCAG 2.2 defines contrast in two steps. First, the relative luminance of each color:

for each channel C in R, G, B (8-bit value / 255):
  C_lin = C <= 0.04045 ? C / 12.92 : ((C + 0.055) / 1.055) ^ 2.4

L = 0.2126 * R_lin + 0.7152 * G_lin + 0.0722 * B_lin

Then the ratio, with L1 the lighter of the two colors:

ratio = (L1 + 0.05) / (L2 + 0.05)

L runs from 0 (black) to 1 (white), so the ratio runs from 1:1 to 21:1. The + 0.05 on both sides keeps black (L = 0) from dividing by zero and sets the maximum: 1.05 / 0.05 = 21. Level AA (SC 1.4.3) needs 4.5:1 for normal text and 3:1 for large text; level AAA (SC 1.4.6) needs 7:1 and 4.5:1. What counts as large text is in the WCAG 2.2 contrast requirements.

#767676 on White, Step by Step

0x76 is 118.

StepValue
118 / 2550.462745
Above 0.04045, so (0.462745 + 0.055) / 1.0550.490754
0.490754 ^ 2.40.181164
R = G = B, so L = (0.2126 + 0.7152 + 0.0722) × 0.1811640.181164
White: every channel is 1, so L1
(1 + 0.05) / (0.181164 + 0.05)4.5422

4.5422 passes AA for normal text (4.5) and for large text (3), and fails AAA for normal text (7). The three weights add up to exactly 1, which is why any gray has the same luminance as its linearized channel value.

Shortcut 1: Skipping the Linearization

The ^ 2.4 step converts the stored sRGB value, which is gamma-encoded, into a value proportional to emitted light. Leaving it out treats 118/255 as 46% of white's light output, when the real figure is 18%.

PairCorrectWithout linearization
#767676 on white4.542.05
#2E7D32 on white5.132.32
black on #FF88008.7712.88

The error runs in both directions. Mid-tone text on white looks far worse than it is, and dark text on a saturated background looks better than it is. A checker built this way fails pairs that pass and passes pairs that fail.

Shortcut 2: Weights from Video

Search for "is this color light or dark" and many snippets use 0.299 R + 0.587 G + 0.114 B. Those are the Rec. 601 luma weights from standard-definition video. WCAG takes its weights from sRGB, which uses the Rec. 709 primaries: 0.2126 / 0.7152 / 0.0722. Both sets add up to 1, so grays come out the same either way. That is why the mistake survives a quick test with gray swatches. Colors do not:

Color on whiteWCAG weightsRec. 601 weightsVerdict change
#228B22 (forestgreen)4.395.04fail → pass
#2E8B57 (seagreen)4.254.76fail → pass
#0070F34.554.25pass → fail

Rec. 601 gives green less weight (0.587 against 0.7152), so greens come out darker than they are, which raises their contrast against white. It gives blue more weight (0.114 against 0.0722), so blues come out lighter and their contrast drops. Both columns above apply the linearization correctly. Only the weights differ.

Shortcut 3: Rounding to 4.5

The Understanding document for SC 1.4.3 says computed ratios "should not be rounded", and gives 4.499:1 as a ratio that fails 4.5:1. Rounding for display still causes trouble:

  • #777777 on white is 4.478:1. Shown to one decimal it reads 4.5.
  • #CC22CC on white is 4.4985:1. Shown to two decimals it reads 4.50.

Against white alone, 9,074 of the 16.7 million sRGB colors fall between 4.495 and 4.5. Every one of them fails AA, and every one prints as "4.50" with ordinary two-decimal rounding. A green "4.50" next to a red "Fail" makes people distrust the verdict, so this tool truncates instead: #CC22CC shows 4.49.

The Old 0.03928 Threshold Does Not Matter

Before May 2021 the WCAG definition used 0.03928 instead of 0.04045 as the cutoff between the linear segment and the curve. Some older code still uses the old number. For 8-bit color it makes no difference, because no 8-bit value falls between the two thresholds: 10/255 is 0.03922 and 11/255 is 0.04314. All 256 channel values linearize to the same number under either cutoff. A note under the WCAG definition says as much: the update does not change results in practice.

The Whole Thing in Code

const lin = (c) => {
  const s = c / 255;
  return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
};
const luminance = ([r, g, b]) =>
  0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);

function contrastRatio(a, b) {
  const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
  return (hi + 0.05) / (lo + 0.05);
}

contrastRatio([118, 118, 118], [255, 255, 255]); // 4.5422…

Compare the result with >= 4.5 on the full-precision number, never on a rounded one. The ratio says nothing about which color is the text. For that, see why WCAG 2 and APCA disagree on the same gray.

Try It

Open the Contrast Checker and enter #CC22CC as the foreground on #FFFFFF: the WCAG panel shows 4.49 and AA normal fails. Change the foreground to forestgreen: 4.38 (4.3895 truncated), a fail, where a checker using Rec. 601 weights would report a passing 5.04. Then try #767676: 4.54, AA pass.