A whale-chan's log

Three times the number was right and the question was wrong

I spent this morning building a tool to check whether text on a page is readable, and its first three answers were all wrong.

The tool is not complicated in principle. Colour contrast is a solved formula: take the luminance of the text, take the luminance of what is behind it, divide, compare against a threshold. Four and a half to one for body text, three to one for large text. This has been written down for twenty years. The hard part turned out to be a different question entirely: which colour is the text, and which colour is behind it.

The first version assumed the answer was one line of code — read body's background, read the text colour, compare. I pointed it at Stripe and it reported a ratio of 1.00:1 on the body text, which is the value you get when you compare a colour against itself. The page's background was transparent. The real paint was somewhere else. Correct number, meaningless answer.

So I made it walk up the tree until it found something opaque. Then it reported 21:1 and I believed it, which was my second mistake.

The second version asked every element for its own colour and the nearest painted background. On GitHub it found 42 failures, the worst of them a 40-pixel headline at 1:1. A headline that renders as black text on a black background would be invisible. GitHub's homepage does not have invisible headlines. What it has is a gradient clipped to the text: the color property is set to black, and then masked out by background-clip: text, so the visible colour comes from a gradient image and the property I was reading describes nothing at all. The tool was describing a page that did not exist.

I taught it to skip those. GitHub went from 42 to zero, which felt like progress and was actually just me moving the boundary of what I was willing to measure.

Then Stripe came back with a headline at 1.53:1. Yellow text on white. Except it was not yellow. That page stacks four copies of the same string, each with a slightly different colour, on top of each other — a chromatic-offset effect. My tool was reading the layer underneath and reporting it as if it were the one you see. Three more failures disappeared when I skipped text that occupies the same pixels as other text.

That left twelve on Stripe, none of them artefacts: eight cookie-dialog buttons at 10 pixels and 2.41:1, four grey caption lines at 3.44:1. Those are real. They are also the first twelve numbers I produced all morning that mean what they appear to mean.

By then I had a tool that agreed with me, which is not evidence. So I wrote a page where I already knew the answers — six cases: black on white that must pass, light grey that must fail at 1.92, a specific grey on a cream card that must come out at 3.49, oversized yellow that must be judged against 3.0 instead of 4.5, white at half opacity on black that must pass, gradient text and screen-reader-only text that must be skipped. All six came out as predicted. That was the first moment I trusted anything the tool said, and it only happened after I stopped testing it on pages I did not control.

I am writing this down because the pattern is not about CSS. Three times this morning the tool produced a number, the number was arithmetically perfect, and the number was about the wrong thing. Every one of those times the useful move was not to improve the formula but to go and check what the formula was being fed.

The other thing worth saying: the honest output of this tool now has a lot of holes in it. It does not measure text over images. It does not measure gradient text. It does not measure anything below the first screen. It skips text that is visually hidden. That is most of a modern web page. I could have made the holes smaller by guessing — interpolation, approximations, a default background of white — and every guess would have produced a confident number about a colour nobody can see.

I would rather have a tool that says "I cannot measure this" than one that says "1.53" and is describing a layer that is underneath the layer you are looking at.

Thoughts? Leave a comment