@tenphi/glaze 0.1.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.
package/dist/index.mjs ADDED
@@ -0,0 +1,1181 @@
1
+ //#region src/okhsl-color-math.ts
2
+ const OKLab_to_LMS_M = [
3
+ [
4
+ 1,
5
+ .3963377773761749,
6
+ .2158037573099136
7
+ ],
8
+ [
9
+ 1,
10
+ -.1055613458156586,
11
+ -.0638541728258133
12
+ ],
13
+ [
14
+ 1,
15
+ -.0894841775298119,
16
+ -1.2914855480194092
17
+ ]
18
+ ];
19
+ const LMS_to_linear_sRGB_M = [
20
+ [
21
+ 4.076741636075959,
22
+ -3.307711539258062,
23
+ .2309699031821041
24
+ ],
25
+ [
26
+ -1.2684379732850313,
27
+ 2.6097573492876878,
28
+ -.3413193760026569
29
+ ],
30
+ [
31
+ -.004196076138675526,
32
+ -.703418617935936,
33
+ 1.7076146940746113
34
+ ]
35
+ ];
36
+ const linear_sRGB_to_LMS_M = [
37
+ [
38
+ .4122214708,
39
+ .5363325363,
40
+ .0514459929
41
+ ],
42
+ [
43
+ .2119034982,
44
+ .6806995451,
45
+ .1073969566
46
+ ],
47
+ [
48
+ .0883024619,
49
+ .2817188376,
50
+ .6299787005
51
+ ]
52
+ ];
53
+ const LMS_to_OKLab_M = [
54
+ [
55
+ .2104542553,
56
+ .793617785,
57
+ -.0040720468
58
+ ],
59
+ [
60
+ 1.9779984951,
61
+ -2.428592205,
62
+ .4505937099
63
+ ],
64
+ [
65
+ .0259040371,
66
+ .7827717662,
67
+ -.808675766
68
+ ]
69
+ ];
70
+ const OKLab_to_linear_sRGB_coefficients = [
71
+ [[-1.8817030993265873, -.8093650129914302], [
72
+ 1.19086277,
73
+ 1.76576728,
74
+ .59662641,
75
+ .75515197,
76
+ .56771245
77
+ ]],
78
+ [[1.8144407988010998, -1.194452667805235], [
79
+ .73956515,
80
+ -.45954404,
81
+ .08285427,
82
+ .12541073,
83
+ -.14503204
84
+ ]],
85
+ [[.13110757611180954, 1.813339709266608], [
86
+ 1.35733652,
87
+ -.00915799,
88
+ -1.1513021,
89
+ -.50559606,
90
+ .00692167
91
+ ]]
92
+ ];
93
+ const TAU = 2 * Math.PI;
94
+ const K1 = .206;
95
+ const K2 = .03;
96
+ const K3 = (1 + K1) / (1 + K2);
97
+ const EPSILON = 1e-10;
98
+ const constrainAngle = (angle) => (angle % 360 + 360) % 360;
99
+ const toe = (x) => .5 * (K3 * x - K1 + Math.sqrt((K3 * x - K1) * (K3 * x - K1) + 4 * K2 * K3 * x));
100
+ const toeInv = (x) => (x ** 2 + K1 * x) / (K3 * (x + K2));
101
+ const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
102
+ const dotXY = (a, b) => a[0] * b[0] + a[1] * b[1];
103
+ const transform = (input, matrix) => [
104
+ dot3(input, matrix[0]),
105
+ dot3(input, matrix[1]),
106
+ dot3(input, matrix[2])
107
+ ];
108
+ const cubed3 = (lms) => [
109
+ lms[0] ** 3,
110
+ lms[1] ** 3,
111
+ lms[2] ** 3
112
+ ];
113
+ const cbrt3 = (lms) => [
114
+ Math.cbrt(lms[0]),
115
+ Math.cbrt(lms[1]),
116
+ Math.cbrt(lms[2])
117
+ ];
118
+ const clampVal = (v, min, max) => Math.max(min, Math.min(max, v));
119
+ const OKLabToLinearSRGB = (lab) => {
120
+ return transform(cubed3(transform(lab, OKLab_to_LMS_M)), LMS_to_linear_sRGB_M);
121
+ };
122
+ const computeMaxSaturationOKLC = (a, b) => {
123
+ const okCoeff = OKLab_to_linear_sRGB_coefficients;
124
+ const lmsToRgb = LMS_to_linear_sRGB_M;
125
+ const tmp2 = [a, b];
126
+ const tmp3 = [
127
+ 0,
128
+ a,
129
+ b
130
+ ];
131
+ let chnlCoeff;
132
+ let chnlLMS;
133
+ if (dotXY(okCoeff[0][0], tmp2) > 1) {
134
+ chnlCoeff = okCoeff[0][1];
135
+ chnlLMS = lmsToRgb[0];
136
+ } else if (dotXY(okCoeff[1][0], tmp2) > 1) {
137
+ chnlCoeff = okCoeff[1][1];
138
+ chnlLMS = lmsToRgb[1];
139
+ } else {
140
+ chnlCoeff = okCoeff[2][1];
141
+ chnlLMS = lmsToRgb[2];
142
+ }
143
+ const [k0, k1, k2, k3, k4] = chnlCoeff;
144
+ const [wl, wm, ws] = chnlLMS;
145
+ let sat = k0 + k1 * a + k2 * b + k3 * (a * a) + k4 * a * b;
146
+ const dotYZ = (mat, vec) => mat[1] * vec[1] + mat[2] * vec[2];
147
+ const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);
148
+ const km = dotYZ(OKLab_to_LMS_M[1], tmp3);
149
+ const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);
150
+ const l_ = 1 + sat * kl;
151
+ const m_ = 1 + sat * km;
152
+ const s_ = 1 + sat * ks;
153
+ const l = l_ ** 3;
154
+ const m = m_ ** 3;
155
+ const s = s_ ** 3;
156
+ const lds = 3 * kl * l_ * l_;
157
+ const mds = 3 * km * m_ * m_;
158
+ const sds = 3 * ks * s_ * s_;
159
+ const lds2 = 6 * kl * kl * l_;
160
+ const mds2 = 6 * km * km * m_;
161
+ const sds2 = 6 * ks * ks * s_;
162
+ const f = wl * l + wm * m + ws * s;
163
+ const f1 = wl * lds + wm * mds + ws * sds;
164
+ const f2 = wl * lds2 + wm * mds2 + ws * sds2;
165
+ sat = sat - f * f1 / (f1 * f1 - .5 * f * f2);
166
+ return sat;
167
+ };
168
+ const findCuspOKLCH = (a, b) => {
169
+ const S_cusp = computeMaxSaturationOKLC(a, b);
170
+ const rgb_at_max = OKLabToLinearSRGB([
171
+ 1,
172
+ S_cusp * a,
173
+ S_cusp * b
174
+ ]);
175
+ const L_cusp = Math.cbrt(1 / Math.max(Math.max(rgb_at_max[0], rgb_at_max[1]), Math.max(rgb_at_max[2], 0)));
176
+ return [L_cusp, L_cusp * S_cusp];
177
+ };
178
+ const findGamutIntersectionOKLCH = (a, b, l1, c1, l0, cusp) => {
179
+ const lmsToRgb = LMS_to_linear_sRGB_M;
180
+ const tmp3 = [
181
+ 0,
182
+ a,
183
+ b
184
+ ];
185
+ const floatMax = Number.MAX_VALUE;
186
+ let t;
187
+ const dotYZ = (mat, vec) => mat[1] * vec[1] + mat[2] * vec[2];
188
+ const dotXYZ = (vec, x, y, z) => vec[0] * x + vec[1] * y + vec[2] * z;
189
+ if ((l1 - l0) * cusp[1] - (cusp[0] - l0) * c1 <= 0) {
190
+ const denom = c1 * cusp[0] + cusp[1] * (l0 - l1);
191
+ t = denom === 0 ? 0 : cusp[1] * l0 / denom;
192
+ } else {
193
+ const denom = c1 * (cusp[0] - 1) + cusp[1] * (l0 - l1);
194
+ t = denom === 0 ? 0 : cusp[1] * (l0 - 1) / denom;
195
+ const dl = l1 - l0;
196
+ const dc = c1;
197
+ const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);
198
+ const km = dotYZ(OKLab_to_LMS_M[1], tmp3);
199
+ const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);
200
+ const L = l0 * (1 - t) + t * l1;
201
+ const C = t * c1;
202
+ const l_ = L + C * kl;
203
+ const m_ = L + C * km;
204
+ const s_ = L + C * ks;
205
+ const l = l_ ** 3;
206
+ const m = m_ ** 3;
207
+ const s = s_ ** 3;
208
+ const ldt = 3 * (dl + dc * kl) * l_ * l_;
209
+ const mdt = 3 * (dl + dc * km) * m_ * m_;
210
+ const sdt = 3 * (dl + dc * ks) * s_ * s_;
211
+ const ldt2 = 6 * (dl + dc * kl) ** 2 * l_;
212
+ const mdt2 = 6 * (dl + dc * km) ** 2 * m_;
213
+ const sdt2 = 6 * (dl + dc * ks) ** 2 * s_;
214
+ const r_ = dotXYZ(lmsToRgb[0], l, m, s) - 1;
215
+ const r1 = dotXYZ(lmsToRgb[0], ldt, mdt, sdt);
216
+ const r2 = dotXYZ(lmsToRgb[0], ldt2, mdt2, sdt2);
217
+ const ur = r1 / (r1 * r1 - .5 * r_ * r2);
218
+ let tr = -r_ * ur;
219
+ const g_ = dotXYZ(lmsToRgb[1], l, m, s) - 1;
220
+ const g1 = dotXYZ(lmsToRgb[1], ldt, mdt, sdt);
221
+ const g2 = dotXYZ(lmsToRgb[1], ldt2, mdt2, sdt2);
222
+ const ug = g1 / (g1 * g1 - .5 * g_ * g2);
223
+ let tg = -g_ * ug;
224
+ const b_ = dotXYZ(lmsToRgb[2], l, m, s) - 1;
225
+ const b1 = dotXYZ(lmsToRgb[2], ldt, mdt, sdt);
226
+ const b2 = dotXYZ(lmsToRgb[2], ldt2, mdt2, sdt2);
227
+ const ub = b1 / (b1 * b1 - .5 * b_ * b2);
228
+ let tb = -b_ * ub;
229
+ tr = ur >= 0 ? tr : floatMax;
230
+ tg = ug >= 0 ? tg : floatMax;
231
+ tb = ub >= 0 ? tb : floatMax;
232
+ t += Math.min(tr, Math.min(tg, tb));
233
+ }
234
+ return t;
235
+ };
236
+ const computeSt = (cusp) => [cusp[1] / cusp[0], cusp[1] / (1 - cusp[0])];
237
+ const computeStMid = (a, b) => [.11516993 + 1 / (7.4477897 + 4.1590124 * b + a * (-2.19557347 + 1.75198401 * b + a * (-2.13704948 - 10.02301043 * b + a * (-4.24894561 + 5.38770819 * b + 4.69891013 * a)))), .11239642 + 1 / (1.6132032 - .68124379 * b + a * (.40370612 + .90148123 * b + a * (-.27087943 + .6122399 * b + a * (.00299215 - .45399568 * b - .14661872 * a))))];
238
+ const getCs = (L, a, b, cusp) => {
239
+ const cMax = findGamutIntersectionOKLCH(a, b, L, 1, L, cusp);
240
+ const stMax = computeSt(cusp);
241
+ const k = cMax / Math.min(L * stMax[0], (1 - L) * stMax[1]);
242
+ const stMid = computeStMid(a, b);
243
+ let ca = L * stMid[0];
244
+ let cb = (1 - L) * stMid[1];
245
+ const cMid = .9 * k * Math.sqrt(Math.sqrt(1 / (1 / ca ** 4 + 1 / cb ** 4)));
246
+ ca = L * .4;
247
+ cb = (1 - L) * .8;
248
+ return [
249
+ Math.sqrt(1 / (1 / ca ** 2 + 1 / cb ** 2)),
250
+ cMid,
251
+ cMax
252
+ ];
253
+ };
254
+ /**
255
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
256
+ * Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
257
+ */
258
+ function okhslToLinearSrgb(h, s, l) {
259
+ const L = toeInv(l);
260
+ let a = 0;
261
+ let b = 0;
262
+ const hNorm = constrainAngle(h) / 360;
263
+ if (L !== 0 && L !== 1 && s !== 0) {
264
+ const a_ = Math.cos(TAU * hNorm);
265
+ const b_ = Math.sin(TAU * hNorm);
266
+ const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
267
+ const mid = .8;
268
+ const midInv = 1.25;
269
+ let t, k0, k1, k2;
270
+ if (s < mid) {
271
+ t = midInv * s;
272
+ k0 = 0;
273
+ k1 = mid * c0;
274
+ k2 = 1 - k1 / cMid;
275
+ } else {
276
+ t = 5 * (s - .8);
277
+ k0 = cMid;
278
+ k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
279
+ k2 = 1 - k1 / (cMax - cMid);
280
+ }
281
+ const c = k0 + t * k1 / (1 - k2 * t);
282
+ a = c * a_;
283
+ b = c * b_;
284
+ }
285
+ return OKLabToLinearSRGB([
286
+ L,
287
+ a,
288
+ b
289
+ ]);
290
+ }
291
+ /**
292
+ * Compute relative luminance Y from linear sRGB channels.
293
+ * Per WCAG 2: Y = 0.2126·R + 0.7152·G + 0.0722·B
294
+ */
295
+ function relativeLuminanceFromLinearRgb(rgb) {
296
+ return .2126 * rgb[0] + .7152 * rgb[1] + .0722 * rgb[2];
297
+ }
298
+ /**
299
+ * WCAG 2 contrast ratio from two luminance values.
300
+ */
301
+ function contrastRatioFromLuminance(yA, yB) {
302
+ const lighter = Math.max(yA, yB);
303
+ const darker = Math.min(yA, yB);
304
+ return (lighter + .05) / (darker + .05);
305
+ }
306
+ const sRGBLinearToGamma = (val) => {
307
+ const sign = val < 0 ? -1 : 1;
308
+ const abs = Math.abs(val);
309
+ return abs > .0031308 ? sign * (1.055 * Math.pow(abs, 1 / 2.4) - .055) : 12.92 * val;
310
+ };
311
+ const sRGBGammaToLinear = (val) => {
312
+ const sign = val < 0 ? -1 : 1;
313
+ const abs = Math.abs(val);
314
+ return abs <= .04045 ? val / 12.92 : sign * Math.pow((abs + .055) / 1.055, 2.4);
315
+ };
316
+ /**
317
+ * Convert OKHSL to gamma-encoded sRGB (clamped to 0–1).
318
+ */
319
+ function okhslToSrgb(h, s, l) {
320
+ const lin = okhslToLinearSrgb(h, s, l);
321
+ return [
322
+ Math.max(0, Math.min(1, sRGBLinearToGamma(lin[0]))),
323
+ Math.max(0, Math.min(1, sRGBLinearToGamma(lin[1]))),
324
+ Math.max(0, Math.min(1, sRGBLinearToGamma(lin[2])))
325
+ ];
326
+ }
327
+ /**
328
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
329
+ */
330
+ function okhslToOklab(h, s, l) {
331
+ const L = toeInv(l);
332
+ let a = 0;
333
+ let b = 0;
334
+ const hNorm = constrainAngle(h) / 360;
335
+ if (L !== 0 && L !== 1 && s !== 0) {
336
+ const a_ = Math.cos(TAU * hNorm);
337
+ const b_ = Math.sin(TAU * hNorm);
338
+ const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
339
+ const mid = .8;
340
+ const midInv = 1.25;
341
+ let t, k0, k1, k2;
342
+ if (s < mid) {
343
+ t = midInv * s;
344
+ k0 = 0;
345
+ k1 = mid * c0;
346
+ k2 = 1 - k1 / cMid;
347
+ } else {
348
+ t = 5 * (s - .8);
349
+ k0 = cMid;
350
+ k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
351
+ k2 = 1 - k1 / (cMax - cMid);
352
+ }
353
+ const c = k0 + t * k1 / (1 - k2 * t);
354
+ a = c * a_;
355
+ b = c * b_;
356
+ }
357
+ return [
358
+ L,
359
+ a,
360
+ b
361
+ ];
362
+ }
363
+ const linearSrgbToOklab = (rgb) => {
364
+ return transform(cbrt3(transform(rgb, linear_sRGB_to_LMS_M)), LMS_to_OKLab_M);
365
+ };
366
+ const oklabToOkhsl = (lab) => {
367
+ const L = lab[0];
368
+ const a = lab[1];
369
+ const b = lab[2];
370
+ const C = Math.sqrt(a * a + b * b);
371
+ if (C < EPSILON) return [
372
+ 0,
373
+ 0,
374
+ toe(L)
375
+ ];
376
+ const a_ = a / C;
377
+ const b_ = b / C;
378
+ let h = Math.atan2(b, a) * (180 / Math.PI);
379
+ h = constrainAngle(h);
380
+ const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
381
+ const mid = .8;
382
+ const midInv = 1.25;
383
+ let s;
384
+ if (C < cMid) {
385
+ const k1 = mid * c0;
386
+ s = C / (k1 + C * (1 - k1 / cMid)) / midInv;
387
+ } else {
388
+ const k0 = cMid;
389
+ const k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
390
+ const k2 = 1 - k1 / (cMax - cMid);
391
+ const cDiff = C - k0;
392
+ s = mid + cDiff / (k1 + cDiff * k2) / 5;
393
+ }
394
+ const l = toe(L);
395
+ return [
396
+ h,
397
+ clampVal(s, 0, 1),
398
+ clampVal(l, 0, 1)
399
+ ];
400
+ };
401
+ /**
402
+ * Convert gamma-encoded sRGB (0–1 per channel) to OKHSL.
403
+ * Returns [h, s, l] where h: 0–360, s: 0–1, l: 0–1.
404
+ */
405
+ function srgbToOkhsl(rgb) {
406
+ return oklabToOkhsl(linearSrgbToOklab([
407
+ sRGBGammaToLinear(rgb[0]),
408
+ sRGBGammaToLinear(rgb[1]),
409
+ sRGBGammaToLinear(rgb[2])
410
+ ]));
411
+ }
412
+ /**
413
+ * Parse a hex color string (#rgb or #rrggbb) to sRGB [r, g, b] in 0–1 range.
414
+ * Returns null if the string is not a valid hex color.
415
+ */
416
+ function parseHex(hex) {
417
+ const h = hex.startsWith("#") ? hex.slice(1) : hex;
418
+ if (h.length === 3) {
419
+ const r = parseInt(h[0] + h[0], 16);
420
+ const g = parseInt(h[1] + h[1], 16);
421
+ const b = parseInt(h[2] + h[2], 16);
422
+ if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
423
+ return [
424
+ r / 255,
425
+ g / 255,
426
+ b / 255
427
+ ];
428
+ }
429
+ if (h.length === 6) {
430
+ const r = parseInt(h.slice(0, 2), 16);
431
+ const g = parseInt(h.slice(2, 4), 16);
432
+ const b = parseInt(h.slice(4, 6), 16);
433
+ if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
434
+ return [
435
+ r / 255,
436
+ g / 255,
437
+ b / 255
438
+ ];
439
+ }
440
+ return null;
441
+ }
442
+ /**
443
+ * Format OKHSL values as a CSS `okhsl(H S% L%)` string.
444
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
445
+ */
446
+ function formatOkhsl(h, s, l) {
447
+ return `okhsl(${h.toFixed(1)} ${s.toFixed(1)}% ${l.toFixed(1)}%)`;
448
+ }
449
+ /**
450
+ * Format OKHSL values as a CSS `rgb(R, G, B)` string with fractional 0–255 values.
451
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
452
+ */
453
+ function formatRgb(h, s, l) {
454
+ const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
455
+ return `rgb(${(r * 255).toFixed(3)}, ${(g * 255).toFixed(3)}, ${(b * 255).toFixed(3)})`;
456
+ }
457
+ /**
458
+ * Format OKHSL values as a CSS `hsl(H, S%, L%)` string.
459
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
460
+ */
461
+ function formatHsl(h, s, l) {
462
+ const [r, g, b] = okhslToSrgb(h, s / 100, l / 100);
463
+ const max = Math.max(r, g, b);
464
+ const min = Math.min(r, g, b);
465
+ const delta = max - min;
466
+ let hh = 0;
467
+ let ss = 0;
468
+ const ll = (max + min) / 2;
469
+ if (delta > 0) {
470
+ ss = ll > .5 ? delta / (2 - max - min) : delta / (max + min);
471
+ if (max === r) hh = ((g - b) / delta + (g < b ? 6 : 0)) * 60;
472
+ else if (max === g) hh = ((b - r) / delta + 2) * 60;
473
+ else hh = ((r - g) / delta + 4) * 60;
474
+ }
475
+ return `hsl(${hh.toFixed(1)}, ${(ss * 100).toFixed(1)}%, ${(ll * 100).toFixed(1)}%)`;
476
+ }
477
+ /**
478
+ * Format OKHSL values as a CSS `oklch(L% C H)` string.
479
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
480
+ */
481
+ function formatOklch(h, s, l) {
482
+ const [L, a, b] = okhslToOklab(h, s / 100, l / 100);
483
+ const C = Math.sqrt(a * a + b * b);
484
+ let hh = Math.atan2(b, a) * (180 / Math.PI);
485
+ hh = constrainAngle(hh);
486
+ return `oklch(${(L * 100).toFixed(1)}% ${C.toFixed(4)} ${hh.toFixed(1)})`;
487
+ }
488
+
489
+ //#endregion
490
+ //#region src/contrast-solver.ts
491
+ /**
492
+ * OKHSL Contrast Solver
493
+ *
494
+ * Finds the closest OKHSL lightness that satisfies a WCAG 2 contrast target
495
+ * against a base color. Used by glaze when resolving dependent colors
496
+ * with `ensureContrast`.
497
+ */
498
+ const CONTRAST_PRESETS = {
499
+ AA: 4.5,
500
+ AAA: 7,
501
+ "AA-large": 3,
502
+ "AAA-large": 4.5
503
+ };
504
+ function resolveMinContrast(value) {
505
+ if (typeof value === "number") return Math.max(1, value);
506
+ return CONTRAST_PRESETS[value];
507
+ }
508
+ const CACHE_SIZE = 512;
509
+ const luminanceCache = /* @__PURE__ */ new Map();
510
+ const cacheOrder = [];
511
+ function cachedLuminance(h, s, l) {
512
+ const lRounded = Math.round(l * 1e4) / 1e4;
513
+ const key = `${h}|${s}|${lRounded}`;
514
+ const cached = luminanceCache.get(key);
515
+ if (cached !== void 0) return cached;
516
+ const y = relativeLuminanceFromLinearRgb(okhslToLinearSrgb(h, s, lRounded));
517
+ if (luminanceCache.size >= CACHE_SIZE) {
518
+ const evict = cacheOrder.shift();
519
+ luminanceCache.delete(evict);
520
+ }
521
+ luminanceCache.set(key, y);
522
+ cacheOrder.push(key);
523
+ return y;
524
+ }
525
+ /**
526
+ * Binary search one branch [lo, hi] for the nearest passing lightness to `preferred`.
527
+ */
528
+ function searchBranch(h, s, lo, hi, yBase, target, epsilon, maxIter, preferred) {
529
+ const yLo = cachedLuminance(h, s, lo);
530
+ const yHi = cachedLuminance(h, s, hi);
531
+ const crLo = contrastRatioFromLuminance(yLo, yBase);
532
+ const crHi = contrastRatioFromLuminance(yHi, yBase);
533
+ if (crLo < target && crHi < target) {
534
+ if (crLo >= crHi) return {
535
+ lightness: lo,
536
+ contrast: crLo,
537
+ met: false
538
+ };
539
+ return {
540
+ lightness: hi,
541
+ contrast: crHi,
542
+ met: false
543
+ };
544
+ }
545
+ let low = lo;
546
+ let high = hi;
547
+ for (let i = 0; i < maxIter; i++) {
548
+ if (high - low < epsilon) break;
549
+ const mid = (low + high) / 2;
550
+ if (contrastRatioFromLuminance(cachedLuminance(h, s, mid), yBase) >= target) if (mid < preferred) low = mid;
551
+ else high = mid;
552
+ else if (mid < preferred) high = mid;
553
+ else low = mid;
554
+ }
555
+ const yLow = cachedLuminance(h, s, low);
556
+ const yHigh = cachedLuminance(h, s, high);
557
+ const crLow = contrastRatioFromLuminance(yLow, yBase);
558
+ const crHigh = contrastRatioFromLuminance(yHigh, yBase);
559
+ const lowPasses = crLow >= target;
560
+ const highPasses = crHigh >= target;
561
+ if (lowPasses && highPasses) {
562
+ if (Math.abs(low - preferred) <= Math.abs(high - preferred)) return {
563
+ lightness: low,
564
+ contrast: crLow,
565
+ met: true
566
+ };
567
+ return {
568
+ lightness: high,
569
+ contrast: crHigh,
570
+ met: true
571
+ };
572
+ }
573
+ if (lowPasses) return {
574
+ lightness: low,
575
+ contrast: crLow,
576
+ met: true
577
+ };
578
+ if (highPasses) return {
579
+ lightness: high,
580
+ contrast: crHigh,
581
+ met: true
582
+ };
583
+ return coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter);
584
+ }
585
+ /**
586
+ * Fallback coarse scan when binary search is unstable near gamut edges.
587
+ */
588
+ function coarseScan(h, s, lo, hi, yBase, target, epsilon, maxIter) {
589
+ const STEPS = 64;
590
+ const step = (hi - lo) / STEPS;
591
+ let bestL = lo;
592
+ let bestCr = 0;
593
+ let bestMet = false;
594
+ for (let i = 0; i <= STEPS; i++) {
595
+ const l = lo + step * i;
596
+ const cr = contrastRatioFromLuminance(cachedLuminance(h, s, l), yBase);
597
+ if (cr >= target && !bestMet) {
598
+ bestL = l;
599
+ bestCr = cr;
600
+ bestMet = true;
601
+ } else if (cr >= target && bestMet) {
602
+ bestL = l;
603
+ bestCr = cr;
604
+ } else if (!bestMet && cr > bestCr) {
605
+ bestL = l;
606
+ bestCr = cr;
607
+ }
608
+ }
609
+ if (bestMet && bestL > lo + step) {
610
+ let rLo = bestL - step;
611
+ let rHi = bestL;
612
+ for (let i = 0; i < maxIter; i++) {
613
+ if (rHi - rLo < epsilon) break;
614
+ const mid = (rLo + rHi) / 2;
615
+ const cr = contrastRatioFromLuminance(cachedLuminance(h, s, mid), yBase);
616
+ if (cr >= target) {
617
+ rHi = mid;
618
+ bestL = mid;
619
+ bestCr = cr;
620
+ } else rLo = mid;
621
+ }
622
+ }
623
+ return {
624
+ lightness: bestL,
625
+ contrast: bestCr,
626
+ met: bestMet
627
+ };
628
+ }
629
+ /**
630
+ * Find the OKHSL lightness that satisfies a WCAG 2 contrast target
631
+ * against a base color, staying as close to `preferredLightness` as possible.
632
+ */
633
+ function findLightnessForContrast(options) {
634
+ const { hue, saturation, preferredLightness, baseLinearRgb, ensureContrast: ensureContrastInput, lightnessRange = [0, 1], epsilon = 1e-4, maxIterations = 14 } = options;
635
+ const target = resolveMinContrast(ensureContrastInput);
636
+ const yBase = relativeLuminanceFromLinearRgb(baseLinearRgb);
637
+ const crPref = contrastRatioFromLuminance(cachedLuminance(hue, saturation, preferredLightness), yBase);
638
+ if (crPref >= target) return {
639
+ lightness: preferredLightness,
640
+ contrast: crPref,
641
+ met: true,
642
+ branch: "preferred"
643
+ };
644
+ const [minL, maxL] = lightnessRange;
645
+ const darkerResult = preferredLightness > minL ? searchBranch(hue, saturation, minL, preferredLightness, yBase, target, epsilon, maxIterations, preferredLightness) : null;
646
+ const lighterResult = preferredLightness < maxL ? searchBranch(hue, saturation, preferredLightness, maxL, yBase, target, epsilon, maxIterations, preferredLightness) : null;
647
+ const darkerPasses = darkerResult?.met ?? false;
648
+ const lighterPasses = lighterResult?.met ?? false;
649
+ if (darkerPasses && lighterPasses) {
650
+ if (Math.abs(darkerResult.lightness - preferredLightness) <= Math.abs(lighterResult.lightness - preferredLightness)) return {
651
+ ...darkerResult,
652
+ branch: "darker"
653
+ };
654
+ return {
655
+ ...lighterResult,
656
+ branch: "lighter"
657
+ };
658
+ }
659
+ if (darkerPasses) return {
660
+ ...darkerResult,
661
+ branch: "darker"
662
+ };
663
+ if (lighterPasses) return {
664
+ ...lighterResult,
665
+ branch: "lighter"
666
+ };
667
+ const candidates = [];
668
+ if (darkerResult) candidates.push({
669
+ ...darkerResult,
670
+ branch: "darker"
671
+ });
672
+ if (lighterResult) candidates.push({
673
+ ...lighterResult,
674
+ branch: "lighter"
675
+ });
676
+ if (candidates.length === 0) return {
677
+ lightness: preferredLightness,
678
+ contrast: crPref,
679
+ met: false,
680
+ branch: "preferred"
681
+ };
682
+ candidates.sort((a, b) => b.contrast - a.contrast);
683
+ return candidates[0];
684
+ }
685
+
686
+ //#endregion
687
+ //#region src/glaze.ts
688
+ /**
689
+ * Glaze — OKHSL-based color theme generator.
690
+ *
691
+ * Generates robust light, dark, and high-contrast colors from a hue/saturation
692
+ * seed, preserving contrast for UI pairs via explicit dependencies.
693
+ */
694
+ let globalConfig = {
695
+ darkLightness: [10, 90],
696
+ darkDesaturation: .1,
697
+ states: {
698
+ dark: "@dark",
699
+ highContrast: "@high-contrast"
700
+ },
701
+ modes: {
702
+ dark: true,
703
+ highContrast: false
704
+ }
705
+ };
706
+ function pairNormal(p) {
707
+ return Array.isArray(p) ? p[0] : p;
708
+ }
709
+ function pairHC(p) {
710
+ return Array.isArray(p) ? p[1] : p;
711
+ }
712
+ function validateColorDefs(defs) {
713
+ const names = new Set(Object.keys(defs));
714
+ for (const [name, def] of Object.entries(defs)) {
715
+ if (def.contrast !== void 0 && !def.base) throw new Error(`glaze: color "${name}" has "contrast" without "base".`);
716
+ if (def.l !== void 0 && def.base !== void 0) console.warn(`glaze: color "${name}" has both "l" and "base". "l" takes precedence.`);
717
+ if (def.base && !names.has(def.base)) throw new Error(`glaze: color "${name}" references non-existent base "${def.base}".`);
718
+ if (def.l === void 0 && def.base === void 0) throw new Error(`glaze: color "${name}" must have either "l" (root) or "base" + "contrast" (dependent).`);
719
+ }
720
+ const visited = /* @__PURE__ */ new Set();
721
+ const inStack = /* @__PURE__ */ new Set();
722
+ function dfs(name) {
723
+ if (inStack.has(name)) throw new Error(`glaze: circular base reference detected involving "${name}".`);
724
+ if (visited.has(name)) return;
725
+ inStack.add(name);
726
+ const def = defs[name];
727
+ if (def.base && def.l === void 0) dfs(def.base);
728
+ inStack.delete(name);
729
+ visited.add(name);
730
+ }
731
+ for (const name of names) dfs(name);
732
+ }
733
+ function topoSort(defs) {
734
+ const result = [];
735
+ const visited = /* @__PURE__ */ new Set();
736
+ function visit(name) {
737
+ if (visited.has(name)) return;
738
+ visited.add(name);
739
+ const def = defs[name];
740
+ if (def.base && def.l === void 0) visit(def.base);
741
+ result.push(name);
742
+ }
743
+ for (const name of Object.keys(defs)) visit(name);
744
+ return result;
745
+ }
746
+ function mapLightnessDark(l, mode) {
747
+ if (mode === "static") return l;
748
+ const [lo, hi] = globalConfig.darkLightness;
749
+ if (mode === "fixed") return l * (hi - lo) / 100 + lo;
750
+ return (100 - l) * (hi - lo) / 100 + lo;
751
+ }
752
+ function mapSaturationDark(s, mode) {
753
+ if (mode === "static") return s;
754
+ return s * (1 - globalConfig.darkDesaturation);
755
+ }
756
+ /**
757
+ * Resolve the effective lightness from a contrast delta.
758
+ */
759
+ function resolveContrastLightness(baseLightness, contrast) {
760
+ if (contrast < 0) return clamp(baseLightness + contrast, 0, 100);
761
+ const candidate = baseLightness + contrast;
762
+ if (candidate > 100) return clamp(baseLightness - contrast, 0, 100);
763
+ return clamp(candidate, 0, 100);
764
+ }
765
+ function clamp(v, min, max) {
766
+ return Math.max(min, Math.min(max, v));
767
+ }
768
+ function resolveRootColor(_name, def, _ctx, isHighContrast) {
769
+ const rawL = def.l;
770
+ return {
771
+ lightL: clamp(isHighContrast ? pairHC(rawL) : pairNormal(rawL), 0, 100),
772
+ sat: clamp(def.sat ?? 1, 0, 1)
773
+ };
774
+ }
775
+ function resolveDependentColor(name, def, ctx, isHighContrast, isDark) {
776
+ const baseName = def.base;
777
+ const baseResolved = ctx.resolved.get(baseName);
778
+ if (!baseResolved) throw new Error(`glaze: base "${baseName}" not yet resolved for "${name}".`);
779
+ const mode = def.mode ?? "auto";
780
+ const sat = clamp(def.sat ?? 1, 0, 1);
781
+ let baseL;
782
+ if (isDark && isHighContrast) baseL = baseResolved.darkContrast.l * 100;
783
+ else if (isDark) baseL = baseResolved.dark.l * 100;
784
+ else if (isHighContrast) baseL = baseResolved.lightContrast.l * 100;
785
+ else baseL = baseResolved.light.l * 100;
786
+ const rawContrast = def.contrast ?? 0;
787
+ let contrast = isHighContrast ? pairHC(rawContrast) : pairNormal(rawContrast);
788
+ if (isDark && mode === "auto") contrast = -contrast;
789
+ const preferredL = resolveContrastLightness(baseL, contrast);
790
+ const rawEnsureContrast = def.ensureContrast;
791
+ if (rawEnsureContrast !== void 0) {
792
+ const minCr = isHighContrast ? pairHC(rawEnsureContrast) : pairNormal(rawEnsureContrast);
793
+ const effectiveSat = isDark ? mapSaturationDark(sat * ctx.saturation / 100, mode) : sat * ctx.saturation / 100;
794
+ let baseH;
795
+ let baseS;
796
+ let baseLNorm;
797
+ if (isDark && isHighContrast) {
798
+ baseH = baseResolved.darkContrast.h;
799
+ baseS = baseResolved.darkContrast.s;
800
+ baseLNorm = baseResolved.darkContrast.l;
801
+ } else if (isDark) {
802
+ baseH = baseResolved.dark.h;
803
+ baseS = baseResolved.dark.s;
804
+ baseLNorm = baseResolved.dark.l;
805
+ } else if (isHighContrast) {
806
+ baseH = baseResolved.lightContrast.h;
807
+ baseS = baseResolved.lightContrast.s;
808
+ baseLNorm = baseResolved.lightContrast.l;
809
+ } else {
810
+ baseH = baseResolved.light.h;
811
+ baseS = baseResolved.light.s;
812
+ baseLNorm = baseResolved.light.l;
813
+ }
814
+ const baseLinearRgb = okhslToLinearSrgb(baseH, baseS, baseLNorm);
815
+ return {
816
+ l: findLightnessForContrast({
817
+ hue: ctx.hue,
818
+ saturation: effectiveSat,
819
+ preferredLightness: preferredL / 100,
820
+ baseLinearRgb,
821
+ ensureContrast: minCr
822
+ }).lightness * 100,
823
+ sat
824
+ };
825
+ }
826
+ return {
827
+ l: clamp(preferredL, 0, 100),
828
+ sat
829
+ };
830
+ }
831
+ function resolveColorForScheme(name, def, ctx, isDark, isHighContrast) {
832
+ const mode = def.mode ?? "auto";
833
+ const isRoot = def.l !== void 0;
834
+ let lightL;
835
+ let sat;
836
+ if (isRoot) {
837
+ const root = resolveRootColor(name, def, ctx, isHighContrast);
838
+ lightL = root.lightL;
839
+ sat = root.sat;
840
+ } else {
841
+ const dep = resolveDependentColor(name, def, ctx, isHighContrast, isDark);
842
+ lightL = dep.l;
843
+ sat = dep.sat;
844
+ }
845
+ let finalL;
846
+ let finalSat;
847
+ if (isDark && isRoot) {
848
+ finalL = mapLightnessDark(lightL, mode);
849
+ finalSat = mapSaturationDark(sat * ctx.saturation / 100, mode);
850
+ } else if (isDark && !isRoot) {
851
+ finalL = lightL;
852
+ finalSat = mapSaturationDark(sat * ctx.saturation / 100, mode);
853
+ } else {
854
+ finalL = lightL;
855
+ finalSat = sat * ctx.saturation / 100;
856
+ }
857
+ return {
858
+ h: ctx.hue,
859
+ s: clamp(finalSat, 0, 1),
860
+ l: clamp(finalL / 100, 0, 1)
861
+ };
862
+ }
863
+ function resolveAllColors(hue, saturation, defs) {
864
+ validateColorDefs(defs);
865
+ const order = topoSort(defs);
866
+ const ctx = {
867
+ hue,
868
+ saturation,
869
+ defs,
870
+ resolved: /* @__PURE__ */ new Map()
871
+ };
872
+ const lightMap = /* @__PURE__ */ new Map();
873
+ for (const name of order) {
874
+ const variant = resolveColorForScheme(name, defs[name], ctx, false, false);
875
+ lightMap.set(name, variant);
876
+ ctx.resolved.set(name, {
877
+ name,
878
+ light: variant,
879
+ dark: variant,
880
+ lightContrast: variant,
881
+ darkContrast: variant,
882
+ mode: defs[name].mode ?? "auto"
883
+ });
884
+ }
885
+ const lightHCMap = /* @__PURE__ */ new Map();
886
+ for (const name of order) ctx.resolved.set(name, {
887
+ ...ctx.resolved.get(name),
888
+ lightContrast: lightMap.get(name)
889
+ });
890
+ for (const name of order) {
891
+ const variant = resolveColorForScheme(name, defs[name], ctx, false, true);
892
+ lightHCMap.set(name, variant);
893
+ ctx.resolved.set(name, {
894
+ ...ctx.resolved.get(name),
895
+ lightContrast: variant
896
+ });
897
+ }
898
+ const darkMap = /* @__PURE__ */ new Map();
899
+ for (const name of order) ctx.resolved.set(name, {
900
+ name,
901
+ light: lightMap.get(name),
902
+ dark: lightMap.get(name),
903
+ lightContrast: lightHCMap.get(name),
904
+ darkContrast: lightHCMap.get(name),
905
+ mode: defs[name].mode ?? "auto"
906
+ });
907
+ for (const name of order) {
908
+ const variant = resolveColorForScheme(name, defs[name], ctx, true, false);
909
+ darkMap.set(name, variant);
910
+ ctx.resolved.set(name, {
911
+ ...ctx.resolved.get(name),
912
+ dark: variant
913
+ });
914
+ }
915
+ const darkHCMap = /* @__PURE__ */ new Map();
916
+ for (const name of order) ctx.resolved.set(name, {
917
+ ...ctx.resolved.get(name),
918
+ darkContrast: darkMap.get(name)
919
+ });
920
+ for (const name of order) {
921
+ const variant = resolveColorForScheme(name, defs[name], ctx, true, true);
922
+ darkHCMap.set(name, variant);
923
+ ctx.resolved.set(name, {
924
+ ...ctx.resolved.get(name),
925
+ darkContrast: variant
926
+ });
927
+ }
928
+ const result = /* @__PURE__ */ new Map();
929
+ for (const name of order) result.set(name, {
930
+ name,
931
+ light: lightMap.get(name),
932
+ dark: darkMap.get(name),
933
+ lightContrast: lightHCMap.get(name),
934
+ darkContrast: darkHCMap.get(name),
935
+ mode: defs[name].mode ?? "auto"
936
+ });
937
+ return result;
938
+ }
939
+ const formatters = {
940
+ okhsl: formatOkhsl,
941
+ rgb: formatRgb,
942
+ hsl: formatHsl,
943
+ oklch: formatOklch
944
+ };
945
+ function formatVariant(v, format = "okhsl") {
946
+ return formatters[format](v.h, v.s * 100, v.l * 100);
947
+ }
948
+ function resolveModes(override) {
949
+ return {
950
+ dark: override?.dark ?? globalConfig.modes.dark,
951
+ highContrast: override?.highContrast ?? globalConfig.modes.highContrast
952
+ };
953
+ }
954
+ function buildTokenMap(resolved, prefix, states, modes, format = "okhsl") {
955
+ const tokens = {};
956
+ for (const [name, color] of resolved) {
957
+ const key = `#${prefix}${name}`;
958
+ const entry = { "": formatVariant(color.light, format) };
959
+ if (modes.dark) entry[states.dark] = formatVariant(color.dark, format);
960
+ if (modes.highContrast) entry[states.highContrast] = formatVariant(color.lightContrast, format);
961
+ if (modes.dark && modes.highContrast) entry[`${states.dark} & ${states.highContrast}`] = formatVariant(color.darkContrast, format);
962
+ tokens[key] = entry;
963
+ }
964
+ return tokens;
965
+ }
966
+ function buildJsonMap(resolved, modes, format = "okhsl") {
967
+ const result = {};
968
+ for (const [name, color] of resolved) {
969
+ const entry = { light: formatVariant(color.light, format) };
970
+ if (modes.dark) entry.dark = formatVariant(color.dark, format);
971
+ if (modes.highContrast) entry.lightContrast = formatVariant(color.lightContrast, format);
972
+ if (modes.dark && modes.highContrast) entry.darkContrast = formatVariant(color.darkContrast, format);
973
+ result[name] = entry;
974
+ }
975
+ return result;
976
+ }
977
+ function createTheme(hue, saturation, initialColors) {
978
+ let colorDefs = initialColors ? { ...initialColors } : {};
979
+ return {
980
+ get hue() {
981
+ return hue;
982
+ },
983
+ get saturation() {
984
+ return saturation;
985
+ },
986
+ colors(defs) {
987
+ colorDefs = {
988
+ ...colorDefs,
989
+ ...defs
990
+ };
991
+ },
992
+ color(name, def) {
993
+ if (def === void 0) return colorDefs[name];
994
+ colorDefs[name] = def;
995
+ },
996
+ remove(names) {
997
+ const list = Array.isArray(names) ? names : [names];
998
+ for (const name of list) delete colorDefs[name];
999
+ },
1000
+ has(name) {
1001
+ return name in colorDefs;
1002
+ },
1003
+ list() {
1004
+ return Object.keys(colorDefs);
1005
+ },
1006
+ reset() {
1007
+ colorDefs = {};
1008
+ },
1009
+ export() {
1010
+ return {
1011
+ hue,
1012
+ saturation,
1013
+ colors: { ...colorDefs }
1014
+ };
1015
+ },
1016
+ extend(options) {
1017
+ return createTheme(options.hue ?? hue, options.saturation ?? saturation, options.colors ? {
1018
+ ...colorDefs,
1019
+ ...options.colors
1020
+ } : { ...colorDefs });
1021
+ },
1022
+ resolve() {
1023
+ return resolveAllColors(hue, saturation, colorDefs);
1024
+ },
1025
+ tokens(options) {
1026
+ return buildTokenMap(resolveAllColors(hue, saturation, colorDefs), "", {
1027
+ dark: options?.states?.dark ?? globalConfig.states.dark,
1028
+ highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
1029
+ }, resolveModes(options?.modes), options?.format);
1030
+ },
1031
+ json(options) {
1032
+ return buildJsonMap(resolveAllColors(hue, saturation, colorDefs), resolveModes(options?.modes), options?.format);
1033
+ }
1034
+ };
1035
+ }
1036
+ function createPalette(themes) {
1037
+ return {
1038
+ tokens(options) {
1039
+ const states = {
1040
+ dark: options?.states?.dark ?? globalConfig.states.dark,
1041
+ highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
1042
+ };
1043
+ const modes = resolveModes(options?.modes);
1044
+ const allTokens = {};
1045
+ for (const [themeName, theme] of Object.entries(themes)) {
1046
+ const resolved = theme.resolve();
1047
+ let prefix = "";
1048
+ if (options?.prefix === true) prefix = `${themeName}-`;
1049
+ else if (typeof options?.prefix === "object" && options.prefix !== null) prefix = options.prefix[themeName] ?? `${themeName}-`;
1050
+ const tokens = buildTokenMap(resolved, prefix, states, modes, options?.format);
1051
+ Object.assign(allTokens, tokens);
1052
+ }
1053
+ return allTokens;
1054
+ },
1055
+ json(options) {
1056
+ const modes = resolveModes(options?.modes);
1057
+ const result = {};
1058
+ for (const [themeName, theme] of Object.entries(themes)) result[themeName] = buildJsonMap(theme.resolve(), modes, options?.format);
1059
+ return result;
1060
+ }
1061
+ };
1062
+ }
1063
+ function createColorToken(input) {
1064
+ const defs = { __color__: {
1065
+ l: input.l,
1066
+ sat: input.sat,
1067
+ mode: input.mode
1068
+ } };
1069
+ return {
1070
+ resolve() {
1071
+ return resolveAllColors(input.hue, input.saturation, defs).get("__color__");
1072
+ },
1073
+ token(options) {
1074
+ return buildTokenMap(resolveAllColors(input.hue, input.saturation, defs), "", {
1075
+ dark: options?.states?.dark ?? globalConfig.states.dark,
1076
+ highContrast: options?.states?.highContrast ?? globalConfig.states.highContrast
1077
+ }, resolveModes(options?.modes), options?.format)["#__color__"];
1078
+ },
1079
+ json(options) {
1080
+ return buildJsonMap(resolveAllColors(input.hue, input.saturation, defs), resolveModes(options?.modes), options?.format)["__color__"];
1081
+ }
1082
+ };
1083
+ }
1084
+ /**
1085
+ * Create a single-hue glaze theme.
1086
+ *
1087
+ * @example
1088
+ * ```ts
1089
+ * const primary = glaze({ hue: 280, saturation: 80 });
1090
+ * // or shorthand:
1091
+ * const primary = glaze(280, 80);
1092
+ * ```
1093
+ */
1094
+ function glaze(hueOrOptions, saturation) {
1095
+ if (typeof hueOrOptions === "number") return createTheme(hueOrOptions, saturation ?? 100);
1096
+ return createTheme(hueOrOptions.hue, hueOrOptions.saturation);
1097
+ }
1098
+ /**
1099
+ * Configure global glaze settings.
1100
+ */
1101
+ glaze.configure = function configure(config) {
1102
+ globalConfig = {
1103
+ darkLightness: config.darkLightness ?? globalConfig.darkLightness,
1104
+ darkDesaturation: config.darkDesaturation ?? globalConfig.darkDesaturation,
1105
+ states: {
1106
+ dark: config.states?.dark ?? globalConfig.states.dark,
1107
+ highContrast: config.states?.highContrast ?? globalConfig.states.highContrast
1108
+ },
1109
+ modes: {
1110
+ dark: config.modes?.dark ?? globalConfig.modes.dark,
1111
+ highContrast: config.modes?.highContrast ?? globalConfig.modes.highContrast
1112
+ }
1113
+ };
1114
+ };
1115
+ /**
1116
+ * Compose multiple themes into a palette.
1117
+ */
1118
+ glaze.palette = function palette(themes) {
1119
+ return createPalette(themes);
1120
+ };
1121
+ /**
1122
+ * Create a theme from a serialized export.
1123
+ */
1124
+ glaze.from = function from(data) {
1125
+ return createTheme(data.hue, data.saturation, data.colors);
1126
+ };
1127
+ /**
1128
+ * Create a standalone single-color token.
1129
+ */
1130
+ glaze.color = function color(input) {
1131
+ return createColorToken(input);
1132
+ };
1133
+ /**
1134
+ * Create a theme from a hex color string.
1135
+ * Extracts hue and saturation from the color.
1136
+ */
1137
+ glaze.fromHex = function fromHex(hex) {
1138
+ const rgb = parseHex(hex);
1139
+ if (!rgb) throw new Error(`glaze: invalid hex color "${hex}".`);
1140
+ const [h, s] = srgbToOkhsl(rgb);
1141
+ return createTheme(h, s * 100);
1142
+ };
1143
+ /**
1144
+ * Create a theme from RGB values (0–255).
1145
+ * Extracts hue and saturation from the color.
1146
+ */
1147
+ glaze.fromRgb = function fromRgb(r, g, b) {
1148
+ const [h, s] = srgbToOkhsl([
1149
+ r / 255,
1150
+ g / 255,
1151
+ b / 255
1152
+ ]);
1153
+ return createTheme(h, s * 100);
1154
+ };
1155
+ /**
1156
+ * Get the current global configuration (for testing/debugging).
1157
+ */
1158
+ glaze.getConfig = function getConfig() {
1159
+ return { ...globalConfig };
1160
+ };
1161
+ /**
1162
+ * Reset global configuration to defaults.
1163
+ */
1164
+ glaze.resetConfig = function resetConfig() {
1165
+ globalConfig = {
1166
+ darkLightness: [10, 90],
1167
+ darkDesaturation: .1,
1168
+ states: {
1169
+ dark: "@dark",
1170
+ highContrast: "@high-contrast"
1171
+ },
1172
+ modes: {
1173
+ dark: true,
1174
+ highContrast: false
1175
+ }
1176
+ };
1177
+ };
1178
+
1179
+ //#endregion
1180
+ export { contrastRatioFromLuminance, findLightnessForContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
1181
+ //# sourceMappingURL=index.mjs.map