Phase 5a-4. The owner chose candidate B of each palette. - Harvest Moon uses copper from Holo's tail as the accent, cream from the tail tip, and red-brown surfaces. - Crimson Pilot uses plugsuit red as the accent, orange hair, blue eyes, and the yellow dress. Borders are a low-chroma wine. Error is a light rose red, so it differs from the accent. - Silver Serpent uses silver from the grey eyes as the accent, Slytherin emerald as the second accent, and charcoal surfaces. - All eight built-in palettes pass every readability rule. The known-failure list in contrast.spec.ts is gone. - characterPalettes.spec.ts measures text selection as the app draws it, at most 65% opaque over the page (UI-029). - Docs: UI-045, and the character references in ui-color.md. pnpm check passes. pnpm test: 446 of 446 pass. Full smoke suite: 95 passed, 2 known.
57 lines
3.0 KiB
TypeScript
57 lines
3.0 KiB
TypeScript
// Readability rules for every built-in theme (src/lib/theme/contrast.ts), and the rule that only theme data names a theme.
|
|
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
import { join, relative } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { BUILTIN_THEMES } from '../../../src/lib/theme/themes/index.ts';
|
|
import { BASE_ROLES, resolveColors, type Mode } from '../../../src/lib/theme/roles.ts';
|
|
import { RULES, checkTheme } from '../../../src/lib/theme/contrast.ts';
|
|
import { composite, contrast, distance, formatHex, mix, parseHex } from '../../../src/lib/theme/color.ts';
|
|
|
|
const variants = BUILTIN_THEMES.flatMap(theme => Object.entries(theme.variants).map(([mode, colors]) => ({ key: `${theme.id}/${mode}`, mode: mode as Mode, colors: colors! })));
|
|
|
|
for (const variant of variants) {
|
|
const findings = checkTheme(resolveColors(variant.colors, variant.mode), variant.mode);
|
|
for (const rule of RULES) {
|
|
const results = findings.filter(finding => finding.rule === rule.id);
|
|
if (!results.length) continue;
|
|
const failures = results.filter(finding => !finding.pass).map(finding => `${finding.subject}: ${finding.value.toFixed(2)} (minimum ${finding.min})`);
|
|
test(`${variant.key} passes the ${rule.id} rule`, () => {
|
|
assert.deepEqual(failures, []);
|
|
});
|
|
}
|
|
}
|
|
|
|
test('Every built-in variant gives all base roles as hex colors', () => {
|
|
for (const variant of variants) {
|
|
for (const role of BASE_ROLES) assert.match(variant.colors[role] ?? '', /^#[0-9a-f]{6}$/, `${variant.key}: ${role}`);
|
|
}
|
|
});
|
|
|
|
test('Color arithmetic matches CSS and WCAG', () => {
|
|
assert.deepEqual(parseHex('#0f08'), [0, 255, 0, 136 / 255]);
|
|
assert.equal(formatHex([255, 0, 0, 1]), '#ff0000');
|
|
assert.equal(mix('#82aaff', '#141414', 0.8), '#6c8cd0');
|
|
assert.equal(mix('#82aaff', '#0000', 0.2), '#82aaff33');
|
|
assert.equal(composite('#ffffff80', '#000000'), '#808080');
|
|
assert.equal(contrast('#000000', '#ffffff').toFixed(1), '21.0');
|
|
assert(distance('#ff0000', '#ff0000') === 0 && distance('#000000', '#ffffff') > 0.99);
|
|
});
|
|
|
|
test('Only theme data names a theme ID', () => {
|
|
const root = fileURLToPath(new URL('../../../src', import.meta.url));
|
|
const ids = BUILTIN_THEMES.flatMap(theme => [theme.id, ...('legacyIds' in theme ? theme.legacyIds : [])]).filter(id => id !== 'default');
|
|
const pattern = new RegExp(`(['"\`])(?:${ids.join('|')})\\1|data-palette=`);
|
|
const files = (function walk(folder: string): string[] {
|
|
return readdirSync(folder).flatMap(name => {
|
|
const path = join(folder, name);
|
|
return statSync(path).isDirectory() ? walk(path) : /\.(ts|svelte|css)$/.test(name) ? [path] : [];
|
|
});
|
|
})(root);
|
|
const found = files.filter(file => !relative(root, file).startsWith(join('lib', 'theme', 'themes')) && !file.endsWith('messages.ts'))
|
|
.filter(file => pattern.test(readFileSync(file, 'utf8')))
|
|
.map(file => relative(root, file));
|
|
assert.deepEqual(found, []);
|
|
});
|