@astryxdesign/cli 0.7.17 → 0.7.18-canary.g57306fd

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 (241) hide show
  1. package/README.md +1 -1
  2. package/api/discover/_adapter.d.mts +7 -5
  3. package/api/discover/_adapter.mjs +12 -11
  4. package/api/docs/_adapter.d.mts +19 -2
  5. package/api/docs/_adapter.mjs +66 -5
  6. package/api/doctor/_adapter.d.mts +11 -62
  7. package/api/doctor/_adapter.mjs +242 -472
  8. package/api/doctor/doctor.d.mts +1 -1
  9. package/api/doctor/doctor.mjs +3 -3
  10. package/api/doctor/theme-checks.mjs +37 -45
  11. package/api/error.d.mts +21 -0
  12. package/api/error.mjs +89 -0
  13. package/api/hook/list/list.d.mts +1 -1
  14. package/api/icon/_adapter.d.mts +8 -0
  15. package/api/icon/_adapter.mjs +17 -0
  16. package/api/icon/_authoring.mjs +6 -6
  17. package/api/icon/_default-look.mjs +16 -16
  18. package/api/icon/add/add.mjs +3 -3
  19. package/api/icon/exportIconIndex.doc.mjs +2 -2
  20. package/api/icon/icon.doc.mjs +2 -2
  21. package/api/icon/icon.type.d.mts +4 -0
  22. package/api/icon/icon.type.mjs +2 -1
  23. package/api/icon/search/icon-index.mjs +16 -3
  24. package/api/icon/search/search.d.mts +15 -5
  25. package/api/icon/search/search.mjs +74 -16
  26. package/api/icon/use/use.mjs +4 -4
  27. package/api/render/_adapter.mjs +233 -40
  28. package/api/render/render.type.d.mts +1 -1
  29. package/api/render/render.type.mjs +1 -1
  30. package/api/search/ORDERING.md +2 -2
  31. package/api/search/_adapter.mjs +4 -0
  32. package/api/search/_candidate-score.mjs +32 -8
  33. package/api/search/_cards.json +1 -1
  34. package/api/search/_compound.d.mts +0 -1
  35. package/api/search/_compound.mjs +93 -2
  36. package/api/search/_icons.d.mts +4 -2
  37. package/api/search/_icons.mjs +4 -2
  38. package/api/search/_lexical.mjs +177 -17
  39. package/api/search/_order.d.mts +3 -1
  40. package/api/search/_order.mjs +16 -5
  41. package/api/search/_result.d.mts +9 -0
  42. package/api/search/_result.mjs +10 -0
  43. package/api/search/_score.mjs +21 -3
  44. package/api/swizzle/_moduleGraph.d.mts +8 -0
  45. package/api/swizzle/_moduleGraph.mjs +61 -3
  46. package/api/template/copy/copy.mjs +5 -3
  47. package/api/theme/_adapter.mjs +48 -12
  48. package/api/theme/build/build.d.mts +4 -0
  49. package/api/theme/build/build.mjs +69 -18
  50. package/api/theme/build/style-keys.mjs +4 -5
  51. package/api/theme/build/theme-object.mjs +5 -0
  52. package/api/theme/palette/generate/color.d.mts +47 -0
  53. package/api/theme/palette/generate/color.mjs +58 -1
  54. package/api/theme/palette/generate/derive-look.d.mts +293 -0
  55. package/api/theme/palette/generate/derive-look.mjs +1362 -0
  56. package/api/theme/palette/generate/editor-look.d.mts +62 -0
  57. package/api/theme/palette/generate/editor-look.mjs +550 -0
  58. package/api/theme/palette/generate/mapping.d.mts +94 -9
  59. package/api/theme/palette/generate/mapping.mjs +94 -22
  60. package/api/theme-edit/_adapter.mjs +13 -2
  61. package/api/theme-edit/_catalog.d.mts +13 -13
  62. package/api/theme-edit/_catalog.mjs +66 -28
  63. package/api/theme-edit/_check.d.mts +82 -6
  64. package/api/theme-edit/_check.mjs +1270 -238
  65. package/api/theme-edit/_color.d.mts +74 -0
  66. package/api/theme-edit/_color.mjs +168 -3
  67. package/api/theme-edit/_compiler.d.mts +3 -5
  68. package/api/theme-edit/_compiler.mjs +122 -23
  69. package/api/theme-edit/_context.d.mts +55 -12
  70. package/api/theme-edit/_context.mjs +114 -60
  71. package/api/theme-edit/_css.d.mts +11 -0
  72. package/api/theme-edit/_css.mjs +40 -0
  73. package/api/theme-edit/_edit.d.mts +15 -2
  74. package/api/theme-edit/_edit.mjs +1469 -133
  75. package/api/theme-edit/_how-commands.d.mts +116 -0
  76. package/api/theme-edit/_how-commands.mjs +227 -115
  77. package/api/theme-edit/_how-question.d.mts +74 -29
  78. package/api/theme-edit/_how-question.mjs +8514 -1256
  79. package/api/theme-edit/_how-sides.d.mts +22 -0
  80. package/api/theme-edit/_how-sides.mjs +173 -0
  81. package/api/theme-edit/_how-steps.d.mts +42 -0
  82. package/api/theme-edit/_how-steps.mjs +208 -0
  83. package/api/theme-edit/_how-values.d.mts +29 -0
  84. package/api/theme-edit/_how-values.mjs +111 -0
  85. package/api/theme-edit/_how-words.d.mts +15 -0
  86. package/api/theme-edit/_how-words.mjs +239 -0
  87. package/api/theme-edit/_how.d.mts +1 -1
  88. package/api/theme-edit/_how.mjs +422 -37
  89. package/api/theme-edit/_missed.d.mts +2 -1
  90. package/api/theme-edit/_missed.mjs +125 -13
  91. package/api/theme-edit/_normal.d.mts +5 -3
  92. package/api/theme-edit/_normal.mjs +54 -14
  93. package/api/theme-edit/_palette.d.mts +204 -4
  94. package/api/theme-edit/_palette.mjs +901 -25
  95. package/api/theme-edit/_part-pieces.d.mts +54 -0
  96. package/api/theme-edit/_part-pieces.mjs +179 -0
  97. package/api/theme-edit/_part-words.mjs +33 -5
  98. package/api/theme-edit/_parts.d.mts +133 -0
  99. package/api/theme-edit/_parts.mjs +599 -10
  100. package/api/theme-edit/_picture-traits.d.mts +259 -0
  101. package/api/theme-edit/_picture-traits.mjs +1030 -0
  102. package/api/theme-edit/_preview.d.mts +95 -4
  103. package/api/theme-edit/_preview.mjs +284 -31
  104. package/api/theme-edit/_project.d.mts +14 -4
  105. package/api/theme-edit/_project.mjs +95 -26
  106. package/api/theme-edit/_resolved.d.mts +46 -0
  107. package/api/theme-edit/_resolved.mjs +800 -0
  108. package/api/theme-edit/_set-line.d.mts +29 -0
  109. package/api/theme-edit/_set-line.mjs +100 -0
  110. package/api/theme-edit/_settings.d.mts +625 -28
  111. package/api/theme-edit/_settings.mjs +4867 -888
  112. package/api/theme-edit/_tables.d.mts +25 -1
  113. package/api/theme-edit/_tables.mjs +115 -11
  114. package/api/theme-edit/_text-looks.d.mts +78 -0
  115. package/api/theme-edit/_text-looks.mjs +376 -0
  116. package/api/theme-edit/_vet.d.mts +3 -26
  117. package/api/theme-edit/_vet.mjs +5 -89
  118. package/api/theme-edit/how/how.d.mts +1 -1
  119. package/api/theme-edit/how/how.mjs +1 -1
  120. package/api/theme-edit/show/show.d.mts +4 -3
  121. package/api/theme-edit/show/show.mjs +3 -2
  122. package/api/theme-edit/theme-edit.type.d.mts +117 -1
  123. package/api/theme-edit/theme-edit.type.mjs +46 -3
  124. package/api/theme-edit/themeEditHow.doc.mjs +3 -3
  125. package/api/theme-edit/themeEditNew.doc.mjs +4 -0
  126. package/api/theme-edit/themeEditSet.doc.mjs +6 -2
  127. package/api/theme-edit/themeEditShow.doc.mjs +22 -3
  128. package/api/theme-edit/themeEditUndo.doc.mjs +4 -0
  129. package/assets/component-list.generated.json +1 -1
  130. package/assets/docs/author-a-theme.doc.mjs +7 -4
  131. package/assets/docs/theme.doc.dense.mjs +10 -0
  132. package/assets/docs/theme.doc.mjs +10 -0
  133. package/assets/docs/theme.doc.zh.mjs +10 -0
  134. package/assets/docs/tree/export-pages.doc.mjs +1 -1
  135. package/assets/docs/tree/icon-sets.doc.mjs +1 -1
  136. package/assets/docs/use-a-theme.doc.mjs +89 -17
  137. package/assets/docs-routes.generated.json +1 -1
  138. package/assets/docs.generated.json +2 -2
  139. package/assets/icons/definitions/article.doc.mjs +9 -0
  140. package/assets/icons/template-icons.json +5 -0
  141. package/assets/search-catalog.generated.json +2 -2
  142. package/assets/self-audit.generated.json +1 -1
  143. package/assets/templates/blocks/components/Citation/CitationInlineText.doc.mjs +1 -1
  144. package/assets/templates/blocks/components/Citation/CitationInlineText.tsx +3 -1
  145. package/assets/templates/blocks/components/Citation/CitationShowcase.doc.mjs +1 -1
  146. package/assets/templates/blocks/components/Citation/CitationShowcase.tsx +3 -1
  147. package/assets/templates/blocks/components/Citation/CitationSourceList.doc.mjs +1 -1
  148. package/assets/templates/blocks/components/Citation/CitationSourceList.tsx +7 -3
  149. package/assets/templates/blocks/components/Markdown/MarkdownCitedContent.doc.mjs +1 -1
  150. package/assets/templates/blocks/components/Markdown/MarkdownCitedContent.tsx +5 -2
  151. package/assets/templates/components.generated.json +1 -1
  152. package/assets/templates/pages/ide/page.tsx +5 -10
  153. package/assets/templates/pages/payment-form/page.tsx +16 -60
  154. package/assets/templates/themes/neutral/neutralTheme.ts +4 -3
  155. package/authoring/debug/parse.d.mts +1 -1
  156. package/authoring/doctypes/_schema.d.mts +13 -13
  157. package/authoring/gap-report/parse.d.mts +1 -1
  158. package/clients/cli/build-id.generated.json +1 -1
  159. package/clients/cli/commands/ensure-core-built.mjs +17 -10
  160. package/clients/cli/commands/experimental/icon.mjs +7 -7
  161. package/clients/cli/commands/experimental/theme.mjs +175 -13
  162. package/clients/cli/commands/experimental-icon.doc.mjs +1 -1
  163. package/clients/cli/commands/experimental-theme-compile.doc.mjs +1 -1
  164. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +11 -3
  165. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +3 -3
  166. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +15 -5
  167. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  168. package/clients/cli/commands/hook.mjs +7 -0
  169. package/clients/cli/commands/theme-build.doc.mjs +1 -1
  170. package/clients/cli/index.mjs +6 -2
  171. package/clients/cli/lib/hook-format.mjs +41 -1
  172. package/clients/cli/lib/json-shim.mjs +34 -14
  173. package/clients/cli/lib/other-cli-words.mjs +7 -0
  174. package/clients/cli/lib/unknown-command.mjs +345 -46
  175. package/clients/cli/manifest.generated.json +1 -1
  176. package/foundation/agent-docs/agent-docs.d.mts +11 -2
  177. package/foundation/agent-docs/agent-docs.mjs +51 -8
  178. package/foundation/config/project.d.mts +11 -1
  179. package/foundation/config/project.mjs +11 -2
  180. package/foundation/discovery/component-replacement.d.mts +5 -8
  181. package/foundation/discovery/component-replacement.mjs +12 -14
  182. package/foundation/discovery/core-augmentation-points.d.mts +35 -0
  183. package/foundation/discovery/core-augmentation-points.mjs +410 -9
  184. package/foundation/discovery/core-elements.d.mts +6 -0
  185. package/foundation/discovery/core-elements.mjs +10 -2
  186. package/foundation/discovery/core-prop-types.d.mts +5 -0
  187. package/foundation/discovery/core-prop-types.mjs +10 -3
  188. package/foundation/discovery/core-props.d.mts +8 -0
  189. package/foundation/discovery/core-props.mjs +29 -13
  190. package/foundation/discovery/icon-ranking.d.mts +5 -2
  191. package/foundation/discovery/icon-ranking.mjs +101 -18
  192. package/foundation/discovery/icon-set-checks.mjs +1 -1
  193. package/foundation/discovery/icon-set-writer.mjs +1 -1
  194. package/foundation/discovery/official-list.d.mts +17 -0
  195. package/foundation/discovery/official-list.mjs +77 -0
  196. package/foundation/discovery/unknown-core-props.d.mts +130 -0
  197. package/foundation/discovery/unknown-core-props.mjs +601 -0
  198. package/foundation/discovery/written-prop-values.d.mts +35 -0
  199. package/foundation/discovery/written-prop-values.mjs +258 -0
  200. package/foundation/fonts/fontsource-families.generated.d.mts +5 -0
  201. package/foundation/fonts/fontsource-families.generated.mjs +2110 -0
  202. package/foundation/fs/package-name.d.mts +10 -0
  203. package/foundation/fs/package-name.mjs +37 -0
  204. package/foundation/fs/project-source-files.d.mts +27 -0
  205. package/foundation/fs/project-source-files.mjs +115 -0
  206. package/foundation/fs/project-sources.d.mts +25 -0
  207. package/foundation/fs/project-sources.mjs +83 -0
  208. package/foundation/fs/read-record.d.mts +18 -0
  209. package/foundation/fs/read-record.mjs +36 -2
  210. package/foundation/integrations/integration-warnings.d.mts +39 -7
  211. package/foundation/integrations/integration-warnings.mjs +141 -19
  212. package/foundation/integrations/missing-dependency.d.mts +11 -0
  213. package/foundation/integrations/missing-dependency.mjs +20 -5
  214. package/foundation/release/folder-lock.d.mts +5 -2
  215. package/foundation/release/folder-lock.mjs +9 -3
  216. package/foundation/release/notices.mjs +2 -8
  217. package/foundation/render/cut-short.d.mts +8 -2
  218. package/foundation/render/cut-short.mjs +50 -5
  219. package/foundation/render/drawn-contrast.d.mts +40 -0
  220. package/foundation/render/drawn-contrast.mjs +355 -0
  221. package/foundation/render/file-header.mjs +4 -0
  222. package/foundation/render/layers.d.mts +2 -1
  223. package/foundation/render/layers.mjs +22 -1
  224. package/foundation/render/package-resolve-hooks.d.mts +38 -9
  225. package/foundation/render/package-resolve-hooks.mjs +124 -32
  226. package/foundation/render/page-worker.mjs +95 -23
  227. package/foundation/render/part-boxes.d.mts +27 -0
  228. package/foundation/render/part-boxes.mjs +45 -0
  229. package/foundation/render/picture-chrome.mjs +36 -4
  230. package/foundation/render/picture-playwright.mjs +26 -2
  231. package/foundation/render/picture-window.d.mts +29 -2
  232. package/foundation/render/picture-window.mjs +99 -1
  233. package/foundation/render/render-page.d.mts +39 -6
  234. package/foundation/render/render-page.mjs +26 -10
  235. package/foundation/render/ts-paths.mjs +2 -1
  236. package/foundation/response/error-codes.doc.mjs +1 -1
  237. package/foundation/response/error-codes.mjs +1 -1
  238. package/foundation/response/response-types.doc.mjs +9 -5
  239. package/foundation/text/written-string.d.mts +4 -1
  240. package/foundation/text/written-string.mjs +4 -1
  241. package/package.json +8 -7
@@ -0,0 +1,1362 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ // Generated from packages/core/src/palette/derive-look.ts by
3
+ // scripts/generate-cli-palette-engine.mjs. Do not edit: change the Core
4
+ // source and run `pnpm generate:cli-palette-engine`.
5
+ /**
6
+ * @file The colors a palette derives beyond its own families: Core's
7
+ * category colors (Badge, Token and Card tints) and the chart colors.
8
+ * @input A generated palette, the theme's text and surface colors per mode.
9
+ * @output The seeds of the derived hues, and each derived token's stops.
10
+ * @position Beside the generator, so the theme editor and a later
11
+ * `defineTheme({palette})` derive the same colors from one function.
12
+ *
13
+ * Each hue grows from Core's own hue at the strength of the theme's accent
14
+ * (the palette's vibrancy then shapes it as it shapes every color). Each
15
+ * token starts at the stop the official neutral theme maps it to: a card
16
+ * tint pale enough that the theme's text and secondary text read on it,
17
+ * text on it and on the page at 4.5:1, icons at 3:1 (WCAG 2.2 SC 1.4.3,
18
+ * 1.4.11), and chart series at 3:1 on the page. Every set is written. A
19
+ * chart series drawn like an earlier one takes the nearest shade apart
20
+ * that still reads. A card tint drawn like an earlier one, or like the
21
+ * page, takes the nearest quieter tint apart, else the nearest stronger
22
+ * one. A status fill stays apart from the accent where a shade does. A
23
+ * status role whose text reads at no shade keeps the base theme's colors.
24
+ * A limit no shade meets is a note.
25
+ */
26
+ import {
27
+ compositeOver,
28
+ contrastRatio,
29
+ hexToRgbTriple as rgbOf,
30
+ rgbTripleToHex as hexOf,
31
+ hexToLinearRgb,
32
+ hexToOklch,
33
+ linearRgbToOklab,
34
+ maxOklchChroma,
35
+ oklchClampedHex,
36
+ ratioDown,
37
+ } from './color.mjs';
38
+ import {perceptualDelta} from './generator.mjs';
39
+
40
+ /** @typedef {'light' | 'dark'} Mode */
41
+
42
+ /**
43
+ * @typedef {object} Stop
44
+ * @property {number} stop
45
+ * @property {string} color
46
+ * @property {string} [family]
47
+ * @property {string} [alpha]
48
+ */
49
+
50
+ /**
51
+ * A derived token: its family and its shade in each mode.
52
+ * @typedef {object} DerivedToken
53
+ * @property {string} family
54
+ * @property {Stop} light
55
+ * @property {Stop} dark
56
+ */
57
+
58
+ /**
59
+ * The category and chart colors a palette derives. Each set is always
60
+ * written when the palette has its hues.
61
+ * @typedef {object} DerivedLook
62
+ * @property {Record<string, DerivedToken>} tokens
63
+ * @property {'derived' | 'none'} badges
64
+ * @property {'derived' | 'none'} chart
65
+ * @property {string[]} limits Each limit no shade meets: a color still drawn alike, or one under its contrast at its most readable shade.
66
+ * @property {string[]} moved Each color moved apart from an earlier one (a tint also from the page).
67
+ * @property {boolean} alike Whether a color is still drawn like an earlier one.
68
+ * @property {boolean} unread Whether a color is under its contrast at its most readable shade.
69
+ */
70
+
71
+ /**
72
+ * What deriveLookColors reads.
73
+ * @typedef {object} DeriveLookInput
74
+ * @property {Record<string, Record<Mode, Record<string | number, string>>>} palette Generated families by id.
75
+ * @property {ReadonlyArray<number>} stops The palette's stops.
76
+ * @property {ReadonlyArray<Mode>} modes
77
+ * @property {(m: Mode) => string[]} text The theme's text and secondary text colors (opaque hex) in a mode.
78
+ * @property {string | null} [accent] The look's accent seed: how deep its card tints sit (tintStops).
79
+ * @property {(m: Mode) => string[]} grounds The surfaces text sits on in a mode: page, surface, card (opaque hex).
80
+ * @property {(token: string) => boolean} [wanted] Whether a token is written (Core has it and the theme does not set it by hand).
81
+ * @property {ReadonlyArray<Mode>} [shown] The modes the theme shows: notes name only these. Every mode by default.
82
+ */
83
+
84
+ /**
85
+ * @typedef {object} Badge
86
+ * @property {Stop} fill
87
+ * @property {{stop: Stop; r: number}} ink
88
+ * @property {{stop: Stop; r: number}} icon
89
+ */
90
+
91
+ /**
92
+ * What deriveStatusColors reads.
93
+ * @typedef {object} DeriveStatusInput
94
+ * @property {Record<string, Record<Mode, Record<string | number, string>>>} palette Generated families by id; a role's family has the role's dial id.
95
+ * @property {ReadonlyArray<number>} stops
96
+ * @property {ReadonlyArray<Mode>} modes
97
+ * @property {ReadonlyArray<string>} roles The dial ids to derive (info, success, warning, danger).
98
+ * @property {(m: Mode) => string[]} grounds Page, surface, card (opaque hex).
99
+ * @property {(m: Mode, role: string) => (Stop & {family: string})[]} inks The text the theme draws on a role's fill in a mode, preferred first (white or near-black).
100
+ * @property {(m: Mode) => string | null} accent The accent fill in a mode, kept apart from.
101
+ * @property {(token: string) => boolean} [wanted]
102
+ * @property {ReadonlyArray<Mode>} [shown] The modes the theme shows: notes name only these. Every mode by default.
103
+ */
104
+
105
+ /**
106
+ * The status colors a palette derives, and each role's state. A token's stop may name its own family (the text on a fill is the neutral color's, or white or black).
107
+ * @typedef {object} DerivedStatus
108
+ * @property {Record<string, DerivedToken>} tokens
109
+ * @property {Record<string, 'derived'>} roles The roles derived and written.
110
+ * @property {string[]} why What the roles gave up to stay written (a fill that cannot meet every limit), and each role that keeps the base theme's colors because its text, icon or border reads at no shade.
111
+ * @property {string[]} short The roles `why` names, by dial id.
112
+ */
113
+
114
+ /**
115
+ * What deriveCodeColors reads.
116
+ * @typedef {object} DeriveCodeInput
117
+ * @property {Record<string, Record<Mode, Record<string | number, string>>>} palette
118
+ * @property {ReadonlyArray<number>} stops
119
+ * @property {ReadonlyArray<Mode>} modes
120
+ * @property {(m: Mode) => string[]} grounds The page colors code sits on, opaque hex.
121
+ * @property {(m: Mode) => string | null} [codeGround] The code background the theme sets itself in a mode (opaque hex), which code colors read on instead of the neutral's.
122
+ * @property {(token: string) => boolean} [wanted]
123
+ */
124
+
125
+ export {ratioDown};
126
+ /** A shade of a family: its stop and color. */
127
+ /**
128
+ * The stops a category fill may take, by mode: it is also a card's tint, so
129
+ * it stays pale in light and deep in dark.
130
+ */
131
+ const TINT_STOPS = /** @type {const} */ ({light: [70, 95], dark: [10, 35]});
132
+ /** Core's category hues; gray is the neutral family's. */
133
+ export const CATEGORY_HUES = /** @type {const} */ ([
134
+ 'red',
135
+ 'orange',
136
+ 'yellow',
137
+ 'green',
138
+ 'teal',
139
+ 'cyan',
140
+ 'blue',
141
+ 'purple',
142
+ 'pink',
143
+ 'gray',
144
+ ]);
145
+ /** The chart's series hues, in Core's series order. */
146
+ export const CHART_SERIES = /** @type {const} */ ([
147
+ 'blue',
148
+ 'orange',
149
+ 'purple',
150
+ 'green',
151
+ 'pink',
152
+ 'cyan',
153
+ 'red',
154
+ 'teal',
155
+ 'brown',
156
+ 'indigo',
157
+ ]);
158
+ /**
159
+ * Each chart scale and the family it is drawn from.
160
+ * @type {Readonly<Record<string, string>>}
161
+ */
162
+ export const CHART_SCALES = {
163
+ blue: 'blue',
164
+ shamrock: 'green',
165
+ orange: 'orange',
166
+ pink: 'pink',
167
+ purple: 'purple',
168
+ red: 'red',
169
+ teal: 'teal',
170
+ yellow: 'yellow',
171
+ gray: 'neutral',
172
+ };
173
+ /** The stops of a scale's five steps, pale to deep. */
174
+ const SCALE_STOPS = [92, 75, 55, 35, 15];
175
+ /**
176
+ * The seed each derived hue grows from: the official neutral palette's
177
+ * category seeds (packages/themes/neutral/palette.config.json; a test keeps
178
+ * them equal), and Core's own chart colors for the two series no category
179
+ * has.
180
+ * @type {Readonly<Record<string, string>>}
181
+ */
182
+ export const HUE_SEEDS = {
183
+ red: '#d62830',
184
+ orange: '#d57113',
185
+ yellow: '#f8c723',
186
+ green: '#358a3a',
187
+ teal: '#0c7365',
188
+ cyan: '#0c6f82',
189
+ blue: '#0074e2',
190
+ purple: '#980fb2',
191
+ pink: '#b10e69',
192
+ brown: '#965E03',
193
+ indigo: '#6F8AFF',
194
+ };
195
+ /**
196
+ * Where each category token starts, `[light, dark]`, as the official
197
+ * neutral theme maps it (a test keeps them equal).
198
+ */
199
+ export const CATEGORY_STOPS = /** @type {const} */ ({
200
+ background: [85, 25],
201
+ border: [80, 65],
202
+ icon: [25, 75],
203
+ text: [25, 80],
204
+ grayBackground: [90, 20],
205
+ grayBorder: [85, 15],
206
+ grayIcon: [30, 65],
207
+ grayText: [10, 85],
208
+ });
209
+ /** WCAG 2.2 AA by role. */
210
+ const TEXT = 4.5;
211
+ const NON_TEXT = 3;
212
+ /** Two colors under this ΔE (×100) read as one. */
213
+ const ALIKE = 2;
214
+ /**
215
+ * How close a card tint can be to another tint, or to a page, before they
216
+ * read as one. Core's own red and orange tints sit about ΔE 2 apart.
217
+ */
218
+ const TINT_ALIKE = 1.5;
219
+ /** Below this chroma a seed is a gray. */
220
+ const GRAY_CHROMA = 0.02;
221
+ /**
222
+ * How strong a color is for its lightness and hue, 0 to 1.
223
+ * @param {string} color
224
+ * @returns {number}
225
+ */
226
+ function strength(color) {
227
+ const {L, C, H} = hexToOklch(color);
228
+ const most = maxOklchChroma(H, L);
229
+ return most > 0 ? Math.min(1, C / most) : 0;
230
+ }
231
+ /**
232
+ * How strong the look's colors are against Core's: the accent's strength
233
+ * over the median of Core's hues, in steps of 0.05 (so an accent of about
234
+ * the same strength leaves them as they are); 1 without a colored accent.
235
+ * @param {string | null | undefined} accent
236
+ * @returns {number}
237
+ */
238
+ function strengthScale(accent) {
239
+ const core = Object.values(HUE_SEEDS)
240
+ .map(strength)
241
+ .sort((a, b) => a - b);
242
+ const median = core[Math.floor(core.length / 2)];
243
+ return accent && hexToOklch(accent).C >= GRAY_CHROMA && median > 0
244
+ ? Math.round(
245
+ Math.min(1.2, Math.max(0.25, strength(accent) / median)) * 20,
246
+ ) / 20
247
+ : 1;
248
+ }
249
+ /**
250
+ * The seeds of the derived hues: Core's hues at the strength of the accent,
251
+ * in steps of 0.05 so an accent of about the same strength leaves them as
252
+ * they are, turned a little toward the accent's and a tinted neutral's hue
253
+ * (hueLean); Core's strength without a colored accent.
254
+ * @param {string | null | undefined} accent
255
+ * @param {string | null} [neutral]
256
+ * @returns {Record<string, string>}
257
+ */
258
+ export function lookSeeds(accent, neutral) {
259
+ const hues = Object.keys(HUE_SEEDS);
260
+ const scale = strengthScale(accent);
261
+ const lean = hueLean(accent, neutral);
262
+ return Object.fromEntries(
263
+ hues.map(h => {
264
+ const {L, C, H} = hexToOklch(HUE_SEEDS[h]);
265
+ return [h, oklchClampedHex(L, C * scale, lean(H))];
266
+ }),
267
+ );
268
+ }
269
+ /** The most a look turns a hue toward its accent, and toward its neutral. */
270
+ const LEAN_TO_ACCENT = 10;
271
+ const LEAN_TO_NEUTRAL = 6;
272
+ /**
273
+ * How the look turns each hue: a share of the way toward the accent's hue
274
+ * (at most 10°), and toward a tinted neutral's (at most 6°), so a slate
275
+ * theme's colors run cool and a warm gray's warm, and two looks with the
276
+ * same strength still differ. Rounded to whole degrees so a nearby color
277
+ * leaves the hues as they are.
278
+ * @param {string | null | undefined} accent
279
+ * @param {string | null | undefined} neutral
280
+ * @returns {(hue: number) => number}
281
+ */
282
+ function hueLean(accent, neutral) {
283
+ const toward =
284
+ /**
285
+ * @param {string | null | undefined} c
286
+ * @param {number} share
287
+ * @param {number} most
288
+ * @returns {((hue: number) => number)}
289
+ */
290
+ (c, share, most) => {
291
+ if (!c) {
292
+ return () => 0;
293
+ }
294
+ const {C, H} = hexToOklch(c);
295
+ if (C < GRAY_CHROMA) {
296
+ return () => 0;
297
+ }
298
+ return hue => {
299
+ const gap = ((H - hue + 540) % 360) - 180;
300
+ return Math.max(-most, Math.min(most, gap * share));
301
+ };
302
+ };
303
+ const a = toward(accent, 0.15, LEAN_TO_ACCENT);
304
+ const n = toward(neutral, 0.1, LEAN_TO_NEUTRAL);
305
+ return hue => (Math.round(hue + a(hue) + n(hue)) + 360) % 360;
306
+ }
307
+ /**
308
+ * Where a look's card tints start, `[light, dark]`: a strong or dark
309
+ * accent draws them a step deeper, a pale accent a step paler; the
310
+ * official neutral theme's stops otherwise.
311
+ * @param {string | null | undefined} accent
312
+ * @returns {readonly [number, number]}
313
+ */
314
+ export function tintStops(accent) {
315
+ if (!accent || hexToOklch(accent).C < GRAY_CHROMA) {
316
+ return CATEGORY_STOPS.background;
317
+ }
318
+ const {L} = hexToOklch(accent);
319
+ if (strengthScale(accent) >= 1.1 || L < 0.4) {
320
+ return [80, 30];
321
+ }
322
+ if (L > 0.8) {
323
+ return [90, 20];
324
+ }
325
+ return CATEGORY_STOPS.background;
326
+ }
327
+ /**
328
+ * Whether a family is one lookSeeds grows: an id, a name and a seed at
329
+ * Core's own lightness and hue for that id (only its strength follows the
330
+ * look).
331
+ * @param {unknown} family
332
+ * @returns {boolean}
333
+ */
334
+ export function isLookFamily(family) {
335
+ const f = /** @type {{id?: unknown; seed?: unknown} | null | undefined} */ (
336
+ family
337
+ );
338
+ const core = typeof f?.id === 'string' ? HUE_SEEDS[f.id] : undefined;
339
+ if (!core || typeof f?.seed !== 'string') {
340
+ return false;
341
+ }
342
+ if (!Object.keys(f).every(k => ['id', 'name', 'seed'].includes(k))) {
343
+ return false;
344
+ }
345
+ /**
346
+ * @type {{L: number; C: number; H: number}}
347
+ */
348
+ let a;
349
+ try {
350
+ a = hexToOklch(f.seed);
351
+ } catch {
352
+ return false;
353
+ }
354
+ const b = hexToOklch(core);
355
+ const turn = Math.abs(((a.H - b.H + 540) % 360) - 180);
356
+ // A weak seed's hue moves a little when written as hex.
357
+ const slack =
358
+ 2 +
359
+ LEAN_TO_ACCENT +
360
+ LEAN_TO_NEUTRAL +
361
+ (Math.atan2(0.004, Math.max(a.C, 1e-6)) * 180) / Math.PI;
362
+ return Math.abs(a.L - b.L) < 0.02 && (turn < slack || a.C < GRAY_CHROMA);
363
+ }
364
+ /**
365
+ * The category and chart colors a palette derives.
366
+ * @param {DeriveLookInput} param0
367
+ * @returns {DerivedLook}
368
+ */
369
+ export function deriveLookColors({
370
+ palette,
371
+ accent = null,
372
+ stops,
373
+ modes,
374
+ text,
375
+ grounds,
376
+ wanted = () => true,
377
+ shown = modes,
378
+ }) {
379
+ /**
380
+ * @type {Record<string, DerivedToken>}
381
+ */
382
+ const tokens = {};
383
+ const tint = tintStops(accent);
384
+ /**
385
+ * @type {string[]}
386
+ */
387
+ const limits = [];
388
+ /**
389
+ * @type {string[]}
390
+ */
391
+ const moved = [];
392
+ /**
393
+ * @type {DerivedLook}
394
+ */
395
+ const out = {
396
+ tokens,
397
+ badges: 'none',
398
+ chart: 'none',
399
+ limits,
400
+ moved,
401
+ alike: false,
402
+ unread: false,
403
+ };
404
+ /** A limit no shade meets in a mode the theme shows: one note, however many times it is met. */
405
+ const unmet =
406
+ /**
407
+ * @param {string} line
408
+ * @param {'alike' | 'unread'} kind
409
+ * @param {Mode} m
410
+ * @returns {void}
411
+ */
412
+ (line, kind, m) => {
413
+ if (!shown.includes(m)) {
414
+ return;
415
+ }
416
+ out[kind] = true;
417
+ if (!limits.includes(line)) {
418
+ limits.push(line);
419
+ }
420
+ };
421
+ const sorted = [...stops].sort((a, b) => a - b);
422
+ const near =
423
+ /**
424
+ * @param {number} n
425
+ * @returns {number}
426
+ */
427
+ n =>
428
+ sorted.reduce(
429
+ (best, s) => (Math.abs(s - n) < Math.abs(best - n) ? s : best),
430
+ sorted[0],
431
+ );
432
+ const at =
433
+ /**
434
+ * @param {string} id
435
+ * @param {Mode} m
436
+ * @param {number} stop
437
+ * @returns {Stop}
438
+ */
439
+ (id, m, stop) => ({
440
+ stop,
441
+ color: palette[id][m][stop],
442
+ });
443
+ /** Stops by distance from `s0`. Ties go darker first in light and
444
+ * lighter first in dark. */
445
+ const outward =
446
+ /**
447
+ * @param {Mode} m
448
+ * @param {number} s0
449
+ * @returns {number[]}
450
+ */
451
+ (m, s0) =>
452
+ [...sorted].sort(
453
+ (a, b) =>
454
+ Math.abs(a - s0) - Math.abs(b - s0) ||
455
+ (m === 'light' ? a - b : b - a),
456
+ );
457
+ const lowest =
458
+ /**
459
+ * @param {string} color
460
+ * @param {string[]} on
461
+ * @returns {number}
462
+ */
463
+ (color, on) => Math.min(...on.map(bg => contrastRatio(color, bg)));
464
+ /**
465
+ * The stop nearest `start` that reads at `needs` on every ground,
466
+ * searched both ways, or the most contrasting one when none does; and
467
+ * the ratio it reads at.
468
+ */
469
+ const nearest =
470
+ /**
471
+ * @param {string} id
472
+ * @param {Mode} m
473
+ * @param {number} start
474
+ * @param {string[]} on
475
+ * @param {number} needs
476
+ * @returns {{stop: Stop; r: number}}
477
+ */
478
+ (id, m, start, on, needs) => {
479
+ const scored = outward(m, near(start)).map(stop => ({
480
+ stop,
481
+ r: lowest(at(id, m, stop).color, on),
482
+ }));
483
+ const best =
484
+ scored.find(x => x.r >= needs) ??
485
+ scored.reduce((x, y) => (y.r > x.r ? y : x));
486
+ return {stop: at(id, m, best.stop), r: best.r};
487
+ };
488
+ /** A color under its contrast at its most readable shade is a note. */
489
+ const noteShort =
490
+ /**
491
+ * @param {string} token
492
+ * @param {Mode} m
493
+ * @param {number} r
494
+ * @param {number} needs
495
+ * @returns {void}
496
+ */
497
+ (token, m, r, needs) => {
498
+ if (r < needs) {
499
+ unmet(
500
+ `${token} reads at only ${ratioDown(r)}:1 in ${m} at its most readable shade (needs ${needs}:1)`,
501
+ 'unread',
502
+ m,
503
+ );
504
+ }
505
+ };
506
+ /** The nearest stop that reads, noted when none does. */
507
+ const pick =
508
+ /**
509
+ * @param {string} token
510
+ * @param {string} id
511
+ * @param {Mode} m
512
+ * @param {number} start
513
+ * @param {string[]} on
514
+ * @param {number} needs
515
+ * @returns {Stop}
516
+ */
517
+ (token, id, m, start, on, needs) => {
518
+ const {stop, r} = nearest(id, m, start, on, needs);
519
+ noteShort(token, m, r, needs);
520
+ return stop;
521
+ };
522
+ const each =
523
+ /**
524
+ * @param {(m: Mode) => Stop} fn
525
+ * @returns {Record<Mode, Stop>}
526
+ */
527
+ fn => {
528
+ const v = /** @type {Record<Mode, Stop>} */ ({});
529
+ for (const m of /** @type {const} */ (['light', 'dark'])) {
530
+ v[m] = fn(modes.includes(m) ? m : modes[0]);
531
+ }
532
+ return v;
533
+ };
534
+ const shift =
535
+ /**
536
+ * @param {number} from
537
+ * @param {number} to
538
+ * @returns {string}
539
+ */
540
+ (from, to) => (to < from ? 'deeper' : 'paler');
541
+ // Category colors: Badge and Token fill and text, Card tints. A hue is
542
+ // drawn alike when its fill and text are both under ΔE 2 from an earlier
543
+ // hue's, or its tint is under TINT_ALIKE from an earlier tint or a
544
+ // page. A Card shows only its tint behind the theme's text. Such a hue
545
+ // takes another tint that carries the text. A limit no tint meets
546
+ // becomes a note.
547
+ const hues = CATEGORY_HUES.filter(h => h !== 'gray' || palette.neutral);
548
+ const famOf =
549
+ /**
550
+ * @param {string} h
551
+ * @returns {string}
552
+ */
553
+ h => (h === 'gray' ? 'neutral' : h);
554
+ if (hues.every(h => palette[famOf(h)])) {
555
+ const start =
556
+ /**
557
+ * @param {string} h
558
+ * @param {'background' | 'border' | 'icon' | 'text'} role
559
+ * @param {Mode} m
560
+ * @returns {number}
561
+ */
562
+ (h, role, m) => {
563
+ const pair =
564
+ h === 'gray'
565
+ ? {
566
+ background: CATEGORY_STOPS.grayBackground,
567
+ border: CATEGORY_STOPS.grayBorder,
568
+ icon: CATEGORY_STOPS.grayIcon,
569
+ text: CATEGORY_STOPS.grayText,
570
+ }[role]
571
+ : CATEGORY_STOPS[role];
572
+ return pair[m === 'light' ? 0 : 1];
573
+ };
574
+ const badgeOn =
575
+ /**
576
+ * @param {string} h
577
+ * @param {Mode} m
578
+ * @param {number} fillStop
579
+ * @returns {Badge}
580
+ */
581
+ (h, m, fillStop) => {
582
+ const id = famOf(h);
583
+ const fill = at(id, m, fillStop);
584
+ const on = [fill.color, ...grounds(m)];
585
+ return {
586
+ fill,
587
+ ink: nearest(id, m, start(h, 'text', m), on, TEXT),
588
+ icon: nearest(id, m, start(h, 'icon', m), on, NON_TEXT),
589
+ };
590
+ };
591
+ /**
592
+ * @type {Record< string, Partial<Record<Mode, Badge>> >}
593
+ */
594
+ const drawn = Object.fromEntries(hues.map(h => [h, {}]));
595
+ for (const m of modes) {
596
+ /**
597
+ * @type {{hue: string; fill: string; ink: string}[]}
598
+ */
599
+ const placed = [];
600
+ for (const h of hues) {
601
+ const id = famOf(h);
602
+ const s0 = near(
603
+ h === 'gray'
604
+ ? start(h, 'background', m)
605
+ : tint[m === 'light' ? 0 : 1],
606
+ );
607
+ // Paler first in light, darker first in dark, inside the tint's
608
+ // stops: a tint, never a solid fill, since it is also the card's.
609
+ const [lo, hi] = TINT_STOPS[m];
610
+ const carrying = [...sorted]
611
+ .filter(x => x >= lo && x <= hi)
612
+ .sort(
613
+ (a, b) =>
614
+ Math.abs(a - s0) - Math.abs(b - s0) ||
615
+ (m === 'light' ? b - a : a - b),
616
+ )
617
+ .filter(x =>
618
+ text(m).every(c => contrastRatio(c, at(id, m, x).color) >= TEXT),
619
+ );
620
+ if (!carrying.length) {
621
+ const worst = Math.min(
622
+ ...text(m).map(c => contrastRatio(c, at(id, m, s0).color)),
623
+ );
624
+ unmet(
625
+ `--color-background-${h} carries the theme's text at only ${ratioDown(worst)}:1 in ${m} (needs ${TEXT}:1)`,
626
+ 'unread',
627
+ m,
628
+ );
629
+ }
630
+ const like =
631
+ /**
632
+ * @param {Badge} b
633
+ * @returns {{hue: string} | undefined}
634
+ */
635
+ b =>
636
+ placed.find(p => {
637
+ const fill = perceptualDelta(p.fill, b.fill.color);
638
+ return (
639
+ fill < TINT_ALIKE ||
640
+ (fill < ALIKE &&
641
+ perceptualDelta(p.ink, b.ink.stop.color) < ALIKE)
642
+ );
643
+ }) ??
644
+ (grounds(m).some(g => perceptualDelta(g, b.fill.color) < TINT_ALIKE)
645
+ ? {hue: 'the page'}
646
+ : undefined);
647
+ let badge = badgeOn(h, m, carrying[0] ?? s0);
648
+ const twin = like(badge);
649
+ if (twin !== undefined) {
650
+ /**
651
+ * @type {Badge | undefined}
652
+ */
653
+ let apart;
654
+ // Paler first in light, darker first in dark: a moved tint stays
655
+ // as quiet as the look; a stronger one only when no quieter one
656
+ // stands apart.
657
+ const first = carrying[0] ?? s0;
658
+ const quieter =
659
+ /**
660
+ * @param {number} x
661
+ */
662
+ x => (m === 'light' ? x > first : x < first);
663
+ const tries = [
664
+ ...carrying.filter(quieter),
665
+ ...carrying.slice(1).filter(x => !quieter(x)),
666
+ ];
667
+ for (const s of tries) {
668
+ const b = badgeOn(h, m, s);
669
+ if (like(b) === undefined) {
670
+ apart = b;
671
+ break;
672
+ }
673
+ }
674
+ if (apart === undefined) {
675
+ unmet(
676
+ `--color-background-${h} is drawn like ${twin.hue} in ${m}: no tint of it that carries the text stands apart`,
677
+ 'alike',
678
+ m,
679
+ );
680
+ } else {
681
+ if (shown.includes(m))
682
+ moved.push(
683
+ `--color-background-${h} takes a ${shift(badge.fill.stop, apart.fill.stop)} tint in ${m}, apart from ${twin.hue}`,
684
+ );
685
+ badge = apart;
686
+ }
687
+ }
688
+ noteShort(`--color-text-${h}`, m, badge.ink.r, TEXT);
689
+ noteShort(`--color-icon-${h}`, m, badge.icon.r, NON_TEXT);
690
+ placed.push({
691
+ hue: h,
692
+ fill: badge.fill.color,
693
+ ink: badge.ink.stop.color,
694
+ });
695
+ drawn[h][m] = badge;
696
+ }
697
+ }
698
+ for (const h of hues) {
699
+ const id = famOf(h);
700
+ const of =
701
+ /**
702
+ * @param {Mode} m
703
+ * @returns {Badge}
704
+ */
705
+ m => /** @type {Badge} */ (drawn[h][m]);
706
+ /**
707
+ * @type {Record<string, Record<Mode, Stop>>}
708
+ */
709
+ const set = {
710
+ [`--color-background-${h}`]: each(m => of(m).fill),
711
+ [`--color-text-${h}`]: each(m => of(m).ink.stop),
712
+ [`--color-icon-${h}`]: each(m => of(m).icon.stop),
713
+ [`--color-border-${h}`]: each(m =>
714
+ at(id, m, near(start(h, 'border', m))),
715
+ ),
716
+ };
717
+ for (const [k, v] of Object.entries(set)) {
718
+ if (wanted(k)) {
719
+ tokens[k] = {family: id, ...v};
720
+ }
721
+ }
722
+ }
723
+ out.badges = 'derived';
724
+ }
725
+ // Chart colors: series at 3:1 on the page (graphics), a gray, and the
726
+ // five-step scales. A series drawn like an earlier one takes the nearest
727
+ // shade that reads at 3:1 and stands apart from every earlier series. A
728
+ // limit the palette cannot meet becomes a note.
729
+ if (CHART_SERIES.every(h => palette[h])) {
730
+ /**
731
+ * @type {Record<string, DerivedToken>}
732
+ */
733
+ const chart = {};
734
+ /**
735
+ * @type {Record<string, Record<Mode, Stop>>}
736
+ */
737
+ const picked = {};
738
+ for (const h of CHART_SERIES) {
739
+ picked[h] = each(m =>
740
+ pick(
741
+ `--color-data-categorical-${h}`,
742
+ h,
743
+ m,
744
+ m === 'light' ? 50 : 60,
745
+ grounds(m),
746
+ NON_TEXT,
747
+ ),
748
+ );
749
+ }
750
+ for (const m of modes) {
751
+ /**
752
+ * @type {{hue: string; color: string}[]}
753
+ */
754
+ const placed = [];
755
+ for (const h of CHART_SERIES) {
756
+ const own = picked[h][m];
757
+ const like = placed.find(
758
+ p => perceptualDelta(p.color, own.color) < ALIKE,
759
+ );
760
+ if (like !== undefined) {
761
+ const apart = outward(m, own.stop).find(stop => {
762
+ const c = at(h, m, stop).color;
763
+ return (
764
+ lowest(c, grounds(m)) >= NON_TEXT &&
765
+ placed.every(p => perceptualDelta(p.color, c) >= ALIKE)
766
+ );
767
+ });
768
+ if (apart === undefined) {
769
+ unmet(
770
+ `--color-data-categorical-${h} is drawn like ${like.hue} in ${m}: no shade of it that reads at ${NON_TEXT}:1 stands apart`,
771
+ 'alike',
772
+ m,
773
+ );
774
+ } else {
775
+ picked[h][m] = at(h, m, apart);
776
+ if (shown.includes(m))
777
+ moved.push(
778
+ `--color-data-categorical-${h} takes a ${shift(own.stop, apart)} shade in ${m}, apart from ${like.hue}`,
779
+ );
780
+ }
781
+ }
782
+ placed.push({hue: h, color: picked[h][m].color});
783
+ }
784
+ }
785
+ for (const h of CHART_SERIES) {
786
+ // A one-mode theme writes its mode on both sides.
787
+ const refs = each(m => picked[h][m]);
788
+ chart[`--color-data-categorical-${h}`] = {family: h, ...refs};
789
+ }
790
+ if (palette.neutral) {
791
+ chart['--color-data-neutral'] = {
792
+ family: 'neutral',
793
+ ...each(m =>
794
+ pick('--color-data-neutral', 'neutral', m, 55, grounds(m), NON_TEXT),
795
+ ),
796
+ };
797
+ }
798
+ for (const [scale, id] of Object.entries(CHART_SCALES)) {
799
+ if (palette[id]) {
800
+ SCALE_STOPS.forEach((stop, i) => {
801
+ chart[`--color-data-${scale}-${i + 1}`] = {
802
+ family: id,
803
+ ...each(m => at(id, m, near(stop))),
804
+ };
805
+ });
806
+ }
807
+ }
808
+ for (const [k, v] of Object.entries(chart)) {
809
+ if (wanted(k)) {
810
+ tokens[k] = v;
811
+ }
812
+ }
813
+ out.chart = 'derived';
814
+ }
815
+ return out;
816
+ }
817
+ /**
818
+ * The status roles a palette derives for a look that leaves them unset: each
819
+ * role's dial id and the word its tokens carry.
820
+ * @type {Readonly<Record<string, string>>}
821
+ */
822
+ export const STATUS_ROLES = {
823
+ info: 'info',
824
+ success: 'success',
825
+ warning: 'warning',
826
+ danger: 'error',
827
+ };
828
+ /**
829
+ * A status fill under this ΔE (×100, an OKLab distance of 0.1) from the
830
+ * accent's reads as the accent at a glance, as two button fills do.
831
+ */
832
+ const APART_FROM_ACCENT = 10;
833
+ /**
834
+ * The seeds of the status roles a look derives: each role's own hue and
835
+ * lightness (the base theme's color for it) at the accent's strength. A
836
+ * role keeps its hue whatever the accent: an error stays the base's red,
837
+ * info its blue. Only its strength follows the look; a role that would
838
+ * then read as the accent takes the nearest shade apart from it
839
+ * (deriveStatusColors).
840
+ * `base` holds each role's base color by dial id (info, success, warning,
841
+ * danger).
842
+ * @param {Record<string, string>} base
843
+ * @param {string | null | undefined} accent
844
+ * @returns {Record<string, string>}
845
+ */
846
+ export function statusSeeds(base, accent) {
847
+ const scale = strengthScale(accent);
848
+ /**
849
+ * @type {Record<string, string>}
850
+ */
851
+ const out = {};
852
+ for (const [id, color] of Object.entries(base)) {
853
+ const {L, C, H} = hexToOklch(color);
854
+ out[id] = oklchClampedHex(L, C * scale, H);
855
+ }
856
+ return out;
857
+ }
858
+ /**
859
+ * Where each status token starts, `[light, dark]`: the fill at its middle,
860
+ * the text, tint and text on the tint where the official neutral theme maps
861
+ * them, icons and borders at the fill.
862
+ */
863
+ export const STATUS_STOPS = /** @type {const} */ ({
864
+ fill: [50, 70],
865
+ warningFill: [85, 85],
866
+ text: [45, 80],
867
+ muted: [85, 75],
868
+ /** The dark tint is see-through, as the official neutral theme's is. */
869
+ mutedDarkAlpha: '3D',
870
+ onMuted: [25, 80],
871
+ });
872
+ /**
873
+ * The status colors a palette derives for the roles a theme leaves unset.
874
+ * @param {DeriveStatusInput} param0
875
+ * @returns {DerivedStatus}
876
+ */
877
+ export function deriveStatusColors({
878
+ palette,
879
+ stops,
880
+ modes,
881
+ roles,
882
+ grounds,
883
+ inks,
884
+ accent,
885
+ wanted = () => true,
886
+ shown = modes,
887
+ }) {
888
+ /**
889
+ * @type {Record<string, DerivedToken>}
890
+ */
891
+ const tokens = {};
892
+ /**
893
+ * @type {Record<string, 'derived'>}
894
+ */
895
+ const state = {};
896
+ /**
897
+ * @type {string[]}
898
+ */
899
+ const why = [];
900
+ /**
901
+ * @type {string[]}
902
+ */
903
+ const short = [];
904
+ const sorted = [...stops].filter(s => s > 0 && s < 100).sort((a, b) => a - b);
905
+ const near =
906
+ /**
907
+ * @param {number} n
908
+ */
909
+ n =>
910
+ sorted.reduce(
911
+ (best, s) => (Math.abs(s - n) < Math.abs(best - n) ? s : best),
912
+ sorted[0],
913
+ );
914
+ const each =
915
+ /**
916
+ * @param {(m: Mode) => Stop} fn
917
+ */
918
+ fn => {
919
+ const v = /** @type {Record<Mode, Stop>} */ ({});
920
+ for (const m of /** @type {Mode[]} */ (['light', 'dark']))
921
+ v[m] = fn(modes.includes(m) ? m : modes[0]);
922
+ return v;
923
+ };
924
+ for (const id of roles) {
925
+ const w = STATUS_ROLES[id];
926
+ const fam = palette[id];
927
+ if (!w || !fam) continue;
928
+ const at =
929
+ /**
930
+ * @param {Mode} m
931
+ * @param {number} stop
932
+ */
933
+ (m, stop) => ({stop, color: fam[m][stop]});
934
+ /**
935
+ * @type {string[]}
936
+ */
937
+ const fails = [];
938
+ // Text or an icon that reads at no shade: the role keeps the base
939
+ // theme's colors, which read.
940
+ let unread = false;
941
+ /** A note for a mode the theme shows. */
942
+ const fail =
943
+ /**
944
+ * @param {Mode} m
945
+ * @param {string} line
946
+ * @returns {void}
947
+ */
948
+ (m, line, text = false) => {
949
+ if (!shown.includes(m)) return;
950
+ fails.push(line);
951
+ if (text) unread = true;
952
+ };
953
+ /**
954
+ * The stop nearest `start` that reads at `needs` on every color in `bg`.
955
+ */
956
+ const pick =
957
+ /**
958
+ * @param {string} token
959
+ * @param {Mode} m
960
+ * @param {number} start
961
+ * @param {string[]} bg
962
+ * @param {number} needs
963
+ * @returns {Stop}
964
+ */
965
+ (token, m, start, bg, needs) => {
966
+ const s0 = near(start);
967
+ const order = [...sorted].sort(
968
+ (a, b) =>
969
+ Math.abs(a - s0) - Math.abs(b - s0) ||
970
+ (m === 'light' ? a - b : b - a),
971
+ );
972
+ const scored = order.map(stop => ({
973
+ stop,
974
+ r: Math.min(...bg.map(c => contrastRatio(at(m, stop).color, c))),
975
+ }));
976
+ const best =
977
+ scored.find(x => x.r >= needs) ??
978
+ scored.reduce((x, y) => (y.r > x.r ? y : x));
979
+ if (best.r < needs)
980
+ fail(
981
+ m,
982
+ `${token} reads at only ${ratioDown(best.r)}:1 in ${m} at its most readable shade (needs ${needs}:1)`,
983
+ true,
984
+ );
985
+ return at(m, best.stop);
986
+ };
987
+ // The fill: nearest its middle that stands out from the page at 3:1 and
988
+ // carries white or near-black text at 4.5:1 (near-black first on
989
+ // warning, white first otherwise).
990
+ const onFill = /** @type {Record<Mode, Stop & {family: string}>} */ ({});
991
+ // Warning is a bright fill that carries near-black text, as Core's is:
992
+ // its text, not its edge, makes it read, so it is not held to 3:1 on
993
+ // the page.
994
+ const bright = id === 'warning';
995
+ const fill = each(m => {
996
+ const s0 = near(
997
+ (bright ? STATUS_STOPS.warningFill : STATUS_STOPS.fill)[
998
+ m === 'light' ? 0 : 1
999
+ ],
1000
+ );
1001
+ const order = [...sorted].sort(
1002
+ (a, b) => Math.abs(a - s0) - Math.abs(b - s0) || b - a,
1003
+ );
1004
+ const choices = inks(m, id);
1005
+ const a = accent(m);
1006
+ const standsOut =
1007
+ /**
1008
+ * @param {string} c
1009
+ */
1010
+ c =>
1011
+ bright ||
1012
+ Math.min(...grounds(m).map(g => contrastRatio(c, g))) >= NON_TEXT;
1013
+ const apart =
1014
+ /**
1015
+ * @param {string} c
1016
+ */
1017
+ c => !a || perceptualDelta(c, a) >= APART_FROM_ACCENT;
1018
+ // (A bright fill already starts at the palest stop that reads as a
1019
+ // fill on a light page: one kept apart from the accent moves deeper,
1020
+ // never paler toward the page.)
1021
+ const allowed = order.filter(s => !(bright && m === 'light' && s > s0));
1022
+ /** The fill at a stop, with a note for each limit it gives up. */
1023
+ const take =
1024
+ /**
1025
+ * @param {number} s
1026
+ * @param {Stop & {family: string}} ink
1027
+ * @returns {Stop}
1028
+ */
1029
+ (s, ink) => {
1030
+ const c = at(m, s).color;
1031
+ if (!standsOut(c))
1032
+ fail(
1033
+ m,
1034
+ `--color-${w} stands out from the page at under ${NON_TEXT}:1 in ${m}`,
1035
+ );
1036
+ if (!apart(c))
1037
+ fail(m, `--color-${w} is drawn like your accent in ${m}`);
1038
+ const r = contrastRatio(ink.color, c);
1039
+ if (r < TEXT)
1040
+ fail(
1041
+ m,
1042
+ `--color-on-${w} reads at only ${ratioDown(r)}:1 on --color-${w} in ${m} at its most readable shade (needs ${TEXT}:1)`,
1043
+ true,
1044
+ );
1045
+ onFill[m] = ink;
1046
+ return at(m, s);
1047
+ };
1048
+ // The limits, dropped one at a time when no stop meets them all:
1049
+ // standing out from the page first, then standing apart from the
1050
+ // accent. Text that reads on no stop keeps the role at the base
1051
+ // theme's colors.
1052
+ for (const keep of [[standsOut, apart], [apart], []])
1053
+ // The role's usual text first: the nearest stop it reads on, before
1054
+ // the other text on a nearer stop.
1055
+ for (const ink of choices)
1056
+ for (const s of allowed) {
1057
+ const c = at(m, s).color;
1058
+ if (keep.every(ok => ok(c)) && contrastRatio(ink.color, c) >= TEXT)
1059
+ return take(s, ink);
1060
+ }
1061
+ // No stop carries readable text: the one that carries it best.
1062
+ const best = allowed.reduce((x, y) =>
1063
+ contrastRatio(choices[0].color, at(m, y).color) >
1064
+ contrastRatio(choices[0].color, at(m, x).color)
1065
+ ? y
1066
+ : x,
1067
+ );
1068
+ return take(best, choices[0]);
1069
+ });
1070
+ const muted = each(m => {
1071
+ let stop = near(STATUS_STOPS.muted[m === 'light' ? 0 : 1]);
1072
+ // (A light tint is paler than its fill: where the fill already sits
1073
+ // at the tint's stop or paler, a bright warning or one an accent made
1074
+ // pale, the tint takes the next paler stop that is another color.)
1075
+ if (m === 'light') {
1076
+ const paler = sorted.filter(
1077
+ s => s > fill[m].stop && at(m, s).color !== fill[m].color,
1078
+ );
1079
+ if (stop <= fill[m].stop || at(m, stop).color === fill[m].color)
1080
+ stop = paler[0] ?? stop;
1081
+ }
1082
+ const solid = at(m, stop);
1083
+ return m === 'dark'
1084
+ ? {
1085
+ stop,
1086
+ color: `${solid.color}${STATUS_STOPS.mutedDarkAlpha.toLowerCase()}`,
1087
+ alpha: STATUS_STOPS.mutedDarkAlpha.toLowerCase(),
1088
+ }
1089
+ : solid;
1090
+ });
1091
+ /** The tint as it shows over each surface. */
1092
+ const tintSeen =
1093
+ /**
1094
+ * @param {Mode} m
1095
+ */
1096
+ m =>
1097
+ muted[m].alpha
1098
+ ? grounds(m).map(g =>
1099
+ hexOf(
1100
+ compositeOver(
1101
+ rgbOf(muted[m].color.slice(0, 7)),
1102
+ parseInt(muted[m].alpha ?? 'ff', 16) / 255,
1103
+ rgbOf(g),
1104
+ ),
1105
+ ),
1106
+ )
1107
+ : [muted[m].color];
1108
+ const set = {
1109
+ [`--color-${w}`]: fill,
1110
+ [`--color-${w}-muted`]: muted,
1111
+ [`--color-text-${w}`]: each(m =>
1112
+ pick(
1113
+ `--color-text-${w}`,
1114
+ m,
1115
+ STATUS_STOPS.text[m === 'light' ? 0 : 1],
1116
+ [...grounds(m), ...tintSeen(m)],
1117
+ TEXT,
1118
+ ),
1119
+ ),
1120
+ [`--color-on-${w}-muted`]: each(m =>
1121
+ pick(
1122
+ `--color-on-${w}-muted`,
1123
+ m,
1124
+ STATUS_STOPS.onMuted[m === 'light' ? 0 : 1],
1125
+ tintSeen(m),
1126
+ TEXT,
1127
+ ),
1128
+ ),
1129
+ [`--color-icon-${w}`]: each(m =>
1130
+ pick(`--color-icon-${w}`, m, fill[m].stop, grounds(m), NON_TEXT),
1131
+ ),
1132
+ [`--color-border-${w}`]: each(m =>
1133
+ pick(`--color-border-${w}`, m, fill[m].stop, grounds(m), NON_TEXT),
1134
+ ),
1135
+ };
1136
+ if (unread) {
1137
+ why.push(
1138
+ `${id} keeps the base theme's colors: no shade of its family reads`,
1139
+ );
1140
+ short.push(id);
1141
+ continue;
1142
+ }
1143
+ // A one-mode theme measures its mode for both sides: one note each.
1144
+ for (const line of fails) if (!why.includes(line)) why.push(line);
1145
+ if (fails.length) short.push(id);
1146
+ state[id] = 'derived';
1147
+ for (const [k, v] of Object.entries(set))
1148
+ if (wanted(k)) tokens[k] = {family: id, ...v};
1149
+ const inkToken = `--color-on-${w}`;
1150
+ if (wanted(inkToken))
1151
+ tokens[inkToken] = {
1152
+ family: onFill.light.family,
1153
+ light: onFill.light,
1154
+ dark: onFill.dark,
1155
+ };
1156
+ }
1157
+ return {tokens, roles: state, why, short};
1158
+ }
1159
+ /**
1160
+ * Code colors, `[family, light stop, dark stop]` by syntax role, as the
1161
+ * official neutral theme maps them. The gray roles read the neutral.
1162
+ */
1163
+ export const SYNTAX_STOPS = /** @type {const} */ ({
1164
+ keyword: ['purple', 30, 80],
1165
+ string: ['green', 30, 80],
1166
+ comment: ['neutral', 35, 75],
1167
+ number: ['orange', 30, 80],
1168
+ function: ['blue', 30, 80],
1169
+ type: ['purple', 30, 80],
1170
+ variable: ['neutral', 5, 90],
1171
+ operator: ['neutral', 35, 75],
1172
+ constant: ['orange', 30, 80],
1173
+ tag: ['red', 30, 80],
1174
+ attribute: ['yellow', 30, 80],
1175
+ property: ['teal', 30, 80],
1176
+ punctuation: ['neutral', 35, 75],
1177
+ });
1178
+ /** The code background and the overlay scrim, from the neutral. */
1179
+ const CODE_GROUND = /** @type {const} */ ({light: 100, dark: 5});
1180
+ const SCRIM = /** @type {const} */ ({
1181
+ light: {stop: 10, alpha: '80'},
1182
+ dark: {stop: 5, alpha: 'cc'},
1183
+ });
1184
+ /**
1185
+ * The code colors and the overlay scrim a palette derives: each syntax role
1186
+ * from its hue's family, the nearest stop that reads at 4.5:1 on the code
1187
+ * background and on the page; the code background and the scrim from the
1188
+ * neutral. A role that cannot read keeps the base theme's.
1189
+ * @param {DeriveCodeInput} param0
1190
+ * @returns {{ tokens: Record<string, DerivedToken>; unreadable: string[]; }}
1191
+ */
1192
+ export function deriveCodeColors({
1193
+ palette,
1194
+ stops,
1195
+ modes,
1196
+ grounds,
1197
+ codeGround = () => null,
1198
+ wanted = () => true,
1199
+ }) {
1200
+ /**
1201
+ * @type {Record<string, DerivedToken>}
1202
+ */
1203
+ const tokens = {};
1204
+ /**
1205
+ * @type {string[]}
1206
+ */
1207
+ const unreadable = [];
1208
+ const sorted = [...stops].sort((a, b) => a - b);
1209
+ const near =
1210
+ /**
1211
+ * @param {number} n
1212
+ */
1213
+ n => sorted.reduce((x, y) => (Math.abs(y - n) < Math.abs(x - n) ? y : x));
1214
+ const at =
1215
+ /**
1216
+ * @param {string} id
1217
+ * @param {Mode} m
1218
+ * @param {number} stop
1219
+ * @returns {Stop}
1220
+ */
1221
+ (id, m, stop) => ({
1222
+ stop,
1223
+ color: palette[id][m][stop],
1224
+ });
1225
+ /**
1226
+ * @type {Partial<Record<Mode, Stop>>}
1227
+ */
1228
+ const ground = {};
1229
+ if (palette.neutral) {
1230
+ const bg = /** @type {Record<Mode, Stop>} */ ({});
1231
+ for (const m of modes) {
1232
+ bg[m] = at('neutral', m, near(CODE_GROUND[m]));
1233
+ ground[m] = bg[m];
1234
+ }
1235
+ if (wanted('--color-syntax-background')) {
1236
+ tokens['--color-syntax-background'] = {family: 'neutral', ...bg};
1237
+ }
1238
+ const scrim = /** @type {Record<Mode, Stop>} */ ({});
1239
+ for (const m of modes) {
1240
+ const s = near(SCRIM[m].stop);
1241
+ const c = palette.neutral[m][s];
1242
+ scrim[m] = {
1243
+ stop: s,
1244
+ color: `${c}${SCRIM[m].alpha}`,
1245
+ alpha: SCRIM[m].alpha,
1246
+ };
1247
+ }
1248
+ if (wanted('--color-overlay')) {
1249
+ tokens['--color-overlay'] = {family: 'neutral', ...scrim};
1250
+ }
1251
+ }
1252
+ for (const [role, [id, light, dark]] of Object.entries(SYNTAX_STOPS)) {
1253
+ const k = `--color-syntax-${role}`;
1254
+ if (!palette[id] || !wanted(k)) {
1255
+ continue;
1256
+ }
1257
+ const refs = /** @type {Record<Mode, Stop>} */ ({});
1258
+ let ok = true;
1259
+ for (const m of modes) {
1260
+ // A code background the theme sets itself is the one code shows on.
1261
+ const own = codeGround(m);
1262
+ const on = [
1263
+ ...(own ? [own] : ground[m] ? [ground[m].color] : []),
1264
+ ...grounds(m),
1265
+ ];
1266
+ const s0 = near(m === 'light' ? light : dark);
1267
+ const order = [...sorted].sort(
1268
+ (a, b) =>
1269
+ Math.abs(a - s0) - Math.abs(b - s0) ||
1270
+ (m === 'light' ? a - b : b - a),
1271
+ );
1272
+ const s = order.find(x =>
1273
+ on.every(bg => contrastRatio(palette[id][m][x], bg) >= TEXT),
1274
+ );
1275
+ if (s == null) {
1276
+ ok = false;
1277
+ unreadable.push(`${k} (${m})`);
1278
+ break;
1279
+ }
1280
+ refs[m] = at(id, m, s);
1281
+ }
1282
+ if (ok) {
1283
+ tokens[k] = {family: id, ...refs};
1284
+ }
1285
+ }
1286
+ return {tokens, unreadable};
1287
+ }
1288
+ /**
1289
+ * How full red-blind (protanopia) and green-blind (deuteranopia) vision maps
1290
+ * linear sRGB (Machado, Oliveira and Fernandes, 2009, severity 1). Anyone
1291
+ * red-weak or green-weak sees between these and normal vision.
1292
+ * @type {Record< 'red-weak' | 'green-weak', ReadonlyArray<ReadonlyArray<number>> >}
1293
+ */
1294
+ const CVD = {
1295
+ 'red-weak': [
1296
+ [0.152286, 1.052583, -0.204868],
1297
+ [0.114503, 0.786281, 0.099216],
1298
+ [-0.003882, -0.048116, 1.051998],
1299
+ ],
1300
+ 'green-weak': [
1301
+ [0.367322, 0.860646, -0.227968],
1302
+ [0.280085, 0.672501, 0.047413],
1303
+ [-0.01182, 0.04294, 0.968881],
1304
+ ],
1305
+ };
1306
+ /** How far toward full red- or green-blindness the check looks. */
1307
+ const CVD_SEVERITY = 0.6;
1308
+ /** Two fills closer than this (OKLab distance) read as one color. */
1309
+ const CVD_ALIKE = 0.06;
1310
+ /**
1311
+ * The pairs of status fills that someone red-weak or green-weak may not
1312
+ * tell apart: their colors as that vision sees them sit closer than
1313
+ * CVD_ALIKE. `fills` holds opaque hex by role.
1314
+ * @param {Record<string, string>} fills
1315
+ * @returns {{a: string; b: string; vision: 'red-weak' | 'green-weak'}[]}
1316
+ */
1317
+ export function cvdClashes(fills) {
1318
+ /**
1319
+ * @type {{a: string; b: string; vision: 'red-weak' | 'green-weak'}[]}
1320
+ */
1321
+ const out = [];
1322
+ const roles = Object.keys(fills).filter(r =>
1323
+ /^#[0-9a-f]{6}$/i.test(fills[r]),
1324
+ );
1325
+ for (const [
1326
+ vision,
1327
+ M,
1328
+ ] of /** @type {[ 'red-weak' | 'green-weak', ReadonlyArray<ReadonlyArray<number>>, ][]} */ (
1329
+ Object.entries(CVD)
1330
+ )) {
1331
+ // Weak, not absent: partway from normal vision to the full matrix.
1332
+ const W = M.map((row, r) =>
1333
+ row.map(
1334
+ (v, c) => (r === c ? 1 : 0) + CVD_SEVERITY * (v - (r === c ? 1 : 0)),
1335
+ ),
1336
+ );
1337
+ const seen =
1338
+ /**
1339
+ * @param {string} hex
1340
+ */
1341
+ hex => {
1342
+ const lin = hexToLinearRgb(hex);
1343
+ const [r, g, b] = W.map(row =>
1344
+ Math.min(
1345
+ 1,
1346
+ Math.max(0, row[0] * lin[0] + row[1] * lin[1] + row[2] * lin[2]),
1347
+ ),
1348
+ );
1349
+ return linearRgbToOklab(r, g, b);
1350
+ };
1351
+ for (let i = 0; i < roles.length; i++) {
1352
+ for (let j = i + 1; j < roles.length; j++) {
1353
+ const x = seen(fills[roles[i]]);
1354
+ const y = seen(fills[roles[j]]);
1355
+ if (Math.hypot(x[0] - y[0], x[1] - y[1], x[2] - y[2]) < CVD_ALIKE) {
1356
+ out.push({a: roles[i], b: roles[j], vision});
1357
+ }
1358
+ }
1359
+ }
1360
+ }
1361
+ return out;
1362
+ }