// WCAG contrast of the token pairs the components actually use, in light and dark, // for every venue theme. Reads tokens.css itself, so a colour edit that breaks // readability fails here. import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { test } from 'node:test' const css = readFileSync(new URL('../src/tokens.css', import.meta.url), 'utf8') /** Channel declarations of the first rule whose selector list is exactly `selector`. */ function rule(selector) { const re = /([^{}]+)\{([^{}]*)\}/g for (const m of css.matchAll(re)) { const sel = m[1].replace(/\/\*[\s\S]*?\*\//g, '').trim().replace(/\s+/g, ' ') if (sel !== selector) continue return Object.fromEntries([...m[2].matchAll(/--x-([\w-]+)-rgb:\s*([\d\s]+);/g)].map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)])) } throw new Error(`no rule ${selector}`) } const lum = ([r, g, b]) => { const f = c => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4 } return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b) } const ratio = (a, b) => { const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p); return (x + 0.05) / (y + 0.05) } const TEXT = 4.5 // normal-size text (AA) const UI = 3 // component boundaries and states (AA, 1.4.11) const pairs = [ ['ink', 'surface', TEXT], ['ink-2', 'surface', TEXT], ['ink-3', 'surface', TEXT], ['ink', 'canvas', TEXT], ['ink-2', 'surface-2', TEXT], ['canvas', 'ink', TEXT], // InfoTip popover ['accent-fg', 'accent', TEXT], ['accent-fg', 'accent-strong', TEXT], ['accent-ink', 'accent-soft', TEXT], ['accent-ink', 'surface', TEXT], ['accent', 'surface', UI], // switch on, focus, filled PIN dots ['ink-3', 'surface', UI], // switch off track ...['success', 'warning', 'danger', 'info'].flatMap(t => [ [`${t}-fg`, t, TEXT], [`${t}-ink`, `${t}-soft`, TEXT], [`${t}-ink`, 'surface', TEXT], [t, 'surface', UI], ]), ] const venues = ['teal', 'amber', 'blue', 'violet', 'rose'] const light = rule(':root') const dark = rule('[data-theme="dark"]') for (const venue of venues) { for (const [mode, base, sel] of [ ['light', light, `[data-venue-theme="${venue}"]`], ['dark', dark, `[data-theme="dark"][data-venue-theme="${venue}"], [data-theme="dark"] [data-venue-theme="${venue}"]`], ]) { test(`${venue} / ${mode}`, () => { const t = { ...base, ...rule(sel) } const failures = pairs .map(([fg, bg, min]) => [fg, bg, min, ratio(t[fg], t[bg])]) .filter(([, , min, r]) => r < min) .map(([fg, bg, min, r]) => `${fg} on ${bg}: ${r.toFixed(2)} < ${min}`) assert.deepEqual(failures, []) }) } } test('system theme mirrors dark', () => { const sys = css.slice(css.indexOf('@media (prefers-color-scheme: dark)')) assert.match(sys, /\[data-theme="system"\]/) const darkCanvas = dark.canvas.join(' ') assert.ok(sys.includes(`--x-canvas-rgb: ${darkCanvas};`), 'system dark uses the dark canvas') })