@spunto/design-system 0.66.0 → 0.68.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.
@@ -0,0 +1,188 @@
1
+ // Palettes — 50 field / signal pairs to wear on anything, like a product's pair but without a product.
2
+ //
3
+ // One table (FIELDS) → 50 palettes, each brought into the sRGB gamut, with the tokens a
4
+ // `data-palette` block sets and the paint of its logo. Pure (no DOM): the same module writes
5
+ // `palettes.css` (scripts/generate-palettes.ts), paints static logos (export-logo.ts --palettes),
6
+ // gives the brand kit its hex (a PNG or an MP4 frame can't read a CSS variable) and is tested in
7
+ // palettes.test.ts (gamut, contrast, the CSS being up to date).
8
+ //
9
+ // Same three values as the products in styles.css: field L 0.27 C 0.07, signal L 0.87, tone
10
+ // (the light theme's primary) L 0.49 C 0.16 — so a palette sits in the universe like a product.
11
+ // To change one, edit its line in FIELDS and run `npm run palettes` (the test fails otherwise).
12
+
13
+ export type Oklch = { l: number; c: number; h: number }
14
+
15
+ export interface Palette {
16
+ /** `<field>-<signal>`, the value of `data-palette`. */
17
+ id: string
18
+ /** French display name: « Lagon · ambre ». */
19
+ name: string
20
+ /** The field's hue family (`dusk`, `lagoon`…, or `graphite`). */
21
+ family: string
22
+ field: Oklch
23
+ signal: Oklch
24
+ tone: Oklch
25
+ }
26
+
27
+ // ─── Colour: OKLCH → sRGB, gamut, contrast ─────────────────────────────────
28
+
29
+ function oklchToLinear({ l, c, h }: Oklch): [number, number, number] {
30
+ const a = c * Math.cos((h * Math.PI) / 180)
31
+ const b = c * Math.sin((h * Math.PI) / 180)
32
+ const l_ = (l + 0.3963377774 * a + 0.2158037573 * b) ** 3
33
+ const m_ = (l - 0.1055613458 * a - 0.0638541728 * b) ** 3
34
+ const s_ = (l - 0.0894841775 * a - 1.291485548 * b) ** 3
35
+ return [
36
+ 4.0767416621 * l_ - 3.3077115913 * m_ + 0.2309699292 * s_,
37
+ -1.2684380046 * l_ + 2.6097574011 * m_ - 0.3413193965 * s_,
38
+ -0.0041960863 * l_ - 0.7034186147 * m_ + 1.707614701 * s_,
39
+ ]
40
+ }
41
+
42
+ const inGamut = (c: Oklch) => oklchToLinear(c).every((v) => v >= -1e-4 && v <= 1 + 1e-4)
43
+
44
+ /** Lowers the chroma until the colour fits in sRGB (hue and lightness kept). */
45
+ export function fit(c: Oklch): Oklch {
46
+ let { l, c: ch, h } = c
47
+ while (ch > 0 && !inGamut({ l, c: ch, h })) ch -= 0.002
48
+ return { l, c: Math.max(0, +ch.toFixed(3)), h }
49
+ }
50
+
51
+ export function hex(c: Oklch): string {
52
+ return (
53
+ "#" +
54
+ oklchToLinear(c)
55
+ .map((v) => Math.min(1, Math.max(0, v)))
56
+ .map((v) => (v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055))
57
+ .map((v) => Math.round(v * 255).toString(16).padStart(2, "0"))
58
+ .join("")
59
+ )
60
+ }
61
+
62
+ const luminance = (c: Oklch) => {
63
+ const [r, g, b] = oklchToLinear(c).map((v) => Math.min(1, Math.max(0, v)))
64
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b
65
+ }
66
+ /** WCAG 2 contrast ratio between two colours. */
67
+ export function contrast(a: Oklch, b: Oklch): number {
68
+ const [x, y] = [luminance(a), luminance(b)].sort((p, q) => q - p)
69
+ return (x + 0.05) / (y + 0.05)
70
+ }
71
+
72
+ export const css = ({ l, c, h }: Oklch) => `oklch(${l} ${c} ${h})`
73
+
74
+ // ─── The table ─────────────────────────────────────────────────────────────
75
+
76
+ // 24 field hues (every 15°) + graphite. Each field gets two signals: « ton sur ton » (its own
77
+ // hue, light) and « contraste » (a hue chosen far from it).
78
+ const FIELDS: Array<[id: string, name: string, hue: number, contrastHue: number]> = [
79
+ ["garnet", "Grenat", 15, 86],
80
+ ["brick", "Brique", 30, 200],
81
+ ["rust", "Rouille", 45, 230],
82
+ ["copper", "Cuivre", 60, 180],
83
+ ["tobacco", "Tabac", 75, 350],
84
+ ["bronze", "Bronze", 90, 260],
85
+ ["olive", "Olive", 105, 30],
86
+ ["moss", "Mousse", 120, 55],
87
+ ["fern", "Fougère", 135, 330],
88
+ ["pine", "Sapin", 150, 110],
89
+ ["emerald", "Émeraude", 165, 15],
90
+ ["lagoon", "Lagon", 180, 75],
91
+ ["petrol", "Pétrole", 195, 45],
92
+ ["abyss", "Abysse", 210, 125],
93
+ ["slate", "Ardoise", 225, 60],
94
+ ["navy", "Marine", 240, 86],
95
+ ["ink", "Encre", 255, 30],
96
+ ["indigo", "Indigo", 270, 140],
97
+ ["dusk", "Crépuscule", 285, 86], // = Spunto Cloud
98
+ ["amethyst", "Améthyste", 300, 160],
99
+ ["aubergine", "Aubergine", 315, 100],
100
+ ["plum", "Prune", 330, 190],
101
+ ["mulberry", "Mûre", 345, 120],
102
+ ["wine", "Lie-de-vin", 0, 70],
103
+ ]
104
+
105
+ // A signal's name, by its hue.
106
+ const SIGNAL_NAMES: Array<[upTo: number, id: string, name: string]> = [
107
+ [10, "petal", "pétale"], [25, "coral", "corail"], [45, "peach", "pêche"], [70, "apricot", "abricot"],
108
+ [92, "amber", "ambre"], [105, "honey", "miel"], [118, "lemon", "citron"], [135, "lime", "pousse"],
109
+ [155, "mint", "menthe"], [175, "jade", "jade"], [195, "aqua", "eau"], [215, "glacier", "glacier"],
110
+ [240, "sky", "ciel"], [265, "periwinkle", "pervenche"], [290, "lilac", "lilas"], [320, "orchid", "orchidée"],
111
+ [345, "rose", "rose"], [361, "petal", "pétale"],
112
+ ]
113
+ const signalName = (h: number) => SIGNAL_NAMES.find(([upTo]) => ((h % 360) + 360) % 360 < upTo)!
114
+
115
+ function palette(fieldId: string, fieldName: string, hue: number, sigHue: number, sigC: number): Palette {
116
+ const field = fit({ l: 0.27, c: 0.07, h: hue })
117
+ // Cyans hold a chroma other hues can't: capped, or they shout.
118
+ const h = ((sigHue % 360) + 360) % 360
119
+ const signal = fit({ l: 0.87, c: h >= 165 && h <= 225 ? Math.min(sigC, 0.1) : sigC, h: sigHue })
120
+ const tone = fit({ l: 0.49, c: 0.16, h: hue })
121
+ const [, sid, sname] = signalName(sigHue)
122
+ return { id: `${fieldId}-${sid}`, name: `${fieldName} · ${sname}`, family: fieldId, field, signal, tone }
123
+ }
124
+
125
+ export const PALETTES: Palette[] = [
126
+ ...FIELDS.flatMap(([id, name, hue, contrastHue]) => [
127
+ palette(id, name, hue, hue + 12, 0.11), // ton sur ton
128
+ palette(id, name, hue, contrastHue, 0.14), // contrast
129
+ ]),
130
+ // The neutrals: graphite · paper (the design system's own pair) and graphite · amber.
131
+ {
132
+ id: "graphite-paper", name: "Graphite · papier", family: "graphite",
133
+ field: { l: 0.22, c: 0.012, h: 165 }, signal: { l: 0.96, c: 0.01, h: 150 }, tone: { l: 0.42, c: 0.02, h: 165 },
134
+ },
135
+ {
136
+ id: "graphite-amber", name: "Graphite · ambre", family: "graphite",
137
+ field: { l: 0.22, c: 0.012, h: 165 }, signal: fit({ l: 0.87, c: 0.14, h: 86 }), tone: { l: 0.42, c: 0.02, h: 165 },
138
+ },
139
+ ]
140
+
141
+ // ─── What a palette sets: its CSS block (light + dark) and its logo ────────
142
+
143
+ const WHITE: Oklch = { l: 0.99, c: 0.006, h: 0 }
144
+
145
+ /** The light and dark blocks for `[data-palette="<id>"]` — literal values, like the product blocks.
146
+ `--night`, Build/Ship/Run and the chart ramp derive from them in styles.css. */
147
+ export function paletteCss(p: Palette): string {
148
+ const { field, signal, tone } = p
149
+ const h = field.h
150
+ const light = {
151
+ "--field": css(field), "--signal": css(signal), "--tone": css(tone),
152
+ "--primary": css(tone), "--primary-foreground": css({ ...WHITE, h }), "--ring": css(tone),
153
+ "--accent": css({ l: 0.945, c: 0.03, h }), "--accent-foreground": css({ l: 0.32, c: 0.07, h }),
154
+ "--sidebar-primary": css(tone), "--sidebar-primary-foreground": css({ ...WHITE, h }),
155
+ "--sidebar-accent": css({ l: 0.935, c: 0.03, h }), "--sidebar-accent-foreground": css({ l: 0.3, c: 0.07, h }),
156
+ "--sidebar-ring": css(tone), "--flame": css(tone),
157
+ }
158
+ const dark = {
159
+ "--field": css(field), "--signal": css(signal), "--tone": css(tone),
160
+ "--primary": css(signal), "--primary-foreground": css(field), "--ring": css(signal),
161
+ "--accent": css({ l: 0.28, c: 0.035, h }), "--accent-foreground": css({ l: 0.95, c: 0.01, h }),
162
+ "--sidebar-primary": css(signal), "--sidebar-primary-foreground": css(field),
163
+ "--sidebar-accent": css({ l: 0.26, c: 0.03, h }), "--sidebar-accent-foreground": css({ l: 0.95, c: 0.01, h }),
164
+ "--sidebar-ring": css(signal), "--flame": css(signal),
165
+ }
166
+ const block = (o: Record<string, string>) => Object.entries(o).map(([k, v]) => ` ${k}: ${v};`).join("\n")
167
+ return `[data-palette="${p.id}"] {\n${block(light)}\n}\n.dark [data-palette="${p.id}"],\n.dark[data-palette="${p.id}"] {\n${block(dark)}\n}\n`
168
+ }
169
+
170
+ /** A palette's logo paint — signal tile, field ink (like `tone="product"`). */
171
+ export const paletteLogo = (p: Palette) => ({ tile: hex(p.signal), ink: hex(p.field) })
172
+
173
+ /** Every palette by id. */
174
+ export const PALETTE_BY_ID: Record<string, Palette> = Object.fromEntries(PALETTES.map((p) => [p.id, p]))
175
+
176
+ /** A palette's three colours in hex — for what can't read CSS (an export, a canvas, an email). */
177
+ export const paletteHex = (p: Palette) => ({ field: hex(p.field), signal: hex(p.signal), tone: hex(p.tone) })
178
+
179
+ /** The whole of `palettes.css` (scripts/generate-palettes.ts writes it, the test compares). */
180
+ export function palettesStylesheet(): string {
181
+ return `/* @spunto/design-system — palettes.css. GENERATED from src/palettes.ts by
182
+ * scripts/generate-palettes.ts (\`npm run palettes\`): don't edit by hand.
183
+ *
184
+ * Opt-in: import it after styles.css, then wear a palette on any subtree with
185
+ * \`data-palette="<id>"\` — the same cascade as \`data-product\`, light and dark. */
186
+
187
+ ${PALETTES.map(paletteCss).join("\n")}`
188
+ }
package/styles.css CHANGED
@@ -670,7 +670,8 @@
670
670
  --code-surface: oklch(0.19 0.012 165);
671
671
  }
672
672
  :root,
673
- [data-product] {
673
+ [data-product],
674
+ [data-palette] {
674
675
  --night: var(--field);
675
676
  }
676
677
 
@@ -678,11 +679,12 @@
678
679
  * Build / Ship / Run (the dashboard's semantics) and the chart ramp are not
679
680
  * colours of their own: they run from the product's signal (Build, the most
680
681
  * alive) to its mid tone `--tone` (Run, what settled). Declared on every
681
- * `[data-product]` so a subtree wearing another product re-derives them —
682
- * same reason as `--night` above. Dark lifts the ramp: Run can't sit at the
682
+ * `[data-product]` (and `[data-palette]`, palettes.css) so a subtree wearing
683
+ * another product re-derives them — same reason as `--night` above. Dark lifts the ramp: Run can't sit at the
683
684
  * mid tone on a near-black ground. */
684
685
  :root,
685
- [data-product] {
686
+ [data-product],
687
+ [data-palette] {
686
688
  --build: color-mix(in oklch, var(--signal) 80%, var(--tone));
687
689
  --ship: color-mix(in oklch, var(--signal) 40%, var(--tone));
688
690
  --run: var(--tone);
@@ -694,7 +696,9 @@
694
696
  }
695
697
  .dark,
696
698
  .dark [data-product],
697
- .dark[data-product] {
699
+ .dark[data-product],
700
+ .dark [data-palette],
701
+ .dark[data-palette] {
698
702
  --build: var(--signal);
699
703
  --ship: color-mix(in oklch, var(--signal) 70%, var(--tone));
700
704
  --run: color-mix(in oklch, var(--signal) 50%, var(--tone));