Most online colour vision tests show scanned Ishihara-style plates. That has three problems. Scans of printed plates come with copyright questions. Scanning and compression shift the exact colours the test depends on. And a fixed set of images can be memorised, which matters when a child takes the test again a month later.
So our colour vision test (anpheros.com/color-vision-test) draws every plate from scratch, in a <canvas>, each time it runs. This post walks through how it works: a digit mask, a dot packer, and the one rule that makes the whole thing a test rather than a picture. It's the third part of a small series on health checks that run in a browser tab.
Step 1: the digit, as a mask
The digit never appears on screen. It's drawn on an offscreen canvas, and its pixels decide which dots belong to the figure:
const N = 320; const mask = document.createElement('canvas'); mask.width = mask.height = N; const m = mask.getContext('2d'); m.font = '900 230px Arial, Helvetica, sans-serif'; m.textAlign = 'center'; m.textBaseline = 'middle'; m.fillText(String(digit), N / 2, N / 2 + 12); // nudge down: 'middle' sits a little high for digits const data = m.getImageData(0, 0, N, N).data; const inFig = (x, y) => data[((y | 0) * N + (x | 0)) * 4 + 3] > 128; // read the alpha channel The very heavy weight matters. The strokes have to be wide enough to hold plenty of whole dots, otherwise the digit turns into a few scattered points.
Step 2: packing dots into a circle
The plate is a circle filled with dots of different sizes that don't overlap. The simplest approach that works is rejection sampling: pick a random radius and position, and keep the dot only if it fits inside the circle and doesn't touch any dot already placed.
const dots = []; let tries = 0; while (dots.length < 560 && tries < 40000) { tries++; const r = 3.5 + Math.random() * 8; const x = r + Math.random() * (N - 2 * r), y = r + Math.random() * (N - 2 * r); const dx = x - N / 2, dy = y - N / 2; if (dx * dx + dy * dy > (N / 2 - 3 - r) ** 2) continue; // stay inside the plate if (dots.some(d => (d.x - x) ** 2 + (d.y - y) ** 2 < (d.r + r + 1.2) ** 2)) continue; // keep a 1.2 px gap dots.push({ x, y, r }); } It's quadratic, and at a few hundred dots that's fine. The tries cap is the safety valve: once random placement stops finding room, the loop ends instead of spinning.
Notice that the placement ignores the digit completely. Only colour decides what's figure and what's background, so the layout itself gives nothing away.
Step 3: the rule that makes it a test, equal luminance
This is the part that's easy to get wrong. Say the figure is red-orange and the background yellow-green, but the figure also happens to be a bit darker. Then everyone reads the digit, colour vision or not. People with a red-green deficiency often rely on lightness differences without realising it, and a plate with a lightness cue tests nothing.
So the figure and the background must differ in hue only. Every dot gets a random target luminance from the same range, whichever group it belongs to, and its colour is solved to hit that luminance:
// WCAG relative luminance of an sRGB colour (channels 0..1) const lum = ([r, g, b]) => { const c = x => (x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4)); return 0.2126 * c(r) + 0.7152 * c(g) + 0.0722 * c(b); }; // Binary search on HSL lightness until the luminance matches the target function colourAt(hue, sat, targetY) { let lo = 0, hi = 100, l = 50; for (let k = 0; k < 12; k++) { l = (lo + hi) / 2; if (lum(hsl2rgb(hue, sat, l)) < targetY) lo = l; else hi = l; } return `hsl(${hue.toFixed(0)} ${sat.toFixed(0)}% ${l.toFixed(1)}%)`; } Binary search is valid here because, for a fixed hue and saturation, every RGB channel rises monotonically with HSL lightness, and so does luminance. Twelve halvings of 0–100 land within about 0.02 lightness points, well below one step of an 8-bit channel.
Then each dot is coloured:
const y = 0.18 + Math.random() * 0.2; // the same luminance range for figure and background fill = inFig(d.x, d.y) ? colourAt(8 + Math.random() * 26, 55 + Math.random() * 25, y) // reds and oranges : colourAt(62 + Math.random() * 40, 40 + Math.random() * 20, y); // yellows and greens Figure hues sit between 8° and 34°, background hues between 62° and 102°. With typical colour vision the two groups are clearly different. The difference between them lies mostly along the red-green direction, which is the one that collapses in protan and deutan deficiencies, and with lightness equalised there's very little else left to see.
Drawing each dot's luminance from a range, instead of using one fixed value, adds the speckle that printed plates have. There's no smooth area of a single tone to lock onto.
Control plates: the eyes, or the screen?
Plates 1 and 5 are controls. Their dots have random hues, and the digit is carried by luminance instead: a dark figure (0.07 to 0.12) on a light background (0.5 to 0.65). Anyone who can see the screen properly reads them, whatever their colour vision.
if (control) fill = colourAt(Math.random() * 360, 30 + Math.random() * 20, fig ? 0.07 + Math.random() * 0.05 : 0.5 + Math.random() * 0.15); If someone misses a control plate, the result isn't "possible colour vision difficulty". It's "the test could not decide": the screen was too dim, night mode was on, sunlight was on the glass, or a finger hit the wrong button. A test that can't tell "your eyes" from "your setup" would blame the eyes every time.
Scoring
Eight plates, two controls, six scored. The rule is deliberately conservative, and the same rule runs on our server, so the website and the app reach the same result from the same answers:
const scored = PLATES - control.length; // 6 let outcome = 'unclear'; if (controlOk < control.length) outcome = 'unclear'; else if (missed >= Math.max(3, Math.round(scored * 0.4))) outcome = 'possible_difficulty'; else if (missed <= 1) outcome = 'normal'; One miss is noise. Three or more, with both controls read correctly, is a pattern worth showing to an optometrist. Two is a grey zone, and the honest answer there is "take it again on another day", not a label.
What a screen can't do
- Uncalibrated colour. The luminance maths assumes sRGB. Real screens differ in gamut and white point, and features like night mode, True Tone or accessibility colour filters shift hues on purpose. The setup asks people to turn them off, and the control plates catch the worst cases, but a plate that's equiluminant on one display can be slightly off on another.
- Red-green only. The hue ranges target the red-green axis. Blue-yellow (tritan) deficiency is rare, and this test doesn't cover it.
- No type, no severity. It can't tell protan from deutan, or mild from strong. An optometrist does that with printed plates under controlled light, and with other tests when needed.
The improvement I'd make next is choosing the hues on real confusion lines, in a cone-response space like LMS, instead of hand-picked HSL ranges. If you've done that in a browser, I'd really like to hear how you dealt with the display-profile problem.
Try it
The colour vision test is at anpheros.com/color-vision-test: eight plates, about two minutes, free, no account. The earlier parts of this series cover the hearing test built on the Web Audio API and the eye test calibrated with a bank card.
Disclosure: I'm the founder of Anpheros, a patient-held health record. The tests are informative only; they don't diagnose colour vision deficiency and don't replace an optometrist.

