decklight 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PLATFORM.md +666 -0
- package/README.md +224 -150
- package/SPEC.md +732 -886
- package/cli/agent-review.mjs +245 -0
- package/cli/agents.mjs +25 -5
- package/cli/associate.mjs +12 -12
- package/cli/audit.mjs +42 -10
- package/cli/banner.mjs +41 -17
- package/cli/bundle-audio.mjs +183 -0
- package/cli/bundle.mjs +383 -31
- package/cli/cast.mjs +7 -7
- package/cli/check.mjs +961 -0
- package/cli/clone-deck.mjs +179 -0
- package/cli/commands.mjs +345 -0
- package/cli/comments.mjs +64 -18
- package/cli/commit-flow.mjs +44 -19
- package/cli/commit-message.mjs +51 -74
- package/cli/commit-split.mjs +601 -0
- package/cli/deck-refs.mjs +59 -0
- package/cli/deck-routes.mjs +67 -0
- package/cli/deckfile.mjs +3 -5
- package/cli/decklight.mjs +68 -289
- package/cli/design-system-deps.mjs +209 -0
- package/cli/design-system-edit.mjs +194 -0
- package/cli/design-system-refs.mjs +409 -0
- package/cli/design-system.mjs +429 -0
- package/cli/doctor.mjs +115 -0
- package/cli/edit-git.mjs +353 -0
- package/cli/edit-handover.mjs +445 -0
- package/cli/edit-look.mjs +388 -0
- package/cli/edit-slides.mjs +683 -0
- package/cli/edit.mjs +2126 -842
- package/cli/enhance.mjs +483 -0
- package/cli/extension.mjs +10 -10
- package/cli/font-refs.mjs +273 -0
- package/cli/font.mjs +246 -0
- package/cli/git-tree.mjs +1 -1
- package/cli/git.mjs +65 -11
- package/cli/import.mjs +56 -10
- package/cli/init.mjs +400 -234
- package/cli/library.mjs +127 -0
- package/cli/loader.mjs +3 -1
- package/cli/marketplace.mjs +215 -21
- package/cli/narrate.mjs +176 -0
- package/cli/open-browser.mjs +63 -0
- package/cli/open.mjs +943 -0
- package/cli/pdf.mjs +41 -13
- package/cli/pkg.mjs +44 -2
- package/cli/plugin.mjs +43 -41
- package/cli/port-conflict.mjs +53 -23
- package/cli/pptx-export.mjs +57 -24
- package/cli/publish.mjs +8 -4
- package/cli/qr.mjs +1 -1
- package/cli/record.mjs +14 -12
- package/cli/remote.mjs +11 -11
- package/cli/restore.mjs +11 -4
- package/cli/review-ref.mjs +125 -0
- package/cli/review-remote.mjs +92 -48
- package/cli/review-routes.mjs +254 -0
- package/cli/review-store.mjs +47 -7
- package/cli/review-submit.mjs +47 -57
- package/cli/runtime-link.mjs +560 -0
- package/cli/serve.mjs +235 -44
- package/cli/setup.mjs +112 -0
- package/cli/sign.mjs +1 -1
- package/cli/skill-content.mjs +190 -33
- package/cli/skills.mjs +107 -24
- package/cli/start.mjs +110 -0
- package/cli/supervise.mjs +6 -6
- package/cli/theme-refs.mjs +515 -0
- package/cli/theme.mjs +181 -137
- package/cli/trust.mjs +83 -0
- package/cli/units.mjs +39 -42
- package/cli/update-check.mjs +1 -5
- package/cli/upgrade.mjs +194 -28
- package/cli/upstream.mjs +4 -3
- package/cli/util.mjs +4 -4
- package/cli/wizard.mjs +6 -6
- package/dist/build-info.json +1 -1
- package/dist/decklight.css +1126 -49
- package/dist/decklight.js +212 -115
- package/dist/decklight.js.map +4 -4
- package/docs/GLOSSARY.md +141 -0
- package/docs/architecture.svg +17 -17
- package/package.json +4 -2
- package/themes/README.md +9 -8
- package/themes/reveal-solarized.css +10 -0
- package/tools/args.mjs +20 -0
- package/tools/atomic-write.mjs +48 -0
- package/tools/brand-palette.mjs +717 -0
- package/tools/bridge.mjs +1 -1
- package/tools/build-groups.mjs +42 -0
- package/tools/color.mjs +48 -0
- package/tools/deck-html.mjs +396 -11
- package/tools/design-system-format.mjs +753 -0
- package/tools/elevenlabs-tts.mjs +110 -17
- package/tools/exec.mjs +1 -1
- package/tools/extension-check.mjs +31 -7
- package/tools/font-format.mjs +190 -0
- package/tools/font-stacks.mjs +24 -0
- package/tools/gemini-tts.mjs +3 -0
- package/tools/lipsync-config.mjs +103 -0
- package/tools/lipsync-engines.mjs +205 -2
- package/tools/lipsync-server.mjs +264 -41
- package/tools/lipsync.mjs +37 -18
- package/tools/local-voice.mjs +32 -42
- package/tools/narration-manifest.mjs +196 -0
- package/tools/narration-synth.mjs +416 -0
- package/tools/pptx.mjs +776 -19
- package/tools/publish-targets.mjs +1 -1
- package/tools/publish-voices.mjs +3 -3
- package/tools/recording-impact.mjs +7 -3
- package/tools/render-theme.mjs +33 -0
- package/tools/review-anchor.mjs +28 -12
- package/tools/semver.mjs +40 -0
- package/tools/sentences.mjs +180 -0
- package/tools/shot.mjs +14 -7
- package/tools/site-voices.mjs +232 -0
- package/tools/template-theme.mjs +1 -1
- package/tools/test-impact.mjs +17 -10
- package/tools/theme-check.mjs +30 -3
- package/tools/tts-cache.mjs +4 -3
- package/tools/tts-engines.mjs +38 -20
- package/tools/tts-setup.mjs +7 -7
- package/tools/video-options.mjs +34 -0
- package/tools/video.mjs +461 -48
- package/tools/voiceover-server.mjs +185 -104
- package/tools/voiceover.mjs +119 -261
- package/tools/zip.mjs +1 -1
- package/cli/dev.mjs +0 -623
- package/cli/present.mjs +0 -853
- package/cli/review.mjs +0 -361
- package/tools/publish-site-voices.mjs +0 -75
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
// Copyright 2026 Gilles Philippart
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// A design system's palette, and the themes drawn from it (SPEC BRAND_PALETTE).
|
|
5
|
+
//
|
|
6
|
+
// The palette is the brand: a dozen or so named colours, each in weights from
|
|
7
|
+
// 100 to 1000. A theme is one way of wearing it — dark, light, a projector's
|
|
8
|
+
// high contrast — and may use nothing BUT its swatches, so a brand theme is on
|
|
9
|
+
// brand by construction: an off-palette colour cannot be written down at all.
|
|
10
|
+
//
|
|
11
|
+
// PURE, like tools/design-system-format.mjs beside it: texts and objects in,
|
|
12
|
+
// objects out, nothing read from disk. `design-system gate` runs it on a
|
|
13
|
+
// package; the servers and `bundle` read what the check already worked out.
|
|
14
|
+
//
|
|
15
|
+
// What is derived is only what the palette lacks. A palette with a neutral
|
|
16
|
+
// scale gets no neutral; a scale with all ten weights gets no weight. What a
|
|
17
|
+
// THEME leaves unstated is chosen among swatches — never mixed — by the same
|
|
18
|
+
// roles the theme generator fills (THEMING R1–R8), and every expanded theme
|
|
19
|
+
// answers the shipped themes' contrast gates (tools/theme-check.mjs).
|
|
20
|
+
|
|
21
|
+
import { contrast, parseColor, toOklab, fromOklab, rgbToHex } from './color.mjs';
|
|
22
|
+
import { validateTheme, NESTED_MIN_DL } from './theme-check.mjs';
|
|
23
|
+
|
|
24
|
+
/** The weights a scale may define: 100, 200 … 1000. */
|
|
25
|
+
export const WEIGHTS = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
|
|
26
|
+
|
|
27
|
+
/** A colour's (and a theme's) name: what a theme writes, so it stays plain. */
|
|
28
|
+
export const SWATCH_NAME_RE = /^[a-z][a-z0-9-]*$/;
|
|
29
|
+
|
|
30
|
+
/** Below this OKLCH chroma everywhere, a scale reads as a neutral. */
|
|
31
|
+
export const NEUTRAL_CHROMA = 0.03;
|
|
32
|
+
|
|
33
|
+
/** The chroma of a derived neutral: a trace of the brand's hue (THEMING R6). */
|
|
34
|
+
const DERIVED_NEUTRAL_CHROMA = 0.012;
|
|
35
|
+
|
|
36
|
+
/** A scale needs at least this many weights before the rest can be derived from it. */
|
|
37
|
+
export const MIN_WEIGHTS = 3;
|
|
38
|
+
|
|
39
|
+
/** A swatch nearer than this (OKLab distance) to a palette colour counts as that colour. */
|
|
40
|
+
export const ON_PALETTE_DISTANCE = 0.02;
|
|
41
|
+
|
|
42
|
+
const HEX_RE = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
43
|
+
|
|
44
|
+
const lab = (hex) => toOklab(parseColor(hex));
|
|
45
|
+
const lightness = (hex) => lab(hex)[0];
|
|
46
|
+
const chromaOf = ([, a, b]) => Math.hypot(a, b);
|
|
47
|
+
const hueOf = ([, a, b]) => ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360;
|
|
48
|
+
const hueGap = (x, y) => { const d = Math.abs(x - y) % 360; return d > 180 ? 360 - d : d; };
|
|
49
|
+
const hexOf = (labValue) => rgbToHex(fromOklab(labValue));
|
|
50
|
+
const labDistance = (p, q) => Math.hypot(p[0] - q[0], p[1] - q[1], p[2] - q[2]);
|
|
51
|
+
const ratio = (fg, bg) => contrast(parseColor(fg), parseColor(bg));
|
|
52
|
+
|
|
53
|
+
// ── the palette ─────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Read a manifest's scale palette: `{ colors: { <name>: { label, role?,
|
|
57
|
+
* weights: { "100": "#hex", … } } } }`.
|
|
58
|
+
*
|
|
59
|
+
* Returns `{ scales, neutral, problems, notes }`. `scales` maps each colour
|
|
60
|
+
* name to `{ name, label, role, weights: { 100: '#hex', … }, derived: [w…] }`
|
|
61
|
+
* with every weight 100–1000 present — the ones the manifest left out derived
|
|
62
|
+
* (OKLab interpolation between its own neighbours; at the ends, its own hue
|
|
63
|
+
* and chroma at the lightness the palette's other scales have there) — plus a
|
|
64
|
+
* derived `neutral` scale when the palette has none. `problems` refuse the
|
|
65
|
+
* package; `notes` are what the engine made up, said so a brand reviewer sees
|
|
66
|
+
* it. Each is `{ rule, msg }`.
|
|
67
|
+
*/
|
|
68
|
+
export function readScales(palette) {
|
|
69
|
+
const problems = [];
|
|
70
|
+
const notes = [];
|
|
71
|
+
const scales = {};
|
|
72
|
+
const colors = palette?.colors;
|
|
73
|
+
if (!colors || typeof colors !== 'object' || Array.isArray(colors) || !Object.keys(colors).length) {
|
|
74
|
+
problems.push({ rule: 'palette-shape', msg: 'palette must be { colors: { <name>: { label, weights: { "100": "#hex", … "1000": "#hex" } } } }' });
|
|
75
|
+
return { scales, neutral: null, problems, notes };
|
|
76
|
+
}
|
|
77
|
+
for (const [name, c] of Object.entries(colors)) {
|
|
78
|
+
const where = `palette.colors.${name}`;
|
|
79
|
+
if (!SWATCH_NAME_RE.test(name)) { problems.push({ rule: 'palette-shape', msg: `${where}: a colour's name is a lowercase word (letters, digits and -), e.g. "blue"` }); continue; }
|
|
80
|
+
if (!c || typeof c !== 'object' || typeof c.label !== 'string' || !c.label.trim() || !c.weights || typeof c.weights !== 'object') {
|
|
81
|
+
problems.push({ rule: 'palette-shape', msg: `${where} must be { label, weights: { "100": "#hex", … } }` });
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (c.role !== undefined && c.role !== 'neutral') problems.push({ rule: 'palette-shape', msg: `${where}.role ${JSON.stringify(c.role)} — the one role a colour can have is "neutral"` });
|
|
85
|
+
const weights = {};
|
|
86
|
+
let bad = false;
|
|
87
|
+
for (const [k, v] of Object.entries(c.weights)) {
|
|
88
|
+
const w = Number(k);
|
|
89
|
+
if (!WEIGHTS.includes(w) || String(w) !== k) { problems.push({ rule: 'palette-shape', msg: `${where}: weight "${k}" — weights are 100, 200 … 1000` }); bad = true; continue; }
|
|
90
|
+
if (typeof v !== 'string' || !HEX_RE.test(v)) { problems.push({ rule: 'palette-shape', msg: `${where}.${k}: ${JSON.stringify(v)} is not a hex colour (#rgb or #rrggbb)` }); bad = true; continue; }
|
|
91
|
+
weights[w] = v.toLowerCase();
|
|
92
|
+
}
|
|
93
|
+
if (bad) continue;
|
|
94
|
+
const have = WEIGHTS.filter((w) => weights[w]);
|
|
95
|
+
if (have.length < MIN_WEIGHTS) {
|
|
96
|
+
problems.push({ rule: 'palette-too-narrow', msg: `${where} defines ${have.length} weight${have.length === 1 ? '' : 's'} — a scale needs at least ${MIN_WEIGHTS} before the rest can be derived from it` });
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
for (let i = 1; i < have.length; i++) {
|
|
100
|
+
const [a, b] = [have[i - 1], have[i]];
|
|
101
|
+
if (!(lightness(weights[b]) < lightness(weights[a]))) {
|
|
102
|
+
problems.push({ rule: 'palette-order', msg: `${where}: ${b} (${weights[b]}) is not darker than ${a} (${weights[a]}) — weights run from 100, the lightest, to 1000, the darkest` });
|
|
103
|
+
bad = true;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (bad) continue;
|
|
108
|
+
scales[name] = { name, label: c.label.trim(), role: c.role === 'neutral' ? 'neutral' : null, weights, derived: [] };
|
|
109
|
+
}
|
|
110
|
+
if (problems.length) return { scales, neutral: null, problems, notes };
|
|
111
|
+
|
|
112
|
+
// the lightness the palette has at each weight: where a scale's own ends are extrapolated to
|
|
113
|
+
const meanL = {};
|
|
114
|
+
for (const w of WEIGHTS) {
|
|
115
|
+
const ls = Object.values(scales).map((s) => s.weights[w]).filter(Boolean).map(lightness);
|
|
116
|
+
meanL[w] = ls.length ? ls.reduce((x, y) => x + y, 0) / ls.length : null;
|
|
117
|
+
}
|
|
118
|
+
// a weight no scale defines at all: between its neighbours' means, or the far ends of the range
|
|
119
|
+
for (const w of WEIGHTS) {
|
|
120
|
+
if (meanL[w] !== null) continue;
|
|
121
|
+
const below = WEIGHTS.filter((v) => v < w && meanL[v] !== null).pop();
|
|
122
|
+
const above = WEIGHTS.find((v) => v > w && meanL[v] !== null);
|
|
123
|
+
meanL[w] = below && above ? meanL[below] + ((meanL[above] - meanL[below]) * (w - below)) / (above - below)
|
|
124
|
+
: below ? Math.max(0.12, meanL[below] - ((w - below) / 100) * 0.08) : Math.min(0.98, meanL[above] + ((above - w) / 100) * 0.03);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const s of Object.values(scales)) {
|
|
128
|
+
const have = WEIGHTS.filter((w) => s.weights[w]);
|
|
129
|
+
for (const w of WEIGHTS) {
|
|
130
|
+
if (s.weights[w]) continue;
|
|
131
|
+
const below = have.filter((v) => v < w).pop();
|
|
132
|
+
const above = have.find((v) => v > w);
|
|
133
|
+
let value;
|
|
134
|
+
if (below && above) {
|
|
135
|
+
const [p, q] = [lab(s.weights[below]), lab(s.weights[above])];
|
|
136
|
+
const t = (w - below) / (above - below);
|
|
137
|
+
value = hexOf(p.map((v, i) => v + (q[i] - v) * t));
|
|
138
|
+
} else {
|
|
139
|
+
// an end: the nearest weight's hue and chroma, at the palette's lightness there
|
|
140
|
+
const near = lab(s.weights[below ?? above]);
|
|
141
|
+
const c = chromaOf(near); const h = (hueOf(near) * Math.PI) / 180;
|
|
142
|
+
const L = below ? Math.min(meanL[w], lightness(s.weights[below]) - 0.03) : Math.max(meanL[w], lightness(s.weights[above]) + 0.03);
|
|
143
|
+
value = hexOf([L, c * Math.cos(h), c * Math.sin(h)]);
|
|
144
|
+
}
|
|
145
|
+
s.weights[w] = value;
|
|
146
|
+
s.derived.push(w);
|
|
147
|
+
}
|
|
148
|
+
if (s.derived.length) {
|
|
149
|
+
notes.push({ rule: 'palette-derived-weight', msg: `${s.name}: ${s.derived.join(', ')} derived from the weights it defines` });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// the neutral scale: the one marked, else the one with no chroma, else derived
|
|
154
|
+
let neutral = Object.values(scales).find((s) => s.role === 'neutral')?.name ?? null;
|
|
155
|
+
if (!neutral) {
|
|
156
|
+
const grey = Object.values(scales).find((s) => WEIGHTS.every((w) => chromaOf(lab(s.weights[w])) < NEUTRAL_CHROMA));
|
|
157
|
+
if (grey) {
|
|
158
|
+
neutral = grey.name;
|
|
159
|
+
notes.push({ rule: 'palette-neutral', msg: `${grey.name} is taken as the neutral scale (no chroma); mark it "role": "neutral" to say so` });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (!neutral) {
|
|
163
|
+
const first = Object.values(scales)[0];
|
|
164
|
+
const h = (hueOf(lab(first.weights[500])) * Math.PI) / 180;
|
|
165
|
+
const name = scales.neutral ? 'neutral-derived' : 'neutral';
|
|
166
|
+
const weights = {};
|
|
167
|
+
for (const w of WEIGHTS) weights[w] = hexOf([meanL[w], DERIVED_NEUTRAL_CHROMA * Math.cos(h), DERIVED_NEUTRAL_CHROMA * Math.sin(h)]);
|
|
168
|
+
scales[name] = { name, label: 'Neutral', role: 'neutral', weights, derived: [...WEIGHTS], generated: true };
|
|
169
|
+
neutral = name;
|
|
170
|
+
notes.push({ rule: 'palette-derived-neutral', msg: `the palette has no neutral scale: "${name}" is derived, tinted with a trace of ${first.name}` });
|
|
171
|
+
}
|
|
172
|
+
return { scales, neutral, problems, notes };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The token a swatch is: `--<prefix><colour>-<weight>`. */
|
|
176
|
+
export const swatchToken = (prefix, color, weight) => `${prefix}${color}-${weight}`;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The stylesheet that defines every swatch as a token, on `.decklight`: what a
|
|
180
|
+
* server serves beside the design system's own stylesheet, and `bundle`
|
|
181
|
+
* inlines. The manifest is the one source of these values.
|
|
182
|
+
*/
|
|
183
|
+
export function paletteCss(scales, prefix) {
|
|
184
|
+
const lines = [];
|
|
185
|
+
for (const s of Object.values(scales)) for (const w of WEIGHTS) lines.push(` ${swatchToken(prefix, s.name, w)}: ${s.weights[w]};`);
|
|
186
|
+
return `.decklight {\n${lines.join('\n')}\n}\n`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The palette as the page is told it (the design-system meta): one entry per
|
|
191
|
+
* swatch, `{ group, label, short, token, hex, derived }`, a colour's ten in
|
|
192
|
+
* weight order — what the Colors tab draws as one row per colour.
|
|
193
|
+
*/
|
|
194
|
+
export function paletteEntries(scales, prefix) {
|
|
195
|
+
const out = [];
|
|
196
|
+
for (const s of Object.values(scales)) {
|
|
197
|
+
for (const w of WEIGHTS) {
|
|
198
|
+
out.push({ group: s.label, label: `${s.label} ${w}`, short: String(w), token: swatchToken(prefix, s.name, w), hex: s.weights[w], derived: s.derived.includes(w) || undefined, ref: `${s.name}-${w}` });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The token a role is: `--<prefix>color-<role>` (SPEC DESIGN_SYSTEM_ROLES). */
|
|
205
|
+
export const roleToken = (prefix, id) => `${prefix}color-${id}`;
|
|
206
|
+
|
|
207
|
+
/** `blue-700` or `blue-700/40` → `{ color, weight, alpha }`, or null for anything else. */
|
|
208
|
+
export function parseSwatchRef(ref) {
|
|
209
|
+
const m = /^([a-z][a-z0-9-]*)-(\d{3,4})(?:\/(\d{1,3}))?$/.exec(String(ref ?? ''));
|
|
210
|
+
if (!m) return null;
|
|
211
|
+
const weight = Number(m[2]);
|
|
212
|
+
const alpha = m[3] === undefined ? null : Number(m[3]);
|
|
213
|
+
if (!WEIGHTS.includes(weight) || (alpha !== null && (alpha < 1 || alpha > 99))) return null;
|
|
214
|
+
return { color: m[1], weight, alpha };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The swatch nearest `hex` (OKLab), as `{ ref, color, weight, hex, distance }`. */
|
|
218
|
+
export function nearestSwatch(scales, hex) {
|
|
219
|
+
const target = lab(hex);
|
|
220
|
+
let best = null;
|
|
221
|
+
for (const s of Object.values(scales)) {
|
|
222
|
+
for (const w of WEIGHTS) {
|
|
223
|
+
const d = labDistance(target, lab(s.weights[w]));
|
|
224
|
+
if (!best || d < best.distance) best = { ref: `${s.name}-${w}`, color: s.name, weight: w, hex: s.weights[w], distance: d };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return best;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ── themes ──────────────────────────────────────────────────────────────────
|
|
231
|
+
|
|
232
|
+
/** The theme contract's colour tokens: what a brand theme may give only a swatch. */
|
|
233
|
+
export const COLOUR_TOKENS = new Set([
|
|
234
|
+
'--bg', '--bg-accent', '--fg', '--muted', '--heading-color', '--link', '--accent', '--accent-contrast',
|
|
235
|
+
'--block-bg', '--block-border', '--code-bg', '--code-fg',
|
|
236
|
+
'--hl-keyword', '--hl-string', '--hl-number', '--hl-comment', '--hl-function', '--hl-type', '--hl-punct',
|
|
237
|
+
'--d-stroke', '--d-text', '--d-muted', '--d-accent',
|
|
238
|
+
...[1, 2, 3, 4, 5, 6].flatMap((i) => [`--d-fill-${i}`, `--d-fill-${i}-in`]),
|
|
239
|
+
'--term-bg', '--term-fg', '--term-prompt', '--term-cursor', '--term-selection',
|
|
240
|
+
...['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'].flatMap((a) => [`--ansi-${a}`, `--ansi-bright-${a}`]),
|
|
241
|
+
]);
|
|
242
|
+
/** …and the ones a brand theme states as plain values, as a theme file would. */
|
|
243
|
+
const PLAIN_TOKENS = {
|
|
244
|
+
'--font-body': /^[^{};<>]{1,200}$/,
|
|
245
|
+
'--font-heading': /^[^{};<>]{1,200}$/,
|
|
246
|
+
'--font-mono': /^[^{};<>]{1,200}$/,
|
|
247
|
+
'--heading-weight': /^[1-9]00$/,
|
|
248
|
+
'--block-radius': /^\d{1,2}(?:\.\d)?px$/,
|
|
249
|
+
'--build-duration': /^\d{2,4}ms$/,
|
|
250
|
+
'--dim-opacity': /^(?:0(?:\.\d{1,2})?|1)$/,
|
|
251
|
+
'--shadow': /^none$/,
|
|
252
|
+
};
|
|
253
|
+
/** Colour tokens a contrast gate reads: these must be opaque. */
|
|
254
|
+
export const GATED = new Set([
|
|
255
|
+
'--bg', '--fg', '--muted', '--heading-color', '--link', '--accent', '--accent-contrast', '--code-bg', '--code-fg',
|
|
256
|
+
'--hl-keyword', '--hl-string', '--hl-number', '--hl-comment', '--hl-function', '--hl-type', '--hl-punct',
|
|
257
|
+
'--d-text', '--d-muted', '--d-accent', ...[1, 2, 3, 4, 5, 6].flatMap((i) => [`--d-fill-${i}`, `--d-fill-${i}-in`]),
|
|
258
|
+
'--term-bg', '--term-fg', '--term-prompt',
|
|
259
|
+
...['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'].flatMap((a) => [`--ansi-${a}`, `--ansi-bright-${a}`]),
|
|
260
|
+
]);
|
|
261
|
+
|
|
262
|
+
const SANS = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
|
|
263
|
+
const MONO = '"SF Mono", SFMono-Regular, ui-monospace, "Cascadia Code", Menlo, Consolas, monospace';
|
|
264
|
+
|
|
265
|
+
// The hue each syntax and terminal role is recognised by (OKLCH degrees): R8,
|
|
266
|
+
// terminal red stays red. A role takes the palette colour nearest its hue.
|
|
267
|
+
const ROLE_HUES = {
|
|
268
|
+
'--hl-keyword': 305, '--hl-string': 145, '--hl-number': 60, '--hl-function': 255, '--hl-type': 195,
|
|
269
|
+
red: 28, green: 145, yellow: 100, blue: 255, magenta: 330, cyan: 205,
|
|
270
|
+
};
|
|
271
|
+
/** Past this hue gap the nearest colour is said to be a stand-in. */
|
|
272
|
+
const ROLE_REACH = 40;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Expand one brand theme: `{ name, title, mode, tokens }` from the manifest
|
|
276
|
+
* against the palette. Returns `{ name, title, mode, css, gateCss, tokens,
|
|
277
|
+
* stated, chosen, problems, notes }`: `css` is the theme as a page applies it
|
|
278
|
+
* (each swatch `var(--acme-blue-700, #hex)`, so it keeps its colours if the
|
|
279
|
+
* design system's stylesheet is missing), `gateCss` the same in plain colours
|
|
280
|
+
* — what the contrast gates read — and `tokens` the plain-colour map.
|
|
281
|
+
*/
|
|
282
|
+
export function expandTheme(read, theme, prefix, { roles = null, contract = null } = {}) {
|
|
283
|
+
const { scales, neutral } = read;
|
|
284
|
+
const problems = [];
|
|
285
|
+
const notes = [];
|
|
286
|
+
const where = `themes: ${theme?.name ?? '?'}`;
|
|
287
|
+
const out = { name: theme?.name, title: theme?.title, mode: theme?.mode, css: '', gateCss: '', tokens: {}, refs: {}, stated: [], chosen: [], roleRefs: {}, roleValues: {}, viaRole: {}, problems, notes };
|
|
288
|
+
const N = scales[neutral];
|
|
289
|
+
const colours = Object.values(scales).filter((s) => s.name !== neutral);
|
|
290
|
+
const hexOfRef = (r) => scales[r.color]?.weights[r.weight];
|
|
291
|
+
// every value: the plain colour (gates), and what the page gets
|
|
292
|
+
const plain = {}; const live = {};
|
|
293
|
+
const swatchCss = (color, weight) => `var(${swatchToken(prefix, color, weight)}, ${scales[color].weights[weight]})`;
|
|
294
|
+
const rgba = (hex, a) => { const [r, g, b] = parseColor(hex); return `rgba(${r}, ${g}, ${b}, ${a / 100})`; };
|
|
295
|
+
const set = (token, color, weight, alpha = null) => {
|
|
296
|
+
const hex = scales[color].weights[weight];
|
|
297
|
+
plain[token] = alpha === null ? hex : rgba(hex, alpha);
|
|
298
|
+
live[token] = alpha === null ? swatchCss(color, weight) : `color-mix(in srgb, ${swatchCss(color, weight)} ${alpha}%, transparent)`;
|
|
299
|
+
out.refs[token] = `${color}-${weight}${alpha === null ? '' : `/${alpha}`}`;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// ── the design system's roles (SPEC DESIGN_SYSTEM_ROLES): every one a swatch, in every theme
|
|
303
|
+
const roleLive = {};
|
|
304
|
+
const themeRoles = theme?.roles && typeof theme.roles === 'object' && !Array.isArray(theme.roles) ? theme.roles : null;
|
|
305
|
+
if (roles) {
|
|
306
|
+
const gatedRole = new Set(Object.entries(contract ?? {}).filter(([t]) => GATED.has(t)).map(([, r]) => r));
|
|
307
|
+
for (const id of Object.keys(roles)) {
|
|
308
|
+
const value = themeRoles?.[id];
|
|
309
|
+
if (value === undefined) {
|
|
310
|
+
problems.push({ rule: 'theme-role-missing', msg: `${where}: gives no swatch to the role ${id} — every theme sets every role, "roles": { "${id}": "<colour>-<weight>" }` });
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
const r = parseSwatchRef(value);
|
|
314
|
+
if (!r || !hexOfRef(r)) {
|
|
315
|
+
problems.push({ rule: 'theme-off-palette', msg: `${where}: role ${id} ${JSON.stringify(value)} names no swatch — a role is a swatch of the palette, "<colour>-<weight>" or "<colour>-<weight>/<opacity>"` });
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (r.alpha !== null && gatedRole.has(id)) {
|
|
319
|
+
problems.push({ rule: 'theme-off-palette', msg: `${where}: role ${id} ${value} — the contract reads it for a colour a contrast gate checks, so it is opaque` });
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const hex = scales[r.color].weights[r.weight];
|
|
323
|
+
out.roleRefs[id] = value;
|
|
324
|
+
out.roleValues[id] = r.alpha === null ? hex : rgba(hex, r.alpha);
|
|
325
|
+
roleLive[roleToken(prefix, id)] = r.alpha === null ? swatchCss(r.color, r.weight) : `color-mix(in srgb, ${swatchCss(r.color, r.weight)} ${r.alpha}%, transparent)`;
|
|
326
|
+
}
|
|
327
|
+
for (const id of Object.keys(themeRoles ?? {})) {
|
|
328
|
+
if (!Object.hasOwn(roles, id)) problems.push({ rule: 'theme-role-unknown', msg: `${where}: sets the role ${id}, which the manifest's "roles" does not declare` });
|
|
329
|
+
}
|
|
330
|
+
} else if (themeRoles && roles !== false) {
|
|
331
|
+
problems.push({ rule: 'theme-role-unknown', msg: `${where}: sets "roles", but the manifest declares none — declare them in a top-level "roles"` });
|
|
332
|
+
}
|
|
333
|
+
if (problems.length) return out;
|
|
334
|
+
|
|
335
|
+
// ── what the brand stated: the contract's roles first, then the theme's own tokens, which win
|
|
336
|
+
const own = theme?.tokens && typeof theme.tokens === 'object' && !Array.isArray(theme.tokens) ? theme.tokens : {};
|
|
337
|
+
const fromRoles = {};
|
|
338
|
+
for (const [token, id] of Object.entries(roles ? contract ?? {} : {})) {
|
|
339
|
+
if (Object.hasOwn(own, token) || out.roleRefs[id] === undefined) continue;
|
|
340
|
+
fromRoles[token] = out.roleRefs[id];
|
|
341
|
+
out.viaRole[token] = id;
|
|
342
|
+
}
|
|
343
|
+
const stated = { ...fromRoles, ...own };
|
|
344
|
+
for (const [token, value] of Object.entries(stated)) {
|
|
345
|
+
if (COLOUR_TOKENS.has(token)) {
|
|
346
|
+
if (token === '--bg' && value && typeof value === 'object' && !Array.isArray(value)) {
|
|
347
|
+
const stops = Array.isArray(value.gradient) ? value.gradient.map(parseSwatchRef) : [];
|
|
348
|
+
const angle = value.angle ?? 180;
|
|
349
|
+
if (stops.length < 2 || stops.some((r) => !r || r.alpha !== null || !hexOfRef(r)) || !Number.isFinite(angle)) {
|
|
350
|
+
problems.push({ rule: 'theme-off-palette', msg: `${where}: --bg gradient must be { "gradient": ["<swatch>", "<swatch>"…], "angle": <deg> }, every stop a swatch of the palette` });
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
plain[token] = `linear-gradient(${angle}deg, ${stops.map(hexOfRef).join(', ')})`;
|
|
354
|
+
live[token] = `linear-gradient(${angle}deg, ${stops.map((r) => swatchCss(r.color, r.weight)).join(', ')})`;
|
|
355
|
+
out.refs[token] = stops.map((r) => `${r.color}-${r.weight}`).join(' → ');
|
|
356
|
+
out.stated.push(token);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const r = parseSwatchRef(value);
|
|
360
|
+
if (!r || !hexOfRef(r)) {
|
|
361
|
+
problems.push({ rule: 'theme-off-palette', msg: `${where}: ${token} ${JSON.stringify(value)} names no swatch — a brand theme's colours are swatches of its palette, "<colour>-<weight>" (e.g. "blue-700"), or "<colour>-<weight>/<opacity>"` });
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (r.alpha !== null && GATED.has(token)) {
|
|
365
|
+
problems.push({ rule: 'theme-off-palette', msg: `${where}: ${token} ${value} — a colour a contrast gate reads is opaque; an opacity is for a shadow, a selection or a block` });
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
set(token, r.color, r.weight, r.alpha);
|
|
369
|
+
out.stated.push(token);
|
|
370
|
+
} else if (PLAIN_TOKENS[token]) {
|
|
371
|
+
if (typeof value !== 'string' || !PLAIN_TOKENS[token].test(value.trim())) {
|
|
372
|
+
problems.push({ rule: 'theme-token', msg: `${where}: ${token} ${JSON.stringify(value)} is not a value this token takes` });
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
plain[token] = live[token] = value.trim();
|
|
376
|
+
out.stated.push(token);
|
|
377
|
+
} else {
|
|
378
|
+
problems.push({ rule: 'theme-token', msg: `${where}: ${token} is not a token of the theme contract (SPEC THEMING)` });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (problems.length) return out;
|
|
382
|
+
|
|
383
|
+
// ── what it left out, chosen among swatches
|
|
384
|
+
const dark = theme.mode === 'dark';
|
|
385
|
+
const inkward = dark ? [...WEIGHTS] : [...WEIGHTS].reverse(); // the most extreme ink first
|
|
386
|
+
const bgward = [...inkward].reverse(); // the canvas end first
|
|
387
|
+
const has = (t) => plain[t] !== undefined;
|
|
388
|
+
const colourOf = (t) => (out.refs[t] ?? '').replace(/-\d+(?:\/\d+)?$/, '') || null;
|
|
389
|
+
const weightOf = (t) => Number(/-(\d+)(?:\/\d+)?$/.exec(out.refs[t] ?? '')?.[1] ?? NaN);
|
|
390
|
+
const choose = (token, color, weight, alpha = null) => { set(token, color, weight, alpha); out.chosen.push(token); };
|
|
391
|
+
const firstHex = (v) => (/#[0-9a-f]{6}/i.exec(v)?.[0] ?? null);
|
|
392
|
+
const bgHex = () => firstHex(plain['--bg']);
|
|
393
|
+
// the step next to `w` toward the ink (or back toward the canvas), clamped
|
|
394
|
+
const step = (w, n) => { const i = WEIGHTS.indexOf(w) + (dark ? -n : n); return WEIGHTS[Math.max(0, Math.min(WEIGHTS.length - 1, i))]; };
|
|
395
|
+
/** The weight of `scale`, in `order`, whose colour clears `min` against every `against`; `softest` takes the passing one nearest the canvas. */
|
|
396
|
+
const pass = (scale, order, against, min, { softest = false } = {}) => {
|
|
397
|
+
const ok = order.filter((w) => against.every((bg) => ratio(scale.weights[w], bg) >= min));
|
|
398
|
+
if (!ok.length) return null;
|
|
399
|
+
return softest ? ok[ok.length - 1] : ok[0];
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
if (!has('--bg')) choose('--bg', N.name, dark ? 1000 : 100);
|
|
403
|
+
const bg = bgHex();
|
|
404
|
+
const ink = (min, softest = true) => pass(N, inkward, [bg], min, { softest });
|
|
405
|
+
// body text one step in from the scale's end, headings at the end: strong, and still the brand's neutral
|
|
406
|
+
const strong = (min) => [inkward[1], inkward[0], inkward[2]].find((w) => ratio(N.weights[w], bg) >= min);
|
|
407
|
+
if (!has('--fg')) choose('--fg', N.name, strong(7) ?? strong(4.5) ?? inkward[0]);
|
|
408
|
+
if (!has('--heading-color')) choose('--heading-color', N.name, ratio(N.weights[inkward[0]], bg) >= 3 ? inkward[0] : weightOf('--fg'));
|
|
409
|
+
if (!has('--muted')) choose('--muted', N.name, ink(4) ?? ink(3) ?? inkward[0]);
|
|
410
|
+
const bgW = colourOf('--bg') === N.name ? weightOf('--bg') : (dark ? 1000 : 100);
|
|
411
|
+
if (!has('--bg-accent')) choose('--bg-accent', N.name, step(bgW, 1));
|
|
412
|
+
if (!has('--block-bg')) choose('--block-bg', N.name, step(bgW, 1));
|
|
413
|
+
if (!has('--block-border')) choose('--block-border', N.name, step(bgW, 2));
|
|
414
|
+
if (!has('--shadow')) { const shade = N.weights[1000]; plain['--shadow'] = live['--shadow'] = `0 8px 24px ${rgba(shade, dark ? 50 : 18)}`; out.chosen.push('--shadow'); }
|
|
415
|
+
|
|
416
|
+
// the accent: stated, else the brand's first colour, mid-scale, readable on the canvas
|
|
417
|
+
const mid = dark ? [500, 400, 600, 300, 700, 200, 800] : [600, 500, 700, 400, 800, 300, 900];
|
|
418
|
+
const accentScale = scales[colourOf('--accent')] ?? colours[0] ?? N;
|
|
419
|
+
if (!has('--accent')) {
|
|
420
|
+
const w = mid.find((v) => ratio(accentScale.weights[v], bg) >= 3) ?? mid[0];
|
|
421
|
+
choose('--accent', accentScale.name, w);
|
|
422
|
+
}
|
|
423
|
+
const accentHex = plain['--accent'];
|
|
424
|
+
if (!has('--accent-contrast')) {
|
|
425
|
+
// the neutral's end that reads on the accent; failing that, whichever swatch reads best on it
|
|
426
|
+
const ends = [100, 1000].map((w) => ({ color: N.name, w, r: ratio(N.weights[w], accentHex) })).sort((x, y) => y.r - x.r);
|
|
427
|
+
let best = ends[0];
|
|
428
|
+
if (best.r < 4.5) {
|
|
429
|
+
for (const sc of Object.values(scales)) for (const w of WEIGHTS) {
|
|
430
|
+
const r = ratio(sc.weights[w], accentHex);
|
|
431
|
+
if (r > best.r) best = { color: sc.name, w, r };
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
choose('--accent-contrast', best.color, best.w);
|
|
435
|
+
}
|
|
436
|
+
if (!has('--link')) {
|
|
437
|
+
const order = [...mid, ...inkward];
|
|
438
|
+
const w = order.find((v) => ratio(accentScale.weights[v], bg) >= 4.5) ?? order.find((v) => ratio(accentScale.weights[v], bg) >= 3) ?? mid[0];
|
|
439
|
+
choose('--link', accentScale.name, w);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// code: a block one step off the canvas, its ink and its six roles
|
|
443
|
+
if (!has('--code-bg')) choose('--code-bg', N.name, step(bgW, 1));
|
|
444
|
+
const codeBg = plain['--code-bg'];
|
|
445
|
+
const onCode = (min) => pass(N, inkward, [codeBg], min, { softest: true });
|
|
446
|
+
const strongOn = (against, min) => [inkward[1], inkward[0], inkward[2]].find((w) => ratio(N.weights[w], against) >= min);
|
|
447
|
+
if (!has('--code-fg')) choose('--code-fg', N.name, strongOn(codeBg, 7) ?? onCode(4.5) ?? inkward[0]);
|
|
448
|
+
if (!has('--hl-punct')) choose('--hl-punct', N.name, onCode(4.5) ?? inkward[0]);
|
|
449
|
+
if (!has('--hl-comment')) choose('--hl-comment', N.name, onCode(3) ?? inkward[0]);
|
|
450
|
+
/** The colour nearest `hue` that has a weight clearing `min` on every `against`, at its softest such weight. */
|
|
451
|
+
const byHue = (token, hue, against, min) => {
|
|
452
|
+
const ranked = colours.map((s) => ({ s, gap: hueGap(hueOf(lab(s.weights[500])), hue) })).sort((x, y) => x.gap - y.gap);
|
|
453
|
+
for (const { s, gap } of ranked) {
|
|
454
|
+
const w = pass(s, inkward, against, min + 0.3, { softest: true }) ?? pass(s, inkward, against, min, { softest: true });
|
|
455
|
+
if (w === null) continue;
|
|
456
|
+
if (gap > ROLE_REACH) notes.push({ rule: 'theme-nearest-role', msg: `${where}: ${token} wants a hue near ${hue}° and the palette's nearest is ${s.name} (${Math.round(gap)}° away)` });
|
|
457
|
+
return { color: s.name, weight: w };
|
|
458
|
+
}
|
|
459
|
+
return null;
|
|
460
|
+
};
|
|
461
|
+
for (const role of ['--hl-keyword', '--hl-string', '--hl-number', '--hl-function', '--hl-type']) {
|
|
462
|
+
if (has(role)) continue;
|
|
463
|
+
const pick = byHue(role, ROLE_HUES[role], [codeBg], 4.5);
|
|
464
|
+
if (pick) choose(role, pick.color, pick.weight);
|
|
465
|
+
else choose(role, N.name, onCode(4.5) ?? inkward[0]);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// diagrams: the ink, six fills on six colours, each fill's nested tone the next weight toward the ink
|
|
469
|
+
if (!has('--d-text')) choose('--d-text', N.name, ink(7, false) ?? inkward[0]);
|
|
470
|
+
const dText = plain['--d-text'];
|
|
471
|
+
const fillOrder = [accentScale, ...colours.filter((s) => s !== accentScale)];
|
|
472
|
+
const fillWeights = dark ? [800, 900, 700, 600] : [200, 100, 300, 400];
|
|
473
|
+
// six fills, six colours where the palette has them: a colour already used waits for the second pass
|
|
474
|
+
const usedRefs = new Set(); const usedColours = new Set();
|
|
475
|
+
for (let i = 1; i <= 6; i++) if (has(`--d-fill-${i}`)) { usedRefs.add(out.refs[`--d-fill-${i}`]); usedColours.add(colourOf(`--d-fill-${i}`)); }
|
|
476
|
+
const fillFits = (s, w) => {
|
|
477
|
+
const nw = step(w, 1);
|
|
478
|
+
return ratio(dText, s.weights[w]) >= 3.6 && ratio(dText, s.weights[nw]) >= 3
|
|
479
|
+
&& Math.abs(lightness(s.weights[nw]) - lightness(s.weights[w])) >= NESTED_MIN_DL;
|
|
480
|
+
};
|
|
481
|
+
for (let i = 1; i <= 6; i++) {
|
|
482
|
+
const t = `--d-fill-${i}`;
|
|
483
|
+
if (has(t)) continue;
|
|
484
|
+
let picked = null;
|
|
485
|
+
for (const fresh of [true, false]) {
|
|
486
|
+
for (const s of fillOrder) {
|
|
487
|
+
if (fresh && usedColours.has(s.name)) continue;
|
|
488
|
+
const w = fillWeights.find((v) => !usedRefs.has(`${s.name}-${v}`) && fillFits(s, v));
|
|
489
|
+
if (w !== undefined) { picked = { s, w }; break; }
|
|
490
|
+
}
|
|
491
|
+
if (picked) break;
|
|
492
|
+
}
|
|
493
|
+
// nothing fits: the gates will say which pair, against the brand's stated ink
|
|
494
|
+
picked ??= { s: fillOrder[(i - 1) % fillOrder.length], w: fillWeights[0] };
|
|
495
|
+
usedRefs.add(`${picked.s.name}-${picked.w}`); usedColours.add(picked.s.name);
|
|
496
|
+
choose(t, picked.s.name, picked.w);
|
|
497
|
+
}
|
|
498
|
+
for (let i = 1; i <= 6; i++) {
|
|
499
|
+
const t = `--d-fill-${i}-in`;
|
|
500
|
+
if (has(t)) continue;
|
|
501
|
+
const c = colourOf(`--d-fill-${i}`); const w = weightOf(`--d-fill-${i}`);
|
|
502
|
+
if (!scales[c] || !Number.isFinite(w)) continue;
|
|
503
|
+
// the next weight toward the ink that is a step the eye reads and still carries the ink
|
|
504
|
+
let nw = null;
|
|
505
|
+
for (let n = 1; n <= 3; n++) {
|
|
506
|
+
const cand = step(w, n);
|
|
507
|
+
if (cand === w) break;
|
|
508
|
+
if (Math.abs(lightness(scales[c].weights[cand]) - lightness(scales[c].weights[w])) >= NESTED_MIN_DL && ratio(dText, scales[c].weights[cand]) >= 3) { nw = cand; break; }
|
|
509
|
+
}
|
|
510
|
+
if (nw !== null) choose(t, c, nw);
|
|
511
|
+
}
|
|
512
|
+
const fills = [1, 2, 3, 4, 5, 6].map((i) => plain[`--d-fill-${i}`]).filter(Boolean);
|
|
513
|
+
if (!has('--d-muted')) choose('--d-muted', N.name, pass(N, inkward, fills, 2.9, { softest: true }) ?? pass(N, inkward, fills, 2.6, { softest: true }) ?? inkward[0]);
|
|
514
|
+
if (!has('--d-accent')) {
|
|
515
|
+
const order = [...mid, ...inkward];
|
|
516
|
+
const w = order.find((v) => fills.every((f) => ratio(accentScale.weights[v], f) >= 2.9)) ?? order.find((v) => fills.every((f) => ratio(accentScale.weights[v], f) >= 2.6));
|
|
517
|
+
if (w) choose('--d-accent', accentScale.name, w);
|
|
518
|
+
else choose('--d-accent', N.name, pass(N, inkward, fills, 2.6) ?? inkward[0]);
|
|
519
|
+
}
|
|
520
|
+
if (!has('--d-stroke')) choose('--d-stroke', N.name, ink(3) ?? inkward[0]);
|
|
521
|
+
|
|
522
|
+
// the terminal: on the code block, its prompt in the accent, ANSI by hue
|
|
523
|
+
if (!has('--term-bg')) { const r = parseSwatchRef(out.refs['--code-bg']); choose('--term-bg', r.color, r.weight); }
|
|
524
|
+
const termBg = plain['--term-bg'];
|
|
525
|
+
const onTerm = (min, softest = true) => pass(N, inkward, [termBg], min, { softest });
|
|
526
|
+
if (!has('--term-fg')) choose('--term-fg', N.name, strongOn(termBg, 7) ?? onTerm(4.5) ?? inkward[0]);
|
|
527
|
+
if (!has('--term-prompt')) {
|
|
528
|
+
const w = [...mid, ...inkward].find((v) => ratio(accentScale.weights[v], termBg) >= 3.5) ?? null;
|
|
529
|
+
if (w) choose('--term-prompt', accentScale.name, w); else choose('--term-prompt', N.name, onTerm(3.5) ?? inkward[0]);
|
|
530
|
+
}
|
|
531
|
+
if (!has('--term-cursor')) { const r = parseSwatchRef(out.refs['--term-prompt']); choose('--term-cursor', r.color, r.weight); }
|
|
532
|
+
if (!has('--term-selection')) choose('--term-selection', accentScale.name, mid[0], 30);
|
|
533
|
+
if (!has('--ansi-black')) choose('--ansi-black', N.name, onTerm(3.3) ?? inkward[0]);
|
|
534
|
+
if (!has('--ansi-bright-black')) choose('--ansi-bright-black', N.name, onTerm(4.5) ?? inkward[0]);
|
|
535
|
+
if (!has('--ansi-white')) choose('--ansi-white', N.name, onTerm(7, false) ?? inkward[0]);
|
|
536
|
+
if (!has('--ansi-bright-white')) choose('--ansi-bright-white', N.name, inkward[0]);
|
|
537
|
+
for (const a of ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan']) {
|
|
538
|
+
const t = `--ansi-${a}`; const tb = `--ansi-bright-${a}`;
|
|
539
|
+
if (!has(t)) {
|
|
540
|
+
const pick = byHue(t, ROLE_HUES[a], [termBg], 3);
|
|
541
|
+
if (pick) choose(t, pick.color, pick.weight); else choose(t, N.name, onTerm(3) ?? inkward[0]);
|
|
542
|
+
}
|
|
543
|
+
if (!has(tb)) {
|
|
544
|
+
const c = colourOf(t); const w = weightOf(t);
|
|
545
|
+
const nw = step(w, 1);
|
|
546
|
+
choose(tb, c, ratio(scales[c].weights[nw], termBg) >= 3 ? nw : w);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// the type: the stated stacks, else the system's
|
|
551
|
+
for (const [t, v] of [['--font-body', SANS], ['--font-heading', SANS], ['--font-mono', MONO], ['--heading-weight', '700'], ['--block-radius', '10px']]) {
|
|
552
|
+
if (!has(t)) { plain[t] = live[t] = v; out.chosen.push(t); }
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
out.tokens = plain;
|
|
556
|
+
out.gateCss = themeCss(theme.name, plain);
|
|
557
|
+
// the roles ride on the theme block, so switching theme recolours every role;
|
|
558
|
+
// the gates read the contract alone
|
|
559
|
+
out.css = themeCss(theme.title || theme.name, { ...live, ...roleLive });
|
|
560
|
+
const verdict = validateTheme(out.gateCss);
|
|
561
|
+
for (const e of verdict.errors) problems.push({ rule: 'theme-contrast', msg: `${where}: ${e}${blame(e, out)}` });
|
|
562
|
+
return out;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* A theme's tokens as the stylesheet a page applies: one `.decklight` block,
|
|
567
|
+
* in the contract's order (what `themegen.js` writes for a generated theme,
|
|
568
|
+
* kept here because `tools/` ships and `src/` is the runtime's).
|
|
569
|
+
*/
|
|
570
|
+
function themeCss(title, tokens) {
|
|
571
|
+
const label = String(title ?? '').replace(/\*\//g, '* /');
|
|
572
|
+
const lines = Object.entries(tokens).map(([k, v]) => ` ${k}: ${v};`);
|
|
573
|
+
return `/* ${label} · a brand theme (SPEC BRAND_PALETTE): every colour a swatch of its palette */\n.decklight {\n${lines.join('\n')}\n}\n`;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** Which of a failing pair the brand stated, so the message points at the choice to change. */
|
|
577
|
+
function blame(error, out) {
|
|
578
|
+
const named = [...error.matchAll(/--([a-z0-9-]+)/g)].map((m) => `--${m[1]}`).filter((t) => out.stated.includes(t));
|
|
579
|
+
return named.length ? ` (stated: ${named.map((t) => `${t} ${out.refs[t]}${out.viaRole[t] ? ` through the role ${out.viaRole[t]}` : ''}`).join(', ')})` : ' (chosen from the palette: no weight of it clears the gate — state this token, or widen the scale)';
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Every theme of a manifest, expanded. Returns `{ themes, problems, notes }`;
|
|
584
|
+
* a theme with a problem is left out of `themes`.
|
|
585
|
+
*/
|
|
586
|
+
export function expandThemes(read, themes, prefix, roleSpec = {}) {
|
|
587
|
+
const problems = [];
|
|
588
|
+
const notes = [];
|
|
589
|
+
const out = [];
|
|
590
|
+
const seen = new Set();
|
|
591
|
+
for (const [i, t] of (Array.isArray(themes) ? themes : []).entries()) {
|
|
592
|
+
if (!t || typeof t !== 'object' || typeof t.name !== 'string' || !/^[a-z][a-z0-9-]*$/.test(t.name)) {
|
|
593
|
+
problems.push({ rule: 'theme-name', msg: `themes[${i}].name ${JSON.stringify(t?.name)} — a theme's name is a lowercase word (letters, digits and -), e.g. "acme-dark"` });
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
if (seen.has(t.name)) { problems.push({ rule: 'theme-name', msg: `themes: "${t.name}" is named twice` }); continue; }
|
|
597
|
+
seen.add(t.name);
|
|
598
|
+
if (t.mode !== 'dark' && t.mode !== 'light') { problems.push({ rule: 'theme-shape', msg: `themes: ${t.name} — "mode" is "dark" or "light"` }); continue; }
|
|
599
|
+
if (t.title !== undefined && (typeof t.title !== 'string' || !t.title.trim())) { problems.push({ rule: 'theme-shape', msg: `themes: ${t.name} — "title" is the name a person reads` }); continue; }
|
|
600
|
+
const e = expandTheme(read, t, prefix, roleSpec);
|
|
601
|
+
problems.push(...e.problems);
|
|
602
|
+
notes.push(...e.notes);
|
|
603
|
+
if (!e.problems.length) out.push(e);
|
|
604
|
+
}
|
|
605
|
+
return { themes: out, problems, notes };
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Is every colour in a theme's tokens a palette colour? Returns the tokens
|
|
610
|
+
* that are not (`[{ token, value, nearest }]`) — `recommended-off-palette`.
|
|
611
|
+
*/
|
|
612
|
+
export function offPaletteTokens(scales, tokens) {
|
|
613
|
+
const out = [];
|
|
614
|
+
for (const [k, v] of Object.entries(tokens ?? {})) {
|
|
615
|
+
if (!COLOUR_TOKENS.has(`--${k}`) && !COLOUR_TOKENS.has(k)) continue;
|
|
616
|
+
for (const hex of String(v).match(/#[0-9a-f]{3,8}\b/gi) ?? []) {
|
|
617
|
+
const n = nearestSwatch(scales, hex.length > 7 ? hex.slice(0, 7) : hex);
|
|
618
|
+
if (n && n.distance > ON_PALETTE_DISTANCE) { out.push({ token: k.startsWith('--') ? k : `--${k}`, value: hex, nearest: n.ref }); break; }
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return out;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// ── roles (SPEC DESIGN_SYSTEM_ROLES) ──────────────────────────────────────────
|
|
625
|
+
|
|
626
|
+
/** A colour written as a value: hex, or a colour function (color-mix is a recipe, not a colour). */
|
|
627
|
+
const LITERAL_COLOUR = /#[0-9a-f]{8}\b|#[0-9a-f]{6}\b|#[0-9a-f]{3,4}\b|\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\([^()]*\)/gi;
|
|
628
|
+
/** `var(--x)` or `var(--x, fallback)`, a fallback holding at most one level of parentheses. */
|
|
629
|
+
const VAR_REF = /var\(\s*(--[A-Za-z0-9_-]+)\s*(?:,(?:[^()]|\([^()]*\))*)?\)/g;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Every colour a CSS value names that is not one of the design system's roles:
|
|
633
|
+
* `[{ kind: 'literal' | 'contract' | 'swatch' | 'token', name, hex }]`. A role
|
|
634
|
+
* read with a fallback (`var(--acme-color-text, #fff)`, what the Colors tab
|
|
635
|
+
* writes) is a role; a swatch or contract token is reported by its name, never
|
|
636
|
+
* again by its fallback's literal. `hex` is the colour it stands for where
|
|
637
|
+
* that is known, for naming the nearest role: `contractHex` maps a contract
|
|
638
|
+
* token to its value in a theme.
|
|
639
|
+
*
|
|
640
|
+
* The design system's other tokens (an alias like `--acme-color-brand:
|
|
641
|
+
* var(--acme-blue-600)`) are reported as `'token'` only when `tokens` is given
|
|
642
|
+
* — `{ name: '#hex' | null }`, what each resolves to (`resolveTokenColours`) —
|
|
643
|
+
* which is how a slide is held to them (`check`). Without it, any other custom
|
|
644
|
+
* property is left be: a component's own token is its business.
|
|
645
|
+
*/
|
|
646
|
+
export function offRoleColours(value, { prefix, scales, roles, contractHex = {}, tokens = null }) {
|
|
647
|
+
const out = [];
|
|
648
|
+
const seen = new Set();
|
|
649
|
+
const add = (kind, name, hex) => { if (!seen.has(name)) { seen.add(name); out.push({ kind, name, hex: hex ?? null }); } };
|
|
650
|
+
const roleNames = new Set(Object.keys(roles ?? {}).map((id) => roleToken(prefix, id)));
|
|
651
|
+
const swatchRe = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([a-z][a-z0-9-]*)-(\\d{3,4})$`);
|
|
652
|
+
const rest = String(value ?? '').replace(VAR_REF, (whole, token) => {
|
|
653
|
+
if (roleNames.has(token)) return ' ';
|
|
654
|
+
const sw = swatchRe.exec(token);
|
|
655
|
+
if (sw && scales[sw[1]]?.weights[Number(sw[2])]) { add('swatch', token, scales[sw[1]].weights[Number(sw[2])]); return ' '; }
|
|
656
|
+
if (COLOUR_TOKENS.has(token)) { add('contract', token, contractHex[token]); return ' '; }
|
|
657
|
+
if (tokens && token.startsWith(prefix)) { add('token', token, tokens[token]); return ' '; }
|
|
658
|
+
return whole;
|
|
659
|
+
});
|
|
660
|
+
for (const lit of rest.match(LITERAL_COLOUR) ?? []) {
|
|
661
|
+
const c = parseColor(lit);
|
|
662
|
+
add('literal', lit, c ? rgbToHex(c.slice(0, 3)) : null);
|
|
663
|
+
}
|
|
664
|
+
return out;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* What each of a design system's own tokens stands for, in one theme:
|
|
669
|
+
* `{ name: '#hex' | null }`. `tokenValues` is what its stylesheet sets each
|
|
670
|
+
* to; a value that reads another token (`var(--acme-blue-600)`, a role, a
|
|
671
|
+
* contract token, another alias, with or without a fallback) is followed to
|
|
672
|
+
* the colour at its end. Null where the chain ends in something that is not
|
|
673
|
+
* one colour (`8px`, a `color-mix()`), loops, or names a token nobody sets.
|
|
674
|
+
*/
|
|
675
|
+
export function resolveTokenColours(tokenValues, { prefix, scales, roles, roleValues = {}, contractHex = {} }) {
|
|
676
|
+
const roleIds = new Map(Object.keys(roles ?? {}).map((id) => [roleToken(prefix, id), id]));
|
|
677
|
+
const swatchRe = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([a-z][a-z0-9-]*)-(\\d{3,4})$`);
|
|
678
|
+
const hexOf = (v) => { const c = parseColor(String(v ?? '').trim()); return c ? rgbToHex(c.slice(0, 3)) : null; };
|
|
679
|
+
const memo = new Map();
|
|
680
|
+
const tokenHex = (name, path) => {
|
|
681
|
+
if (roleIds.has(name)) return hexOf(roleValues[roleIds.get(name)]);
|
|
682
|
+
const sw = swatchRe.exec(name);
|
|
683
|
+
if (sw && scales[sw[1]]?.weights[Number(sw[2])]) return scales[sw[1]].weights[Number(sw[2])];
|
|
684
|
+
if (COLOUR_TOKENS.has(name)) return hexOf(contractHex[name]);
|
|
685
|
+
if (memo.has(name)) return memo.get(name);
|
|
686
|
+
if (path.has(name) || !(name in (tokenValues ?? {}))) return null;
|
|
687
|
+
path.add(name);
|
|
688
|
+
const hex = valueHex(tokenValues[name], path);
|
|
689
|
+
path.delete(name);
|
|
690
|
+
memo.set(name, hex);
|
|
691
|
+
return hex;
|
|
692
|
+
};
|
|
693
|
+
const valueHex = (v, path) => {
|
|
694
|
+
const m = /^var\(\s*(--[A-Za-z0-9_-]+)\s*(?:,\s*([\s\S]*))?\)$/.exec(String(v ?? '').trim());
|
|
695
|
+
if (!m) return hexOf(v);
|
|
696
|
+
return tokenHex(m[1], path) ?? (m[2] !== undefined ? valueHex(m[2], path) : null);
|
|
697
|
+
};
|
|
698
|
+
const out = {};
|
|
699
|
+
for (const name of Object.keys(tokenValues ?? {})) out[name] = tokenHex(name, new Set());
|
|
700
|
+
return out;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/** The role whose value (in one theme) is nearest `hex`, by OKLab; `roleValues` is `{ id: '#hex' | 'rgba(…)' }`. */
|
|
704
|
+
export function nearestRole(roleValues, hex) {
|
|
705
|
+
if (!hex) return null;
|
|
706
|
+
const c = parseColor(hex);
|
|
707
|
+
if (!c) return null;
|
|
708
|
+
const target = toOklab(c.slice(0, 3));
|
|
709
|
+
let best = null;
|
|
710
|
+
for (const [id, v] of Object.entries(roleValues ?? {})) {
|
|
711
|
+
const rc = parseColor(v);
|
|
712
|
+
if (!rc) continue;
|
|
713
|
+
const d = labDistance(target, toOklab(rc.slice(0, 3)));
|
|
714
|
+
if (!best || d < best.d) best = { id, d };
|
|
715
|
+
}
|
|
716
|
+
return best?.id ?? null;
|
|
717
|
+
}
|