@astryxdesign/cli 0.7.14-canary.gfb823d9 → 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 (135) hide show
  1. package/CHANGELOG.md +197 -0
  2. package/api/component/_adapter.d.mts +15 -0
  3. package/api/component/_adapter.mjs +76 -0
  4. package/api/component/component.mjs +8 -1
  5. package/api/search/_named-keywords.d.mts +31 -0
  6. package/api/search/_named-keywords.mjs +177 -0
  7. package/api/search/search-index.mjs +8 -1
  8. package/api/search/search.d.mts +6 -1
  9. package/api/search/search.mjs +49 -8
  10. package/api/theme/_adapter.d.mts +33 -0
  11. package/api/theme/_adapter.mjs +352 -16
  12. package/api/theme/build/build.mjs +261 -10
  13. package/api/theme/eject/eject.mjs +13 -1
  14. package/api/theme/list/list.mjs +11 -2
  15. package/api/theme/palette/generate/color.d.mts +2 -0
  16. package/api/theme/palette/generate/color.mjs +1 -1
  17. package/api/theme/template/template.mjs +139 -6
  18. package/api/theme/theme.type.d.mts +15 -1
  19. package/api/theme/theme.type.mjs +9 -3
  20. package/api/theme/themeAdd.doc.mjs +1 -1
  21. package/api/theme/themeBuild.doc.mjs +10 -2
  22. package/api/theme/themeEject.doc.mjs +2 -2
  23. package/api/theme/themeListAvailable.doc.mjs +1 -1
  24. package/api/theme/themeRemove.doc.mjs +1 -1
  25. package/api/theme/themeTemplate.doc.mjs +8 -0
  26. package/api/theme/themeUse.doc.mjs +1 -1
  27. package/api/theme-edit/_adapter.d.mts +36 -2
  28. package/api/theme-edit/_adapter.mjs +340 -12
  29. package/api/theme-edit/_check.d.mts +28 -25
  30. package/api/theme-edit/_check.mjs +876 -308
  31. package/api/theme-edit/_color.d.mts +53 -4
  32. package/api/theme-edit/_color.mjs +406 -79
  33. package/api/theme-edit/_compiler.d.mts +14 -6
  34. package/api/theme-edit/_compiler.mjs +89 -59
  35. package/api/theme-edit/_context.d.mts +28 -8
  36. package/api/theme-edit/_context.mjs +142 -19
  37. package/api/theme-edit/_edit.d.mts +3 -1
  38. package/api/theme-edit/_edit.mjs +746 -76
  39. package/api/theme-edit/_how-commands.d.mts +91 -0
  40. package/api/theme-edit/_how-commands.mjs +269 -0
  41. package/api/theme-edit/_how-question.d.mts +57 -0
  42. package/api/theme-edit/_how-question.mjs +2574 -0
  43. package/api/theme-edit/_how.d.mts +27 -8
  44. package/api/theme-edit/_how.mjs +776 -175
  45. package/api/theme-edit/_overview.d.mts +9 -0
  46. package/api/theme-edit/_overview.mjs +296 -0
  47. package/api/theme-edit/_palette.d.mts +155 -15
  48. package/api/theme-edit/_palette.mjs +1446 -262
  49. package/api/theme-edit/_parts.mjs +61 -5
  50. package/api/theme-edit/_png.d.mts +18 -6
  51. package/api/theme-edit/_png.mjs +135 -25
  52. package/api/theme-edit/_preview-app.d.mts +32 -11
  53. package/api/theme-edit/_preview-app.mjs +1976 -285
  54. package/api/theme-edit/_preview.d.mts +39 -8
  55. package/api/theme-edit/_preview.mjs +367 -41
  56. package/api/theme-edit/_settings.d.mts +147 -11
  57. package/api/theme-edit/_settings.mjs +908 -99
  58. package/api/theme-edit/_tables.d.mts +4 -16
  59. package/api/theme-edit/_tables.mjs +51 -117
  60. package/api/theme-edit/theme-edit.type.d.mts +102 -66
  61. package/api/theme-edit/theme-edit.type.mjs +58 -40
  62. package/api/theme-edit/themeEditHow.doc.mjs +17 -6
  63. package/api/theme-edit/themeEditNew.doc.mjs +21 -5
  64. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +18 -5
  65. package/api/theme-edit/themeEditPreviewApp.doc.mjs +12 -9
  66. package/api/theme-edit/themeEditPreviewList.doc.mjs +11 -2
  67. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +29 -3
  68. package/api/theme-edit/themeEditSet.doc.mjs +14 -8
  69. package/api/theme-edit/themeEditShow.doc.mjs +10 -5
  70. package/api/theme-edit/themeEditUndo.doc.mjs +9 -4
  71. package/assets/discover/official-packages.json +4 -0
  72. package/assets/docs/tree/icon-sets.doc.mjs +3 -3
  73. package/assets/docs.generated.json +1 -0
  74. package/assets/icons/definitions/bookmarkAdd.doc.mjs +9 -0
  75. package/assets/icons/definitions/documentSearch.doc.mjs +9 -0
  76. package/assets/icons/definitions/keyboard.doc.mjs +9 -0
  77. package/assets/icons/definitions/paintBrush.doc.mjs +9 -0
  78. package/assets/icons/definitions/wallet.doc.mjs +9 -0
  79. package/assets/preview-app/gitignore +3 -0
  80. package/assets/templates/blocks/components/ButtonGroup/ButtonGroupShowcase.doc.mjs +1 -1
  81. package/assets/templates/blocks/components/Lightbox/LightboxGallery.tsx +1 -1
  82. package/assets/templates/components.generated.json +1 -0
  83. package/assets/templates/pages/gallery-hero/page.tsx +8 -2
  84. package/assets/templates/pages/settings-dialog/template.doc.mjs +3 -3
  85. package/assets/templates/pages/side-gallery/page.tsx +1 -1
  86. package/assets/templates/pages/table-filter/template.doc.mjs +2 -2
  87. package/assets/templates/pages/theme-showcase/template.doc.mjs +1 -1
  88. package/clients/cli/bin/astryx.mjs +37 -5
  89. package/clients/cli/commands/experimental/theme.mjs +422 -57
  90. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +22 -9
  91. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +103 -4
  92. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +13 -8
  93. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +5 -5
  94. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +5 -5
  95. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  96. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +3 -2
  97. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +2 -8
  98. package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +2 -2
  99. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
  100. package/clients/cli/commands/experimental-theme-targets.doc.mjs +1 -1
  101. package/clients/cli/commands/experimental-theme.doc.mjs +3 -4
  102. package/clients/cli/commands/theme-build.doc.mjs +1 -1
  103. package/clients/cli/commands/theme-eject.doc.mjs +3 -3
  104. package/clients/cli/commands/theme-remove.doc.mjs +2 -2
  105. package/clients/cli/commands/theme-template.doc.mjs +11 -4
  106. package/clients/cli/commands/theme.mjs +145 -46
  107. package/clients/cli/commands.generated.json +1 -0
  108. package/clients/cli/index.mjs +96 -5
  109. package/clients/cli/lib/json-shim.mjs +29 -1
  110. package/foundation/config/project.d.mts +16 -2
  111. package/foundation/config/project.mjs +40 -6
  112. package/foundation/config/theme-state.d.mts +9 -0
  113. package/foundation/config/theme-state.mjs +20 -1
  114. package/foundation/discovery/template-adapter.d.mts +12 -1
  115. package/foundation/discovery/template-adapter.mjs +29 -4
  116. package/foundation/discovery/template-icons.d.mts +13 -0
  117. package/foundation/discovery/template-icons.mjs +76 -0
  118. package/foundation/discovery/template-needs.d.mts +7 -0
  119. package/foundation/discovery/template-needs.mjs +117 -8
  120. package/foundation/discovery/template-source-tree.d.mts +5 -1
  121. package/foundation/discovery/template-source-tree.mjs +6 -4
  122. package/foundation/discovery/theme-discovery.d.mts +14 -3
  123. package/foundation/discovery/theme-discovery.mjs +45 -12
  124. package/foundation/doc-compiler/import.mjs +68 -3
  125. package/foundation/doc-compiler/stored-docs.d.mts +27 -0
  126. package/foundation/doc-compiler/stored-docs.mjs +65 -0
  127. package/foundation/fs/fs-error.d.mts +17 -0
  128. package/foundation/fs/fs-error.mjs +34 -0
  129. package/foundation/response/error-codes.d.mts +3 -1
  130. package/foundation/response/error-codes.doc.mjs +13 -1
  131. package/foundation/response/error-codes.mjs +11 -1
  132. package/foundation/response/response-types.doc.mjs +6 -6
  133. package/package.json +6 -7
  134. package/clients/cli/commands/experimental-theme-palette-generate.doc.mjs +0 -73
  135. package/clients/cli/commands/experimental-theme-palette.doc.mjs +0 -37
@@ -1,22 +1,71 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
+ /**
5
+ * The opacity of a CSS color, 0 to 1: the alpha of an 8- or 4-digit hex,
6
+ * `rgba()`/`hsla()`, or a `/ alpha` part; 0 for `transparent`; 1 otherwise.
7
+ * @param {unknown} value
8
+ * @returns {number}
9
+ */
10
+ export function alphaOf(value: unknown): number;
11
+ /**
12
+ * A color drawn at `alpha` over `under`, as the opaque color the eye sees.
13
+ * @param {Rgb} over
14
+ * @param {number} alpha
15
+ * @param {Rgb} under
16
+ * @returns {Rgb}
17
+ */
18
+ export function compositeOver(over: Rgb, alpha: number, under: Rgb): Rgb;
4
19
  /**
5
20
  * The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
6
- * hwb(), lab(), lch(), oklab() or oklch(). Null for a value with no fixed
7
- * 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}.
8
25
  * @param {unknown} value
9
26
  * @returns {Rgb | null}
10
27
  */
11
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;
12
47
  /**
13
48
  * The WCAG contrast ratio of two colors.
14
49
  * @param {number[]} a
15
50
  * @param {number[]} b
16
51
  */
17
52
  export function ratio(a: number[], b: number[]): number;
53
+ /**
54
+ * Whether a value can sit in a CSS declaration as it is: no `<>`, control
55
+ * character, comment or `!important` anywhere; outside a quoted string or a
56
+ * `url(data:…)`, no `;{}`; brackets and quotes closed. So `content: "a;b"`
57
+ * and a base64 data URL are values, and nothing can end the declaration.
58
+ * @param {string} v
59
+ */
60
+ export function isSafeValue(v: string): boolean;
61
+ /** The CSS named colors, for suggestions. */
62
+ export const COLOR_NAMES: string[];
18
63
  export function hex(rgb: number[]): string;
19
64
  export function isColorValue(v: unknown): boolean;
20
- export function isSize(v: unknown): boolean;
21
- export function isNumber(v: unknown): boolean;
65
+ export function isSize(v: unknown, name?: string): boolean;
66
+ export function isNumber(v: unknown, name?: string): boolean;
67
+ export type Num = {
68
+ n: number;
69
+ unit: string;
70
+ };
22
71
  export type Rgb = [number, number, number];
@@ -2,12 +2,13 @@
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
 
10
- import {luminance} from '../theme/palette/generate/color.mjs';
11
+ import {luminance, oklabToLinearRgb} from '../theme/palette/generate/color.mjs';
11
12
 
12
13
  /** @typedef {[number, number, number]} Rgb */
13
14
 
@@ -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 => {
@@ -259,75 +334,250 @@ function labToRgb(l, a, b) {
259
334
 
260
335
  /** @param {number} l @param {number} a @param {number} b OKLab @returns {Rgb} */
261
336
  function oklabToRgb(l, a, b) {
262
- const l1 = (l + 0.3963377774 * a + 0.2158037573 * b) ** 3;
263
- const m1 = (l - 0.1055613458 * a - 0.0638541728 * b) ** 3;
264
- const s1 = (l - 0.0894841775 * a - 1.291485548 * b) ** 3;
337
+ return fromLinear(oklabToLinearRgb(l, a, b));
338
+ }
339
+
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];
265
364
  return fromLinear([
266
- 4.0767416621 * l1 - 3.3077115913 * m1 + 0.2309699292 * s1,
267
- -1.2684380046 * l1 + 2.6097574011 * m1 - 0.3413193965 * s1,
268
- -0.0041960863 * l1 - 0.7034186147 * m1 + 1.707614701 * s1,
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,
269
368
  ]);
270
369
  }
271
370
 
272
- /** @param {number} c @param {number} h degrees @returns {[number, number]} */
371
+ /** @param {number} c chroma, never below 0 @param {number} h degrees @returns {[number, number]} */
273
372
  const polar = (c, h) => [
274
373
  c * Math.cos((h * Math.PI) / 180),
275
374
  c * Math.sin((h * Math.PI) / 180),
276
375
  ];
277
376
 
278
377
  /**
279
- * The RGB of a fixed CSS color: a hex, a named color, or rgb(), hsl(),
280
- * hwb(), lab(), lch(), oklab() or oklch(). Null for a value with no fixed
281
- * color (`var()`, `color-mix()`, `light-dark()`, `color()`, a keyword).
378
+ * The opacity of a CSS color, 0 to 1: the alpha of an 8- or 4-digit hex,
379
+ * `rgba()`/`hsla()`, or a `/ alpha` part; 0 for `transparent`; 1 otherwise.
282
380
  * @param {unknown} value
283
- * @returns {Rgb | null}
381
+ * @returns {number}
284
382
  */
285
- export function parseColor(value) {
383
+ export function alphaOf(value) {
286
384
  const s = String(value ?? '').trim();
385
+ if (/^transparent$/i.test(s)) return 0;
386
+ const read = readColor(s);
387
+ if (read) return read.alpha;
388
+ const h = /^#([0-9a-f]{4}|[0-9a-f]{8})$/i.exec(s);
389
+ if (h) {
390
+ const a = h[1].length === 4 ? h[1][3].repeat(2) : h[1].slice(6);
391
+ return parseInt(a, 16) / 255;
392
+ }
393
+ const slash = /\/\s*(-?[\d.]+)(%?)\s*\)$/.exec(s);
394
+ const comma = /^(?:rgba|hsla)\((?:[^,]*,){3}\s*(-?[\d.]+)(%?)\s*\)$/i.exec(s);
395
+ const m = slash ?? comma;
396
+ if (!m) return 1;
397
+ const n = Number(m[1]) / (m[2] ? 100 : 1);
398
+ return Math.max(0, Math.min(1, n));
399
+ }
400
+
401
+ /**
402
+ * A color drawn at `alpha` over `under`, as the opaque color the eye sees.
403
+ * @param {Rgb} over
404
+ * @param {number} alpha
405
+ * @param {Rgb} under
406
+ * @returns {Rgb}
407
+ */
408
+ export function compositeOver(over, alpha, under) {
409
+ return /** @type {Rgb} */ (
410
+ over.map((c, i) => c * alpha + under[i] * (1 - alpha))
411
+ );
412
+ }
413
+
414
+ /**
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}
421
+ */
422
+ function readColor(s) {
423
+ clipped = false;
287
424
  const m = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(s);
288
425
  if (m) {
289
426
  let h = m[1];
290
427
  if (h.length <= 4) h = [...h].map(c => c + c).join('');
291
428
  const n = parseInt(h.slice(0, 6), 16);
292
- 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
+ };
293
433
  }
294
434
  const named = NAMED[s.toLowerCase()];
295
- if (named) return parseColor(named);
296
- const fn = /^([a-z]+)\((.*)\)$/i.exec(s);
297
- const a = fn && args(fn[2]);
298
- if (!fn || !a || a.length < 3) return null;
299
- const [x, y, z] = a;
300
- 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) {
301
459
  case 'rgb':
302
460
  case 'rgba':
303
- return [amount(x, 255), amount(y, 255), amount(z, 255)];
461
+ rgb = [amount(x, 255), amount(y, 255), amount(z, 255)];
462
+ break;
304
463
  case 'hsl':
305
464
  case 'hsla':
306
- 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;
307
471
  case 'hwb': {
308
- let w = amount(y, 100) / 100;
309
- 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);
310
474
  if (w + b >= 1) {
311
475
  const g = (w / (w + b)) * 255;
312
- return [g, g, g];
313
- }
314
- return /** @type {Rgb} */ (
315
- hslToRgb(degrees(x), 1, 0.5).map(
316
- c => (c / 255) * (1 - w - b) * 255 + w * 255,
317
- )
318
- );
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;
319
484
  }
320
485
  case 'lab':
321
- 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;
322
492
  case 'lch':
323
- 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;
324
498
  case 'oklab':
325
- return oklabToRgb(amount(x, 1), amount(y, 0.4), amount(z, 0.4));
326
- case 'oklch':
327
- 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;
328
505
  default:
329
- return null;
506
+ rgb = oklabToRgb(
507
+ clamp(amount(x, 1), 0, 1),
508
+ ...polar(Math.max(0, amount(y, 0.4)), degrees(z)),
509
+ );
330
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
+ };
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'};
331
581
  }
332
582
 
333
583
  /** @param {number[]} rgb */
@@ -352,26 +602,103 @@ export function ratio(a, b) {
352
602
  }
353
603
 
354
604
  /**
355
- * Whether a value is one CSS color: a hex, a color function, a named color,
356
- * 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.
357
609
  * @param {unknown} v
358
610
  */
359
611
  export const isColorValue = v => {
360
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
+ );
361
623
  return (
362
624
  /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(s) ||
363
625
  FUNCTIONS.test(s) ||
364
626
  /^var\(--[a-z0-9-]+\)$/i.test(s) ||
365
627
  Object.hasOwn(NAMED, s.toLowerCase()) ||
366
- KEYWORDS.includes(s.toLowerCase())
628
+ KEYWORDS.includes(s.toLowerCase()) ||
629
+ SYSTEM.includes(s.toLowerCase())
367
630
  );
368
631
  };
369
632
 
370
- /** Whether a value reads as one CSS size. @param {unknown} v */
371
- export const isSize = v =>
372
- /^(-?\d*\.?\d+(px|rem|em|%|vh|vw)?|0|var\(--[a-z0-9-]+\)|calc\(.+\))$/i.test(
373
- String(v).trim(),
374
- );
633
+ /**
634
+ * Whether a value reads as one CSS size for the token: a length in any CSS
635
+ * unit, 0, a variable, env(), or calc()/min()/max()/clamp() with a length
636
+ * inside; below zero only where the property takes it (an offset).
637
+ * @param {unknown} v
638
+ * @param {string} [name]
639
+ */
640
+ export const isSize = (v, name = '') => {
641
+ const s = String(v).trim();
642
+ const length =
643
+ /^-?(\d+\.?\d*|\.\d+)(e-?\d+)?(px|rem|em|ex|ch|lh|rlh|cap|ic|pt|pc|cm|mm|in|q|%|vh|vw|vmin|vmax|dvh|dvw|svh|svw|lvh|lvw|cqw|cqh|cqi|cqb)$/i;
644
+ if (s === '0') return true;
645
+ // Only a property that takes a negative length gets one: an offset, a
646
+ // letter spacing, an indent.
647
+ if (length.test(s))
648
+ return (
649
+ !s.startsWith('-') || /offset|tracking|letter-spacing|indent/.test(name)
650
+ );
651
+ if (/^var\(--[a-z0-9-]+\)$/i.test(s)) return true;
652
+ if (/^env\([a-z-]+(,.*)?\)$/i.test(s)) return true;
653
+ return /^(calc|min|max|clamp)\(.*(\d|var\().*\)$/i.test(s);
654
+ };
655
+
656
+ /**
657
+ * Whether a value reads as a CSS number for the token: a weight is 1 to
658
+ * 1000 or a weight keyword; a line height is a positive number, length or
659
+ * percentage, or `normal`.
660
+ * @param {unknown} v
661
+ * @param {string} [name]
662
+ */
663
+ export const isNumber = (v, name = '') => {
664
+ const s = String(v).trim();
665
+ if (/^var\(--[a-z0-9-]+\)$/i.test(s)) return true;
666
+ if (/weight/.test(name))
667
+ return (
668
+ /^(normal|bold|bolder|lighter)$/.test(s) ||
669
+ (/^\d+(\.\d+)?$/.test(s) && Number(s) >= 1 && Number(s) <= 1000)
670
+ );
671
+ if (/leading|line-height/.test(name))
672
+ return (
673
+ s === 'normal' ||
674
+ (/^\d*\.?\d+(px|rem|em|%)?$/.test(s) && Number.parseFloat(s) > 0) ||
675
+ /^calc\(.*\d.*\)$/.test(s)
676
+ );
677
+ return /^-?\d*\.?\d+$/.test(s);
678
+ };
375
679
 
376
- /** Whether a value reads as a number. @param {unknown} v */
377
- export const isNumber = v => /^-?\d*\.?\d+$|^var\(/.test(String(v).trim());
680
+ /**
681
+ * Whether a value can sit in a CSS declaration as it is: no `<>`, control
682
+ * character, comment or `!important` anywhere; outside a quoted string or a
683
+ * `url(data:…)`, no `;{}`; brackets and quotes closed. So `content: "a;b"`
684
+ * and a base64 data URL are values, and nothing can end the declaration.
685
+ * @param {string} v
686
+ */
687
+ export function isSafeValue(v) {
688
+ // A shell comment ("# direct"), as a command run without a shell passes
689
+ // it, is a comment too.
690
+ // eslint-disable-next-line no-control-regex
691
+ if (/[<>\u0000-\u001f\u007f]|\/\*|!important|(^|\s)#(\s|$)/i.test(v))
692
+ return false;
693
+ const outside = v
694
+ .replace(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g, '""')
695
+ .replace(/url\(data:[^\s"'()\\]*\)/gi, 'url()');
696
+ if (/[;{}\\]/.test(outside)) return false;
697
+ let depth = 0;
698
+ for (const c of outside.replace(/""/g, '')) {
699
+ if (c === '"' || c === "'") return false;
700
+ if (c === '(') depth++;
701
+ if (c === ')' && --depth < 0) return false;
702
+ }
703
+ return depth === 0;
704
+ }