~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
98 lines · 4.1 KB Raw History
// The workspace's colours, held to WCAG 2.2's AA contrast (1.4.3): text and
// what it is drawn on at least 4.5:1, in light and in dark, as
// web/workspace/tokens.css defines them (each a `light-dark()`). A colour
// is only ever text on some surfaces, which contrast.json, beside this,
// names; a new colour that is text goes there. A theme's palette is held
// to the same by hud-core's own check (`theme::problems`, its pairs the
// palette's), which every bundled theme passes in its tests and every
// person's must to be made or chosen. As knotless's harness/contrast.mjs.
//
//   node harness/contrast.mjs    `just lint`
//
// Prints each pair that falls short as `theme: text on surface ratio`,
// failing if any.

import { readFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const AA = 4.5;

const here = dirname(fileURLToPath(import.meta.url));
const root = resolve(here, '..');
const css = readFileSync(join(root, 'web/workspace/tokens.css'), 'utf8');
const { pairs } = JSON.parse(readFileSync(join(here, 'contrast.json'), 'utf8'));
const each = (groups) => groups.flatMap((g) => g.texts.flatMap((t) => g.on.map((s) => [t, s])));

/** The declarations of the block that opens with `start` in `css`, by name. */
function declarations(start) {
	const at = css.indexOf(start);
	if (at < 0) throw new Error(`no block ${start}`);
	const body = css.slice(at, css.indexOf('\n}', at));
	return Object.fromEntries([...body.matchAll(/^\s*--([\w-]+):\s*([^;]+);/gm)].map((m) => [m[1], m[2].trim()]));
}

/** `value` with each `light-dark(a, b)` in it as its `half` (0 light, 1 dark) says. */
function scheme(value, half) {
	const at = value.indexOf('light-dark(');
	if (at < 0) return value;
	let depth = 0;
	let comma = -1;
	let i = at + 'light-dark('.length;
	for (; i < value.length; i++) {
		const c = value[i];
		if (c === '(') depth++;
		else if (c === ')' && depth-- === 0) break;
		else if (c === ',' && depth === 0) comma = i;
	}
	const args = [value.slice(at + 11, comma), value.slice(comma + 1, i)].map((a) => a.trim());
	return scheme(value.slice(0, at) + args[half] + value.slice(i + 1), half);
}

const tokens = declarations(':root {');
const light = Object.fromEntries(Object.entries(tokens).map(([k, v]) => [k, scheme(v, 0)]));
const dark = Object.fromEntries(Object.entries(tokens).map(([k, v]) => [k, scheme(v, 1)]));

/** `value` in `env` as `[r, g, b, alpha]`, each channel 0 to 255. */
function colour(env, value) {
	value = value.trim();
	let m;
	if ((m = value.match(/^var\(--([\w-]+)\)$/))) return colour(env, env[m[1]]);
	if ((m = value.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i))) {
		const h = m[1].length === 3 ? [...m[1]].map((c) => c + c).join('') : m[1];
		return [0, 2, 4].map((i) => parseInt(h.slice(i, i + 2), 16)).concat(1);
	}
	if ((m = value.match(/^rgb\((\d+) (\d+) (\d+) \/ ([\d.]+)\)$/))) return [+m[1], +m[2], +m[3], +m[4]];
	throw new Error(`cannot read ${value}`);
}

/** `top` drawn over the opaque `under`. */
const over = (top, under) => [0, 1, 2].map((i) => top[i] * top[3] + under[i] * (1 - top[3])).concat(1);

const luminance = ([r, g, b]) => {
	const [lr, lg, lb] = [r, g, b].map((c) => ((c /= 255) <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
	return 0.2126 * lr + 0.7152 * lg + 0.0722 * lb;
};

const ratio = (a, b) => {
	const [x, y] = [luminance(a), luminance(b)].sort((p, q) => q - p);
	return (x + 0.05) / (y + 0.05);
};

const problems = [];
/** Each of `checked` in `env`, whose surfaces lie on `base`. */
function check(id, env, base, checked) {
	const name = (n) => `var(--${n})`;
	const bg = colour(env, name(base));
	for (const [text, on] of checked) {
		const surface = over(colour(env, name(on)), bg);
		const found = ratio(over(colour(env, name(text)), surface), surface);
		if (found < AA) problems.push(`${id}: ${text} on ${on} ${found.toFixed(2)}:1, under ${AA}:1`);
	}
}

for (const [id, env] of Object.entries({ light, dark })) check(id, env, 'bg', each(pairs));

for (const p of problems) console.log(p);
if (problems.length) process.exit(1);
console.log(`contrast: light and dark, every text at least ${AA}:1 on what it is drawn on`);