@designtools/tokens 0.1.1

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,1500 @@
1
+ // src/oklch.ts
2
+ import {
3
+ formatHex,
4
+ formatRgb,
5
+ formatHsl,
6
+ converter,
7
+ filterDeficiencyProt,
8
+ filterDeficiencyDeuter,
9
+ filterDeficiencyTrit,
10
+ parse
11
+ } from "culori";
12
+ import { APCAcontrast, sRGBtoY } from "apca-w3";
13
+ var toOklch = converter("oklch");
14
+ var toRgbConverter = converter("rgb");
15
+ var toHslConverter = converter("hsl");
16
+ function parseToOklch(color) {
17
+ const result = toOklch(color);
18
+ if (!result) {
19
+ throw new Error(`Invalid color: ${color}`);
20
+ }
21
+ return result;
22
+ }
23
+ function oklchToHex(color) {
24
+ const hex = formatHex(color);
25
+ return hex || "#000000";
26
+ }
27
+ function computeScaleChroma(scaleValue, baseChroma) {
28
+ if (scaleValue < 0.03) {
29
+ const t2 = scaleValue / 0.03;
30
+ return baseChroma * (0.08 + 0.15 * Math.pow(t2, 1.5));
31
+ }
32
+ if (scaleValue < 0.12) {
33
+ const t2 = (scaleValue - 0.03) / 0.09;
34
+ return baseChroma * (0.23 + 0.32 * Math.pow(t2, 1.3));
35
+ }
36
+ if (scaleValue < 0.28) {
37
+ const t2 = (scaleValue - 0.12) / 0.16;
38
+ return baseChroma * (0.55 + 0.3 * Math.pow(t2, 1.1));
39
+ }
40
+ if (scaleValue < 0.45) {
41
+ const t2 = (scaleValue - 0.28) / 0.17;
42
+ return baseChroma * (0.85 + 0.15 * t2);
43
+ }
44
+ if (scaleValue < 0.85) {
45
+ return baseChroma;
46
+ }
47
+ const t = (scaleValue - 0.85) / 0.15;
48
+ return baseChroma * (1 - 0.06 * t);
49
+ }
50
+ function generateColorScale(baseColor) {
51
+ const base = parseToOklch(baseColor);
52
+ const baseLightness = base.l;
53
+ const baseChroma = base.c || 0;
54
+ const baseHue = base.h || 0;
55
+ const isAchromatic = baseChroma < 0.01;
56
+ let lightnessSteps;
57
+ const maxLightness = 0.98;
58
+ const minLightness = 0.15;
59
+ const headroomUp = maxLightness - baseLightness;
60
+ const headroomDown = baseLightness - minLightness;
61
+ if (isAchromatic) {
62
+ lightnessSteps = {
63
+ 25: baseLightness + headroomUp * 0.98,
64
+ // Nearly white
65
+ 50: baseLightness + headroomUp * 0.94,
66
+ // Very light
67
+ 100: baseLightness + headroomUp * 0.88,
68
+ // Very light
69
+ 200: baseLightness + headroomUp * 0.78,
70
+ // Light
71
+ 300: baseLightness + headroomUp * 0.64,
72
+ // Light
73
+ 400: baseLightness + headroomUp * 0.32,
74
+ // Medium-light
75
+ 500: baseLightness,
76
+ // Exact match
77
+ 600: baseLightness - headroomDown * 0.3,
78
+ // Medium-dark
79
+ 700: baseLightness - headroomDown * 0.48,
80
+ // Dark
81
+ 800: baseLightness - headroomDown * 0.66,
82
+ // Dark
83
+ 900: baseLightness - headroomDown * 0.82,
84
+ // Very dark
85
+ 950: baseLightness - headroomDown * 0.92
86
+ // Extremely dark
87
+ };
88
+ } else {
89
+ lightnessSteps = {
90
+ 25: baseLightness + headroomUp * 0.98,
91
+ // Nearly white (subtle tint)
92
+ 50: baseLightness + headroomUp * 0.93,
93
+ // Very light background
94
+ 100: baseLightness + headroomUp * 0.86,
95
+ // Light background
96
+ 200: baseLightness + headroomUp * 0.71,
97
+ // Soft background
98
+ 300: baseLightness + headroomUp * 0.6,
99
+ // UI element background
100
+ 400: baseLightness + headroomUp * 0.38,
101
+ // Hover state
102
+ 500: baseLightness,
103
+ // Base color
104
+ 600: baseLightness - headroomDown * 0.2,
105
+ // Active/pressed state (smaller step)
106
+ 700: baseLightness - headroomDown * 0.38,
107
+ // Borders
108
+ 800: baseLightness - headroomDown * 0.56,
109
+ // Solid backgrounds
110
+ 900: baseLightness - headroomDown * 0.82,
111
+ // High contrast text
112
+ 950: baseLightness - headroomDown * 0.95
113
+ // Highest contrast
114
+ };
115
+ }
116
+ const scale = {};
117
+ for (const [shade, targetLightness] of Object.entries(lightnessSteps)) {
118
+ const shadeNum = parseInt(shade);
119
+ if (shadeNum === 500) {
120
+ scale[500] = getOklchString(baseColor);
121
+ continue;
122
+ }
123
+ const lightness = Math.max(0.15, Math.min(0.98, targetLightness));
124
+ const normalizedPosition = (shadeNum - 25) / 925;
125
+ let chroma = baseChroma;
126
+ if (!isAchromatic) {
127
+ chroma = computeScaleChroma(normalizedPosition, baseChroma);
128
+ }
129
+ const hue = baseHue;
130
+ const shadeColor = {
131
+ mode: "oklch",
132
+ l: lightness,
133
+ c: Math.max(0, chroma),
134
+ // Ensure chroma doesn't go negative
135
+ h: hue
136
+ };
137
+ const l = (shadeColor.l * 100).toFixed(2) + "%";
138
+ const c = shadeColor.c.toFixed(4);
139
+ const h = (shadeColor.h || 0).toFixed(2);
140
+ scale[shadeNum] = `oklch(${l} ${c} ${h})`;
141
+ }
142
+ return scale;
143
+ }
144
+ function adjustLightness(color, lightness) {
145
+ const oklchColor = parseToOklch(color);
146
+ return oklchToHex({
147
+ ...oklchColor,
148
+ l: Math.max(0, Math.min(1, lightness))
149
+ });
150
+ }
151
+ function adjustChroma(color, chroma) {
152
+ const oklchColor = parseToOklch(color);
153
+ return oklchToHex({
154
+ ...oklchColor,
155
+ c: Math.max(0, chroma)
156
+ });
157
+ }
158
+ function toRgb(color) {
159
+ const rgb = toRgbConverter(color);
160
+ if (!rgb || !("r" in rgb) || !("g" in rgb) || !("b" in rgb)) {
161
+ throw new Error(`Cannot convert ${color} to RGB`);
162
+ }
163
+ return {
164
+ r: rgb.r,
165
+ g: rgb.g,
166
+ b: rgb.b
167
+ };
168
+ }
169
+ function getRelativeLuminance(color) {
170
+ const rgb = toRgb(color);
171
+ const rsRGB = rgb.r;
172
+ const gsRGB = rgb.g;
173
+ const bsRGB = rgb.b;
174
+ const r = rsRGB <= 0.03928 ? rsRGB / 12.92 : Math.pow((rsRGB + 0.055) / 1.055, 2.4);
175
+ const g = gsRGB <= 0.03928 ? gsRGB / 12.92 : Math.pow((gsRGB + 0.055) / 1.055, 2.4);
176
+ const b = bsRGB <= 0.03928 ? bsRGB / 12.92 : Math.pow((bsRGB + 0.055) / 1.055, 2.4);
177
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
178
+ }
179
+ function getContrastRatio(color1, color2) {
180
+ const lum1 = getRelativeLuminance(color1);
181
+ const lum2 = getRelativeLuminance(color2);
182
+ const lighter = Math.max(lum1, lum2);
183
+ const darker = Math.min(lum1, lum2);
184
+ return (lighter + 0.05) / (darker + 0.05);
185
+ }
186
+ function meetsWCAG_AA(ratio, isLargeText = false) {
187
+ return isLargeText ? ratio >= 3 : ratio >= 4.5;
188
+ }
189
+ function meetsWCAG_AAA(ratio, isLargeText = false) {
190
+ return isLargeText ? ratio >= 4.5 : ratio >= 7;
191
+ }
192
+ function getAPCAContrast(foreground, background) {
193
+ const fgRgb = toRgb(foreground);
194
+ const bgRgb = toRgb(background);
195
+ const fgY = sRGBtoY([fgRgb.r * 255, fgRgb.g * 255, fgRgb.b * 255]);
196
+ const bgY = sRGBtoY([bgRgb.r * 255, bgRgb.g * 255, bgRgb.b * 255]);
197
+ return Math.abs(APCAcontrast(fgY, bgY));
198
+ }
199
+ function findContrastingColorAPCA(background, lightColor, darkColor) {
200
+ const lightLc = getAPCAContrast(lightColor, background);
201
+ const darkLc = getAPCAContrast(darkColor, background);
202
+ return darkLc > lightLc ? darkColor : lightColor;
203
+ }
204
+ var cvdFilters = {
205
+ protanopia: filterDeficiencyProt(1),
206
+ deuteranopia: filterDeficiencyDeuter(1),
207
+ tritanopia: filterDeficiencyTrit(1)
208
+ };
209
+ function simulateCVD(color, cvd) {
210
+ if (cvd === "none") return color;
211
+ const parsed = parse(color);
212
+ if (!parsed) return color;
213
+ const simulated = cvdFilters[cvd](parsed);
214
+ return formatHex(simulated) || color;
215
+ }
216
+ function classifyAPCALevel(lcValue) {
217
+ if (lcValue >= 90) return "Fluent";
218
+ if (lcValue >= 75) return "Body text";
219
+ if (lcValue >= 60) return "Large text";
220
+ if (lcValue >= 45) return "Minimum";
221
+ return "Fail";
222
+ }
223
+ function generateColorScaleAPCA(baseColor) {
224
+ const base = parseToOklch(baseColor);
225
+ const baseLightness = base.l;
226
+ const baseChroma = base.c || 0;
227
+ const baseHue = base.h || 0;
228
+ const isAchromatic = baseChroma < 0.01;
229
+ const maxLightness = 0.98;
230
+ const minLightness = 0.15;
231
+ const headroomUp = maxLightness - baseLightness;
232
+ const headroomDown = baseLightness - minLightness;
233
+ let lightnessSteps;
234
+ if (isAchromatic) {
235
+ lightnessSteps = {
236
+ 25: baseLightness + headroomUp * 0.98,
237
+ 50: baseLightness + headroomUp * 0.95,
238
+ 100: baseLightness + headroomUp * 0.9,
239
+ 200: baseLightness + headroomUp * 0.8,
240
+ 300: baseLightness + headroomUp * 0.66,
241
+ 400: baseLightness + headroomUp * 0.38,
242
+ 500: baseLightness,
243
+ 600: baseLightness - headroomDown * 0.26,
244
+ 700: baseLightness - headroomDown * 0.44,
245
+ 800: baseLightness - headroomDown * 0.62,
246
+ 900: baseLightness - headroomDown * 0.8,
247
+ 950: baseLightness - headroomDown * 0.92
248
+ };
249
+ } else {
250
+ lightnessSteps = {
251
+ 25: baseLightness + headroomUp * 0.97,
252
+ 50: baseLightness + headroomUp * 0.92,
253
+ 100: baseLightness + headroomUp * 0.84,
254
+ 200: baseLightness + headroomUp * 0.68,
255
+ 300: baseLightness + headroomUp * 0.52,
256
+ 400: baseLightness + headroomUp * 0.3,
257
+ 500: baseLightness,
258
+ 600: baseLightness - headroomDown * 0.22,
259
+ 700: baseLightness - headroomDown * 0.4,
260
+ 800: baseLightness - headroomDown * 0.58,
261
+ 900: baseLightness - headroomDown * 0.82,
262
+ 950: baseLightness - headroomDown * 0.95
263
+ };
264
+ }
265
+ const scale = {};
266
+ for (const [shade, targetLightness] of Object.entries(lightnessSteps)) {
267
+ const shadeNum = parseInt(shade);
268
+ if (shadeNum === 500) {
269
+ scale[500] = getOklchString(baseColor);
270
+ continue;
271
+ }
272
+ const lightness = Math.max(0.15, Math.min(0.98, targetLightness));
273
+ const normalizedPosition = (shadeNum - 25) / 925;
274
+ let chroma = baseChroma;
275
+ if (!isAchromatic) {
276
+ chroma = computeScaleChromaPerceptual(normalizedPosition, baseChroma);
277
+ }
278
+ const shadeColor = {
279
+ mode: "oklch",
280
+ l: lightness,
281
+ c: Math.max(0, chroma),
282
+ h: baseHue
283
+ };
284
+ const l = (shadeColor.l * 100).toFixed(2) + "%";
285
+ const c = shadeColor.c.toFixed(4);
286
+ const h = (shadeColor.h || 0).toFixed(2);
287
+ scale[shadeNum] = `oklch(${l} ${c} ${h})`;
288
+ }
289
+ return scale;
290
+ }
291
+ function computeScaleChromaPerceptual(scaleValue, baseChroma) {
292
+ if (scaleValue < 0.03) {
293
+ const t2 = scaleValue / 0.03;
294
+ return baseChroma * (0.3 + 0.1 * t2);
295
+ }
296
+ if (scaleValue < 0.12) {
297
+ const t2 = (scaleValue - 0.03) / 0.09;
298
+ return baseChroma * (0.4 + 0.25 * t2);
299
+ }
300
+ if (scaleValue < 0.3) {
301
+ const t2 = (scaleValue - 0.12) / 0.18;
302
+ return baseChroma * (0.65 + 0.25 * t2);
303
+ }
304
+ if (scaleValue < 0.45) {
305
+ const t2 = (scaleValue - 0.3) / 0.15;
306
+ return baseChroma * (0.9 + 0.1 * t2);
307
+ }
308
+ if (scaleValue < 0.85) {
309
+ return baseChroma;
310
+ }
311
+ const t = (scaleValue - 0.85) / 0.15;
312
+ return baseChroma * (1 - 0.08 * t);
313
+ }
314
+ function formatColorShort(color, format) {
315
+ const full = formatColorAs(color, format);
316
+ const match = full.match(/^(?:oklch|rgb|hsl)\((.+)\)$/);
317
+ if (match) return match[1];
318
+ return full;
319
+ }
320
+ function getOklchString(color) {
321
+ const oklch = parseToOklch(color);
322
+ const l = (oklch.l * 100).toFixed(2) + "%";
323
+ const c = oklch.c.toFixed(4);
324
+ const h = (oklch.h || 0).toFixed(2);
325
+ return `oklch(${l} ${c} ${h})`;
326
+ }
327
+ function formatColorAs(color, format) {
328
+ switch (format) {
329
+ case "hex":
330
+ return formatHex(color) || "#000000";
331
+ case "rgb": {
332
+ const rgb = toRgbConverter(color);
333
+ if (!rgb) return "rgb(0, 0, 0)";
334
+ return formatRgb(rgb) || "rgb(0, 0, 0)";
335
+ }
336
+ case "hsl": {
337
+ const hsl = toHslConverter(color);
338
+ if (!hsl) return "hsl(0, 0%, 0%)";
339
+ return formatHsl(hsl) || "hsl(0, 0%, 0%)";
340
+ }
341
+ case "oklch":
342
+ default:
343
+ return getOklchString(color);
344
+ }
345
+ }
346
+
347
+ // src/primitives.ts
348
+ var DEFAULT_NEUTRAL_COLOR = "#747474";
349
+ function clamp(v, min, max) {
350
+ return Math.min(max, Math.max(min, v));
351
+ }
352
+ function deriveSuccessColor(primaryColor) {
353
+ const p = parseToOklch(primaryColor);
354
+ const pC = p.c || 0.1;
355
+ const success = {
356
+ mode: "oklch",
357
+ l: clamp(0.55 + pC * 0.5, 0.55, 0.72),
358
+ c: clamp(pC * 1.1, 0.09, 0.19),
359
+ h: 145
360
+ };
361
+ return oklchToHex(success);
362
+ }
363
+ function deriveDestructiveColor(primaryColor) {
364
+ const p = parseToOklch(primaryColor);
365
+ const pC = p.c || 0.1;
366
+ const destructive = {
367
+ mode: "oklch",
368
+ l: clamp(0.5 + pC * 0.3, 0.5, 0.6),
369
+ c: clamp(pC * 1.2, 0.12, 0.22),
370
+ h: 25
371
+ };
372
+ return oklchToHex(destructive);
373
+ }
374
+ function deriveWarningColor(primaryColor) {
375
+ const p = parseToOklch(primaryColor);
376
+ const pC = p.c || 0.1;
377
+ const warning = {
378
+ mode: "oklch",
379
+ l: clamp(0.7 + pC * 0.3, 0.72, 0.82),
380
+ c: clamp(pC * 1, 0.1, 0.17),
381
+ h: 75
382
+ };
383
+ return oklchToHex(warning);
384
+ }
385
+ function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed") {
386
+ const genScale = lightnessStrategy === "perceptual" ? generateColorScaleAPCA : generateColorScale;
387
+ const palette = {};
388
+ palette.black = "oklch(0 0 0)";
389
+ palette.white = "oklch(1 0 0)";
390
+ palette.primary = genScale(brandColors.primary);
391
+ palette.neutral = genScale(brandColors.neutral);
392
+ const successScale = genScale(deriveSuccessColor(brandColors.primary));
393
+ const destructiveScale = genScale(deriveDestructiveColor(brandColors.primary));
394
+ const warningScale = genScale(deriveWarningColor(brandColors.primary));
395
+ palette.success = successScale;
396
+ palette.destructive = destructiveScale;
397
+ palette.warning = warningScale;
398
+ palette.green = successScale;
399
+ palette.red = destructiveScale;
400
+ palette.yellow = warningScale;
401
+ if (brandColors.additionalColors) {
402
+ for (const [name, color] of Object.entries(brandColors.additionalColors)) {
403
+ palette[name] = genScale(color);
404
+ }
405
+ }
406
+ return palette;
407
+ }
408
+ function getShade(palette, colorName, shade) {
409
+ const scale = palette[colorName];
410
+ if (!scale) {
411
+ throw new Error(`Color "${colorName}" not found in palette`);
412
+ }
413
+ return scale[shade];
414
+ }
415
+
416
+ // src/semantic.ts
417
+ var SHADE_KEYS = [25, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
418
+ function getContrastThresholds(algorithm, target) {
419
+ if (algorithm === "apca") {
420
+ switch (target) {
421
+ case "aa-large":
422
+ return { floor: 45, target: 45 };
423
+ case "aa":
424
+ return { floor: 60, target: 75 };
425
+ case "aaa":
426
+ return { floor: 75, target: 90 };
427
+ }
428
+ } else {
429
+ switch (target) {
430
+ case "aa-large":
431
+ return { floor: 3, target: 3 };
432
+ case "aa":
433
+ return { floor: 4.5, target: 5.5 };
434
+ case "aaa":
435
+ return { floor: 7, target: 7 };
436
+ }
437
+ }
438
+ }
439
+ function findBestForeground(background, scale, neutralScale, white, black, algorithm, contrastTarget, colorName) {
440
+ const { floor, target } = getContrastThresholds(algorithm, contrastTarget);
441
+ const getContrast = (fg) => algorithm === "apca" ? getAPCAContrast(fg, background) : getContrastRatio(background, fg);
442
+ function searchScale(s, refColorName) {
443
+ let bestPassing = null;
444
+ let bestOverall = null;
445
+ for (const shade of SHADE_KEYS) {
446
+ const c = getContrast(s[shade]);
447
+ if (c >= floor) {
448
+ if (!bestPassing || Math.abs(c - target) < Math.abs(bestPassing.contrast - target)) {
449
+ bestPassing = { shade, contrast: c };
450
+ }
451
+ }
452
+ if (!bestOverall || c > bestOverall.contrast) {
453
+ bestOverall = { shade, contrast: c };
454
+ }
455
+ }
456
+ const pick = bestPassing ?? bestOverall;
457
+ if (!pick) return null;
458
+ return {
459
+ value: s[pick.shade],
460
+ ref: { type: "shade", colorName: refColorName, shade: pick.shade },
461
+ contrast: pick.contrast,
462
+ passes: pick === bestPassing
463
+ };
464
+ }
465
+ const ownResult = searchScale(scale, colorName);
466
+ if (ownResult?.passes) {
467
+ return { value: ownResult.value, ref: ownResult.ref };
468
+ }
469
+ const neutralResult = searchScale(neutralScale, "neutral");
470
+ if (neutralResult?.passes) {
471
+ if (!ownResult?.passes) {
472
+ return { value: neutralResult.value, ref: neutralResult.ref };
473
+ }
474
+ if (Math.abs(neutralResult.contrast - target) < Math.abs(ownResult.contrast - target)) {
475
+ return { value: neutralResult.value, ref: neutralResult.ref };
476
+ }
477
+ return { value: ownResult.value, ref: ownResult.ref };
478
+ }
479
+ if (ownResult && (!neutralResult || ownResult.contrast >= neutralResult.contrast)) {
480
+ return { value: ownResult.value, ref: ownResult.ref };
481
+ }
482
+ if (neutralResult) {
483
+ return { value: neutralResult.value, ref: neutralResult.ref };
484
+ }
485
+ const whiteC = getContrast(white);
486
+ const blackC = getContrast(black);
487
+ if (whiteC >= blackC) return { value: white, ref: { type: "white" } };
488
+ return { value: black, ref: { type: "black" } };
489
+ }
490
+ function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary") {
491
+ const findFg = (bg) => findBestForeground(bg, scale, neutral, white, black, contrastAlgorithm, contrastTarget, colorName);
492
+ const defaults = mode === "light" ? { DEFAULT: 500, subdued: 50, highlight: 700 } : { DEFAULT: 600, subdued: 200, highlight: 400 };
493
+ const defaultShade = overrides?.DEFAULT ?? defaults.DEFAULT;
494
+ const subduedShade = overrides?.subdued ?? defaults.subdued;
495
+ const highlightShade = overrides?.highlight ?? defaults.highlight;
496
+ const DEFAULT = scale[defaultShade];
497
+ const subdued = scale[subduedShade];
498
+ const highlight = scale[highlightShade];
499
+ const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(DEFAULT);
500
+ const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : findFg(subdued);
501
+ const hfgResult = overrides?.["on-highlight"] ? { value: scale[overrides["on-highlight"]], ref: { type: "shade", colorName, shade: overrides["on-highlight"] } } : findFg(highlight);
502
+ return {
503
+ DEFAULT,
504
+ foreground: fgResult.value,
505
+ subdued,
506
+ "subdued-foreground": sfgResult.value,
507
+ highlight,
508
+ "highlight-foreground": hfgResult.value,
509
+ _refs: {
510
+ foreground: fgResult.ref,
511
+ "subdued-foreground": sfgResult.ref,
512
+ "highlight-foreground": hfgResult.ref
513
+ }
514
+ };
515
+ }
516
+ function generateSemanticTokens(palette, mode, options) {
517
+ const contrastAlgorithm = options?.contrastAlgorithm ?? "wcag";
518
+ const contrastTarget = options?.contrastTarget ?? "aa";
519
+ const shadeOverrides = options?.shadeOverrides ?? {};
520
+ const neutral = palette.neutral;
521
+ if (!neutral) {
522
+ throw new Error("Neutral palette not found");
523
+ }
524
+ const black = palette.black;
525
+ const white = palette.white;
526
+ const primary = palette.primary;
527
+ const success = palette.success;
528
+ const destructive = palette.destructive;
529
+ const warning = palette.warning;
530
+ if (!primary || !success || !destructive || !warning) {
531
+ throw new Error("Required color palettes not found");
532
+ }
533
+ const muted = mode === "light" ? {
534
+ DEFAULT: neutral[25],
535
+ foreground: neutral[500]
536
+ } : {
537
+ DEFAULT: neutral[800],
538
+ foreground: neutral[200]
539
+ };
540
+ const tokens = {
541
+ primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary"),
542
+ neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral"),
543
+ success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success"),
544
+ destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive"),
545
+ warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning"),
546
+ muted
547
+ };
548
+ const coreColors = [
549
+ "black",
550
+ "white",
551
+ "primary",
552
+ "neutral",
553
+ "success",
554
+ "destructive",
555
+ "warning",
556
+ "green",
557
+ "red",
558
+ "yellow"
559
+ ];
560
+ for (const [colorName, colorValue] of Object.entries(palette)) {
561
+ if (!coreColors.includes(colorName) && typeof colorValue !== "string" && colorValue) {
562
+ tokens[colorName] = createExtendedSemanticColor(
563
+ colorValue,
564
+ neutral,
565
+ white,
566
+ black,
567
+ mode,
568
+ contrastAlgorithm,
569
+ contrastTarget,
570
+ shadeOverrides[colorName]?.[mode],
571
+ colorName
572
+ );
573
+ }
574
+ }
575
+ return tokens;
576
+ }
577
+ function generateSurfaceTokens(palette, mode, overrides) {
578
+ const neutral = palette.neutral;
579
+ if (!neutral) {
580
+ throw new Error("Neutral palette not found");
581
+ }
582
+ const black = palette.black;
583
+ const white = palette.white;
584
+ const defaults = mode === "light" ? { canvas: 25, "canvas-foreground": 800, surface: "white", "surface-foreground": 800, overlay: "white", "overlay-foreground": 800 } : { canvas: 950, "canvas-foreground": "white", surface: "black", "surface-foreground": "white", overlay: "black", "overlay-foreground": "white" };
585
+ function resolve(key) {
586
+ const val = overrides?.[key] ?? defaults[key];
587
+ if (val === "white") return { value: white, ref: { type: "white" } };
588
+ if (val === "black") return { value: black, ref: { type: "black" } };
589
+ return { value: neutral[val], ref: { type: "shade", shade: val } };
590
+ }
591
+ const canvas = resolve("canvas");
592
+ const canvasFg = resolve("canvas-foreground");
593
+ const surface = resolve("surface");
594
+ const surfaceFg = resolve("surface-foreground");
595
+ const overlay = resolve("overlay");
596
+ const overlayFg = resolve("overlay-foreground");
597
+ return {
598
+ canvas: canvas.value,
599
+ "canvas-foreground": canvasFg.value,
600
+ surface: surface.value,
601
+ "surface-foreground": surfaceFg.value,
602
+ overlay: overlay.value,
603
+ "overlay-foreground": overlayFg.value,
604
+ _refs: {
605
+ canvas: canvas.ref,
606
+ "canvas-foreground": canvasFg.ref,
607
+ surface: surface.ref,
608
+ "surface-foreground": surfaceFg.ref,
609
+ overlay: overlay.ref,
610
+ "overlay-foreground": overlayFg.ref
611
+ }
612
+ };
613
+ }
614
+ function generateUtilityTokens(palette, mode, overrides) {
615
+ const neutral = palette.neutral;
616
+ const primary = palette.primary;
617
+ if (!neutral || !primary) {
618
+ throw new Error("Required palettes not found");
619
+ }
620
+ const defaults = mode === "light" ? { border: 200, input: 200, ring: 400, shadow: 950 } : { border: 800, input: 800, ring: 700, shadow: 950 };
621
+ return {
622
+ border: neutral[overrides?.border ?? defaults.border],
623
+ input: neutral[overrides?.input ?? defaults.input],
624
+ ring: primary[overrides?.ring ?? defaults.ring],
625
+ shadow: neutral[overrides?.shadow ?? defaults.shadow]
626
+ };
627
+ }
628
+ function checkSemanticContrast(tokens) {
629
+ const results = {};
630
+ for (const [name, color] of Object.entries(tokens)) {
631
+ if (name === "muted") {
632
+ continue;
633
+ }
634
+ const extendedColor = color;
635
+ const ratio = getContrastRatio(
636
+ extendedColor.DEFAULT,
637
+ extendedColor.foreground
638
+ );
639
+ results[`${name}/default`] = {
640
+ ratio: Math.round(ratio * 100) / 100,
641
+ passesAA: meetsWCAG_AA(ratio),
642
+ passesAAA: ratio >= 7
643
+ };
644
+ const subduedRatio = getContrastRatio(
645
+ extendedColor.subdued,
646
+ extendedColor["subdued-foreground"]
647
+ );
648
+ results[`${name}/subdued`] = {
649
+ ratio: Math.round(subduedRatio * 100) / 100,
650
+ passesAA: meetsWCAG_AA(subduedRatio),
651
+ passesAAA: subduedRatio >= 7
652
+ };
653
+ const highlightRatio = getContrastRatio(
654
+ extendedColor.highlight,
655
+ extendedColor["highlight-foreground"]
656
+ );
657
+ results[`${name}/highlight`] = {
658
+ ratio: Math.round(highlightRatio * 100) / 100,
659
+ passesAA: meetsWCAG_AA(highlightRatio),
660
+ passesAAA: highlightRatio >= 7
661
+ };
662
+ }
663
+ const mutedRatio = getContrastRatio(
664
+ tokens.muted.DEFAULT,
665
+ tokens.muted.foreground
666
+ );
667
+ results["muted"] = {
668
+ ratio: Math.round(mutedRatio * 100) / 100,
669
+ passesAA: meetsWCAG_AA(mutedRatio),
670
+ passesAAA: mutedRatio >= 7
671
+ };
672
+ return results;
673
+ }
674
+ function checkSemanticContrastAPCA(tokens) {
675
+ const results = {};
676
+ for (const [name, color] of Object.entries(tokens)) {
677
+ if (name === "muted") {
678
+ continue;
679
+ }
680
+ const ext = color;
681
+ const defaultLc = getAPCAContrast(ext.foreground, ext.DEFAULT);
682
+ results[`${name}/default`] = {
683
+ lcValue: Math.round(defaultLc * 10) / 10,
684
+ level: classifyAPCALevel(defaultLc)
685
+ };
686
+ const subduedLc = getAPCAContrast(ext["subdued-foreground"], ext.subdued);
687
+ results[`${name}/subdued`] = {
688
+ lcValue: Math.round(subduedLc * 10) / 10,
689
+ level: classifyAPCALevel(subduedLc)
690
+ };
691
+ const highlightLc = getAPCAContrast(ext["highlight-foreground"], ext.highlight);
692
+ results[`${name}/highlight`] = {
693
+ lcValue: Math.round(highlightLc * 10) / 10,
694
+ level: classifyAPCALevel(highlightLc)
695
+ };
696
+ }
697
+ const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT);
698
+ results["muted"] = {
699
+ lcValue: Math.round(mutedLc * 10) / 10,
700
+ level: classifyAPCALevel(mutedLc)
701
+ };
702
+ return results;
703
+ }
704
+ function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa") {
705
+ const { floor } = getContrastThresholds(algorithm, contrastTarget);
706
+ const skipKeys = /* @__PURE__ */ new Set(["muted"]);
707
+ let passed = 0;
708
+ let total = 0;
709
+ if (algorithm === "wcag") {
710
+ const results = checkSemanticContrast(tokens);
711
+ for (const [key, result] of Object.entries(results)) {
712
+ const colorName = key.split("/")[0];
713
+ if (skipKeys.has(colorName)) continue;
714
+ total++;
715
+ if (result.ratio >= floor) {
716
+ passed++;
717
+ }
718
+ }
719
+ } else {
720
+ const results = checkSemanticContrastAPCA(tokens);
721
+ for (const [key, result] of Object.entries(results)) {
722
+ const colorName = key.split("/")[0];
723
+ if (skipKeys.has(colorName)) continue;
724
+ total++;
725
+ if (result.lcValue >= floor) {
726
+ passed++;
727
+ }
728
+ }
729
+ }
730
+ const score = total > 0 ? Math.round(passed / total * 100) : 0;
731
+ return { score, passed, total };
732
+ }
733
+
734
+ // src/generate.ts
735
+ var defaultTypography = {
736
+ fontSize: {
737
+ xs: "0.75rem",
738
+ sm: "0.875rem",
739
+ base: "1rem",
740
+ lg: "1.125rem",
741
+ xl: "1.25rem",
742
+ "2xl": "1.5rem",
743
+ "3xl": "1.875rem",
744
+ "4xl": "2.25rem",
745
+ "5xl": "3rem",
746
+ "6xl": "3.75rem",
747
+ "7xl": "4.5rem",
748
+ "8xl": "6rem",
749
+ "9xl": "8rem"
750
+ },
751
+ fontWeight: {
752
+ thin: 100,
753
+ extralight: 200,
754
+ light: 300,
755
+ normal: 400,
756
+ medium: 500,
757
+ semibold: 600,
758
+ bold: 700,
759
+ extrabold: 800,
760
+ black: 900
761
+ },
762
+ tracking: {
763
+ tighter: "-0.05em",
764
+ tight: "-0.025em",
765
+ normal: "0em",
766
+ wide: "0.025em",
767
+ wider: "0.05em",
768
+ widest: "0.1em"
769
+ },
770
+ leading: {
771
+ tight: 1.25,
772
+ snug: 1.375,
773
+ normal: 1.5,
774
+ relaxed: 1.625,
775
+ loose: 2
776
+ }
777
+ };
778
+ var defaultRadii = {
779
+ xs: "0.125rem",
780
+ sm: "0.25rem",
781
+ md: "0.375rem",
782
+ lg: "0.5rem",
783
+ xl: "0.75rem",
784
+ "2xl": "1rem",
785
+ "3xl": "1.5rem",
786
+ "4xl": "2rem"
787
+ };
788
+ var defaultShadows = {
789
+ light: {
790
+ "2xs": "0 1px rgb(0 0 0 / 0.05)",
791
+ xs: "0 1px 2px 0 rgb(0 0 0 / 0.05)",
792
+ sm: "0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)",
793
+ md: "0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)",
794
+ lg: "0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)",
795
+ xl: "0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1)",
796
+ "2xl": "0 25px 50px -12px rgb(0 0 0 / 0.25)"
797
+ },
798
+ dark: {
799
+ "2xs": "0 1px rgb(0 0 0 / 0.05)",
800
+ xs: "0 1px 2px 0 rgb(0 0 0 / 0.05)",
801
+ sm: "0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)",
802
+ md: "0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)",
803
+ lg: "0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)",
804
+ xl: "0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1)",
805
+ "2xl": "0 25px 50px -12px rgb(0 0 0 / 0.25)"
806
+ }
807
+ };
808
+ function generateTokens(brandColors, options) {
809
+ const primitives = generatePrimitivePalette(
810
+ brandColors,
811
+ options?.lightnessStrategy ?? "fixed"
812
+ );
813
+ const lightSemantic = generateSemanticTokens(primitives, "light", options);
814
+ const darkSemantic = generateSemanticTokens(primitives, "dark", options);
815
+ const lightSurface = generateSurfaceTokens(primitives, "light", options?.surfaceShadeOverrides?.light);
816
+ const darkSurface = generateSurfaceTokens(primitives, "dark", options?.surfaceShadeOverrides?.dark);
817
+ const lightUtility = generateUtilityTokens(primitives, "light", options?.utilityShadeOverrides?.light);
818
+ const darkUtility = generateUtilityTokens(primitives, "dark", options?.utilityShadeOverrides?.dark);
819
+ return {
820
+ primitives,
821
+ semantic: {
822
+ light: lightSemantic,
823
+ dark: darkSemantic
824
+ },
825
+ surface: {
826
+ light: lightSurface,
827
+ dark: darkSurface
828
+ },
829
+ utility: {
830
+ light: lightUtility,
831
+ dark: darkUtility
832
+ },
833
+ spacing: {},
834
+ typography: defaultTypography,
835
+ radii: defaultRadii,
836
+ shadows: defaultShadows
837
+ };
838
+ }
839
+
840
+ // src/export.ts
841
+ function formatColor(color, format) {
842
+ return formatColorAs(color, format);
843
+ }
844
+ function colorScaleToCssVars(name, scale, colorFormat = "hex") {
845
+ return Object.entries(scale).map(
846
+ ([shade, color]) => ` --color-${name}-${shade}: ${formatColor(color, colorFormat)};`
847
+ );
848
+ }
849
+ function getExtendedMappings(mode, colorName, overrides) {
850
+ const defaults = DEFAULT_SHADE_MAPPINGS[mode].extended;
851
+ const ov = overrides?.[colorName]?.[mode];
852
+ if (!ov) return defaults;
853
+ return {
854
+ DEFAULT: ov.DEFAULT ?? defaults.DEFAULT,
855
+ subdued: ov.subdued ?? defaults.subdued,
856
+ highlight: ov.highlight ?? defaults.highlight
857
+ };
858
+ }
859
+ var DEFAULT_SHADE_MAPPINGS = {
860
+ light: {
861
+ extended: {
862
+ DEFAULT: 500,
863
+ subdued: 50,
864
+ highlight: 700
865
+ },
866
+ muted: {
867
+ DEFAULT: 25,
868
+ foreground: 500
869
+ }
870
+ },
871
+ dark: {
872
+ extended: {
873
+ DEFAULT: 600,
874
+ subdued: 200,
875
+ highlight: 400
876
+ },
877
+ muted: {
878
+ DEFAULT: 800,
879
+ foreground: 200
880
+ }
881
+ }
882
+ };
883
+ var UTILITY_SHADE_MAPPINGS = {
884
+ light: {
885
+ border: "neutral-200",
886
+ input: "neutral-200",
887
+ ring: "primary-400",
888
+ shadow: "neutral-950"
889
+ },
890
+ dark: {
891
+ border: "neutral-800",
892
+ input: "neutral-800",
893
+ ring: "primary-700",
894
+ shadow: "neutral-950"
895
+ }
896
+ };
897
+ function primitiveToVar(ref) {
898
+ return `var(--color-${ref})`;
899
+ }
900
+ function refToVar(ref) {
901
+ switch (ref.type) {
902
+ case "white":
903
+ return "var(--color-white)";
904
+ case "black":
905
+ return "var(--color-black)";
906
+ case "shade":
907
+ return `var(--color-${ref.colorName}-${ref.shade})`;
908
+ }
909
+ }
910
+ function surfaceRefToVar(ref) {
911
+ switch (ref.type) {
912
+ case "white":
913
+ return "var(--color-white)";
914
+ case "black":
915
+ return "var(--color-black)";
916
+ case "shade":
917
+ return `var(--color-neutral-${ref.shade})`;
918
+ }
919
+ }
920
+ function emitSurfaceVars(surface, colorFormat, indent) {
921
+ const lines = [];
922
+ const keys = [
923
+ "canvas",
924
+ "canvas-foreground",
925
+ "surface",
926
+ "surface-foreground",
927
+ "overlay",
928
+ "overlay-foreground"
929
+ ];
930
+ for (const key of keys) {
931
+ if (surface._refs) {
932
+ lines.push(`${indent}--${key}: ${surfaceRefToVar(surface._refs[key])};`);
933
+ } else {
934
+ lines.push(`${indent}--${key}: ${formatColor(surface[key], colorFormat)};`);
935
+ }
936
+ }
937
+ return lines;
938
+ }
939
+ function emitShadcnAliases(tokens, mode, indent) {
940
+ const lines = [];
941
+ const chartShade = mode === "light" ? 500 : 400;
942
+ lines.push("");
943
+ lines.push(`${indent}/* shadcn/ui compatibility */`);
944
+ lines.push(`${indent}--background: var(--canvas);`);
945
+ lines.push(`${indent}--foreground: var(--canvas-foreground);`);
946
+ lines.push(`${indent}--card: var(--surface);`);
947
+ lines.push(`${indent}--card-foreground: var(--surface-foreground);`);
948
+ lines.push(`${indent}--popover: var(--overlay);`);
949
+ lines.push(`${indent}--popover-foreground: var(--overlay-foreground);`);
950
+ lines.push(`${indent}--secondary: var(--neutral-subdued);`);
951
+ lines.push(`${indent}--secondary-foreground: var(--neutral-subdued-foreground);`);
952
+ lines.push(`${indent}--accent: var(--neutral-subdued);`);
953
+ lines.push(`${indent}--accent-foreground: var(--neutral-subdued-foreground);`);
954
+ lines.push(`${indent}--sidebar: var(--surface);`);
955
+ lines.push(`${indent}--sidebar-foreground: var(--surface-foreground);`);
956
+ lines.push(`${indent}--sidebar-primary: var(--primary);`);
957
+ lines.push(`${indent}--sidebar-primary-foreground: var(--primary-foreground);`);
958
+ lines.push(`${indent}--sidebar-accent: var(--neutral-subdued);`);
959
+ lines.push(`${indent}--sidebar-accent-foreground: var(--neutral-subdued-foreground);`);
960
+ lines.push(`${indent}--sidebar-border: var(--border);`);
961
+ lines.push(`${indent}--sidebar-ring: var(--ring);`);
962
+ const chartColors = ["primary", "success", "destructive", "warning", "neutral"];
963
+ for (let i = 0; i < chartColors.length; i++) {
964
+ const scale = tokens.primitives[chartColors[i]];
965
+ if (scale && typeof scale !== "string") {
966
+ lines.push(`${indent}--chart-${i + 1}: var(--color-${chartColors[i]}-${chartShade});`);
967
+ }
968
+ }
969
+ return lines;
970
+ }
971
+ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent) {
972
+ const lines = [];
973
+ const extendedSemanticNames = ["primary", "neutral", "success", "destructive", "warning"];
974
+ for (const name of extendedSemanticNames) {
975
+ const color = tokens.semantic[mode][name];
976
+ const mappings = getExtendedMappings(mode, name, shadeOverrides);
977
+ lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
978
+ lines.push(`${indent}--${name}-foreground: ${color._refs ? refToVar(color._refs.foreground) : formatColor(color.foreground, colorFormat)};`);
979
+ lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
980
+ lines.push(`${indent}--${name}-subdued-foreground: ${color._refs ? refToVar(color._refs["subdued-foreground"]) : formatColor(color["subdued-foreground"], colorFormat)};`);
981
+ lines.push(`${indent}--${name}-highlight: var(--color-${name}-${mappings.highlight});`);
982
+ lines.push(`${indent}--${name}-highlight-foreground: ${color._refs ? refToVar(color._refs["highlight-foreground"]) : formatColor(color["highlight-foreground"], colorFormat)};`);
983
+ }
984
+ for (const [name, color] of Object.entries(tokens.semantic[mode])) {
985
+ if (!extendedSemanticNames.includes(name) && name !== "muted") {
986
+ const ext = color;
987
+ const mappings = getExtendedMappings(mode, name, shadeOverrides);
988
+ lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
989
+ lines.push(`${indent}--${name}-foreground: ${ext._refs ? refToVar(ext._refs.foreground) : formatColor(ext.foreground, colorFormat)};`);
990
+ lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
991
+ lines.push(`${indent}--${name}-subdued-foreground: ${ext._refs ? refToVar(ext._refs["subdued-foreground"]) : formatColor(ext["subdued-foreground"], colorFormat)};`);
992
+ lines.push(`${indent}--${name}-highlight: var(--color-${name}-${mappings.highlight});`);
993
+ lines.push(`${indent}--${name}-highlight-foreground: ${ext._refs ? refToVar(ext._refs["highlight-foreground"]) : formatColor(ext["highlight-foreground"], colorFormat)};`);
994
+ }
995
+ }
996
+ lines.push(`${indent}--muted: var(--color-neutral-${DEFAULT_SHADE_MAPPINGS[mode].muted.DEFAULT});`);
997
+ lines.push(`${indent}--muted-foreground: var(--color-neutral-${DEFAULT_SHADE_MAPPINGS[mode].muted.foreground});`);
998
+ return lines;
999
+ }
1000
+ function emitUtilityVars(mode, indent) {
1001
+ const lines = [];
1002
+ for (const [token, ref] of Object.entries(UTILITY_SHADE_MAPPINGS[mode])) {
1003
+ lines.push(`${indent}--${token}: ${primitiveToVar(ref)};`);
1004
+ }
1005
+ return lines;
1006
+ }
1007
+ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, compat = true) {
1008
+ const lines = [];
1009
+ lines.push(":root {");
1010
+ lines.push(" /* Primitive Colors */");
1011
+ for (const [name, scale] of Object.entries(tokens.primitives)) {
1012
+ if (typeof scale === "string") {
1013
+ lines.push(` --color-${name}: ${formatColor(scale, colorFormat)};`);
1014
+ } else {
1015
+ lines.push(...colorScaleToCssVars(name, scale, colorFormat));
1016
+ }
1017
+ }
1018
+ lines.push("}");
1019
+ lines.push("");
1020
+ lines.push("/* Light Mode */");
1021
+ lines.push(':root, [data-theme="light"] {');
1022
+ lines.push(" /* Surface */");
1023
+ lines.push(...emitSurfaceVars(tokens.surface.light, colorFormat, " "));
1024
+ lines.push("");
1025
+ lines.push(" /* Utility */");
1026
+ lines.push(...emitUtilityVars("light", " "));
1027
+ lines.push("");
1028
+ lines.push(" /* Semantic Colors */");
1029
+ lines.push(...emitSemanticVars(tokens, "light", colorFormat, shadeOverrides, " "));
1030
+ if (compat) lines.push(...emitShadcnAliases(tokens, "light", " "));
1031
+ lines.push("}");
1032
+ lines.push("");
1033
+ lines.push("/* Dark Mode */");
1034
+ lines.push('[data-theme="dark"] {');
1035
+ lines.push(" /* Surface */");
1036
+ lines.push(...emitSurfaceVars(tokens.surface.dark, colorFormat, " "));
1037
+ lines.push("");
1038
+ lines.push(" /* Utility */");
1039
+ lines.push(...emitUtilityVars("dark", " "));
1040
+ lines.push("");
1041
+ lines.push(" /* Semantic Colors */");
1042
+ lines.push(...emitSemanticVars(tokens, "dark", colorFormat, shadeOverrides, " "));
1043
+ if (compat) lines.push(...emitShadcnAliases(tokens, "dark", " "));
1044
+ lines.push("}");
1045
+ lines.push("");
1046
+ lines.push("/* System Preference: Dark Mode */");
1047
+ lines.push("@media (prefers-color-scheme: dark) {");
1048
+ lines.push(' :root:not([data-theme="light"]) {');
1049
+ lines.push(...emitSurfaceVars(tokens.surface.dark, colorFormat, " "));
1050
+ lines.push(...emitUtilityVars("dark", " "));
1051
+ lines.push(...emitSemanticVars(tokens, "dark", colorFormat, shadeOverrides, " "));
1052
+ if (compat) lines.push(...emitShadcnAliases(tokens, "dark", " "));
1053
+ lines.push(" }");
1054
+ lines.push("}");
1055
+ return lines.join("\n");
1056
+ }
1057
+ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, compat = true) {
1058
+ const lines = [];
1059
+ const extendedSemanticNames = ["primary", "neutral", "success", "destructive", "warning"];
1060
+ lines.push("@theme {");
1061
+ lines.push(" /* Colors */");
1062
+ for (const [name, scale] of Object.entries(tokens.primitives)) {
1063
+ if (typeof scale === "string") {
1064
+ lines.push(` --color-${name}: var(--${name});`);
1065
+ } else {
1066
+ for (const shade of Object.keys(scale)) {
1067
+ lines.push(` --color-${name}-${shade}: var(--${name}-${shade});`);
1068
+ }
1069
+ }
1070
+ }
1071
+ lines.push("");
1072
+ lines.push(" /* Semantic Colors */");
1073
+ for (const name of extendedSemanticNames) {
1074
+ lines.push(` --color-${name}: var(--${name});`);
1075
+ lines.push(` --color-${name}-foreground: var(--${name}-foreground);`);
1076
+ lines.push(` --color-${name}-subdued: var(--${name}-subdued);`);
1077
+ lines.push(` --color-${name}-subdued-foreground: var(--${name}-subdued-foreground);`);
1078
+ lines.push(` --color-${name}-highlight: var(--${name}-highlight);`);
1079
+ lines.push(` --color-${name}-highlight-foreground: var(--${name}-highlight-foreground);`);
1080
+ }
1081
+ for (const name of Object.keys(tokens.semantic.light)) {
1082
+ if (!extendedSemanticNames.includes(name) && name !== "muted") {
1083
+ lines.push(` --color-${name}: var(--${name});`);
1084
+ lines.push(` --color-${name}-foreground: var(--${name}-foreground);`);
1085
+ lines.push(` --color-${name}-subdued: var(--${name}-subdued);`);
1086
+ lines.push(` --color-${name}-subdued-foreground: var(--${name}-subdued-foreground);`);
1087
+ lines.push(` --color-${name}-highlight: var(--${name}-highlight);`);
1088
+ lines.push(` --color-${name}-highlight-foreground: var(--${name}-highlight-foreground);`);
1089
+ }
1090
+ }
1091
+ lines.push("");
1092
+ lines.push(" /* Muted */");
1093
+ lines.push(" --color-muted: var(--muted);");
1094
+ lines.push(" --color-muted-foreground: var(--muted-foreground);");
1095
+ lines.push(" --color-black: var(--black);");
1096
+ lines.push(" --color-white: var(--white);");
1097
+ lines.push("");
1098
+ lines.push(" /* Surface Colors */");
1099
+ lines.push(" --color-canvas: var(--canvas);");
1100
+ lines.push(" --color-canvas-foreground: var(--canvas-foreground);");
1101
+ lines.push(" --color-surface: var(--surface);");
1102
+ lines.push(" --color-surface-foreground: var(--surface-foreground);");
1103
+ lines.push(" --color-overlay: var(--overlay);");
1104
+ lines.push(" --color-overlay-foreground: var(--overlay-foreground);");
1105
+ lines.push("");
1106
+ lines.push(" /* Utility Colors */");
1107
+ lines.push(" --color-border: var(--border);");
1108
+ lines.push(" --color-input: var(--input);");
1109
+ lines.push(" --color-ring: var(--ring);");
1110
+ lines.push(" --color-shadow: var(--shadow);");
1111
+ if (compat) {
1112
+ lines.push("");
1113
+ lines.push(" /* shadcn/ui compatibility */");
1114
+ lines.push(" --color-background: var(--background);");
1115
+ lines.push(" --color-foreground: var(--foreground);");
1116
+ lines.push(" --color-card: var(--card);");
1117
+ lines.push(" --color-card-foreground: var(--card-foreground);");
1118
+ lines.push(" --color-popover: var(--popover);");
1119
+ lines.push(" --color-popover-foreground: var(--popover-foreground);");
1120
+ lines.push(" --color-secondary: var(--secondary);");
1121
+ lines.push(" --color-secondary-foreground: var(--secondary-foreground);");
1122
+ lines.push(" --color-accent: var(--accent);");
1123
+ lines.push(" --color-accent-foreground: var(--accent-foreground);");
1124
+ lines.push(" --color-sidebar: var(--sidebar);");
1125
+ lines.push(" --color-sidebar-foreground: var(--sidebar-foreground);");
1126
+ lines.push(" --color-sidebar-primary: var(--sidebar-primary);");
1127
+ lines.push(" --color-sidebar-primary-foreground: var(--sidebar-primary-foreground);");
1128
+ lines.push(" --color-sidebar-accent: var(--sidebar-accent);");
1129
+ lines.push(" --color-sidebar-accent-foreground: var(--sidebar-accent-foreground);");
1130
+ lines.push(" --color-sidebar-border: var(--sidebar-border);");
1131
+ lines.push(" --color-sidebar-ring: var(--sidebar-ring);");
1132
+ }
1133
+ lines.push("}");
1134
+ lines.push("");
1135
+ lines.push(":root {");
1136
+ for (const [name, scale] of Object.entries(tokens.primitives)) {
1137
+ if (typeof scale === "string") {
1138
+ lines.push(` --${name}: ${formatColor(scale, colorFormat)};`);
1139
+ } else {
1140
+ for (const [shade, color] of Object.entries(scale)) {
1141
+ lines.push(` --${name}-${shade}: ${formatColor(color, colorFormat)};`);
1142
+ }
1143
+ }
1144
+ }
1145
+ lines.push(...emitSurfaceVars(tokens.surface.light, colorFormat, " "));
1146
+ lines.push(...emitSemanticVars(tokens, "light", colorFormat, shadeOverrides, " "));
1147
+ lines.push(...emitUtilityVars("light", " "));
1148
+ if (compat) lines.push(...emitShadcnAliases(tokens, "light", " "));
1149
+ lines.push("}");
1150
+ lines.push("");
1151
+ lines.push(".dark {");
1152
+ lines.push(...emitSurfaceVars(tokens.surface.dark, colorFormat, " "));
1153
+ lines.push(...emitSemanticVars(tokens, "dark", colorFormat, shadeOverrides, " "));
1154
+ lines.push(...emitUtilityVars("dark", " "));
1155
+ if (compat) lines.push(...emitShadcnAliases(tokens, "dark", " "));
1156
+ lines.push("}");
1157
+ return lines.join("\n");
1158
+ }
1159
+ function exportToTailwindV3(tokens, colorFormat = "hex") {
1160
+ const config = {
1161
+ theme: {
1162
+ extend: {
1163
+ colors: {}
1164
+ }
1165
+ }
1166
+ };
1167
+ for (const [name, scale] of Object.entries(tokens.primitives)) {
1168
+ if (typeof scale === "string") {
1169
+ config.theme.extend.colors[name] = formatColor(scale, colorFormat);
1170
+ } else {
1171
+ config.theme.extend.colors[name] = {};
1172
+ for (const [shade, color] of Object.entries(scale)) {
1173
+ config.theme.extend.colors[name][shade] = formatColor(color, colorFormat);
1174
+ }
1175
+ }
1176
+ }
1177
+ const extendedSemanticColors = ["primary", "neutral", "success", "destructive", "warning"];
1178
+ for (const name of extendedSemanticColors) {
1179
+ const primitiveScale = tokens.primitives[name];
1180
+ const colorObj = {
1181
+ DEFAULT: `var(--${name})`,
1182
+ foreground: `var(--${name}-foreground)`,
1183
+ subdued: `var(--${name}-subdued)`,
1184
+ "subdued-foreground": `var(--${name}-subdued-foreground)`,
1185
+ highlight: `var(--${name}-highlight)`,
1186
+ "highlight-foreground": `var(--${name}-highlight-foreground)`
1187
+ };
1188
+ if (primitiveScale && typeof primitiveScale !== "string") {
1189
+ for (const [shade, color] of Object.entries(primitiveScale)) {
1190
+ colorObj[shade] = formatColor(color, colorFormat);
1191
+ }
1192
+ }
1193
+ config.theme.extend.colors[name] = colorObj;
1194
+ }
1195
+ for (const name of Object.keys(tokens.semantic.light)) {
1196
+ if (!extendedSemanticColors.includes(name) && name !== "muted") {
1197
+ const primitiveScale = tokens.primitives[name];
1198
+ const colorObj = {
1199
+ DEFAULT: `var(--${name})`,
1200
+ foreground: `var(--${name}-foreground)`,
1201
+ subdued: `var(--${name}-subdued)`,
1202
+ "subdued-foreground": `var(--${name}-subdued-foreground)`,
1203
+ highlight: `var(--${name}-highlight)`,
1204
+ "highlight-foreground": `var(--${name}-highlight-foreground)`
1205
+ };
1206
+ if (primitiveScale && typeof primitiveScale !== "string") {
1207
+ for (const [shade, color] of Object.entries(primitiveScale)) {
1208
+ colorObj[shade] = formatColor(color, colorFormat);
1209
+ }
1210
+ }
1211
+ config.theme.extend.colors[name] = colorObj;
1212
+ }
1213
+ }
1214
+ config.theme.extend.colors["muted"] = {
1215
+ DEFAULT: "var(--muted)",
1216
+ foreground: "var(--muted-foreground)"
1217
+ };
1218
+ config.theme.extend.colors["canvas"] = {
1219
+ DEFAULT: "var(--canvas)",
1220
+ foreground: "var(--canvas-foreground)"
1221
+ };
1222
+ config.theme.extend.colors["surface"] = {
1223
+ DEFAULT: "var(--surface)",
1224
+ foreground: "var(--surface-foreground)"
1225
+ };
1226
+ config.theme.extend.colors["overlay"] = {
1227
+ DEFAULT: "var(--overlay)",
1228
+ foreground: "var(--overlay-foreground)"
1229
+ };
1230
+ config.theme.extend.colors["background"] = "var(--background)";
1231
+ config.theme.extend.colors["foreground"] = "var(--foreground)";
1232
+ config.theme.extend.colors["card"] = {
1233
+ DEFAULT: "var(--card)",
1234
+ foreground: "var(--card-foreground)"
1235
+ };
1236
+ config.theme.extend.colors["popover"] = {
1237
+ DEFAULT: "var(--popover)",
1238
+ foreground: "var(--popover-foreground)"
1239
+ };
1240
+ config.theme.extend.colors["secondary"] = {
1241
+ DEFAULT: "var(--secondary)",
1242
+ foreground: "var(--secondary-foreground)"
1243
+ };
1244
+ config.theme.extend.colors["accent"] = {
1245
+ DEFAULT: "var(--accent)",
1246
+ foreground: "var(--accent-foreground)"
1247
+ };
1248
+ config.theme.extend.colors["sidebar"] = {
1249
+ DEFAULT: "var(--sidebar)",
1250
+ foreground: "var(--sidebar-foreground)",
1251
+ primary: "var(--sidebar-primary)",
1252
+ "primary-foreground": "var(--sidebar-primary-foreground)",
1253
+ accent: "var(--sidebar-accent)",
1254
+ "accent-foreground": "var(--sidebar-accent-foreground)",
1255
+ border: "var(--sidebar-border)",
1256
+ ring: "var(--sidebar-ring)"
1257
+ };
1258
+ config.theme.extend.colors["border"] = "var(--border)";
1259
+ config.theme.extend.colors["input"] = "var(--input)";
1260
+ config.theme.extend.colors["ring"] = "var(--ring)";
1261
+ return `/** @type {import('tailwindcss').Config} */
1262
+ module.exports = ${JSON.stringify(config, null, 2)};`;
1263
+ }
1264
+ function dtcgColorValue(color) {
1265
+ return {
1266
+ $value: formatColorAs(color, "hex"),
1267
+ $type: "color"
1268
+ };
1269
+ }
1270
+ function exportToDtcg(tokens) {
1271
+ const dtcg = {};
1272
+ for (const [name, scale] of Object.entries(tokens.primitives)) {
1273
+ if (typeof scale === "string") {
1274
+ dtcg[name] = dtcgColorValue(scale);
1275
+ } else {
1276
+ const group = { $type: "color" };
1277
+ for (const [shade, color] of Object.entries(scale)) {
1278
+ group[shade] = { $value: formatColorAs(color, "hex") };
1279
+ }
1280
+ dtcg[name] = group;
1281
+ }
1282
+ }
1283
+ for (const mode of ["light", "dark"]) {
1284
+ const semanticGroup = {};
1285
+ const extendedNames = ["primary", "neutral", "success", "destructive", "warning"];
1286
+ for (const colorName of extendedNames) {
1287
+ const color = tokens.semantic[mode][colorName];
1288
+ semanticGroup[colorName] = {
1289
+ $type: "color",
1290
+ DEFAULT: { $value: formatColorAs(color.DEFAULT, "hex") },
1291
+ foreground: { $value: formatColorAs(color.foreground, "hex") },
1292
+ subdued: { $value: formatColorAs(color.subdued, "hex") },
1293
+ "subdued-foreground": {
1294
+ $value: formatColorAs(color["subdued-foreground"], "hex")
1295
+ },
1296
+ highlight: { $value: formatColorAs(color.highlight, "hex") },
1297
+ "highlight-foreground": {
1298
+ $value: formatColorAs(color["highlight-foreground"], "hex")
1299
+ }
1300
+ };
1301
+ }
1302
+ for (const [colorName, color] of Object.entries(tokens.semantic[mode])) {
1303
+ if (!extendedNames.includes(colorName) && colorName !== "muted") {
1304
+ const ext = color;
1305
+ semanticGroup[colorName] = {
1306
+ $type: "color",
1307
+ DEFAULT: { $value: formatColorAs(ext.DEFAULT, "hex") },
1308
+ foreground: { $value: formatColorAs(ext.foreground, "hex") },
1309
+ subdued: { $value: formatColorAs(ext.subdued, "hex") },
1310
+ "subdued-foreground": {
1311
+ $value: formatColorAs(ext["subdued-foreground"], "hex")
1312
+ },
1313
+ highlight: { $value: formatColorAs(ext.highlight, "hex") },
1314
+ "highlight-foreground": {
1315
+ $value: formatColorAs(ext["highlight-foreground"], "hex")
1316
+ }
1317
+ };
1318
+ }
1319
+ }
1320
+ const muted = tokens.semantic[mode].muted;
1321
+ semanticGroup.muted = {
1322
+ $type: "color",
1323
+ DEFAULT: { $value: formatColorAs(muted.DEFAULT, "hex") },
1324
+ foreground: { $value: formatColorAs(muted.foreground, "hex") }
1325
+ };
1326
+ const surf = tokens.surface[mode];
1327
+ semanticGroup.surface = {
1328
+ $type: "color",
1329
+ canvas: { $value: formatColorAs(surf.canvas, "hex") },
1330
+ "canvas-foreground": { $value: formatColorAs(surf["canvas-foreground"], "hex") },
1331
+ surface: { $value: formatColorAs(surf.surface, "hex") },
1332
+ "surface-foreground": { $value: formatColorAs(surf["surface-foreground"], "hex") },
1333
+ overlay: { $value: formatColorAs(surf.overlay, "hex") },
1334
+ "overlay-foreground": { $value: formatColorAs(surf["overlay-foreground"], "hex") }
1335
+ };
1336
+ const util = tokens.utility[mode];
1337
+ semanticGroup.utility = {
1338
+ $type: "color",
1339
+ border: { $value: formatColorAs(util.border, "hex") },
1340
+ input: { $value: formatColorAs(util.input, "hex") },
1341
+ ring: { $value: formatColorAs(util.ring, "hex") },
1342
+ shadow: { $value: formatColorAs(util.shadow, "hex") }
1343
+ };
1344
+ dtcg[mode] = semanticGroup;
1345
+ }
1346
+ return JSON.stringify(dtcg, null, 2);
1347
+ }
1348
+ function exportToLovable(tokens, colorFormat = "hex") {
1349
+ const lines = [];
1350
+ lines.push("/* Lovable Theme \u2014 shadcn/ui + Tailwind v4 */");
1351
+ lines.push("/* Paste into your globals.css or src/index.css */");
1352
+ lines.push("");
1353
+ lines.push('@import "tailwindcss";');
1354
+ lines.push("");
1355
+ lines.push("@theme inline {");
1356
+ lines.push(" /* Surface */");
1357
+ lines.push(" --color-background: var(--background);");
1358
+ lines.push(" --color-foreground: var(--foreground);");
1359
+ lines.push(" --color-card: var(--card);");
1360
+ lines.push(" --color-card-foreground: var(--card-foreground);");
1361
+ lines.push(" --color-popover: var(--popover);");
1362
+ lines.push(" --color-popover-foreground: var(--popover-foreground);");
1363
+ lines.push("");
1364
+ lines.push(" /* Semantic */");
1365
+ lines.push(" --color-primary: var(--primary);");
1366
+ lines.push(" --color-primary-foreground: var(--primary-foreground);");
1367
+ lines.push(" --color-secondary: var(--secondary);");
1368
+ lines.push(" --color-secondary-foreground: var(--secondary-foreground);");
1369
+ lines.push(" --color-muted: var(--muted);");
1370
+ lines.push(" --color-muted-foreground: var(--muted-foreground);");
1371
+ lines.push(" --color-accent: var(--accent);");
1372
+ lines.push(" --color-accent-foreground: var(--accent-foreground);");
1373
+ lines.push(" --color-destructive: var(--destructive);");
1374
+ lines.push(" --color-destructive-foreground: var(--destructive-foreground);");
1375
+ lines.push("");
1376
+ lines.push(" /* Utility */");
1377
+ lines.push(" --color-border: var(--border);");
1378
+ lines.push(" --color-input: var(--input);");
1379
+ lines.push(" --color-ring: var(--ring);");
1380
+ lines.push("");
1381
+ lines.push(" /* Sidebar */");
1382
+ lines.push(" --color-sidebar: var(--sidebar);");
1383
+ lines.push(" --color-sidebar-foreground: var(--sidebar-foreground);");
1384
+ lines.push(" --color-sidebar-primary: var(--sidebar-primary);");
1385
+ lines.push(" --color-sidebar-primary-foreground: var(--sidebar-primary-foreground);");
1386
+ lines.push(" --color-sidebar-accent: var(--sidebar-accent);");
1387
+ lines.push(" --color-sidebar-accent-foreground: var(--sidebar-accent-foreground);");
1388
+ lines.push(" --color-sidebar-border: var(--sidebar-border);");
1389
+ lines.push(" --color-sidebar-ring: var(--sidebar-ring);");
1390
+ lines.push("");
1391
+ lines.push(" --radius: 0.5rem;");
1392
+ lines.push("}");
1393
+ lines.push("");
1394
+ const resolveColor = (color) => formatColor(color, colorFormat);
1395
+ function emitLovableMode(mode, selector) {
1396
+ const sem = tokens.semantic[mode];
1397
+ const surf = tokens.surface[mode];
1398
+ const util = tokens.utility[mode];
1399
+ const neutral = sem.neutral;
1400
+ lines.push(`${selector} {`);
1401
+ lines.push(` --background: ${resolveColor(surf.canvas)};`);
1402
+ lines.push(` --foreground: ${resolveColor(surf["canvas-foreground"])};`);
1403
+ lines.push(` --card: ${resolveColor(surf.surface)};`);
1404
+ lines.push(` --card-foreground: ${resolveColor(surf["surface-foreground"])};`);
1405
+ lines.push(` --popover: ${resolveColor(surf.overlay)};`);
1406
+ lines.push(` --popover-foreground: ${resolveColor(surf["overlay-foreground"])};`);
1407
+ lines.push("");
1408
+ lines.push(` --primary: ${resolveColor(sem.primary.DEFAULT)};`);
1409
+ lines.push(` --primary-foreground: ${resolveColor(sem.primary.foreground)};`);
1410
+ lines.push(` --secondary: ${resolveColor(neutral.subdued)};`);
1411
+ lines.push(` --secondary-foreground: ${resolveColor(neutral["subdued-foreground"])};`);
1412
+ lines.push(` --muted: ${resolveColor(sem.muted.DEFAULT)};`);
1413
+ lines.push(` --muted-foreground: ${resolveColor(sem.muted.foreground)};`);
1414
+ lines.push(` --accent: ${resolveColor(neutral.subdued)};`);
1415
+ lines.push(` --accent-foreground: ${resolveColor(neutral["subdued-foreground"])};`);
1416
+ lines.push(` --destructive: ${resolveColor(sem.destructive.DEFAULT)};`);
1417
+ lines.push(` --destructive-foreground: ${resolveColor(sem.destructive.foreground)};`);
1418
+ lines.push("");
1419
+ lines.push(` --border: ${resolveColor(util.border)};`);
1420
+ lines.push(` --input: ${resolveColor(util.input)};`);
1421
+ lines.push(` --ring: ${resolveColor(util.ring)};`);
1422
+ lines.push("");
1423
+ lines.push(` --sidebar: ${resolveColor(surf.surface)};`);
1424
+ lines.push(` --sidebar-foreground: ${resolveColor(surf["surface-foreground"])};`);
1425
+ lines.push(` --sidebar-primary: ${resolveColor(sem.primary.DEFAULT)};`);
1426
+ lines.push(` --sidebar-primary-foreground: ${resolveColor(sem.primary.foreground)};`);
1427
+ lines.push(` --sidebar-accent: ${resolveColor(neutral.subdued)};`);
1428
+ lines.push(` --sidebar-accent-foreground: ${resolveColor(neutral["subdued-foreground"])};`);
1429
+ lines.push(` --sidebar-border: ${resolveColor(util.border)};`);
1430
+ lines.push(` --sidebar-ring: ${resolveColor(util.ring)};`);
1431
+ const chartShade = mode === "light" ? 500 : 400;
1432
+ const chartColors = ["primary", "success", "destructive", "warning", "neutral"];
1433
+ for (let i = 0; i < chartColors.length; i++) {
1434
+ const scale = tokens.primitives[chartColors[i]];
1435
+ if (scale && typeof scale !== "string") {
1436
+ lines.push(` --chart-${i + 1}: ${resolveColor(scale[chartShade])};`);
1437
+ }
1438
+ }
1439
+ lines.push("}");
1440
+ }
1441
+ emitLovableMode("light", ":root");
1442
+ lines.push("");
1443
+ emitLovableMode("dark", ".dark");
1444
+ return lines.join("\n");
1445
+ }
1446
+ function exportTokens(tokens, format, options) {
1447
+ const colorFormat = options?.colorFormat ?? "hex";
1448
+ const shadeOverrides = options?.shadeOverrides;
1449
+ const compat = options?.compat !== false;
1450
+ switch (format) {
1451
+ case "css":
1452
+ return exportToCss(tokens, colorFormat, shadeOverrides, compat);
1453
+ case "tailwind-v3":
1454
+ return exportToTailwindV3(tokens, colorFormat);
1455
+ case "tailwind-v4":
1456
+ return exportToTailwindV4(tokens, colorFormat, shadeOverrides, compat);
1457
+ case "dtcg":
1458
+ return exportToDtcg(tokens);
1459
+ case "lovable":
1460
+ return exportToLovable(tokens, colorFormat);
1461
+ default:
1462
+ throw new Error(`Unknown export format: ${format}`);
1463
+ }
1464
+ }
1465
+
1466
+ export {
1467
+ parseToOklch,
1468
+ oklchToHex,
1469
+ generateColorScale,
1470
+ adjustLightness,
1471
+ adjustChroma,
1472
+ getRelativeLuminance,
1473
+ getContrastRatio,
1474
+ meetsWCAG_AA,
1475
+ meetsWCAG_AAA,
1476
+ getAPCAContrast,
1477
+ findContrastingColorAPCA,
1478
+ simulateCVD,
1479
+ classifyAPCALevel,
1480
+ generateColorScaleAPCA,
1481
+ formatColorShort,
1482
+ getOklchString,
1483
+ formatColorAs,
1484
+ DEFAULT_NEUTRAL_COLOR,
1485
+ generatePrimitivePalette,
1486
+ getShade,
1487
+ generateSemanticTokens,
1488
+ generateSurfaceTokens,
1489
+ generateUtilityTokens,
1490
+ checkSemanticContrast,
1491
+ checkSemanticContrastAPCA,
1492
+ computeAccessibilityScore,
1493
+ generateTokens,
1494
+ exportToCss,
1495
+ exportToTailwindV4,
1496
+ exportToTailwindV3,
1497
+ exportToDtcg,
1498
+ exportToLovable,
1499
+ exportTokens
1500
+ };