- The waiter's --bg/--text/--accent… are made of @xenia/ui tokens: light / dark (user menu) and the venue's accent reach every screen; the 751 colour literals in waiter_pwa/src became tokens (by hue family and lightness), hex-alpha suffixes became fade() (color-mix), fixing 'var(--accent)18' tints that never showed. - @xenia/ui: new `special` (violet: offers, discounts, deals; contrast-tested) and `on-colour` tokens. - Setting venue.theme (teal | amber | blue | violet | rose, default amber), bootstrap venue.theme, manager Settings → Venue → Colour; the waiter sets data-venue-theme and the theme before the first render. - Guard tests/no-colours.test.mjs; tests/e2e/waiter_look.mjs (every page light + dark, side-by-side compare page); waiter e2e +3; backend test_venue_theme.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
4.0 KiB
JavaScript
85 lines
4.0 KiB
JavaScript
// 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', 'special'].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')
|
|
})
|
|
|
|
// The board (kitchen display, 3.2b-2): always dark, so one set. Done items (card-muted) and
|
|
// empty states (ink-4) are meant to recede and aren't checked.
|
|
test('board token pairs are readable', () => {
|
|
const b = Object.fromEntries([...css.matchAll(/--x-board-([\w-]+)-rgb:\s*([\d\s]+);/g)].map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)]))
|
|
const boardPairs = [
|
|
['ink', 'bg', TEXT], ['ink-2', 'bg', TEXT], ['ink-2', 'line', TEXT], ['ink-3', 'rail', TEXT], ['bg', 'tab-active', TEXT],
|
|
['card-ink', 'card', TEXT], ['card-ink-2', 'card-soft', TEXT], ['note-ink', 'note', TEXT],
|
|
['late-deep', 'card', TEXT], ['fresh-deep', 'card', TEXT], ['on-status', 'fresh-deep', TEXT], ['on-status', 'late-deep', TEXT],
|
|
['warning-deep', 'card', UI], ['progress', 'card', UI], ['on-status', 'warning-deep', UI],
|
|
]
|
|
for (const [fg, bg, min] of boardPairs) {
|
|
assert.ok(b[fg] && b[bg], `board tokens ${fg} / ${bg} exist`)
|
|
const r = ratio(b[fg], b[bg])
|
|
assert.ok(r >= min, `board ${fg} on ${bg}: ${r.toFixed(2)} < ${min}`)
|
|
}
|
|
})
|