ScienceComputer Science

Color Contrast
checker.

Test whether text and background colors are far enough apart to pass WCAG 2.2.

How it's worked out

WCAG measures contrast with relative luminance: how bright a color is, from 0 for black to 1 for white. Each hex code gives three channels, red, green and blue, from 0 to 255. Divide each by 255, then undo the screen's gamma curve so the value is linear light.

c\displaystyle c=channel255\displaystyle {}= \frac{\text{channel}}{255}
c\displaystyle c≤0.04045:linear\displaystyle {}\le 0.04045:\; \text{linear}=c12.92\displaystyle {}= \frac{c}{12.92}
c\displaystyle c>0.04045:linear\displaystyle {}> 0.04045:\; \text{linear}=(c+0.0551.055)2.4\displaystyle {}= \left(\frac{c + 0.055}{1.055}\right)^{2.4}

Then weight the three channels. Green counts most and blue least, because the weights come from the sRGB standard's model of how bright each primary looks.

L\displaystyle L=0.2126R\displaystyle {}= 0.2126\,R+0.7152G\displaystyle {}+ 0.7152\,G+0.0722B\displaystyle {}+ 0.0722\,B

The ratio puts the lighter luminance, L1L_1, over the darker one, L2L_2. Adding 0.05 to both stands in for the glare of a real screen, and it keeps the ratio finite for pure black.

ratio\displaystyle \text{ratio}=L1+0.05L2+0.05\displaystyle {}= \frac{L_1 + 0.05}{L_2 + 0.05}

The answer runs from 1.00:1 for two identical colors to 21.00:1 for black on white. Order does not matter: white text on 595959 gives 7.00:1, the same as 595959 text on white. The steps show every figure for your pair and then put the ratio back in to check it.

Worked examples

Priya checks the body text on a new blog theme

Priya's theme sets paragraphs in 595959 on a white page. Each channel is 89, which is 0.349 of full scale and 0.0999 in linear light. Because all three channels match, the luminance is the same 0.0999.

1+0.050.0999+0.05\displaystyle \frac{1 + 0.05}{0.0999 + 0.05}=7.00\displaystyle {}= 7.00

At 7.00:1 the pair reaches AAA, the strictest level, for text of any size. She can keep the gray and still meet the 7:1 that success criterion 1.4.6 asks for.

Open this example in the tool

Marco fixes a gray label an audit flagged

An audit flags Marco's form labels, set in 777777 on white. The ratio is 4.47:1, just short of 4.5:1. WCAG says not to round, so this fails AA for normal text even though some tools print 4.48 or 4.5.

It still passes the 3:1 for large text, so the same gray would be fine on a heading. For the labels, the nearest darker gray is 767676, one step down in each channel, at 4.54:1. The eye cannot tell the two grays apart; the audit can.

Open this example in the tool

Dana tests white text on a yellow button

Dana's brand yellow, FACC15, is bright, so white text on it gives only 1.53:1. That fails every check, even the 3:1 for large text and icons.

Keeping white text means a darker button. The nearest background that reaches 4.5:1 is 8E740C, a deep olive, at 4.51:1. The other way out is dark text on the yellow, which keeps the brand color: the nearest passing text color is 5A5A5A.

Open this example in the tool

Reading your result

Normal text needs 4.5:1 for AA under success criterion 1.4.3, and 7:1 for AAA under 1.4.6. Most laws and contracts that cite WCAG ask for AA.

Large text needs 3:1 for AA and 4.5:1 for AAA. WCAG defines large as at least 18 point, or 14 point bold. The Understanding page puts those at about 24 px and 18.5 px, but it says to check the font you use, since thin or unusual fonts read smaller.

Icons, input borders and focus rings need 3:1 against the colors next to them, under 1.4.11 Non-text Contrast. A 1 px gray border that fails here makes a text box hard to find, even when the text inside it passes.

Each check is pass or fail at its line. 4.47:1 is as much a fail of 4.5:1 as 2:1 is. A higher ratio than you need gains nothing in the rules, but it does help people reading on a phone in sunlight. Dark themes follow the same numbers: A3A3A3 on 121212 gives 7.42:1, which passes AAA.

Some text is exempt. Logos and brand names have no requirement, and nor does text on a control that is visible but not usable, such as a grayed-out submit button. Placeholder text in a form field is not exempt.

Common mistakes

  • Trusting a rounded ratio. A checker that shows 777777 on white as 4.5:1 has rounded 4.47:1 up. WCAG compares the exact value, so it fails.
  • Testing only the brand color as text. FACC15 text on white gives 1.53:1, and white on it gives the same. Swapping text and background never fixes a pair; only a lighter or darker color does.
  • Forgetting the default placeholder gray. 808080 on white is 3.94:1, which fails AA for normal text.
  • Judging white on a mid blue by eye. White on 2563EB looks strong and gives 5.16:1: AA, but not AAA for normal text.
  • Checking text over a photo at one spot. Contrast changes across an image. Test the lightest part behind the text, or put a solid panel behind it.

Questions people ask

No. The W3C's Understanding page for 1.4.3 says the computed value should not be rounded, so even 4.499:1 fails. This tool cuts the ratio to two places instead of rounding it, so what you see never claims a pass that the exact value misses.

Sources