basalt-ui 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/AGENTS.md +2 -2
  2. package/CHANGELOG.md +30 -0
  3. package/README.md +47 -5
  4. package/agent/rules/basalt-app.md +126 -0
  5. package/agent/rules/basalt-content.md +3 -1
  6. package/agent/skills/basalt-app/SKILL.md +28 -7
  7. package/dist/agent/part-list.d.ts.map +1 -1
  8. package/dist/agent/part-list.js +17 -7
  9. package/dist/agent/stick-to-bottom.js +2 -2
  10. package/dist/agent-chat/thread-detail-panel.d.ts.map +1 -1
  11. package/dist/agent-chat/thread-detail-panel.js +37 -33
  12. package/dist/agent-chat/thread-message.d.ts.map +1 -1
  13. package/dist/agent-chat/thread-message.js +8 -5
  14. package/dist/agent-chat/thread-outcome-card.js +3 -3
  15. package/dist/agent-chat/thread-workspace.js +1 -1
  16. package/dist/charts/primitives/ChartLegend.js +2 -0
  17. package/dist/charts/primitives/ChartTooltip.js +2 -1
  18. package/dist/cli/index.d.ts +15 -0
  19. package/dist/cli/index.d.ts.map +1 -1
  20. package/dist/cli/index.js +29 -7
  21. package/dist/connectivity/connectivity-indicator.d.ts.map +1 -1
  22. package/dist/connectivity/connectivity-indicator.js +3 -3
  23. package/dist/content/article-card.module.css +7 -5
  24. package/dist/content/article-layout.module.css +16 -12
  25. package/dist/content/callout.module.css +4 -4
  26. package/dist/content/code-block.module.css +8 -7
  27. package/dist/content/guide.module.css +3 -3
  28. package/dist/content/mermaid.module.css +2 -2
  29. package/dist/content/prose.module.css +59 -29
  30. package/dist/content/reading-progress.module.css +2 -0
  31. package/dist/content/toc.module.css +6 -3
  32. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  33. package/dist/dashboard/delta-badge.js +10 -2
  34. package/dist/dashboard/empty-state.d.ts.map +1 -1
  35. package/dist/dashboard/empty-state.js +3 -16
  36. package/dist/dashboard/settings-section.js +2 -2
  37. package/dist/dashboard/settings-section.module.css +2 -2
  38. package/dist/dashboard/stat-card.js +2 -1
  39. package/dist/data/virtual-list.d.ts.map +1 -1
  40. package/dist/data/virtual-list.js +12 -2
  41. package/dist/forms/FormErrorSummary.js +2 -2
  42. package/dist/guard/index.d.ts +20 -4
  43. package/dist/guard/index.d.ts.map +1 -1
  44. package/dist/guard/index.js +250 -37
  45. package/dist/guard/types.d.ts +24 -3
  46. package/dist/guard/types.d.ts.map +1 -1
  47. package/dist/index.d.ts +1 -0
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/provider/index.d.ts +30 -1
  50. package/dist/provider/index.d.ts.map +1 -1
  51. package/dist/provider/index.js +32 -7
  52. package/dist/provider/lab-theme.d.ts +39 -0
  53. package/dist/provider/lab-theme.d.ts.map +1 -0
  54. package/dist/provider/lab-theme.js +52 -0
  55. package/dist/shell/app-header.module.css +2 -2
  56. package/dist/shell/app-mobile-nav.module.css +1 -1
  57. package/dist/shell/app-sidebar-account.d.ts.map +1 -1
  58. package/dist/shell/app-sidebar-account.js +4 -2
  59. package/dist/shell/app-sidebar.d.ts.map +1 -1
  60. package/dist/shell/app-sidebar.js +4 -2
  61. package/dist/shell/app-sidebar.module.css +36 -26
  62. package/dist/shell/index.d.ts.map +1 -1
  63. package/dist/shell/index.js +11 -4
  64. package/dist/shell/sidebar-search.module.css +16 -4
  65. package/dist/styles.css +1 -1
  66. package/dist/surfaces.d.ts +15 -11
  67. package/dist/surfaces.d.ts.map +1 -1
  68. package/dist/surfaces.js +4 -3
  69. package/dist/theme/controls.module.css +1 -1
  70. package/dist/theme/floating.module.css +1 -1
  71. package/dist/theme/index.d.ts +116 -11
  72. package/dist/theme/index.d.ts.map +1 -1
  73. package/dist/theme/index.js +703 -566
  74. package/dist/theme/nav-link.module.css +1 -1
  75. package/dist/theme/use-basalt-spacing.d.ts +3 -0
  76. package/dist/theme/use-basalt-spacing.d.ts.map +1 -0
  77. package/dist/theme/use-basalt-spacing.js +8 -0
  78. package/dist/theme-lab/derive-controls.d.ts +13 -0
  79. package/dist/theme-lab/derive-controls.d.ts.map +1 -0
  80. package/dist/theme-lab/derive-controls.js +137 -0
  81. package/dist/theme-lab/derive-state.d.ts +55 -0
  82. package/dist/theme-lab/derive-state.d.ts.map +1 -0
  83. package/dist/theme-lab/derive-state.js +81 -0
  84. package/dist/theme-lab/index.d.ts +12 -2
  85. package/dist/theme-lab/index.d.ts.map +1 -1
  86. package/dist/theme-lab/index.js +13 -62
  87. package/dist/theme-toggle/index.d.ts.map +1 -1
  88. package/dist/theme-toggle/index.js +3 -2
  89. package/dist/tokens/derive.d.ts +89 -0
  90. package/dist/tokens/derive.d.ts.map +1 -0
  91. package/dist/tokens/derive.js +250 -0
  92. package/dist/tokens/hct.d.ts +34 -0
  93. package/dist/tokens/hct.d.ts.map +1 -0
  94. package/dist/tokens/hct.js +159 -0
  95. package/dist/tokens/index.d.ts +71 -7
  96. package/dist/tokens/index.d.ts.map +1 -1
  97. package/dist/tokens/index.js +284 -46
  98. package/dist/tokens/palette.d.ts +984 -167
  99. package/dist/tokens/palette.d.ts.map +1 -1
  100. package/dist/tokens/palette.js +617 -119
  101. package/dist/vite-color-mix.d.ts +30 -0
  102. package/dist/vite-color-mix.d.ts.map +1 -0
  103. package/dist/vite-color-mix.js +80 -0
  104. package/dist/vite.d.ts +82 -1
  105. package/dist/vite.d.ts.map +1 -1
  106. package/dist/vite.js +312 -0
  107. package/llms.txt +5 -4
  108. package/package.json +24 -9
  109. package/src/agent/part-list.tsx +13 -7
  110. package/src/agent/stick-to-bottom.tsx +2 -2
  111. package/src/agent-chat/thread-detail-panel.tsx +13 -10
  112. package/src/agent-chat/thread-message.tsx +8 -5
  113. package/src/agent-chat/thread-outcome-card.tsx +2 -2
  114. package/src/agent-chat/thread-workspace.tsx +2 -2
  115. package/src/charts/primitives/ChartLegend.tsx +2 -2
  116. package/src/charts/primitives/ChartTooltip.tsx +2 -2
  117. package/src/cli/check-theme.test.ts +18 -0
  118. package/src/cli/index.ts +55 -5
  119. package/src/connectivity/connectivity-indicator.tsx +8 -2
  120. package/src/content/article-card.module.css +7 -5
  121. package/src/content/article-layout.module.css +16 -12
  122. package/src/content/callout.module.css +4 -4
  123. package/src/content/code-block.module.css +8 -7
  124. package/src/content/guide.module.css +3 -3
  125. package/src/content/mermaid.module.css +2 -2
  126. package/src/content/prose.module.css +59 -29
  127. package/src/content/prose.module.css.test.ts +55 -0
  128. package/src/content/reading-progress.module.css +2 -0
  129. package/src/content/toc.module.css +6 -3
  130. package/src/dashboard/delta-badge.tsx +10 -4
  131. package/src/dashboard/empty-state.tsx +3 -16
  132. package/src/dashboard/settings-section.module.css +2 -2
  133. package/src/dashboard/settings-section.tsx +2 -2
  134. package/src/dashboard/stat-card.tsx +2 -2
  135. package/src/data/virtual-list.tsx +12 -2
  136. package/src/forms/FormErrorSummary.tsx +1 -1
  137. package/src/guard/check-source.test.ts +416 -5
  138. package/src/guard/index.ts +367 -45
  139. package/src/guard/types.ts +24 -2
  140. package/src/index.ts +1 -0
  141. package/src/provider/index.test.tsx +100 -0
  142. package/src/provider/index.tsx +79 -7
  143. package/src/provider/lab-theme.test.ts +147 -0
  144. package/src/provider/lab-theme.ts +166 -0
  145. package/src/shell/app-header.module.css +2 -2
  146. package/src/shell/app-mobile-nav.module.css +1 -1
  147. package/src/shell/app-sidebar-account.tsx +9 -2
  148. package/src/shell/app-sidebar.module.css +36 -26
  149. package/src/shell/app-sidebar.tsx +12 -2
  150. package/src/shell/index.tsx +14 -3
  151. package/src/shell/sidebar-search.module.css +16 -4
  152. package/src/styles.css +1 -1
  153. package/src/surfaces.ts +17 -10
  154. package/src/theme/contrast.test.ts +19 -3
  155. package/src/theme/controls.module.css +1 -1
  156. package/src/theme/density-option.test.ts +96 -0
  157. package/src/theme/density-relations.test.ts +428 -0
  158. package/src/theme/derive-theme.test.ts +101 -0
  159. package/src/theme/floating.module.css +1 -1
  160. package/src/theme/fonts-option.test.ts +69 -0
  161. package/src/theme/index.ts +930 -563
  162. package/src/theme/nav-link.module.css +1 -1
  163. package/src/theme/radius-option.test.ts +86 -0
  164. package/src/theme/radius.test.ts +132 -0
  165. package/src/theme/spacing.test.ts +432 -0
  166. package/src/theme/use-basalt-spacing.test.tsx +51 -0
  167. package/src/theme/use-basalt-spacing.ts +38 -0
  168. package/src/theme-lab/derive-controls.test.ts +94 -0
  169. package/src/theme-lab/derive-controls.tsx +227 -0
  170. package/src/theme-lab/derive-state.test.ts +71 -0
  171. package/src/theme-lab/derive-state.ts +163 -0
  172. package/src/theme-lab/index.tsx +29 -68
  173. package/src/theme-lab/theme-lab.test.ts +4 -18
  174. package/src/theme-toggle/index.tsx +9 -4
  175. package/src/tokens/build-fonts-css.test.ts +51 -0
  176. package/src/tokens/build-radius-css.test.ts +25 -0
  177. package/src/tokens/density.test.ts +274 -0
  178. package/src/tokens/derive-radius.test.ts +62 -0
  179. package/src/tokens/derive.test.ts +30 -0
  180. package/src/tokens/derive.ts +481 -0
  181. package/src/tokens/hct.ts +231 -0
  182. package/src/tokens/index.ts +395 -45
  183. package/src/tokens/palette.test.ts +114 -0
  184. package/src/tokens/palette.ts +1118 -180
  185. package/src/vite-color-mix.test.ts +46 -0
  186. package/src/vite-color-mix.ts +137 -0
  187. package/src/vite.test.ts +192 -0
  188. package/src/vite.ts +502 -1
@@ -0,0 +1,30 @@
1
+ /**
2
+ * `derive.ts`'s fill-page contrast clamp — the law that an accent fill shifted off the band centre
3
+ * by `accentBrightness` must still clear `FILL_PAGE_CONTRAST_MIN` (3.0:1) against BOTH derived page
4
+ * backgrounds, never just the label-vs-fill floor. See the law comment above `FILL_LUMINANCE` in
5
+ * `derive.ts`.
6
+ */
7
+ import { describe, expect, test } from 'bun:test'
8
+ import { contrastRatio } from './hct'
9
+ import { DEFAULT_DERIVE_CONFIG, deriveTokens } from './derive'
10
+
11
+ const FILL_PAGE_CONTRAST_MIN = 3.0
12
+
13
+ describe('the fill-page contrast clamp', () => {
14
+ test('the default config (accentBrightness 0) is a no-op — accentFill hex is unchanged', () => {
15
+ expect(deriveTokens(DEFAULT_DERIVE_CONFIG).accentFill.light).toBe('#4374a6')
16
+ })
17
+
18
+ for (const accentBrightness of [5, -5]) {
19
+ test(`accentBrightness ${accentBrightness} at the default seed clears 3.0:1 vs both derived page backgrounds`, () => {
20
+ const data = deriveTokens({ ...DEFAULT_DERIVE_CONFIG, accentBrightness })
21
+ const fill = data.accentFill.light
22
+ expect(contrastRatio(fill, data.surface.bg.light)).toBeGreaterThanOrEqual(
23
+ FILL_PAGE_CONTRAST_MIN,
24
+ )
25
+ expect(contrastRatio(fill, data.surface.bg.dark)).toBeGreaterThanOrEqual(
26
+ FILL_PAGE_CONTRAST_MIN,
27
+ )
28
+ })
29
+ }
30
+ })
@@ -0,0 +1,481 @@
1
+ /**
2
+ * "One accent color in -> calculated palette out" derivation engine — ported from the
3
+ * `apps/playground/src/poc/derive.ts` proof of concept (browser-validated and calibration-checked
4
+ * there; see that file's history for the reproduction table this derivation was scored against).
5
+ * Every constant below (tones, chromas, hue offsets, hover deltas) is a calibrated law, not a taste
6
+ * call — do not re-tune without re-running that calibration.
7
+ *
8
+ * `palette.ts` derives the shipped default palette from `deriveTokens(DEFAULT_DERIVE_CONFIG)` at
9
+ * module load. Retuning the shipped identity means editing `DEFAULT_DERIVE_CONFIG` or the
10
+ * constants below, never hand-editing a hex in `palette.ts`.
11
+ */
12
+ import { contrastRatio, hctToHex, hexToHct, yToTone } from './hct'
13
+ import type { ColorPair } from './index'
14
+
15
+ export type DeriveConfig = {
16
+ /**
17
+ * Seed hex — the one color a consumer picks. Drives the entire accent family + `onAccent`.
18
+ *
19
+ * The seed contributes its HUE and nothing else: the derivation laws normalize its chroma
20
+ * (`max(seed, 40)`) and pin its tone to the Y=0.165 fill band, so a lighter/darker or more/less
21
+ * saturated seed produces the SAME palette as long as the hue matches. That is deliberate —
22
+ * contrast stays derivation-guaranteed instead of seed-dependent. `vibrancy` and
23
+ * `accentBrightness` below are the ONLY expressive controls layered on top of those laws.
24
+ */
25
+ accent: string
26
+ /** Which low-chroma neutral family backs every surface + ink stop. */
27
+ neutral: 'zinc' | 'neutral' | 'stone' | 'slate'
28
+ /** -5..5 — shifts the LIGHT scheme's surface tone stops by `2 x level + 2`. Positive =
29
+ * lighter. The `+2` baseline bakes the preferred "one level lighter" default into level 0. */
30
+ lightLevel: number
31
+ /** -5..5 — shifts the DARK scheme's surface tone stops by `2 x level + 2`. Positive = lighter.
32
+ * Same baked `+2` baseline as `lightLevel`. */
33
+ darkLevel: number
34
+ /**
35
+ * -5..5 (default 0) — scales the derived chroma of the accent family AND the 12 categorical
36
+ * fills via `0.72 x 1.2^level` (level 0 = the preferred x0.72 baseline, one vibrancy step above
37
+ * the original x0.6 muted center; level -5 ~= x0.29, the calibrated x1.0 spec saturation is
38
+ * crossed at ~+2 (0.72 x 1.2^2 ~= 1.037), +5 ~= x1.79). Gamut mapping in `hctToHex` absorbs any
39
+ * overshoot. Neutral surfaces/ink (and the status hues) are NOT affected.
40
+ */
41
+ vibrancy: number
42
+ /**
43
+ * -5..5 (default 0) — shifts the accent-family tones by `+5 x level` (fill, fill hover, dark
44
+ * ink accent, dark accent hover; hover deltas stay relative). The 12 categorical fills stay
45
+ * pinned on the fill band — chart colors must remain uniform. `onAccent` is still decided by
46
+ * the contrast law (3.0:1 white floor): white survives up to about +2 for typical seeds; from
47
+ * ~+3 the fill is light enough that the safety flip to dark ink engages.
48
+ */
49
+ accentBrightness: number
50
+ }
51
+
52
+ /** The default derive config — the shipped palette's baseline (all four knobs at level 0). */
53
+ export const DEFAULT_DERIVE_CONFIG: DeriveConfig = {
54
+ accent: '#0077bd',
55
+ neutral: 'zinc',
56
+ lightLevel: 0,
57
+ darkLevel: 0,
58
+ vibrancy: 0,
59
+ accentBrightness: 0,
60
+ }
61
+
62
+ const DERIVE_CONFIG_KEYS = Object.keys(DEFAULT_DERIVE_CONFIG) as (keyof DeriveConfig)[]
63
+
64
+ /**
65
+ * Resolve a partial derive config against {@link DEFAULT_DERIVE_CONFIG} — the single place a
66
+ * consumer-supplied `{ derive }` option (`createBasaltTheme`'s second argument) becomes a full,
67
+ * six-knob `DeriveConfig`. Omitted knobs fall back to the shipped default for that knob.
68
+ */
69
+ export function resolveDeriveConfig(partial?: Partial<DeriveConfig>): DeriveConfig {
70
+ return { ...DEFAULT_DERIVE_CONFIG, ...partial }
71
+ }
72
+
73
+ /**
74
+ * Whether a resolved config is identical to {@link DEFAULT_DERIVE_CONFIG} — the gate
75
+ * `createBasaltTheme` and `BasaltProvider` use to stay on the pre-baked static path (zero extra
76
+ * derivation work) instead of re-deriving the palette.
77
+ */
78
+ export function isDefaultDeriveConfig(config: DeriveConfig): boolean {
79
+ return DERIVE_CONFIG_KEYS.every((key) => config[key] === DEFAULT_DERIVE_CONFIG[key])
80
+ }
81
+
82
+ type Side = 'light' | 'dark'
83
+
84
+ const clamp = (v: number, lo: number, hi: number): number => Math.max(lo, Math.min(hi, v))
85
+
86
+ const ACCENT_HEX_RE = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i
87
+
88
+ /** Validate a `DeriveConfig.accent` seed hex (`#rgb`/`#rrggbb`, case-insensitive) — the public-API
89
+ * boundary every consumer-facing config eventually passes through `deriveTokens` below. Malformed
90
+ * input throws rather than silently producing NaN math further down the derivation pipeline;
91
+ * `hct.ts`'s hot conversion paths stay unvalidated by design. */
92
+ function assertValidAccentHex(accent: string): void {
93
+ if (!ACCENT_HEX_RE.test(accent)) {
94
+ throw new Error(
95
+ `Invalid DeriveConfig.accent: expected a #rgb or #rrggbb hex color, got "${accent}"`,
96
+ )
97
+ }
98
+ }
99
+
100
+ // ── Fill band (shared by the accent fill AND the 12 categorical fills) ─────────────────────────
101
+ // Law: every filled surface sits at relative luminance ~0.165 so a white label clears 4.5:1 on it
102
+ // AND the fill clears 3:1 against the page — regardless of hue. `toneToY`/`yToTone` in hct.ts make
103
+ // the WCAG relative-luminance axis and the HCT tone axis (L*) the SAME axis (both are the
104
+ // linear-light Y from the sRGB->XYZ step), so this is a formula, not a calibrated constant.
105
+ //
106
+ // `accentBrightness` shifts the ACCENT fill's tone off that band centre (the 12 categorical fills
107
+ // never take the shift — they stay pinned). An unclamped shift breaks the "clears 3:1 against the
108
+ // page" half of the law at the extremes (e.g. +5 sits at ~1.9:1 against the light page) — see
109
+ // `clampFillTone` below, which the accent fill (and its hover, derived from the clamped tone) runs
110
+ // through before `deriveTokensRaw` returns. The 12 categorical fills take no shift, so they never
111
+ // need the clamp.
112
+ const FILL_LUMINANCE = 0.165
113
+
114
+ // ── Accent family ────────────────────────────────────────────────────────────────────────────
115
+ // Fill hover: fillTone - FILL_HOVER_TONE_DELTA, fillChroma + FILL_HOVER_CHROMA_DELTA.
116
+ const FILL_HOVER_TONE_DELTA = 4.8
117
+ const FILL_HOVER_CHROMA_DELTA = -3.6
118
+ // Dark-mode accent-as-INK: lighter tone, chroma pulled back off the fill's saturation so it reads
119
+ // as an ink color rather than a second fill.
120
+ const ACCENT_INK_DARK_TONE = 78
121
+ const ACCENT_INK_DARK_CHROMA = 36
122
+ // Dark-mode accent hover: darkens + boosts chroma off the dark ink baseline.
123
+ const ACCENT_HOVER_DARK_TONE_DELTA = 12.8
124
+ const ACCENT_HOVER_DARK_CHROMA_DELTA = 18
125
+ // `onAccent`: a near-black ink at the seed's hue, used only when white fails the floor below.
126
+ const ON_ACCENT_DARK_TONE = 10
127
+ const ON_ACCENT_DARK_CHROMA = 8
128
+ // White floor is 3.0:1 — the WCAG 1.4.11 non-text/UI-component (and large-text) level, which is
129
+ // what button labels on filled controls target (Mantine's autoContrast aims at effectively the
130
+ // same bar). 4.5 was too strict: at brightness +1/+2 (fill tone ~52.6/57.6) white sits at
131
+ // ~3.4-4.4:1 and would flip dark even though white labels still read fine there. The dark-ink
132
+ // flip remains only as a safety for genuinely light fills (e.g. a yellow-ish seed at high
133
+ // vibrancy).
134
+ const ON_ACCENT_WHITE_CONTRAST_MIN = 3.0
135
+
136
+ // Fill-vs-page floor is also 3.0:1 (WCAG 1.4.11 non-text/UI-component) — a DIFFERENT pairing than
137
+ // `ON_ACCENT_WHITE_CONTRAST_MIN` above (fill vs. its own label, not fill vs. the page it sits on).
138
+ // `clampFillTone` below enforces it against BOTH schemes' derived page background.
139
+ const FILL_PAGE_CONTRAST_MIN = 3.0
140
+ // Step size (in tone/L*) the clamp backs off by per iteration — fine enough that the clamped fill
141
+ // never overshoots past clearing both floors by more than a fraction of a tone.
142
+ const FILL_CLAMP_STEP = 0.5
143
+
144
+ /**
145
+ * Step `rawTone` back toward `bandTone` (the fill band's centre — the law's fixed point, never
146
+ * stepped past) in {@link FILL_CLAMP_STEP} increments until the resulting fill hex clears
147
+ * {@link FILL_PAGE_CONTRAST_MIN} against BOTH `pageBg.light` and `pageBg.dark` — or until it
148
+ * reaches `bandTone` itself, in which case it stops there even if the floor still isn't cleared
149
+ * (an extreme surface config can theoretically make the band centre itself fail one page; there is
150
+ * no further room to give). A no-op when `rawTone === bandTone` (the default-config path), so the
151
+ * shipped identity never runs the search.
152
+ */
153
+ function clampFillTone(
154
+ rawTone: number,
155
+ bandTone: number,
156
+ hue: number,
157
+ chroma: number,
158
+ pageBg: Record<Side, string>,
159
+ ): number {
160
+ if (rawTone === bandTone) return rawTone
161
+ const towardCenter = rawTone > bandTone ? -FILL_CLAMP_STEP : FILL_CLAMP_STEP
162
+ const clearsBothPages = (tone: number): boolean => {
163
+ const hex = hctToHex({ hue, chroma, tone })
164
+ return (
165
+ contrastRatio(hex, pageBg.light) >= FILL_PAGE_CONTRAST_MIN &&
166
+ contrastRatio(hex, pageBg.dark) >= FILL_PAGE_CONTRAST_MIN
167
+ )
168
+ }
169
+ let tone = rawTone
170
+ while (!clearsBothPages(tone) && tone !== bandTone) {
171
+ tone =
172
+ towardCenter < 0
173
+ ? Math.max(bandTone, tone + towardCenter)
174
+ : Math.min(bandTone, tone + towardCenter)
175
+ }
176
+ return tone
177
+ }
178
+
179
+ // ── Expressive knobs (the ONLY seed-independent saturation/brightness controls) ───────────────
180
+ // `vibrancy` level -> chroma multiplier: 0.72 x 1.2^level (level -5 ~= x0.29, 0 = x0.72,
181
+ // +5 ~= x1.79; the calibrated x1.0 spec saturation is crossed at ~+2). Level 0 is the shipped
182
+ // default — one vibrancy step above the original x0.6 muted center.
183
+ const VIBRANCY_CENTER_CHROMA_MULT = 0.72
184
+ const VIBRANCY_STEP_MULT = 1.2
185
+ const vibrancyChromaMult = (level: number): number =>
186
+ VIBRANCY_CENTER_CHROMA_MULT * VIBRANCY_STEP_MULT ** level
187
+ // `accentBrightness` level -> tone shift per unit, applied to the accent family only (never the
188
+ // 12 categorical fills). At the default fill band (~47.6) the -5..5 range spans ~22.6..72.6.
189
+ const ACCENT_BRIGHTNESS_TONE_STEP = 5
190
+ // Surface levels: tone shift per scheme = 2 x level + 2. The +2 baseline bakes the preferred
191
+ // "one level lighter than the original hand-tuned surfaces" default into level 0; the slider
192
+ // range is -5..5 around that center (extremes clamp at pure white/black via `hctToHex`).
193
+ const SURFACE_LEVEL_TONE_STEP = 2
194
+ const SURFACE_BASELINE_TONE_SHIFT = 2
195
+
196
+ // ── The 12 categorical fills — baked hue + chroma, a separate hue-fixed family. Retuning the
197
+ // accent seed does NOT move these — they are only re-lit onto the same fill-luminance band every
198
+ // scheme.
199
+ export type FillFamily =
200
+ | 'gray'
201
+ | 'red'
202
+ | 'pink'
203
+ | 'grape'
204
+ | 'violet'
205
+ | 'indigo'
206
+ | 'cyan'
207
+ | 'teal'
208
+ | 'green'
209
+ | 'lime'
210
+ | 'yellow'
211
+ | 'orange'
212
+
213
+ const FILL_HUE: Record<FillFamily, number> = {
214
+ gray: 290.81,
215
+ red: 28.33,
216
+ pink: 5.0,
217
+ grape: 327.38,
218
+ violet: 327.38,
219
+ indigo: 303.5,
220
+ cyan: 255.96,
221
+ teal: 185.37,
222
+ green: 139.74,
223
+ lime: 116.65,
224
+ yellow: 80.94,
225
+ orange: 66.54,
226
+ }
227
+ const FILL_CHROMA: Record<FillFamily, number> = {
228
+ gray: 4.72,
229
+ red: 64.31,
230
+ pink: 66.76,
231
+ grape: 65.38,
232
+ violet: 65.38,
233
+ indigo: 73.89,
234
+ cyan: 35.19,
235
+ teal: 32.0,
236
+ green: 60.84,
237
+ lime: 51.45,
238
+ yellow: 54.14,
239
+ orange: 55.69,
240
+ }
241
+ // The "clamp into a band so all 12 feel even" law — a no-op against the current data (already
242
+ // 4.7-73.9), kept as a real clamp so a wildly saturated/desaturated future family can't blow the
243
+ // band out.
244
+ const FILL_CHROMA_MIN = 4
245
+ const FILL_CHROMA_MAX = 90
246
+ const FILL_ORDER: readonly FillFamily[] = [
247
+ 'gray',
248
+ 'red',
249
+ 'pink',
250
+ 'grape',
251
+ 'violet',
252
+ 'indigo',
253
+ 'cyan',
254
+ 'teal',
255
+ 'green',
256
+ 'lime',
257
+ 'yellow',
258
+ 'orange',
259
+ ]
260
+
261
+ // ── Neutral system — one (hue, chroma) per preset. 'zinc' is calibrated against the framework's
262
+ // original hand-tuned surface + ink hexes; 'neutral'/'stone'/'slate' are achromatic/warm/cool
263
+ // alternates with no current data to calibrate against.
264
+ const NEUTRAL_PRESETS: Record<DeriveConfig['neutral'], { hue: number; chroma: number }> = {
265
+ zinc: { hue: 290.7, chroma: 2 },
266
+ neutral: { hue: 0, chroma: 0 },
267
+ stone: { hue: 70, chroma: 4 },
268
+ slate: { hue: 260, chroma: 5 },
269
+ }
270
+
271
+ // Surface tone stops. `lightLevel`/`darkLevel` shift these +-2 tones/unit.
272
+ export type SurfaceKey =
273
+ | 'bg'
274
+ | 'panel'
275
+ | 'panelHover'
276
+ | 'elevated'
277
+ | 'subtle'
278
+ | 'field'
279
+ | 'border'
280
+ | 'hairline'
281
+
282
+ const SURFACE_TONE: Record<Side, Record<SurfaceKey, number>> = {
283
+ light: {
284
+ bg: 93.45,
285
+ panel: 96.21,
286
+ panelHover: 98.27,
287
+ elevated: 98.27,
288
+ subtle: 94.15,
289
+ field: 100,
290
+ border: 87.86,
291
+ hairline: 90.94,
292
+ },
293
+ dark: {
294
+ bg: 13.82,
295
+ panel: 15.75,
296
+ panelHover: 21.42,
297
+ elevated: 21.42,
298
+ subtle: 18.69,
299
+ field: 13.82,
300
+ border: 26.89,
301
+ hairline: 31.29,
302
+ },
303
+ }
304
+
305
+ // Ink ramp tone stops (achromatic). NOT shifted by lightLevel/darkLevel — text tone stays stable
306
+ // while surfaces move around it.
307
+ export type InkKey = 'ink' | 'ink2' | 'muted' | 'faint'
308
+
309
+ const INK_TONE: Record<Side, Record<InkKey, number>> = {
310
+ light: { ink: 15.16, ink2: 27.09, muted: 34.88, faint: 48.44 },
311
+ dark: { ink: 90.94, ink2: 88.12, muted: 84.91, faint: 66.24 },
312
+ }
313
+
314
+ // ── Status/semantic — three baked hues, each with its own calibrated tone+chroma stop per scheme.
315
+ export type StatusKey = 'good' | 'warn' | 'bad'
316
+
317
+ const STATUS_HUE: Record<StatusKey, number> = { good: 151.975, warn: 76.64, bad: 29.905 }
318
+ const STATUS_STOP: Record<StatusKey, Record<Side, { tone: number; chroma: number }>> = {
319
+ good: { light: { tone: 45.75, chroma: 37.75 }, dark: { tone: 70.23, chroma: 53.23 } },
320
+ warn: { light: { tone: 54.64, chroma: 61.12 }, dark: { tone: 72.34, chroma: 62.55 } },
321
+ bad: { light: { tone: 43.86, chroma: 54.46 }, dark: { tone: 58.58, chroma: 53.79 } },
322
+ }
323
+
324
+ /**
325
+ * Continuous derivation parameters — the derivation CORE's inputs, decoupled from the -5..5
326
+ * slider levels. `deriveTokens` maps levels onto these (with the re-centered baselines baked in).
327
+ */
328
+ export type DeriveParams = {
329
+ /** Chroma multiplier for the accent family + 12 categorical fills. 1 = calibrated chroma. */
330
+ chromaMult: number
331
+ /** Tone shift applied to the accent family only. 0 = calibrated tones. */
332
+ accentToneShift: number
333
+ /** Tone shift for the LIGHT scheme's surface stops. 0 = calibrated stops. */
334
+ lightSurfaceShift: number
335
+ /** Tone shift for the DARK scheme's surface stops. 0 = calibrated stops. */
336
+ darkSurfaceShift: number
337
+ }
338
+
339
+ /** The full derived token set, grouped by family — the shape `palette.ts` builds ACCENT/FILL/
340
+ * SURFACE/INK/STATUS from. */
341
+ export type DerivedPalette = {
342
+ accent: ColorPair
343
+ accentHover: ColorPair
344
+ accentFill: ColorPair
345
+ accentFillHover: ColorPair
346
+ onAccent: ColorPair
347
+ fill: Record<FillFamily, ColorPair>
348
+ surface: Record<SurfaceKey, ColorPair>
349
+ ink: Record<InkKey, ColorPair>
350
+ status: Record<StatusKey, ColorPair>
351
+ }
352
+
353
+ /** Derive the full palette from a seed accent + neutral family + surface levels.
354
+ * Public level-based entry: maps the -5..5 sliders onto `DeriveParams` (vibrancy x0.72 center,
355
+ * surface +2 baseline) and delegates to `deriveTokensRaw`. */
356
+ export function deriveTokens(config: DeriveConfig): DerivedPalette {
357
+ assertValidAccentHex(config.accent)
358
+ return deriveTokensRaw(config, {
359
+ chromaMult: vibrancyChromaMult(config.vibrancy),
360
+ accentToneShift: config.accentBrightness * ACCENT_BRIGHTNESS_TONE_STEP,
361
+ lightSurfaceShift: SURFACE_LEVEL_TONE_STEP * config.lightLevel + SURFACE_BASELINE_TONE_SHIFT,
362
+ darkSurfaceShift: SURFACE_LEVEL_TONE_STEP * config.darkLevel + SURFACE_BASELINE_TONE_SHIFT,
363
+ })
364
+ }
365
+
366
+ /** Derivation core over continuous params — see `DeriveParams` for the calibration contract. */
367
+ export function deriveTokensRaw(
368
+ config: Pick<DeriveConfig, 'accent' | 'neutral'>,
369
+ params: DeriveParams,
370
+ ): DerivedPalette {
371
+ const seed = hexToHct(config.accent)
372
+ const { chromaMult, accentToneShift } = params
373
+ // The band tone stays the categorical fills' home; only the accent family takes the shift.
374
+ const bandTone = yToTone(FILL_LUMINANCE)
375
+ const fillChroma = Math.max(seed.chroma, 40) * chromaMult
376
+
377
+ const neutral = NEUTRAL_PRESETS[config.neutral]
378
+ const surfaceShift: Record<Side, number> = {
379
+ light: params.lightSurfaceShift,
380
+ dark: params.darkSurfaceShift,
381
+ }
382
+ // The two derived page backgrounds the fill-contrast clamp checks against — same formula the
383
+ // SURFACE.bg loop below uses, computed up front since the clamp must run before the accent fill.
384
+ const pageBg: Record<Side, string> = {
385
+ light: hctToHex({
386
+ hue: neutral.hue,
387
+ chroma: neutral.chroma,
388
+ tone: clamp(SURFACE_TONE.light.bg + surfaceShift.light, 0, 100),
389
+ }),
390
+ dark: hctToHex({
391
+ hue: neutral.hue,
392
+ chroma: neutral.chroma,
393
+ tone: clamp(SURFACE_TONE.dark.bg + surfaceShift.dark, 0, 100),
394
+ }),
395
+ }
396
+ const fillTone = clampFillTone(bandTone + accentToneShift, bandTone, seed.hue, fillChroma, pageBg)
397
+
398
+ const accentFill = hctToHex({ hue: seed.hue, chroma: fillChroma, tone: fillTone })
399
+ const accentFillHover = hctToHex({
400
+ hue: seed.hue,
401
+ chroma: fillChroma + FILL_HOVER_CHROMA_DELTA * chromaMult,
402
+ tone: fillTone - FILL_HOVER_TONE_DELTA,
403
+ })
404
+ const accentInkDark = hctToHex({
405
+ hue: seed.hue,
406
+ chroma: ACCENT_INK_DARK_CHROMA * chromaMult,
407
+ tone: ACCENT_INK_DARK_TONE + accentToneShift,
408
+ })
409
+ const accentHoverDark = hctToHex({
410
+ hue: seed.hue,
411
+ chroma: (ACCENT_INK_DARK_CHROMA + ACCENT_HOVER_DARK_CHROMA_DELTA) * chromaMult,
412
+ tone: ACCENT_INK_DARK_TONE + accentToneShift - ACCENT_HOVER_DARK_TONE_DELTA,
413
+ })
414
+ // Contrast law: white wins whenever it clears the 3.0:1 UI-component floor on the (possibly
415
+ // brightness-shifted) fill — see ON_ACCENT_WHITE_CONTRAST_MIN — otherwise a near-black ink at
416
+ // the seed hue.
417
+ const onAccent =
418
+ contrastRatio(accentFill, '#ffffff') >= ON_ACCENT_WHITE_CONTRAST_MIN
419
+ ? '#ffffff'
420
+ : hctToHex({ hue: seed.hue, chroma: ON_ACCENT_DARK_CHROMA, tone: ON_ACCENT_DARK_TONE })
421
+
422
+ const fill = {} as Record<FillFamily, ColorPair>
423
+ for (const family of FILL_ORDER) {
424
+ const chroma = clamp(FILL_CHROMA[family], FILL_CHROMA_MIN, FILL_CHROMA_MAX) * chromaMult
425
+ const hex = hctToHex({ hue: FILL_HUE[family], chroma, tone: bandTone })
426
+ fill[family] = { light: hex, dark: hex }
427
+ }
428
+
429
+ const surface = {} as Record<SurfaceKey, ColorPair>
430
+ for (const key of Object.keys(SURFACE_TONE.light) as SurfaceKey[]) {
431
+ surface[key] = {
432
+ light: hctToHex({
433
+ hue: neutral.hue,
434
+ chroma: neutral.chroma,
435
+ tone: clamp(SURFACE_TONE.light[key] + surfaceShift.light, 0, 100),
436
+ }),
437
+ dark: hctToHex({
438
+ hue: neutral.hue,
439
+ chroma: neutral.chroma,
440
+ tone: clamp(SURFACE_TONE.dark[key] + surfaceShift.dark, 0, 100),
441
+ }),
442
+ }
443
+ }
444
+
445
+ const ink = {} as Record<InkKey, ColorPair>
446
+ for (const key of Object.keys(INK_TONE.light) as InkKey[]) {
447
+ ink[key] = {
448
+ light: hctToHex({ hue: neutral.hue, chroma: neutral.chroma, tone: INK_TONE.light[key] }),
449
+ dark: hctToHex({ hue: neutral.hue, chroma: neutral.chroma, tone: INK_TONE.dark[key] }),
450
+ }
451
+ }
452
+
453
+ const status = {} as Record<StatusKey, ColorPair>
454
+ for (const key of Object.keys(STATUS_HUE) as StatusKey[]) {
455
+ const hue = STATUS_HUE[key]
456
+ status[key] = {
457
+ light: hctToHex({
458
+ hue,
459
+ chroma: STATUS_STOP[key].light.chroma,
460
+ tone: STATUS_STOP[key].light.tone,
461
+ }),
462
+ dark: hctToHex({
463
+ hue,
464
+ chroma: STATUS_STOP[key].dark.chroma,
465
+ tone: STATUS_STOP[key].dark.tone,
466
+ }),
467
+ }
468
+ }
469
+
470
+ return {
471
+ accent: { light: accentFill, dark: accentInkDark },
472
+ accentHover: { light: accentFillHover, dark: accentHoverDark },
473
+ accentFill: { light: accentFill, dark: accentFill },
474
+ accentFillHover: { light: accentFillHover, dark: accentFillHover },
475
+ onAccent: { light: onAccent, dark: onAccent },
476
+ fill,
477
+ surface,
478
+ ink,
479
+ status,
480
+ }
481
+ }