|
| 1 | +// Rendered contract for the marginalia figure tokens. |
| 2 | +// |
| 3 | +// Figures paint with var(--fig-ink) and friends instead of literal colours, |
| 4 | +// so one paint function renders in both colour schemes. That holds only if |
| 5 | +// the page defines every token in light and dark, with the colours |
| 6 | +// src/marginalia_grammar.py documents (the social cards and gestalt pages |
| 7 | +// embed the same values). check_browser_layout.mjs measures each scheme on |
| 8 | +// the rendered page with figureThemeExpression() and reports |
| 9 | +// figureThemeFailures(); nothing here reads CSS text. |
| 10 | +import { execFileSync } from 'node:child_process'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { fileURLToPath } from 'node:url'; |
| 13 | + |
| 14 | +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 15 | + |
| 16 | +// The grammar's FIGURE_TOKENS_LIGHT and FIGURE_TOKENS_DARK, imported from Python. |
| 17 | +export function documentedFigureTokens() { |
| 18 | + const program = 'import json; from src.marginalia_grammar import FIGURE_TOKENS_DARK, FIGURE_TOKENS_LIGHT; ' |
| 19 | + + 'print(json.dumps({"light": FIGURE_TOKENS_LIGHT, "dark": FIGURE_TOKENS_DARK}))'; |
| 20 | + const output = execFileSync('uv', ['run', '--python', '3.13', 'python', '-c', program], { |
| 21 | + cwd: root, |
| 22 | + encoding: 'utf8', |
| 23 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 24 | + }); |
| 25 | + return JSON.parse(output); |
| 26 | +} |
| 27 | + |
| 28 | +// Runs in the page under the current colour scheme. `documented` maps each |
| 29 | +// token to the colour the grammar gives it in that scheme. |
| 30 | +function measureFigures(documented) { |
| 31 | + // Compare colours as the browser resolves them, not as they are spelled. |
| 32 | + const resolve = value => { |
| 33 | + const probe = document.createElement('span'); |
| 34 | + probe.style.color = value; |
| 35 | + if (!probe.style.color) return null; |
| 36 | + document.body.append(probe); |
| 37 | + const colour = getComputedStyle(probe).color; |
| 38 | + probe.remove(); |
| 39 | + return colour; |
| 40 | + }; |
| 41 | + const luminance = colour => { |
| 42 | + const [r, g, b] = colour.match(/[\d.]+/g).slice(0, 3).map(value => { |
| 43 | + const channel = Number(value) / 255; |
| 44 | + return channel <= .04045 ? channel / 12.92 : ((channel + .055) / 1.055) ** 2.4; |
| 45 | + }); |
| 46 | + return .2126 * r + .7152 * g + .0722 * b; |
| 47 | + }; |
| 48 | + const contrast = (a, b) => { |
| 49 | + const [high, low] = [luminance(a), luminance(b)].sort((x, y) => y - x); |
| 50 | + return (high + .05) / (low + .05); |
| 51 | + }; |
| 52 | + const pageBackground = getComputedStyle(document.body).backgroundColor; |
| 53 | + const svgs = [...document.querySelectorAll('.cell-banner figure svg')]; |
| 54 | + const inherited = svgs[0] ? getComputedStyle(svgs[0]) : null; |
| 55 | + const tokens = {}; |
| 56 | + for (const [name, value] of Object.entries(documented)) { |
| 57 | + tokens[name] = { value: inherited ? resolve(inherited.getPropertyValue(name).trim()) : null, documented: resolve(value) }; |
| 58 | + } |
| 59 | + // Every stroke and fill a figure paints through a token, in document order. |
| 60 | + const paints = []; |
| 61 | + for (const node of svgs.flatMap(svg => [...svg.querySelectorAll('*')])) { |
| 62 | + for (const attr of ['stroke', 'fill']) { |
| 63 | + const token = /^var\((--fig-[a-z-]+)\)$/.exec(node.getAttribute(attr) || '')?.[1]; |
| 64 | + if (!token) continue; |
| 65 | + const style = getComputedStyle(node); |
| 66 | + paints.push({ token, attr, painted: style[attr], tokenColour: resolve(style.getPropertyValue(token).trim()) }); |
| 67 | + } |
| 68 | + } |
| 69 | + const ink = paints.find(paint => paint.token === '--fig-ink' && paint.attr === 'stroke')?.painted ?? null; |
| 70 | + return { |
| 71 | + figures: svgs.length, |
| 72 | + pageBackground, |
| 73 | + chips: svgs.map(svg => getComputedStyle(svg).backgroundColor), |
| 74 | + tokens, |
| 75 | + paints, |
| 76 | + ink, |
| 77 | + inkContrast: ink?.startsWith('rgb') ? contrast(ink, pageBackground) : null, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +export function figureThemeExpression(documented) { |
| 82 | + return `(${measureFigures.toString()})(${JSON.stringify(documented)})`; |
| 83 | +} |
| 84 | + |
| 85 | +export function figureThemeFailures({ light, dark }) { |
| 86 | + const failures = []; |
| 87 | + for (const [scheme, theme] of [['light', light], ['dark', dark]]) { |
| 88 | + if (!theme.figures) { |
| 89 | + failures.push(`${scheme}: no marginalia figure on the page to measure`); |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (!(theme.inkContrast >= 4.5)) failures.push(`${scheme} marginalia figure ink contrast is ${theme.inkContrast?.toFixed(2)}:1 (ink ${theme.ink} on ${theme.pageBackground})`); |
| 93 | + // No light "paper chip" behind figures: the tokens do the work. |
| 94 | + const chips = [...new Set(theme.chips)].filter(chip => chip !== 'rgba(0, 0, 0, 0)'); |
| 95 | + if (chips.length) failures.push(`${scheme} marginalia figures sit on a painted chip (${chips.join(', ')})`); |
| 96 | + for (const [name, token] of Object.entries(theme.tokens)) { |
| 97 | + if (!token.value) failures.push(`${scheme} figure token ${name} does not resolve to a colour`); |
| 98 | + else if (token.value !== token.documented) failures.push(`${scheme} figure token ${name} is ${token.value}, not the ${token.documented} src/marginalia_grammar.py documents`); |
| 99 | + } |
| 100 | + // Against the grammar's colour as well as the node's own token, so a |
| 101 | + // token redefined on the figure or inside it cannot pass by agreeing with itself. |
| 102 | + const offToken = theme.paints.filter(paint => !paint.tokenColour || paint.painted !== paint.tokenColour |
| 103 | + || paint.painted !== theme.tokens[paint.token]?.documented); |
| 104 | + if (offToken.length) failures.push(`${scheme}: ${offToken.length} figure strokes/fills do not paint their token's colour (${offToken.slice(0, 3).map(paint => `${paint.attr}=var(${paint.token}) painted ${paint.painted}`).join('; ')})`); |
| 105 | + } |
| 106 | + // Dark mode recolours figures through the tokens, not with a second palette. |
| 107 | + if (!light.figures || !dark.figures) return failures; |
| 108 | + const kept = light.paints.filter((paint, index) => paint.painted === dark.paints[index]?.painted); |
| 109 | + if (!light.paints.length) failures.push('Marginalia figures paint no stroke or fill through a --fig-* token'); |
| 110 | + else if (light.paints.length !== dark.paints.length) failures.push(`Marginalia figures paint ${light.paints.length} token strokes/fills in light but ${dark.paints.length} in dark`); |
| 111 | + else if (kept.length) failures.push(`Marginalia figures did not recolour with the dark palette (${kept.length} of ${light.paints.length} strokes/fills kept their light colour: ${[...new Set(kept.map(paint => paint.token))].join(', ')})`); |
| 112 | + return failures; |
| 113 | +} |
0 commit comments