/* Assert WCAG contrast for every theme in tokens.css. * * Colour contrast is the classic silently-regressing property: you nudge one * hex value, it still "looks fine" on your monitor, and the muted text is now * unreadable for someone else. So it gets a test rather than a promise. * * No dependencies -- run with `npm run check:contrast`. */ import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; const here = dirname(fileURLToPath(import.meta.url)); const css = readFileSync(join(here, "..", "src", "styles", "tokens.css"), "utf8"); /** Extract custom properties per theme from the stylesheet. */ function parseThemes(source) { const themes = {}; const blockRe = /([^{}]+)\{([^}]*)\}/g; let match; while ((match = blockRe.exec(source)) !== null) { const selectors = match[1] .replace(/\/\*[\s\S]*?\*\//g, "") .split(",") .map((s) => s.trim()); const body = match[2]; const props = {}; const propRe = /(--[\w-]+)\s*:\s*([^;]+);/g; let prop; while ((prop = propRe.exec(body)) !== null) { props[prop[1]] = prop[2].trim(); } for (const selector of selectors) { const named = selector.match(/\[data-theme="([\w-]+)"\]/); if (named) { themes[named[1]] = { ...(themes[named[1]] ?? {}), ...props }; } } } return themes; } function toLinear(channel) { const c = channel / 255; return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4); } function luminance(hex) { const clean = hex.replace("#", "").trim(); if (!/^[0-9a-fA-F]{6}$/.test(clean)) return null; const r = parseInt(clean.slice(0, 2), 16); const g = parseInt(clean.slice(2, 4), 16); const b = parseInt(clean.slice(4, 6), 16); return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b); } function contrast(fg, bg) { const a = luminance(fg); const b = luminance(bg); if (a === null || b === null) return null; const [hi, lo] = a > b ? [a, b] : [b, a]; return (hi + 0.05) / (lo + 0.05); } /* Each pair: [foreground token, background token, minimum ratio]. * 4.5 is WCAG AA for body text; 3.0 is AA for large text and for the * boundaries of interactive components. */ const PAIRS = [ ["--text", "--bg", 4.5], ["--text", "--surface", 4.5], ["--text", "--surface-raised", 4.5], ["--text-muted", "--bg", 4.5], ["--text-muted", "--surface", 4.5], ["--accent", "--bg", 4.5], ["--accent", "--surface", 4.5], ["--text-inverse", "--accent", 4.5], ["--success", "--surface", 4.5], ["--warning", "--surface", 4.5], ["--danger", "--surface", 4.5], ["--border-strong", "--surface", 3.0], ]; const themes = parseThemes(css); const names = Object.keys(themes); if (names.length === 0) { console.error("no themes found in tokens.css - has the file moved?"); process.exit(1); } let failures = 0; let checked = 0; for (const name of names) { const tokens = themes[name]; console.log(`\n ${name}`); for (const [fgToken, bgToken, min] of PAIRS) { const fg = tokens[fgToken]; const bg = tokens[bgToken]; if (!fg || !bg) { console.log(` ? ${fgToken} on ${bgToken} - token missing`); failures++; continue; } const ratio = contrast(fg, bg); if (ratio === null) { console.log(` ? ${fgToken} on ${bgToken} - not a plain hex colour`); continue; } checked++; const ok = ratio >= min; if (!ok) failures++; const mark = ok ? "ok " : "FAIL"; console.log( ` ${mark} ${fgToken} on ${bgToken} ${ratio.toFixed(2)}:1 (need ${min})` ); } } console.log( `\n${checked} pairs checked across ${names.length} themes, ${failures} failure(s)\n` ); process.exit(failures > 0 ? 1 : 0);