@tenphi/glaze 0.0.0-snapshot.4c063ef

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.
package/README.md ADDED
@@ -0,0 +1,931 @@
1
+ <p align="center">
2
+ <img src="assets/glaze.svg" width="128" height="128" alt="Glaze logo">
3
+ </p>
4
+
5
+ <h1 align="center">Glaze</h1>
6
+
7
+ <p align="center">
8
+ OKHSL-based color theme generator with WCAG contrast solving
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/@tenphi/glaze"><img src="https://img.shields.io/npm/v/@tenphi/glaze.svg" alt="npm version"></a>
13
+ <a href="https://github.com/tenphi/glaze/actions/workflows/ci.yml"><img src="https://github.com/tenphi/glaze/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
14
+ <a href="https://github.com/tenphi/glaze/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@tenphi/glaze.svg" alt="license"></a>
15
+ </p>
16
+
17
+ ---
18
+
19
+ Glaze generates robust **light**, **dark**, and **high-contrast** color schemes from a single hue/saturation seed. It preserves WCAG contrast ratios for UI color pairs via explicit dependency declarations — no hidden role math, no magic multipliers.
20
+
21
+ ## Features
22
+
23
+ - **OKHSL color space** — perceptually uniform hue and saturation
24
+ - **WCAG 2 contrast solving** — automatic lightness adjustment to meet AA/AAA targets
25
+ - **Shadow colors** — OKHSL-native shadow computation with automatic alpha, fg/bg tinting, and per-scheme adaptation
26
+ - **Light + Dark + High-Contrast** — all schemes from one definition
27
+ - **Per-color hue override** — absolute or relative hue shifts within a theme
28
+ - **Multi-format output** — `okhsl`, `rgb`, `hsl`, `oklch` with modern CSS space syntax
29
+ - **CSS custom properties export** — ready-to-use `--var: value;` declarations per scheme
30
+ - **Import/Export** — serialize and restore theme configurations
31
+ - **Create from hex/RGB** — start from an existing brand color
32
+ - **Zero dependencies** — pure math, runs anywhere (Node.js, browser, edge)
33
+ - **Tree-shakeable ESM + CJS** — dual-format package
34
+ - **TypeScript-first** — full type definitions included
35
+
36
+ ## Installation
37
+
38
+ ```bash
39
+ pnpm add @tenphi/glaze
40
+ ```
41
+
42
+ ```bash
43
+ npm install @tenphi/glaze
44
+ ```
45
+
46
+ ```bash
47
+ yarn add @tenphi/glaze
48
+ ```
49
+
50
+ ## Quick Start
51
+
52
+ ```ts
53
+ import { glaze } from '@tenphi/glaze';
54
+
55
+ // Create a theme from a hue (0–360) and saturation (0–100)
56
+ const primary = glaze(280, 80);
57
+
58
+ // Define colors with explicit lightness and contrast relationships
59
+ primary.colors({
60
+ surface: { lightness: 97, saturation: 0.75 },
61
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
62
+ border: { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' },
63
+ 'accent-fill': { lightness: 52, mode: 'fixed' },
64
+ 'accent-text': { base: 'accent-fill', lightness: '+48', contrast: 'AA', mode: 'fixed' },
65
+ });
66
+
67
+ // Create status themes by rotating the hue
68
+ const danger = primary.extend({ hue: 23 });
69
+ const success = primary.extend({ hue: 157 });
70
+
71
+ // Compose into a palette and export
72
+ const palette = glaze.palette({ primary, danger, success });
73
+ const tokens = palette.tokens({ prefix: true });
74
+ // → { light: { 'primary-surface': 'okhsl(...)', ... }, dark: { 'primary-surface': 'okhsl(...)', ... } }
75
+ ```
76
+
77
+ ## Core Concepts
78
+
79
+ ### One Theme = One Hue Family
80
+
81
+ A single `glaze` theme is tied to one hue/saturation seed. Status colors (danger, success, warning) are derived via `extend`, which inherits all color definitions and replaces the seed.
82
+
83
+ Individual colors can override the hue via the `hue` prop (see [Per-Color Hue Override](#per-color-hue-override)), but the primary purpose of a theme is to scope colors with the same hue.
84
+
85
+ ### Color Definitions
86
+
87
+ Every color is defined explicitly. No implicit roles — every value is stated.
88
+
89
+ #### Root Colors (explicit position)
90
+
91
+ ```ts
92
+ primary.colors({
93
+ surface: { lightness: 97, saturation: 0.75 },
94
+ border: { lightness: 90, saturation: 0.20 },
95
+ });
96
+ ```
97
+
98
+ - `lightness` — lightness in the light scheme (0–100)
99
+ - `saturation` — saturation factor applied to the seed saturation (0–1, default: `1`)
100
+
101
+ #### Dependent Colors (relative to base)
102
+
103
+ ```ts
104
+ primary.colors({
105
+ surface: { lightness: 97, saturation: 0.75 },
106
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
107
+ });
108
+ ```
109
+
110
+ - `base` — name of another color in the same theme
111
+ - `lightness` — position of this color (see [Lightness Values](#lightness-values))
112
+ - `contrast` — ensures the WCAG contrast ratio meets a target floor against the base
113
+
114
+ ### Lightness Values
115
+
116
+ The `lightness` prop accepts two forms:
117
+
118
+ | Form | Example | Meaning |
119
+ |---|---|---|
120
+ | Number (absolute) | `lightness: 45` | Absolute lightness 0–100 |
121
+ | String (relative) | `lightness: '-52'` | Relative to base color's lightness |
122
+
123
+ **Absolute lightness** on a dependent color (with `base`) positions the color independently. In dark mode, it is dark-mapped on its own. The `contrast` WCAG solver acts as a safety net.
124
+
125
+ **Relative lightness** applies a signed delta to the base color's resolved lightness. In dark mode with `auto` adaptation, the sign flips automatically.
126
+
127
+ ```ts
128
+ // Relative: 97 - 52 = 45 in light mode
129
+ 'text': { base: 'surface', lightness: '-52' }
130
+
131
+ // Absolute: lightness 45 in light mode, dark-mapped independently
132
+ 'text': { base: 'surface', lightness: 45 }
133
+ ```
134
+
135
+ A dependent color with `base` but no `lightness` inherits the base's lightness (equivalent to a delta of 0).
136
+
137
+ ### Per-Color Hue Override
138
+
139
+ Individual colors can override the theme's hue. The `hue` prop accepts:
140
+
141
+ | Form | Example | Meaning |
142
+ |---|---|---|
143
+ | Number (absolute) | `hue: 120` | Absolute hue 0–360 |
144
+ | String (relative) | `hue: '+20'` | Relative to the **theme seed** hue |
145
+
146
+ **Important:** Relative hue is always relative to the **theme seed hue**, not to a base color's hue.
147
+
148
+ ```ts
149
+ const theme = glaze(280, 80);
150
+ theme.colors({
151
+ surface: { lightness: 97 },
152
+ // Gradient end — slight hue shift from seed (280 + 20 = 300)
153
+ gradientEnd: { lightness: 90, hue: '+20' },
154
+ // Entirely different hue
155
+ warning: { lightness: 60, hue: 40 },
156
+ });
157
+ ```
158
+
159
+ ### contrast (WCAG Floor)
160
+
161
+ Ensures the WCAG contrast ratio meets a target floor. Accepts a numeric ratio or a preset string:
162
+
163
+ ```ts
164
+ type MinContrast = number | 'AA' | 'AAA' | 'AA-large' | 'AAA-large';
165
+ ```
166
+
167
+ | Preset | Ratio |
168
+ |---|---|
169
+ | `'AA'` | 4.5 |
170
+ | `'AAA'` | 7 |
171
+ | `'AA-large'` | 3 |
172
+ | `'AAA-large'` | 4.5 |
173
+
174
+ You can also pass any numeric ratio directly (e.g., `contrast: 4.5`, `contrast: 7`, `contrast: 11`).
175
+
176
+ The constraint is applied independently for each scheme. If the `lightness` already satisfies the floor, it's kept. Otherwise, the solver adjusts lightness until the target is met.
177
+
178
+ ### High-Contrast via Array Values
179
+
180
+ `lightness` and `contrast` accept a `[normal, high-contrast]` pair:
181
+
182
+ ```ts
183
+ 'border': { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' }
184
+ // ↑ ↑
185
+ // normal high-contrast
186
+ ```
187
+
188
+ A single value applies to both modes. All control is local and explicit.
189
+
190
+ ```ts
191
+ 'text': { base: 'surface', lightness: '-52', contrast: 'AAA' }
192
+ 'border': { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' }
193
+ 'muted': { base: 'surface', lightness: ['-35', '-50'], contrast: ['AA-large', 'AA'] }
194
+ ```
195
+
196
+ ## Theme Color Management
197
+
198
+ ### Adding Colors
199
+
200
+ `.colors(defs)` performs an **additive merge** — it adds new colors and overwrites existing ones by name, but does not remove other colors:
201
+
202
+ ```ts
203
+ const theme = glaze(280, 80);
204
+ theme.colors({ surface: { lightness: 97 } });
205
+ theme.colors({ text: { lightness: 30 } });
206
+ // Both 'surface' and 'text' are now defined
207
+ ```
208
+
209
+ ### Single Color Getter/Setter
210
+
211
+ `.color(name)` returns the definition, `.color(name, def)` sets it:
212
+
213
+ ```ts
214
+ theme.color('surface', { lightness: 97, saturation: 0.75 }); // set
215
+ const def = theme.color('surface'); // get → { lightness: 97, saturation: 0.75 }
216
+ ```
217
+
218
+ ### Removing Colors
219
+
220
+ `.remove(name)` or `.remove([name1, name2])` deletes color definitions:
221
+
222
+ ```ts
223
+ theme.remove('surface');
224
+ theme.remove(['text', 'border']);
225
+ ```
226
+
227
+ ### Introspection
228
+
229
+ ```ts
230
+ theme.has('surface'); // → true/false
231
+ theme.list(); // → ['surface', 'text', 'border', ...]
232
+ ```
233
+
234
+ ### Clearing All Colors
235
+
236
+ ```ts
237
+ theme.reset(); // removes all color definitions
238
+ ```
239
+
240
+ ## Import / Export
241
+
242
+ Serialize a theme's configuration (hue, saturation, color definitions) to a plain JSON-safe object, and restore it later:
243
+
244
+ ```ts
245
+ // Export
246
+ const snapshot = theme.export();
247
+ // → { hue: 280, saturation: 80, colors: { surface: { lightness: 97, saturation: 0.75 }, ... } }
248
+
249
+ const jsonString = JSON.stringify(snapshot);
250
+
251
+ // Import
252
+ const restored = glaze.from(JSON.parse(jsonString));
253
+ // restored is a fully functional GlazeTheme
254
+ ```
255
+
256
+ The export contains only the configuration — not resolved color values. Resolved values are recomputed on demand.
257
+
258
+ ## Standalone Color Token
259
+
260
+ Create a single color token without a full theme:
261
+
262
+ ```ts
263
+ const accent = glaze.color({ hue: 280, saturation: 80, lightness: 52, mode: 'fixed' });
264
+
265
+ accent.resolve(); // → ResolvedColor with light/dark/lightContrast/darkContrast
266
+ accent.token(); // → { '': 'okhsl(...)', '@dark': 'okhsl(...)' } (tasty format)
267
+ accent.tasty(); // → { '': 'okhsl(...)', '@dark': 'okhsl(...)' } (same as token)
268
+ accent.json(); // → { light: 'okhsl(...)', dark: 'okhsl(...)' }
269
+ ```
270
+
271
+ Standalone colors are always root colors (no `base`/`contrast`).
272
+
273
+ ## From Existing Colors
274
+
275
+ Create a theme from an existing brand color by extracting its OKHSL hue and saturation:
276
+
277
+ ```ts
278
+ // From hex
279
+ const brand = glaze.fromHex('#7a4dbf');
280
+
281
+ // From RGB (0–255)
282
+ const brand = glaze.fromRgb(122, 77, 191);
283
+ ```
284
+
285
+ The resulting theme has the extracted hue and saturation. Add colors as usual:
286
+
287
+ ```ts
288
+ brand.colors({
289
+ surface: { lightness: 97, saturation: 0.75 },
290
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
291
+ });
292
+ ```
293
+
294
+ ## Shadow Colors
295
+
296
+ Shadow colors are colors with computed alpha. Instead of a parallel shadow system, they extend the existing color pipeline. All math is done natively in OKHSL.
297
+
298
+ ### Defining Shadow Colors
299
+
300
+ Shadow colors use `type: 'shadow'` and reference a `bg` (background) color and optionally an `fg` (foreground) color for tinting and intensity modulation:
301
+
302
+ ```ts
303
+ theme.colors({
304
+ surface: { lightness: 95 },
305
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
306
+
307
+ 'shadow-sm': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 5 },
308
+ 'shadow-md': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 10 },
309
+ 'shadow-lg': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 20 },
310
+ });
311
+ ```
312
+
313
+ Shadow colors are included in all output methods (`tokens()`, `tasty()`, `css()`, `json()`) alongside regular colors:
314
+
315
+ ```ts
316
+ theme.tokens({ format: 'oklch' });
317
+ // light: { 'shadow-md': 'oklch(0.15 0.009 282 / 0.1)', ... }
318
+ // dark: { 'shadow-md': 'oklch(0.06 0.004 0 / 0.49)', ... }
319
+ ```
320
+
321
+ ### How Shadows Work
322
+
323
+ The shadow algorithm computes a dark, low-saturation pigment color and an alpha value that produces the desired visual intensity:
324
+
325
+ 1. **Contrast weight** — when `fg` is provided, shadow strength scales with `|l_bg - l_fg|`. Dark text on a light background produces a strong shadow; near-background-lightness elements produce barely visible shadows.
326
+ 2. **Pigment color** — hue blended between fg and bg, low saturation, dark lightness.
327
+ 3. **Alpha** — computed via a `tanh` curve that saturates smoothly toward `alphaMax` (default 0.6), ensuring well-separated shadow levels even on dark backgrounds.
328
+
329
+ ### Achromatic Shadows
330
+
331
+ Omit `fg` for a pure achromatic shadow at full user-specified intensity:
332
+
333
+ ```ts
334
+ theme.colors({
335
+ 'drop-shadow': { type: 'shadow', bg: 'surface', intensity: 12 },
336
+ });
337
+ ```
338
+
339
+ ### High-Contrast Intensity
340
+
341
+ `intensity` supports `[normal, highContrast]` pairs:
342
+
343
+ ```ts
344
+ theme.colors({
345
+ 'shadow-card': { type: 'shadow', bg: 'surface', fg: 'text', intensity: [10, 20] },
346
+ });
347
+ ```
348
+
349
+ ### Fixed Opacity (Regular Colors)
350
+
351
+ For a simple fixed-alpha color (no shadow algorithm), use `opacity` on a regular color:
352
+
353
+ ```ts
354
+ theme.colors({
355
+ overlay: { lightness: 0, opacity: 0.5 },
356
+ });
357
+ // → 'oklch(0 0 0 / 0.5)'
358
+ ```
359
+
360
+ ### Shadow Tuning
361
+
362
+ Fine-tune shadow behavior per-color or globally:
363
+
364
+ ```ts
365
+ // Per-color tuning
366
+ theme.colors({
367
+ 'shadow-soft': {
368
+ type: 'shadow', bg: 'surface', intensity: 10,
369
+ tuning: { alphaMax: 0.3, saturationFactor: 0.1 },
370
+ },
371
+ });
372
+
373
+ // Global tuning
374
+ glaze.configure({
375
+ shadowTuning: { alphaMax: 0.5, bgHueBlend: 0.3 },
376
+ });
377
+ ```
378
+
379
+ Available tuning parameters:
380
+
381
+ | Parameter | Default | Description |
382
+ |---|---|---|
383
+ | `saturationFactor` | 0.18 | Fraction of fg saturation kept in pigment |
384
+ | `maxSaturation` | 0.25 | Upper clamp on pigment saturation |
385
+ | `lightnessFactor` | 0.25 | Multiplier for bg lightness to pigment lightness |
386
+ | `lightnessBounds` | [0.05, 0.20] | Clamp range for pigment lightness |
387
+ | `minGapTarget` | 0.05 | Target minimum gap between pigment and bg lightness |
388
+ | `alphaMax` | 0.6 | Asymptotic maximum alpha |
389
+ | `bgHueBlend` | 0.2 | Blend weight pulling pigment hue toward bg hue |
390
+
391
+ ### Standalone Shadow Computation
392
+
393
+ Compute a shadow outside of a theme:
394
+
395
+ ```ts
396
+ const v = glaze.shadow({
397
+ bg: '#f0eef5',
398
+ fg: '#1a1a2e',
399
+ intensity: 10,
400
+ });
401
+ // → { h: 280, s: 0.14, l: 0.2, alpha: 0.1 }
402
+
403
+ const css = glaze.format(v, 'oklch');
404
+ // → 'oklch(0.15 0.014 280 / 0.1)'
405
+ ```
406
+
407
+ ### Consuming in CSS
408
+
409
+ ```css
410
+ .card {
411
+ box-shadow: 0 2px 6px var(--shadow-sm-color),
412
+ 0 8px 24px var(--shadow-md-color);
413
+ }
414
+ ```
415
+
416
+ ## Output Formats
417
+
418
+ Control the color format in exports with the `format` option:
419
+
420
+ ```ts
421
+ // Default: OKHSL
422
+ theme.tokens(); // → 'okhsl(280 60% 97%)'
423
+
424
+ // RGB (modern space syntax, rounded integers)
425
+ theme.tokens({ format: 'rgb' }); // → 'rgb(244 240 250)'
426
+
427
+ // HSL (modern space syntax)
428
+ theme.tokens({ format: 'hsl' }); // → 'hsl(270.5 45.2% 95.8%)'
429
+
430
+ // OKLCH
431
+ theme.tokens({ format: 'oklch' }); // → 'oklch(0.965 0.0123 280)'
432
+ ```
433
+
434
+ The `format` option works on all export methods: `theme.tokens()`, `theme.tasty()`, `theme.json()`, `theme.css()`, `palette.tokens()`, `palette.tasty()`, `palette.json()`, `palette.css()`, and standalone `glaze.color().token()` / `.tasty()` / `.json()`.
435
+
436
+ Colors with `alpha < 1` (shadow colors, or regular colors with `opacity`) include an alpha component:
437
+
438
+ ```ts
439
+ // → 'oklch(0.15 0.009 282 / 0.1)'
440
+ // → 'rgb(34 28 42 / 0.1)'
441
+ ```
442
+
443
+ Available formats:
444
+
445
+ | Format | Output (alpha = 1) | Output (alpha < 1) | Notes |
446
+ |---|---|---|---|
447
+ | `'okhsl'` (default) | `okhsl(H S% L%)` | `okhsl(H S% L% / A)` | Native format, not a CSS function |
448
+ | `'rgb'` | `rgb(R G B)` | `rgb(R G B / A)` | Rounded integers, space syntax |
449
+ | `'hsl'` | `hsl(H S% L%)` | `hsl(H S% L% / A)` | Modern space syntax |
450
+ | `'oklch'` | `oklch(L C H)` | `oklch(L C H / A)` | OKLab-based LCH |
451
+
452
+ All numeric output strips trailing zeros for cleaner CSS (e.g., `95` not `95.0`).
453
+
454
+ ## Adaptation Modes
455
+
456
+ Modes control how colors adapt across schemes:
457
+
458
+ | Mode | Behavior |
459
+ |---|---|
460
+ | `'auto'` (default) | Full adaptation. Light ↔ dark inversion. High-contrast boost. |
461
+ | `'fixed'` | Color stays recognizable. Only safety corrections. For brand buttons, CTAs. |
462
+ | `'static'` | No adaptation. Same value in every scheme. |
463
+
464
+ ### How Relative Lightness Adapts
465
+
466
+ **`auto` mode** — relative lightness sign flips in dark scheme:
467
+
468
+ ```ts
469
+ // Light: surface L=97, text lightness='-52' → L=45 (dark text on light bg)
470
+ // Dark: surface inverts to L≈14, sign flips → L=14+52=66
471
+ // contrast solver may push further (light text on dark bg)
472
+ ```
473
+
474
+ **`fixed` mode** — lightness is mapped (not inverted), relative sign preserved:
475
+
476
+ ```ts
477
+ // Light: accent-fill L=52, accent-text lightness='+48' → L=100 (white on brand)
478
+ // Dark: accent-fill maps to L≈51.6, sign preserved → L≈99.6
479
+ ```
480
+
481
+ **`static` mode** — no adaptation, same value in every scheme.
482
+
483
+ ## Light Scheme Mapping
484
+
485
+ ### Lightness
486
+
487
+ Root color lightness is mapped linearly within the configured `lightLightness` window:
488
+
489
+ ```ts
490
+ const [lo, hi] = lightLightness; // default: [10, 100]
491
+ const mappedL = (lightness * (hi - lo)) / 100 + lo;
492
+ ```
493
+
494
+ Both `auto` and `fixed` modes use the same linear formula. `static` mode bypasses the mapping entirely.
495
+
496
+ | Color | Raw L | Mapped L (default [10, 100]) |
497
+ |---|---|---|
498
+ | surface (L=97) | 97 | 97.3 |
499
+ | accent-fill (L=52) | 52 | 56.8 |
500
+ | near-black (L=0) | 0 | 10 |
501
+
502
+ ## Dark Scheme Mapping
503
+
504
+ ### Lightness
505
+
506
+ **`auto`** — inverted within the configured window:
507
+
508
+ ```ts
509
+ const [lo, hi] = darkLightness; // default: [15, 95]
510
+ const invertedL = ((100 - lightness) * (hi - lo)) / 100 + lo;
511
+ ```
512
+
513
+ **`fixed`** — mapped without inversion:
514
+
515
+ ```ts
516
+ const mappedL = (lightness * (hi - lo)) / 100 + lo;
517
+ ```
518
+
519
+ | Color | Light L | Auto (inverted) | Fixed (mapped) |
520
+ |---|---|---|---|
521
+ | surface (L=97) | 97 | 17.4 | 92.6 |
522
+ | accent-fill (L=52) | 52 | 53.4 | 56.6 |
523
+ | accent-text (L=100) | 100 | 15 | 95 |
524
+
525
+ ### Saturation
526
+
527
+ `darkDesaturation` reduces saturation for all colors in dark scheme:
528
+
529
+ ```ts
530
+ S_dark = S_light * (1 - darkDesaturation) // default: 0.1
531
+ ```
532
+
533
+ ## Inherited Themes (`extend`)
534
+
535
+ `extend` creates a new theme inheriting all color definitions, replacing the hue and/or saturation seed:
536
+
537
+ ```ts
538
+ const primary = glaze(280, 80);
539
+ primary.colors({ /* ... */ });
540
+
541
+ const danger = primary.extend({ hue: 23 });
542
+ const success = primary.extend({ hue: 157 });
543
+ const warning = primary.extend({ hue: 84 });
544
+ ```
545
+
546
+ Override individual colors (additive merge):
547
+
548
+ ```ts
549
+ const danger = primary.extend({
550
+ hue: 23,
551
+ colors: { 'accent-fill': { lightness: 48, mode: 'fixed' } },
552
+ });
553
+ ```
554
+
555
+ ## Palette Composition
556
+
557
+ Combine multiple themes into a single palette:
558
+
559
+ ```ts
560
+ const palette = glaze.palette({ primary, danger, success, warning });
561
+ ```
562
+
563
+ ### Token Export
564
+
565
+ Tokens are grouped by scheme variant, with plain color names as keys:
566
+
567
+ ```ts
568
+ const tokens = palette.tokens({ prefix: true });
569
+ // → {
570
+ // light: { 'primary-surface': 'okhsl(...)', 'danger-surface': 'okhsl(...)' },
571
+ // dark: { 'primary-surface': 'okhsl(...)', 'danger-surface': 'okhsl(...)' },
572
+ // }
573
+ ```
574
+
575
+ Custom prefix mapping:
576
+
577
+ ```ts
578
+ palette.tokens({ prefix: { primary: 'brand-', danger: 'error-' } });
579
+ ```
580
+
581
+ ### Tasty Export (for [Tasty](https://cube-ui-kit.vercel.app/?path=/docs/tasty-documentation--docs) style system)
582
+
583
+ The `tasty()` method exports tokens in the [Tasty](https://cube-ui-kit.vercel.app/?path=/docs/tasty-documentation--docs) style-to-state binding format — `#name` color token keys with state aliases (`''`, `@dark`, etc.):
584
+
585
+ ```ts
586
+ const tastyTokens = palette.tasty({ prefix: true });
587
+ // → {
588
+ // '#primary-surface': { '': 'okhsl(...)', '@dark': 'okhsl(...)' },
589
+ // '#danger-surface': { '': 'okhsl(...)', '@dark': 'okhsl(...)' },
590
+ // }
591
+ ```
592
+
593
+ Apply as global styles to make color tokens available app-wide:
594
+
595
+ ```ts
596
+ import { useGlobalStyles } from '@cube-dev/ui-kit';
597
+
598
+ // In your root component
599
+ useGlobalStyles('body', tastyTokens);
600
+ ```
601
+
602
+ For zero-runtime builds, use `tastyStatic` to generate the CSS at build time:
603
+
604
+ ```ts
605
+ import { tastyStatic } from '@cube-dev/ui-kit';
606
+
607
+ tastyStatic('body', tastyTokens);
608
+ ```
609
+
610
+ Alternatively, register as a recipe via `configure()`:
611
+
612
+ ```ts
613
+ import { configure, tasty } from '@cube-dev/ui-kit';
614
+
615
+ configure({
616
+ recipes: {
617
+ 'all-themes': tastyTokens,
618
+ },
619
+ });
620
+
621
+ const Page = tasty({
622
+ styles: {
623
+ recipe: 'all-themes',
624
+ fill: '#primary-surface',
625
+ color: '#primary-text',
626
+ },
627
+ });
628
+ ```
629
+
630
+ Or spread directly into component styles:
631
+
632
+ ```ts
633
+ const Card = tasty({
634
+ styles: {
635
+ ...tastyTokens,
636
+ fill: '#primary-surface',
637
+ color: '#primary-text',
638
+ },
639
+ });
640
+ ```
641
+
642
+ Custom prefix mapping:
643
+
644
+ ```ts
645
+ palette.tasty({ prefix: { primary: 'brand-', danger: 'error-' } });
646
+ ```
647
+
648
+ Custom state aliases:
649
+
650
+ ```ts
651
+ palette.tasty({ states: { dark: '@dark', highContrast: '@hc' } });
652
+ ```
653
+
654
+ ### JSON Export (Framework-Agnostic)
655
+
656
+ ```ts
657
+ const data = palette.json({ prefix: true });
658
+ // → {
659
+ // primary: { surface: { light: 'okhsl(...)', dark: 'okhsl(...)' } },
660
+ // danger: { surface: { light: 'okhsl(...)', dark: 'okhsl(...)' } },
661
+ // }
662
+ ```
663
+
664
+ ### CSS Export
665
+
666
+ Export as CSS custom property declarations, grouped by scheme variant. Each variant is a string of `--name-color: value;` lines that you can wrap in your own selectors and media queries.
667
+
668
+ ```ts
669
+ const css = theme.css();
670
+ // css.light → "--surface-color: rgb(...);\n--text-color: rgb(...);"
671
+ // css.dark → "--surface-color: rgb(...);\n--text-color: rgb(...);"
672
+ // css.lightContrast → "--surface-color: rgb(...);\n--text-color: rgb(...);"
673
+ // css.darkContrast → "--surface-color: rgb(...);\n--text-color: rgb(...);"
674
+ ```
675
+
676
+ Use in a stylesheet:
677
+
678
+ ```ts
679
+ const css = palette.css({ prefix: true });
680
+
681
+ const stylesheet = `
682
+ :root { ${css.light} }
683
+ @media (prefers-color-scheme: dark) {
684
+ :root { ${css.dark} }
685
+ }
686
+ `;
687
+ ```
688
+
689
+ Options:
690
+
691
+ | Option | Default | Description |
692
+ |---|---|---|
693
+ | `format` | `'rgb'` | Color format (`'rgb'`, `'hsl'`, `'okhsl'`, `'oklch'`) |
694
+ | `suffix` | `'-color'` | Suffix appended to each CSS property name |
695
+ | `prefix` | — | (palette only) Same prefix behavior as `tokens()` |
696
+
697
+ ```ts
698
+ // Custom suffix
699
+ theme.css({ suffix: '' });
700
+ // → "--surface: rgb(...);"
701
+
702
+ // Custom format
703
+ theme.css({ format: 'hsl' });
704
+ // → "--surface-color: hsl(...);"
705
+
706
+ // Palette with prefix
707
+ palette.css({ prefix: true });
708
+ // → "--primary-surface-color: rgb(...);\n--danger-surface-color: rgb(...);"
709
+ ```
710
+
711
+ ## Output Modes
712
+
713
+ Control which scheme variants appear in exports:
714
+
715
+ ```ts
716
+ // Light only
717
+ palette.tokens({ modes: { dark: false, highContrast: false } });
718
+ // → { light: { ... } }
719
+
720
+ // Light + dark (default)
721
+ palette.tokens({ modes: { highContrast: false } });
722
+ // → { light: { ... }, dark: { ... } }
723
+
724
+ // All four variants
725
+ palette.tokens({ modes: { dark: true, highContrast: true } });
726
+ // → { light: { ... }, dark: { ... }, lightContrast: { ... }, darkContrast: { ... } }
727
+ ```
728
+
729
+ The `modes` option works the same way on `tokens()`, `tasty()`, `json()`, and `css()`.
730
+
731
+ Resolution priority (highest first):
732
+
733
+ 1. `tokens({ modes })` / `tasty({ modes })` / `json({ modes })` / `css({ ... })` — per-call override
734
+ 2. `glaze.configure({ modes })` — global config
735
+ 3. Built-in default: `{ dark: true, highContrast: false }`
736
+
737
+ ## Configuration
738
+
739
+ ```ts
740
+ glaze.configure({
741
+ lightLightness: [10, 100], // Light scheme lightness window [lo, hi]
742
+ darkLightness: [15, 95], // Dark scheme lightness window [lo, hi]
743
+ darkDesaturation: 0.1, // Saturation reduction in dark scheme (0–1)
744
+ states: {
745
+ dark: '@dark', // State alias for dark mode tokens
746
+ highContrast: '@high-contrast',
747
+ },
748
+ modes: {
749
+ dark: true, // Include dark variants in exports
750
+ highContrast: false, // Include high-contrast variants
751
+ },
752
+ shadowTuning: { // Default tuning for all shadow colors
753
+ alphaMax: 0.6,
754
+ bgHueBlend: 0.2,
755
+ },
756
+ });
757
+ ```
758
+
759
+ ## Color Definition Shape
760
+
761
+ `ColorDef` is a discriminated union of regular colors and shadow colors:
762
+
763
+ ```ts
764
+ type ColorDef = RegularColorDef | ShadowColorDef;
765
+
766
+ interface RegularColorDef {
767
+ lightness?: HCPair<number | RelativeValue>;
768
+ saturation?: number;
769
+ hue?: number | RelativeValue;
770
+ base?: string;
771
+ contrast?: HCPair<MinContrast>;
772
+ mode?: 'auto' | 'fixed' | 'static';
773
+ opacity?: number; // fixed alpha (0–1)
774
+ }
775
+
776
+ interface ShadowColorDef {
777
+ type: 'shadow';
778
+ bg: string; // background color name (non-shadow)
779
+ fg?: string; // foreground color name (non-shadow)
780
+ intensity: HCPair<number>; // 0–100
781
+ tuning?: ShadowTuning;
782
+ }
783
+ ```
784
+
785
+ A root color must have absolute `lightness` (a number). A dependent color must have `base`. Relative `lightness` (a string) requires `base`. Shadow colors use `type: 'shadow'` and must reference a non-shadow `bg` color.
786
+
787
+ ## Validation
788
+
789
+ | Condition | Behavior |
790
+ |---|---|
791
+ | Both absolute `lightness` and `base` on same color | Warning, `lightness` takes precedence |
792
+ | `contrast` without `base` | Validation error |
793
+ | Relative `lightness` without `base` | Validation error |
794
+ | `lightness` resolves outside 0–100 | Clamp silently |
795
+ | `saturation` outside 0–1 | Clamp silently |
796
+ | Circular `base` references | Validation error |
797
+ | `base` references non-existent name | Validation error |
798
+ | Shadow `bg` references non-existent color | Validation error |
799
+ | Shadow `fg` references non-existent color | Validation error |
800
+ | Shadow `bg` references another shadow color | Validation error |
801
+ | Shadow `fg` references another shadow color | Validation error |
802
+ | Regular color `base` references a shadow color | Validation error |
803
+ | Shadow `intensity` outside 0–100 | Clamp silently |
804
+ | `contrast` + `opacity` combined | Warning |
805
+
806
+ ## Advanced: Color Math Utilities
807
+
808
+ Glaze re-exports its internal color math for advanced use:
809
+
810
+ ```ts
811
+ import {
812
+ okhslToLinearSrgb,
813
+ okhslToSrgb,
814
+ okhslToOklab,
815
+ srgbToOkhsl,
816
+ parseHex,
817
+ relativeLuminanceFromLinearRgb,
818
+ contrastRatioFromLuminance,
819
+ formatOkhsl,
820
+ formatRgb,
821
+ formatHsl,
822
+ formatOklch,
823
+ findLightnessForContrast,
824
+ resolveMinContrast,
825
+ } from '@tenphi/glaze';
826
+ ```
827
+
828
+ ## Full Example
829
+
830
+ ```ts
831
+ import { glaze } from '@tenphi/glaze';
832
+
833
+ const primary = glaze(280, 80);
834
+
835
+ primary.colors({
836
+ surface: { lightness: 97, saturation: 0.75 },
837
+ text: { base: 'surface', lightness: '-52', contrast: 'AAA' },
838
+ border: { base: 'surface', lightness: ['-7', '-20'], contrast: 'AA-large' },
839
+ bg: { lightness: 97, saturation: 0.75 },
840
+ icon: { lightness: 60, saturation: 0.94 },
841
+ 'accent-fill': { lightness: 52, mode: 'fixed' },
842
+ 'accent-text': { base: 'accent-fill', lightness: '+48', contrast: 'AA', mode: 'fixed' },
843
+ disabled: { lightness: 81, saturation: 0.4 },
844
+
845
+ // Shadow colors — computed alpha, automatic dark-mode adaptation
846
+ 'shadow-sm': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 5 },
847
+ 'shadow-md': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 10 },
848
+ 'shadow-lg': { type: 'shadow', bg: 'surface', fg: 'text', intensity: 20 },
849
+
850
+ // Fixed-alpha overlay
851
+ overlay: { lightness: 0, opacity: 0.5 },
852
+ });
853
+
854
+ const danger = primary.extend({ hue: 23 });
855
+ const success = primary.extend({ hue: 157 });
856
+ const warning = primary.extend({ hue: 84 });
857
+ const note = primary.extend({ hue: 302 });
858
+
859
+ const palette = glaze.palette({ primary, danger, success, warning, note });
860
+
861
+ // Export as flat token map grouped by variant
862
+ const tokens = palette.tokens({ prefix: true });
863
+ // tokens.light → { 'primary-surface': 'okhsl(...)', 'primary-shadow-md': 'okhsl(... / 0.1)' }
864
+
865
+ // Export as tasty style-to-state bindings (for Tasty style system)
866
+ const tastyTokens = palette.tasty({ prefix: true });
867
+
868
+ // Export as CSS custom properties (rgb format by default)
869
+ const css = palette.css({ prefix: true });
870
+ // css.light → "--primary-surface-color: rgb(...);\n--primary-shadow-md-color: rgb(... / 0.1);"
871
+
872
+ // Standalone shadow computation
873
+ const v = glaze.shadow({ bg: '#f0eef5', fg: '#1a1a2e', intensity: 10 });
874
+ const shadowCss = glaze.format(v, 'oklch');
875
+ // → 'oklch(0.15 0.014 280 / 0.1)'
876
+
877
+ // Save and restore a theme
878
+ const snapshot = primary.export();
879
+ const restored = glaze.from(snapshot);
880
+
881
+ // Create from an existing brand color
882
+ const brand = glaze.fromHex('#7a4dbf');
883
+ brand.colors({ surface: { lightness: 97 }, text: { base: 'surface', lightness: '-52' } });
884
+ ```
885
+
886
+ ## API Reference
887
+
888
+ ### Theme Creation
889
+
890
+ | Method | Description |
891
+ |---|---|
892
+ | `glaze(hue, saturation?)` | Create a theme from hue (0–360) and saturation (0–100) |
893
+ | `glaze({ hue, saturation })` | Create a theme from an options object |
894
+ | `glaze.from(data)` | Create a theme from an exported configuration |
895
+ | `glaze.fromHex(hex)` | Create a theme from a hex color (`#rgb` or `#rrggbb`) |
896
+ | `glaze.fromRgb(r, g, b)` | Create a theme from RGB values (0–255) |
897
+ | `glaze.color(input)` | Create a standalone color token |
898
+ | `glaze.shadow(input)` | Compute a standalone shadow color (returns `ResolvedColorVariant`) |
899
+ | `glaze.format(variant, format?)` | Format any `ResolvedColorVariant` as a CSS string |
900
+
901
+ ### Theme Methods
902
+
903
+ | Method | Description |
904
+ |---|---|
905
+ | `theme.colors(defs)` | Add/replace colors (additive merge) |
906
+ | `theme.color(name)` | Get a color definition |
907
+ | `theme.color(name, def)` | Set a single color definition |
908
+ | `theme.remove(names)` | Remove one or more colors |
909
+ | `theme.has(name)` | Check if a color is defined |
910
+ | `theme.list()` | List all defined color names |
911
+ | `theme.reset()` | Clear all color definitions |
912
+ | `theme.export()` | Export configuration as JSON-safe object |
913
+ | `theme.extend(options)` | Create a child theme |
914
+ | `theme.resolve()` | Resolve all colors |
915
+ | `theme.tokens(options?)` | Export as flat token map grouped by variant |
916
+ | `theme.tasty(options?)` | Export as [Tasty](https://cube-ui-kit.vercel.app/?path=/docs/tasty-documentation--docs) style-to-state bindings |
917
+ | `theme.json(options?)` | Export as plain JSON |
918
+ | `theme.css(options?)` | Export as CSS custom property declarations |
919
+
920
+ ### Global Configuration
921
+
922
+ | Method | Description |
923
+ |---|---|
924
+ | `glaze.configure(config)` | Set global configuration |
925
+ | `glaze.palette(themes)` | Compose themes into a palette |
926
+ | `glaze.getConfig()` | Get current global config |
927
+ | `glaze.resetConfig()` | Reset to defaults |
928
+
929
+ ## License
930
+
931
+ [MIT](LICENSE)