Files
lucisv/editors/vscode/hierarchyPalette_test.js
T
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

154 lines
6.4 KiB
JavaScript

// Author: Guoliang Chen, a Senior Digital Design Engineer
// The current-path palette, measured: the five tints and the bar contributed in
// package.json, blended over representative sidebar colours of the built-in
// themes, must keep the row label readable (WCAG AA, 4.5:1) at every level —
// hovered too — and keep neighbouring levels visibly apart (ΔE76 ≥ 3). This is
// what stops a palette tweak from silently making the brightest row the
// hardest one to read (QA F1/F2, docs/features/hierarchy-current-path).
const assert = require('assert/strict');
const fs = require('fs');
const path = require('path');
const { BAR_COLOR, LIT_COLORS } = require('./hierarchyCurrent');
/** Sidebar background, list foreground, list hover background per theme. */
const THEMES = [
{ kind: 'dark', name: 'Dark Modern', background: '#181818', foreground: '#CCCCCC', hover: '#2A2D2E' },
{ kind: 'dark', name: 'Dark+', background: '#252526', foreground: '#CCCCCC', hover: '#2A2D2E' },
{ kind: 'light', name: 'Light Modern', background: '#F8F8F8', foreground: '#3B3B3B', hover: '#F2F2F2' },
{ kind: 'light', name: 'Light+', background: '#F3F3F3', foreground: '#616161', hover: '#E8E8E8' },
{ kind: 'highContrast', name: 'HC Dark', background: '#000000', foreground: '#FFFFFF' },
{ kind: 'highContrastLight', name: 'HC Light', background: '#FFFFFF', foreground: '#292929' },
];
const TEXT_CONTRAST = 4.5;
const BAR_CONTRAST = 3;
const NEIGHBOUR_DELTA_E = 3;
function rgba(hex) {
const value = hex.replace('#', '');
const channel = (at) => parseInt(value.slice(at, at + 2), 16);
return { rgb: [channel(0), channel(2), channel(4)], alpha: value.length === 8 ? channel(6) / 255 : 1 };
}
/** Source-over, rounded to 8 bits as the compositor does. */
function over(hex, background) {
const { rgb, alpha } = rgba(hex);
const base = rgba(background).rgb;
return rgb.map((c, at) => Math.round(c * alpha + base[at] * (1 - alpha)));
}
function linear(channel) {
const c = channel / 255;
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
}
function luminance(rgb) {
const [r, g, b] = rgb.map(linear);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function contrast(a, b) {
const [high, low] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (high + 0.05) / (low + 0.05);
}
function lab(rgb) {
const [r, g, b] = rgb.map(linear);
const x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
const y = r * 0.2126 + g * 0.7152 + b * 0.0722;
const z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
const f = (t) => (t > 0.008856 ? Math.cbrt(t) : 7.787 * t + 16 / 116);
return [116 * f(y) - 16, 500 * (f(x) - f(y)), 200 * (f(y) - f(z))];
}
function deltaE(a, b) {
const [p, q] = [lab(a), lab(b)];
return Math.hypot(p[0] - q[0], p[1] - q[1], p[2] - q[2]);
}
const toHex = (rgb) => `#${rgb.map((c) => c.toString(16).padStart(2, '0')).join('')}`;
const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
const byId = new Map(manifest.contributes.colors.map((colour) => [colour.id, colour]));
const defaultsOf = (id) => {
assert(byId.has(id), `${id} is contributed`);
return byId.get(id).defaults;
};
/** One table row per theme, printed so a palette change shows its numbers. */
function measure(theme) {
const levels = LIT_COLORS.map((id) => over(defaultsOf(id)[theme.kind], theme.background));
const unlit = rgba(theme.background).rgb;
const foreground = rgba(theme.foreground).rgb;
return {
levels,
text: levels.map((level) => contrast(foreground, level)),
hovered: theme.hover
? LIT_COLORS.map((id) => contrast(foreground, over(defaultsOf(id)[theme.kind], theme.hover)))
: [],
// Neighbours from the brightest down, then the floor against an unlit row.
steps: [...levels.slice(1).map((level, at) => deltaE(level, levels[at])), deltaE(levels.at(-1), unlit)],
fromUnlit: levels.map((level) => deltaE(level, unlit)),
bar: contrast(over(defaultsOf(BAR_COLOR)[theme.kind], theme.background), unlit),
};
}
function everyLevelKeepsTheLabelReadable() {
for (const theme of THEMES) {
const { text, hovered } = measure(theme);
text.forEach((ratio, level) => assert(ratio >= TEXT_CONTRAST,
`${theme.name}: label on level ${level} is ${ratio.toFixed(2)}:1, under ${TEXT_CONTRAST}`));
hovered.forEach((ratio, level) => assert(ratio >= TEXT_CONTRAST,
`${theme.name}: label on hovered level ${level} is ${ratio.toFixed(2)}:1, under ${TEXT_CONTRAST}`));
}
}
function neighbouringLevelsCanBeToldApart() {
for (const theme of THEMES) {
measure(theme).steps.forEach((step, at) => {
const pair = at === LIT_COLORS.length - 1 ? `floor/unlit` : `${at}/${at + 1}`;
assert(step >= NEIGHBOUR_DELTA_E, `${theme.name}: levels ${pair} differ by ΔE ${step.toFixed(1)}`);
});
}
}
function theCurrentRowIsTheBrightest() {
for (const theme of THEMES) {
const { fromUnlit } = measure(theme);
assert(fromUnlit.every((distance, at) => at === 0 || distance < fromUnlit[at - 1]),
`${theme.name}: each level is further from an unlit row than the next`);
}
}
/** The bar carries the brightness the tint gave up for legibility, so it must
* itself be visible: WCAG 1.4.11 non-text contrast against the sidebar. */
function theBarStandsOutFromTheSidebar() {
for (const theme of THEMES) {
const { bar } = measure(theme);
assert(bar >= BAR_CONTRAST, `${theme.name}: the current-row bar is ${bar.toFixed(2)}:1, under ${BAR_CONTRAST}`);
}
}
function printTable() {
for (const theme of THEMES) {
const m = measure(theme);
const fmt = (list, digits) => list.map((value) => value.toFixed(digits)).join(' ');
console.log(` ${theme.name.padEnd(12)} text ${fmt(m.text, 2)}${m.hovered.length ? ` | hovered ${fmt(m.hovered, 2)}` : ''}`
+ ` | ΔE 0/1..floor/unlit ${fmt(m.steps, 1)} | bar ${m.bar.toFixed(2)} | ${m.levels.map(toHex).join(' ')}`);
}
}
const tests = [
everyLevelKeepsTheLabelReadable,
neighbouringLevelsCanBeToldApart,
theCurrentRowIsTheBrightest,
theBarStandsOutFromTheSidebar,
];
for (const test of tests) test();
if (process.env.LUCISV_PALETTE_TABLE) printTable();
console.log(`hierarchy palette: OK — ${tests.length} checks over ${THEMES.length} themes: label ≥ ${TEXT_CONTRAST}:1 on every level and hovered, neighbours ΔE ≥ ${NEIGHBOUR_DELTA_E}, brightest first, bar ≥ ${BAR_CONTRAST}:1`);
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com