@astryxdesign/cli 0.7.14-canary.gfdb9213 → 0.7.14

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.
Files changed (95) hide show
  1. package/CHANGELOG.md +197 -0
  2. package/api/theme/_adapter.d.mts +33 -0
  3. package/api/theme/_adapter.mjs +352 -16
  4. package/api/theme/build/build.mjs +261 -10
  5. package/api/theme/eject/eject.mjs +13 -1
  6. package/api/theme/list/list.mjs +11 -2
  7. package/api/theme/template/template.mjs +139 -6
  8. package/api/theme/theme.type.d.mts +15 -1
  9. package/api/theme/theme.type.mjs +9 -3
  10. package/api/theme/themeAdd.doc.mjs +1 -1
  11. package/api/theme/themeBuild.doc.mjs +10 -2
  12. package/api/theme/themeEject.doc.mjs +2 -2
  13. package/api/theme/themeListAvailable.doc.mjs +1 -1
  14. package/api/theme/themeRemove.doc.mjs +1 -1
  15. package/api/theme/themeTemplate.doc.mjs +8 -0
  16. package/api/theme/themeUse.doc.mjs +1 -1
  17. package/api/theme-edit/_adapter.d.mts +10 -2
  18. package/api/theme-edit/_adapter.mjs +52 -31
  19. package/api/theme-edit/_check.d.mts +28 -25
  20. package/api/theme-edit/_check.mjs +586 -548
  21. package/api/theme-edit/_color.d.mts +28 -2
  22. package/api/theme-edit/_color.mjs +306 -68
  23. package/api/theme-edit/_compiler.d.mts +8 -4
  24. package/api/theme-edit/_compiler.mjs +30 -27
  25. package/api/theme-edit/_context.d.mts +10 -4
  26. package/api/theme-edit/_context.mjs +21 -4
  27. package/api/theme-edit/_edit.mjs +300 -43
  28. package/api/theme-edit/_how-commands.d.mts +91 -0
  29. package/api/theme-edit/_how-commands.mjs +269 -0
  30. package/api/theme-edit/_how-question.d.mts +57 -0
  31. package/api/theme-edit/_how-question.mjs +2574 -0
  32. package/api/theme-edit/_how.d.mts +25 -17
  33. package/api/theme-edit/_how.mjs +727 -305
  34. package/api/theme-edit/_palette.d.mts +127 -19
  35. package/api/theme-edit/_palette.mjs +1313 -399
  36. package/api/theme-edit/_parts.mjs +36 -3
  37. package/api/theme-edit/_png.d.mts +18 -6
  38. package/api/theme-edit/_png.mjs +135 -25
  39. package/api/theme-edit/_preview-app.d.mts +26 -7
  40. package/api/theme-edit/_preview-app.mjs +1611 -280
  41. package/api/theme-edit/_preview.d.mts +4 -2
  42. package/api/theme-edit/_preview.mjs +18 -7
  43. package/api/theme-edit/_settings.d.mts +56 -5
  44. package/api/theme-edit/_settings.mjs +197 -49
  45. package/api/theme-edit/_tables.d.mts +0 -12
  46. package/api/theme-edit/_tables.mjs +24 -97
  47. package/api/theme-edit/theme-edit.type.d.mts +22 -5
  48. package/api/theme-edit/theme-edit.type.mjs +17 -6
  49. package/api/theme-edit/themeEditHow.doc.mjs +13 -3
  50. package/api/theme-edit/themeEditNew.doc.mjs +2 -1
  51. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +16 -4
  52. package/api/theme-edit/themeEditPreviewApp.doc.mjs +12 -4
  53. package/api/theme-edit/themeEditPreviewList.doc.mjs +9 -1
  54. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +27 -2
  55. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  56. package/assets/docs.generated.json +1 -0
  57. package/assets/preview-app/gitignore +3 -0
  58. package/assets/templates/components.generated.json +1 -0
  59. package/clients/cli/bin/astryx.mjs +37 -5
  60. package/clients/cli/commands/experimental/theme.mjs +231 -31
  61. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +14 -5
  62. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +0 -1
  63. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  64. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +3 -2
  65. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +2 -2
  66. package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +2 -2
  67. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
  68. package/clients/cli/commands/experimental-theme-targets.doc.mjs +1 -1
  69. package/clients/cli/commands/experimental-theme.doc.mjs +2 -2
  70. package/clients/cli/commands/theme-build.doc.mjs +1 -1
  71. package/clients/cli/commands/theme-eject.doc.mjs +3 -3
  72. package/clients/cli/commands/theme-remove.doc.mjs +2 -2
  73. package/clients/cli/commands/theme-template.doc.mjs +11 -4
  74. package/clients/cli/commands/theme.mjs +104 -40
  75. package/clients/cli/commands.generated.json +1 -0
  76. package/clients/cli/index.mjs +96 -4
  77. package/clients/cli/lib/json-shim.mjs +29 -1
  78. package/foundation/config/project.d.mts +16 -2
  79. package/foundation/config/project.mjs +40 -6
  80. package/foundation/config/theme-state.d.mts +9 -0
  81. package/foundation/config/theme-state.mjs +20 -1
  82. package/foundation/discovery/template-adapter.d.mts +12 -1
  83. package/foundation/discovery/template-adapter.mjs +24 -1
  84. package/foundation/discovery/template-needs.d.mts +7 -0
  85. package/foundation/discovery/template-needs.mjs +117 -8
  86. package/foundation/discovery/template-source-tree.d.mts +5 -1
  87. package/foundation/discovery/template-source-tree.mjs +6 -4
  88. package/foundation/discovery/theme-discovery.d.mts +14 -3
  89. package/foundation/discovery/theme-discovery.mjs +45 -12
  90. package/foundation/doc-compiler/import.mjs +68 -3
  91. package/foundation/doc-compiler/stored-docs.d.mts +27 -0
  92. package/foundation/doc-compiler/stored-docs.mjs +65 -0
  93. package/foundation/fs/fs-error.d.mts +17 -0
  94. package/foundation/fs/fs-error.mjs +34 -0
  95. package/package.json +6 -6
@@ -18,12 +18,32 @@ export function alphaOf(value: unknown): number;
18
18
  export function compositeOver(over: Rgb, alpha: number, under: Rgb): Rgb;
19
19
  /**
20
20
  * The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
21
- * hwb(), lab(), lch(), oklab() or oklch(). Null for a value with no fixed
22
- * color (`var()`, `color-mix()`, `light-dark()`, `color()`, a keyword).
21
+ * hwb(), lab(), lch(), oklab(), oklch() or color() in srgb, srgb-linear or
22
+ * display-p3. Null for a value with no fixed color (`var()`,
23
+ * `color-mix()`, `light-dark()`, a keyword, a system color) or a malformed
24
+ * one. The alpha is not part of the RGB: see {@link alphaOf}.
23
25
  * @param {unknown} value
24
26
  * @returns {Rgb | null}
25
27
  */
26
28
  export function parseColor(value: unknown): Rgb | null;
29
+ /**
30
+ * Whether a fixed color lies outside sRGB (a wide-gamut `color(display-p3
31
+ * …)` or a strong `oklch()`), so the RGB {@link parseColor} gives is the
32
+ * nearest sRGB color, not the color itself.
33
+ * @param {unknown} value
34
+ */
35
+ export function isClipped(value: unknown): boolean;
36
+ /**
37
+ * Why a value cannot seed a color dial, or null when it is one fixed,
38
+ * opaque color: `opaque` is the color without its alpha, for a suggestion.
39
+ * @param {unknown} value
40
+ * @returns {{kind: 'system' | 'space' | 'malformed' | 'unfixed' | 'translucent' | 'unknown', message: string, opaque?: string} | null}
41
+ */
42
+ export function colorProblem(value: unknown): {
43
+ kind: "system" | "space" | "malformed" | "unfixed" | "translucent" | "unknown";
44
+ message: string;
45
+ opaque?: string;
46
+ } | null;
27
47
  /**
28
48
  * The WCAG contrast ratio of two colors.
29
49
  * @param {number[]} a
@@ -38,8 +58,14 @@ export function ratio(a: number[], b: number[]): number;
38
58
  * @param {string} v
39
59
  */
40
60
  export function isSafeValue(v: string): boolean;
61
+ /** The CSS named colors, for suggestions. */
62
+ export const COLOR_NAMES: string[];
41
63
  export function hex(rgb: number[]): string;
42
64
  export function isColorValue(v: unknown): boolean;
43
65
  export function isSize(v: unknown, name?: string): boolean;
44
66
  export function isNumber(v: unknown, name?: string): boolean;
67
+ export type Num = {
68
+ n: number;
69
+ unit: string;
70
+ };
45
71
  export type Rgb = [number, number, number];
@@ -2,8 +2,9 @@
2
2
 
3
3
  /**
4
4
  * @file Color reading for `astryx experimental theme`: parse a CSS color to
5
- * RGB, the contrast ratio (on the palette code's luminance), and value type
6
- * checks. The theme's colors themselves come from the palette generator.
5
+ * RGB as CSS Color 4 reads it, say why a value cannot seed a color, the
6
+ * contrast ratio (on the palette code's luminance), and value type checks.
7
+ * The theme's colors themselves come from the palette generator.
7
8
  * @position api/theme-edit — pure helpers
8
9
  */
9
10
 
@@ -163,6 +164,9 @@ const NAMED = /** @type {Record<string, string>} */ ({
163
164
  yellowgreen: '#9acd32',
164
165
  });
165
166
 
167
+ /** The CSS named colors, for suggestions. */
168
+ export const COLOR_NAMES = Object.keys(NAMED);
169
+
166
170
  /** Color keywords that are valid colors but have no fixed value. */
167
171
  const KEYWORDS = [
168
172
  'transparent',
@@ -173,52 +177,123 @@ const KEYWORDS = [
173
177
  'revert',
174
178
  ];
175
179
 
180
+ /** CSS system colors: valid, but they follow the user's device. */
181
+ const SYSTEM = [
182
+ 'accentcolor',
183
+ 'accentcolortext',
184
+ 'activetext',
185
+ 'buttonborder',
186
+ 'buttonface',
187
+ 'buttontext',
188
+ 'canvas',
189
+ 'canvastext',
190
+ 'field',
191
+ 'fieldtext',
192
+ 'graytext',
193
+ 'highlight',
194
+ 'highlighttext',
195
+ 'linktext',
196
+ 'mark',
197
+ 'marktext',
198
+ 'selecteditem',
199
+ 'selecteditemtext',
200
+ 'visitedtext',
201
+ ];
202
+
176
203
  /** The CSS color functions. */
177
204
  const FUNCTIONS =
178
205
  /^(rgb|rgba|hsl|hsla|hwb|lab|lch|oklab|oklch|color|color-mix|light-dark)\(.+\)$/i;
179
206
 
207
+ /** The functions that give one fixed color from numbers. */
208
+ const FIXED = /^(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)$/i;
209
+
210
+ /** The `color()` spaces this reader converts to sRGB. */
211
+ const COLOR_SPACES = ['srgb', 'srgb-linear', 'display-p3'];
212
+
213
+ /** @typedef {{n: number, unit: string}} Num */
214
+
215
+ /** One number with its unit, or null. @param {string} part @param {boolean} modern */
216
+ function num(part, modern) {
217
+ if (modern && part.toLowerCase() === 'none') return {n: 0, unit: ''};
218
+ const m =
219
+ /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|grad|rad|turn)?$/i.exec(
220
+ part,
221
+ );
222
+ return m ? {n: Number(m[1]), unit: (m[2] ?? '').toLowerCase()} : null;
223
+ }
224
+
180
225
  /**
181
- * The numbers inside a color function, each as a number with its unit:
182
- * commas, spaces and a `/ alpha` part are all accepted; the alpha is dropped.
226
+ * The parts of a color function body as CSS Color 4 reads them: three
227
+ * channels and an optional alpha, either comma-separated (rgb() and hsl()
228
+ * only, alpha as the fourth) or space-separated with `/ alpha`. Null when
229
+ * the body is not one of those shapes.
230
+ * @param {string} fn
183
231
  * @param {string} body
184
- * @returns {Array<{n: number, unit: string}> | null}
232
+ * @returns {{channels: Num[], alpha: number} | null}
185
233
  */
186
- function args(body) {
187
- const parts = body
188
- .split('/')[0]
189
- .trim()
190
- .split(/[\s,]+/)
191
- .filter(Boolean);
192
- const out = [];
193
- for (const part of parts) {
194
- if (part === 'none') {
195
- out.push({n: 0, unit: ''});
196
- continue;
234
+ function args(fn, body) {
235
+ /** @type {string[]} */
236
+ let channels;
237
+ /** @type {string | undefined} */
238
+ let alpha;
239
+ const legacy = body.includes(',');
240
+ if (legacy) {
241
+ if (!/^(rgba?|hsla?)$/i.test(fn) || body.includes('/')) return null;
242
+ const parts = body.split(',').map(x => x.trim());
243
+ if (parts.length !== 3 && parts.length !== 4) return null;
244
+ if (parts.some(x => !x || /\s/.test(x))) return null;
245
+ channels = parts.slice(0, 3);
246
+ alpha = parts[3];
247
+ } else {
248
+ const [left, right, ...extra] = body.split('/');
249
+ if (extra.length) return null;
250
+ channels = left.trim().split(/\s+/).filter(Boolean);
251
+ if (right !== undefined) {
252
+ alpha = right.trim();
253
+ if (!alpha || /\s/.test(alpha)) return null;
197
254
  }
198
- const m = /^(-?\d*\.?\d+(?:e-?\d+)?)(%|deg|grad|rad|turn)?$/i.exec(part);
199
- if (!m) return null;
200
- out.push({n: Number(m[1]), unit: (m[2] ?? '').toLowerCase()});
201
255
  }
202
- return out;
256
+ if (channels.length !== 3) return null;
257
+ const read = channels.map(x => num(x, !legacy));
258
+ if (read.some(x => !x)) return null;
259
+ let a = 1;
260
+ if (alpha !== undefined) {
261
+ const v = num(alpha, !legacy);
262
+ if (!v || (v.unit && v.unit !== '%')) return null;
263
+ a = Math.max(0, Math.min(1, v.unit === '%' ? v.n / 100 : v.n));
264
+ }
265
+ return {channels: /** @type {Num[]} */ (read), alpha: a};
203
266
  }
204
267
 
205
- /** A hue in degrees. @param {{n: number, unit: string}} h */
206
- const degrees = h =>
207
- h.unit === 'turn'
208
- ? h.n * 360
209
- : h.unit === 'rad'
210
- ? (h.n * 180) / Math.PI
211
- : h.unit === 'grad'
212
- ? h.n * 0.9
213
- : h.n;
214
-
215
- /** A number, with `%` read against `full`. @param {{n: number, unit: string}} v @param {number} full */
268
+ /** A hue in degrees, from 0 up to 360. @param {Num} h */
269
+ const degrees = h => {
270
+ const d =
271
+ h.unit === 'turn'
272
+ ? h.n * 360
273
+ : h.unit === 'rad'
274
+ ? (h.n * 180) / Math.PI
275
+ : h.unit === 'grad'
276
+ ? h.n * 0.9
277
+ : h.n;
278
+ return ((d % 360) + 360) % 360;
279
+ };
280
+
281
+ /** A number, with `%` read against `full`. @param {Num} v @param {number} full */
216
282
  const amount = (v, full) => (v.unit === '%' ? (v.n / 100) * full : v.n);
217
283
 
284
+ /** @param {number} v @param {number} lo @param {number} hi */
285
+ const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
286
+
287
+ /** How far a linear channel may stray past 0..1 before it counts as clipped. */
288
+ const GAMUT_SLACK = 0.0005;
289
+ /** Set when the color read last fell outside sRGB and was clipped. */
290
+ let clipped = false;
291
+
218
292
  /** @param {number[]} lin linear sRGB @returns {Rgb} */
219
293
  const fromLinear = lin =>
220
294
  /** @type {Rgb} */ (
221
295
  lin.map(c => {
296
+ if (c < -GAMUT_SLACK || c > 1 + GAMUT_SLACK) clipped = true;
222
297
  const v = Math.max(0, Math.min(1, c));
223
298
  return (
224
299
  255 *
@@ -227,7 +302,7 @@ const fromLinear = lin =>
227
302
  })
228
303
  );
229
304
 
230
- /** @param {number} h @param {number} s @param {number} l 0..1 @returns {Rgb} */
305
+ /** @param {number} h degrees, 0 to 360 @param {number} s @param {number} l 0..1 @returns {Rgb} */
231
306
  function hslToRgb(h, s, l) {
232
307
  /** @param {number} n */
233
308
  const f = n => {
@@ -262,7 +337,38 @@ function oklabToRgb(l, a, b) {
262
337
  return fromLinear(oklabToLinearRgb(l, a, b));
263
338
  }
264
339
 
265
- /** @param {number} c @param {number} h degrees @returns {[number, number]} */
340
+ /**
341
+ * A `color()` value in sRGB: srgb as it is, srgb-linear and display-p3
342
+ * through linear light, clipped to the sRGB gamut.
343
+ * @param {string} space @param {number[]} c 0..1
344
+ * @returns {Rgb}
345
+ */
346
+ function colorSpaceToRgb(space, c) {
347
+ if (space === 'srgb')
348
+ return /** @type {Rgb} */ (c.map(v => clamp(v, 0, 1) * 255));
349
+ if (space === 'srgb-linear') return fromLinear(c);
350
+ const lin = c.map(v =>
351
+ Math.abs(v) <= 0.04045
352
+ ? v / 12.92
353
+ : Math.sign(v) * ((Math.abs(v) + 0.055) / 1.055) ** 2.4,
354
+ );
355
+ const x =
356
+ 0.4865709486482162 * lin[0] +
357
+ 0.26566769316909306 * lin[1] +
358
+ 0.1982172852343625 * lin[2];
359
+ const y =
360
+ 0.2289745640697488 * lin[0] +
361
+ 0.6917385218365064 * lin[1] +
362
+ 0.079286914093745 * lin[2];
363
+ const z = 0.04511338185890264 * lin[1] + 1.043944368900976 * lin[2];
364
+ return fromLinear([
365
+ 3.2409699419045226 * x - 1.537383177570094 * y - 0.4986107602930034 * z,
366
+ -0.9692436362808796 * x + 1.8759675015077202 * y + 0.04155505740717559 * z,
367
+ 0.05563007969699366 * x - 0.20397695888897652 * y + 1.0569715142428786 * z,
368
+ ]);
369
+ }
370
+
371
+ /** @param {number} c chroma, never below 0 @param {number} h degrees @returns {[number, number]} */
266
372
  const polar = (c, h) => [
267
373
  c * Math.cos((h * Math.PI) / 180),
268
374
  c * Math.sin((h * Math.PI) / 180),
@@ -277,6 +383,8 @@ const polar = (c, h) => [
277
383
  export function alphaOf(value) {
278
384
  const s = String(value ?? '').trim();
279
385
  if (/^transparent$/i.test(s)) return 0;
386
+ const read = readColor(s);
387
+ if (read) return read.alpha;
280
388
  const h = /^#([0-9a-f]{4}|[0-9a-f]{8})$/i.exec(s);
281
389
  if (h) {
282
390
  const a = h[1].length === 4 ? h[1][3].repeat(2) : h[1].slice(6);
@@ -304,58 +412,172 @@ export function compositeOver(over, alpha, under) {
304
412
  }
305
413
 
306
414
  /**
307
- * The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
308
- * hwb(), lab(), lch(), oklab() or oklch(). Null for a value with no fixed
309
- * color (`var()`, `color-mix()`, `light-dark()`, `color()`, a keyword).
310
- * @param {unknown} value
311
- * @returns {Rgb | null}
415
+ * A fixed CSS color read as CSS Color 4 reads it: hues wrap, saturation,
416
+ * whiteness, blackness and lightness clamp to their range, and chroma
417
+ * never goes below 0. Null for a value with no fixed color or with a
418
+ * malformed function body.
419
+ * @param {string} s trimmed
420
+ * @returns {{rgb: Rgb, alpha: number} | null}
312
421
  */
313
- export function parseColor(value) {
314
- const s = String(value ?? '').trim();
422
+ function readColor(s) {
423
+ clipped = false;
315
424
  const m = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(s);
316
425
  if (m) {
317
426
  let h = m[1];
318
427
  if (h.length <= 4) h = [...h].map(c => c + c).join('');
319
428
  const n = parseInt(h.slice(0, 6), 16);
320
- return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
429
+ return {
430
+ rgb: [(n >> 16) & 255, (n >> 8) & 255, n & 255],
431
+ alpha: h.length === 8 ? parseInt(h.slice(6), 16) / 255 : 1,
432
+ };
321
433
  }
322
434
  const named = NAMED[s.toLowerCase()];
323
- if (named) return parseColor(named);
324
- const fn = /^([a-z]+)\((.*)\)$/i.exec(s);
325
- const a = fn && args(fn[2]);
326
- if (!fn || !a || a.length < 3) return null;
327
- const [x, y, z] = a;
328
- switch (fn[1].toLowerCase()) {
435
+ if (named) return readColor(named);
436
+ const fn = /^([a-z-]+)\((.*)\)$/i.exec(s);
437
+ if (!fn || !FIXED.test(fn[1])) return null;
438
+ const name = fn[1].toLowerCase();
439
+ if (name === 'color') {
440
+ const cm = /^\s*([a-z0-9-]+)\s+(.*)$/i.exec(fn[2]);
441
+ const space = cm?.[1].toLowerCase() ?? '';
442
+ if (!cm || !COLOR_SPACES.includes(space)) return null;
443
+ const a = args('color', cm[2]);
444
+ if (!a || a.channels.some(c => c.unit && c.unit !== '%')) return null;
445
+ return {
446
+ rgb: colorSpaceToRgb(
447
+ space,
448
+ a.channels.map(c => amount(c, 1)),
449
+ ),
450
+ alpha: a.alpha,
451
+ };
452
+ }
453
+ const a = args(name, fn[2]);
454
+ if (!a) return null;
455
+ const [x, y, z] = a.channels;
456
+ /** @type {Rgb} */
457
+ let rgb;
458
+ switch (name) {
329
459
  case 'rgb':
330
460
  case 'rgba':
331
- return [amount(x, 255), amount(y, 255), amount(z, 255)];
461
+ rgb = [amount(x, 255), amount(y, 255), amount(z, 255)];
462
+ break;
332
463
  case 'hsl':
333
464
  case 'hsla':
334
- return hslToRgb(degrees(x), amount(y, 100) / 100, amount(z, 100) / 100);
465
+ rgb = hslToRgb(
466
+ degrees(x),
467
+ clamp(amount(y, 100) / 100, 0, 1),
468
+ clamp(amount(z, 100) / 100, 0, 1),
469
+ );
470
+ break;
335
471
  case 'hwb': {
336
- let w = amount(y, 100) / 100;
337
- let b = amount(z, 100) / 100;
472
+ const w = clamp(amount(y, 100) / 100, 0, 1);
473
+ const b = clamp(amount(z, 100) / 100, 0, 1);
338
474
  if (w + b >= 1) {
339
475
  const g = (w / (w + b)) * 255;
340
- return [g, g, g];
341
- }
342
- return /** @type {Rgb} */ (
343
- hslToRgb(degrees(x), 1, 0.5).map(
344
- c => (c / 255) * (1 - w - b) * 255 + w * 255,
345
- )
346
- );
476
+ rgb = [g, g, g];
477
+ } else
478
+ rgb = /** @type {Rgb} */ (
479
+ hslToRgb(degrees(x), 1, 0.5).map(
480
+ c => (c / 255) * (1 - w - b) * 255 + w * 255,
481
+ )
482
+ );
483
+ break;
347
484
  }
348
485
  case 'lab':
349
- return labToRgb(amount(x, 100), amount(y, 125), amount(z, 125));
486
+ rgb = labToRgb(
487
+ clamp(amount(x, 100), 0, 100),
488
+ amount(y, 125),
489
+ amount(z, 125),
490
+ );
491
+ break;
350
492
  case 'lch':
351
- return labToRgb(amount(x, 100), ...polar(amount(y, 150), degrees(z)));
493
+ rgb = labToRgb(
494
+ clamp(amount(x, 100), 0, 100),
495
+ ...polar(Math.max(0, amount(y, 150)), degrees(z)),
496
+ );
497
+ break;
352
498
  case 'oklab':
353
- return oklabToRgb(amount(x, 1), amount(y, 0.4), amount(z, 0.4));
354
- case 'oklch':
355
- return oklabToRgb(amount(x, 1), ...polar(amount(y, 0.4), degrees(z)));
499
+ rgb = oklabToRgb(
500
+ clamp(amount(x, 1), 0, 1),
501
+ amount(y, 0.4),
502
+ amount(z, 0.4),
503
+ );
504
+ break;
356
505
  default:
357
- return null;
506
+ rgb = oklabToRgb(
507
+ clamp(amount(x, 1), 0, 1),
508
+ ...polar(Math.max(0, amount(y, 0.4)), degrees(z)),
509
+ );
510
+ }
511
+ return {rgb, alpha: a.alpha};
512
+ }
513
+
514
+ /**
515
+ * The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
516
+ * hwb(), lab(), lch(), oklab(), oklch() or color() in srgb, srgb-linear or
517
+ * display-p3. Null for a value with no fixed color (`var()`,
518
+ * `color-mix()`, `light-dark()`, a keyword, a system color) or a malformed
519
+ * one. The alpha is not part of the RGB: see {@link alphaOf}.
520
+ * @param {unknown} value
521
+ * @returns {Rgb | null}
522
+ */
523
+ export function parseColor(value) {
524
+ return readColor(String(value ?? '').trim())?.rgb ?? null;
525
+ }
526
+
527
+ /**
528
+ * Whether a fixed color lies outside sRGB (a wide-gamut `color(display-p3
529
+ * …)` or a strong `oklch()`), so the RGB {@link parseColor} gives is the
530
+ * nearest sRGB color, not the color itself.
531
+ * @param {unknown} value
532
+ */
533
+ export function isClipped(value) {
534
+ return readColor(String(value ?? '').trim()) != null && clipped;
535
+ }
536
+
537
+ /**
538
+ * Why a value cannot seed a color dial, or null when it is one fixed,
539
+ * opaque color: `opaque` is the color without its alpha, for a suggestion.
540
+ * @param {unknown} value
541
+ * @returns {{kind: 'system' | 'space' | 'malformed' | 'unfixed' | 'translucent' | 'unknown', message: string, opaque?: string} | null}
542
+ */
543
+ export function colorProblem(value) {
544
+ const s = String(value ?? '').trim();
545
+ const read = readColor(s);
546
+ if (read && read.alpha >= 1) return null;
547
+ if (read)
548
+ return {
549
+ kind: 'translucent',
550
+ message: `is ${Math.round(read.alpha * 1000) / 1000 >= 1 ? 'not fully opaque' : `see-through (opacity ${Math.round(read.alpha * 1000) / 1000})`}, and a color needs to be opaque to generate shades from`,
551
+ opaque: hex(read.rgb),
552
+ };
553
+ const lower = s.toLowerCase();
554
+ if (SYSTEM.includes(lower))
555
+ return {
556
+ kind: 'system',
557
+ message:
558
+ "is a system color: it follows the user's device, so no shades can be generated from it. Give a fixed color",
559
+ };
560
+ const fn = /^([a-z-]+)\((.*)\)$/i.exec(s);
561
+ if (fn && fn[1].toLowerCase() === 'color') {
562
+ const space = /^\s*([a-z0-9-]+)\s/i.exec(fn[2])?.[1].toLowerCase();
563
+ if (
564
+ space &&
565
+ !COLOR_SPACES.includes(space) &&
566
+ !/var\(|calc\(|\bfrom\b/i.test(fn[2])
567
+ )
568
+ return {
569
+ kind: 'space',
570
+ message: `uses the ${space} color space, and color() is read in ${COLOR_SPACES.join(', ')} only`,
571
+ };
358
572
  }
573
+ if (fn && FIXED.test(fn[1]) && !/var\(|calc\(|env\(|\bfrom\b/i.test(fn[2]))
574
+ return {kind: 'malformed', message: 'is not a valid CSS color'};
575
+ if (isColorValue(s))
576
+ return {
577
+ kind: 'unfixed',
578
+ message: 'has no fixed color, so no shades can be generated from it',
579
+ };
580
+ return {kind: 'unknown', message: 'is not a CSS color'};
359
581
  }
360
582
 
361
583
  /** @param {number[]} rgb */
@@ -380,18 +602,31 @@ export function ratio(a, b) {
380
602
  }
381
603
 
382
604
  /**
383
- * Whether a value is one CSS color: a hex, a color function, a named color,
384
- * a color keyword, or a variable.
605
+ * Whether a value is one CSS color: a hex, a color function, a named or
606
+ * system color, a color keyword, or a variable. A function with plain
607
+ * numbers must be well formed; one with `var()`, `calc()`, `env()` or a
608
+ * relative color is taken as written.
385
609
  * @param {unknown} v
386
610
  */
387
611
  export const isColorValue = v => {
388
612
  const s = String(v).trim();
613
+ const fn = /^([a-z-]+)\((.*)\)$/i.exec(s);
614
+ if (fn && FIXED.test(fn[1]) && !/var\(|calc\(|env\(|\bfrom\b/i.test(fn[2]))
615
+ return (
616
+ readColor(s) != null ||
617
+ (fn[1].toLowerCase() === 'color' &&
618
+ !COLOR_SPACES.includes(
619
+ /^\s*([a-z0-9-]+)\s/i.exec(fn[2])?.[1].toLowerCase() ?? '',
620
+ ) &&
621
+ /^\s*[a-z0-9-]+(\s+\S+){3}(\s*\/\s*\S+)?\s*$/i.test(fn[2]))
622
+ );
389
623
  return (
390
624
  /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(s) ||
391
625
  FUNCTIONS.test(s) ||
392
626
  /^var\(--[a-z0-9-]+\)$/i.test(s) ||
393
627
  Object.hasOwn(NAMED, s.toLowerCase()) ||
394
- KEYWORDS.includes(s.toLowerCase())
628
+ KEYWORDS.includes(s.toLowerCase()) ||
629
+ SYSTEM.includes(s.toLowerCase())
395
630
  );
396
631
  };
397
632
 
@@ -450,8 +685,11 @@ export const isNumber = (v, name = '') => {
450
685
  * @param {string} v
451
686
  */
452
687
  export function isSafeValue(v) {
688
+ // A shell comment ("# direct"), as a command run without a shell passes
689
+ // it, is a comment too.
453
690
  // eslint-disable-next-line no-control-regex
454
- if (/[<>\u0000-\u001f\u007f]|\/\*|!important/i.test(v)) return false;
691
+ if (/[<>\u0000-\u001f\u007f]|\/\*|!important|(^|\s)#(\s|$)/i.test(v))
692
+ return false;
455
693
  const outside = v
456
694
  .replace(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g, '""')
457
695
  .replace(/url\(data:[^\s"'()\\]*\)/gi, 'url()');
@@ -3,11 +3,13 @@
3
3
 
4
4
  /** @typedef {{token: string, props: string[], when: string[]}} Read */
5
5
  /** @typedef {{message: string, run: string[], also: string[], soft?: boolean}} CheckFailure */
6
+ /** @typedef {import('./_parts.mjs').PartData} PartData */
7
+ /** @typedef {import('./_parts.mjs').TextPair} TextPair */
6
8
  /**
7
9
  * @param {import('./_project.mjs').ThemeProject} project
8
- * @param {import('./_parts.mjs').PartData} parts
10
+ * @param {PartData} parts
9
11
  */
10
- export function makeCompiler(project: import("./_project.mjs").ThemeProject, parts: import("./_parts.mjs").PartData): {
12
+ export function makeCompiler(project: import("./_project.mjs").ThemeProject, parts: PartData): {
11
13
  MAP: Record<string, import("./_parts.mjs").PartRecord>;
12
14
  typeOf: (tok: string) => string;
13
15
  feeds: (target: string, token: string) => Array<{
@@ -29,9 +31,9 @@ export function makeCompiler(project: import("./_project.mjs").ThemeProject, par
29
31
  allReads: (target: string) => Set<string>;
30
32
  anyReader: (tok: string) => string[];
31
33
  checkTokenOn: (theme: string, target: string, token: string, value: string, P: string) => CheckFailure | null;
32
- checkTokenGlobal: (token: string, P: string) => CheckFailure | null;
34
+ checkTokenGlobal: (token: string, _P: string) => CheckFailure | null;
33
35
  variantValues: (target: string) => Record<string, string[]>;
34
- contrast: (value: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, baseValue: (pair: import("./_parts.mjs").TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, modes: string[], { parseColor, ratio, alphaOf, compositeOver, page }: {
36
+ contrast: (value: (pair: TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, baseValue: (pair: TextPair, side: "fg" | "bg", mode: string) => string | null | undefined, modes: string[], { parseColor, ratio, alphaOf, compositeOver, page }: {
35
37
  parseColor: (v: unknown) => [number, number, number] | null;
36
38
  ratio: (a: number[], b: number[]) => number;
37
39
  alphaOf: (v: unknown) => number;
@@ -64,4 +66,6 @@ export type CheckFailure = {
64
66
  also: string[];
65
67
  soft?: boolean;
66
68
  };
69
+ export type PartData = import("./_parts.mjs").PartData;
70
+ export type TextPair = import("./_parts.mjs").TextPair;
67
71
  export type Compiler = ReturnType<typeof makeCompiler>;
@@ -25,10 +25,12 @@ const SOFT =
25
25
 
26
26
  /** @typedef {{token: string, props: string[], when: string[]}} Read */
27
27
  /** @typedef {{message: string, run: string[], also: string[], soft?: boolean}} CheckFailure */
28
+ /** @typedef {import('./_parts.mjs').PartData} PartData */
29
+ /** @typedef {import('./_parts.mjs').TextPair} TextPair */
28
30
 
29
31
  /**
30
32
  * @param {import('./_project.mjs').ThemeProject} project
31
- * @param {import('./_parts.mjs').PartData} parts
33
+ * @param {PartData} parts
32
34
  */
33
35
  export function makeCompiler(project, parts) {
34
36
  const {core} = project;
@@ -262,17 +264,11 @@ export function makeCompiler(project, parts) {
262
264
  : g === 'background'
263
265
  ? ['background-color', 'background', 'background-image']
264
266
  : null;
265
- if (!wanted || !fed.length || fed.some(f => wanted.includes(f.prop)))
266
- return null;
267
- const direct = g === 'text' ? 'color' : 'background-color';
268
- return {
269
- message: `${target} reads ${token} only for ${fmtFeeds(fed, target)}, not for its ${g}, so this would not change what you expect.`,
270
- run: [
271
- `${P} set ${theme} ${direct} ${quote(value)} --on ${target} # sets its ${g} directly`,
272
- `${HOW} ${target}`,
273
- ],
274
- also: [],
275
- };
267
+ // The part reads the token the user named: it is set as asked, and
268
+ // the result names what it feeds.
269
+ void wanted;
270
+ void fed;
271
+ return null;
276
272
  }
277
273
  const reads = g ? partReads(target, g) : [];
278
274
  const v = quote(value);
@@ -287,9 +283,23 @@ export function makeCompiler(project, parts) {
287
283
  `${P} set ${theme} ${r.token} ${v} --on ${target}${fmtWhen(r.when) ? ` #${fmtWhen(r.when)}` : ''}`,
288
284
  ),
289
285
  );
290
- if (g && DIRECT[g])
286
+ // The property that takes this kind of value: a size goes to
287
+ // font-size, never to font-weight.
288
+ const direct =
289
+ g === 'font'
290
+ ? /-(size)$|font-size-/.test(token)
291
+ ? 'font-size'
292
+ : /-leading$/.test(token)
293
+ ? 'line-height'
294
+ : /-tracking$/.test(token)
295
+ ? 'letter-spacing'
296
+ : /family/.test(token)
297
+ ? 'font-family'
298
+ : 'font-weight'
299
+ : g && DIRECT[g];
300
+ if (direct)
291
301
  run.push(
292
- `${P} set ${theme} ${DIRECT[g]} ${v} --on ${target} # sets it directly, whatever token it uses`,
302
+ `${P} set ${theme} ${direct} ${v} --on ${target} # sets it directly, whatever token it uses`,
293
303
  );
294
304
  const kids = g
295
305
  ? (MAP[target]?.contains ?? [])
@@ -322,10 +332,10 @@ export function makeCompiler(project, parts) {
322
332
  /**
323
333
  * Whether a whole-theme token is read by any part.
324
334
  * @param {string} token
325
- * @param {string} P
335
+ * @param {string} _P the command prefix (unused: the result is a note)
326
336
  * @returns {CheckFailure | null}
327
337
  */
328
- function checkTokenGlobal(token, P) {
338
+ function checkTokenGlobal(token, _P) {
329
339
  if (anyReader(token).length || SOFT.test(token)) return null;
330
340
  const g = groupOfToken(token);
331
341
  const near = g
@@ -336,15 +346,8 @@ export function makeCompiler(project, parts) {
336
346
  : [];
337
347
  return {
338
348
  message: `No component reads ${token}, so setting it changes nothing.${near.length ? ` Tokens components do read for ${g}: ${near.join(', ')}.` : ''}`,
339
- run: near.length
340
- ? near.slice(0, 3).map(t => `${P} set <theme> ${t} <value>`)
341
- : [`${HOW} "<what you want>"`],
342
- also:
343
- g === 'shadow'
344
- ? [
345
- `shadows are set per part: ${P} set <theme> box-shadow "0 2px 6px rgb(0 0 0 / 0.3)" --on card`,
346
- ]
347
- : [],
349
+ run: [`${HOW} ${quote(token)}`],
350
+ also: [],
348
351
  };
349
352
  }
350
353
 
@@ -374,8 +377,8 @@ export function makeCompiler(project, parts) {
374
377
  * in each mode, below 4.5:1 now but not as low in the base theme. Also
375
378
  * the values this theme set that have no fixed color to check (a
376
379
  * `color-mix()`, a `var()`), so the result can say what it skipped.
377
- * @param {(pair: import('./_parts.mjs').TextPair, side: 'fg' | 'bg', mode: string) => string | null | undefined} value
378
- * @param {(pair: import('./_parts.mjs').TextPair, side: 'fg' | 'bg', mode: string) => string | null | undefined} baseValue
380
+ * @param {(pair: TextPair, side: 'fg' | 'bg', mode: string) => string | null | undefined} value
381
+ * @param {(pair: TextPair, side: 'fg' | 'bg', mode: string) => string | null | undefined} baseValue
379
382
  * @param {string[]} modes
380
383
  * @param {{parseColor: (v: unknown) => [number, number, number] | null, ratio: (a: number[], b: number[]) => number, alphaOf: (v: unknown) => number, compositeOver: (over: [number, number, number], alpha: number, under: [number, number, number]) => [number, number, number], page: (mode: string) => string | null | undefined}} helpers
381
384
  * `page` is the page color in a mode: a see-through background is drawn