@designtools/tokens 0.3.0 → 0.5.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/CHANGELOG.md +85 -0
- package/README.md +402 -119
- package/dist/{chunk-3OG7U5V2.js → chunk-D6675UF5.js} +598 -199
- package/dist/cli.js +318 -41
- package/dist/index.d.ts +298 -22
- package/dist/index.js +27 -1
- package/package.json +39 -4
|
@@ -68,12 +68,43 @@ import {
|
|
|
68
68
|
filterDeficiencyProt,
|
|
69
69
|
filterDeficiencyDeuter,
|
|
70
70
|
filterDeficiencyTrit,
|
|
71
|
-
parse
|
|
71
|
+
parse,
|
|
72
|
+
toGamut,
|
|
73
|
+
inGamut,
|
|
74
|
+
clampChroma
|
|
72
75
|
} from "culori";
|
|
73
|
-
import { APCAcontrast, sRGBtoY } from "apca-w3";
|
|
76
|
+
import { APCAcontrast, sRGBtoY, displayP3toY } from "apca-w3";
|
|
74
77
|
var toOklch = converter("oklch");
|
|
75
|
-
var toRgbConverter = converter("rgb");
|
|
76
78
|
var toHslConverter = converter("hsl");
|
|
79
|
+
var GAMUT_SPACE = { srgb: "rgb", p3: "p3" };
|
|
80
|
+
var GAMUT_MAPPERS = {
|
|
81
|
+
srgb: toGamut("rgb", "oklch"),
|
|
82
|
+
p3: toGamut("p3", "oklch")
|
|
83
|
+
};
|
|
84
|
+
var GAMUT_CHECKS = {
|
|
85
|
+
srgb: inGamut("rgb"),
|
|
86
|
+
p3: inGamut("p3")
|
|
87
|
+
};
|
|
88
|
+
var GAMUT_CONVERTERS = {
|
|
89
|
+
srgb: converter("rgb"),
|
|
90
|
+
p3: converter("p3")
|
|
91
|
+
};
|
|
92
|
+
function isInGamut(color, gamut = "srgb") {
|
|
93
|
+
const parsed = parse(color);
|
|
94
|
+
if (!parsed) throw new Error(`Invalid color: ${color}`);
|
|
95
|
+
return GAMUT_CHECKS[gamut](parsed);
|
|
96
|
+
}
|
|
97
|
+
function displayed(color, gamut = "srgb") {
|
|
98
|
+
const parsed = typeof color === "string" ? parse(color) : color;
|
|
99
|
+
if (!parsed) throw new Error(`Cannot convert ${String(color)} to RGB`);
|
|
100
|
+
const mapped = GAMUT_CHECKS[gamut](parsed) ? GAMUT_CONVERTERS[gamut](parsed) : GAMUT_MAPPERS[gamut](parsed);
|
|
101
|
+
if (!mapped) throw new Error(`Cannot convert ${color} to ${GAMUT_SPACE[gamut]}`);
|
|
102
|
+
const unit = (v) => Math.min(1, Math.max(0, v ?? 0));
|
|
103
|
+
return { r: unit(mapped.r), g: unit(mapped.g), b: unit(mapped.b) };
|
|
104
|
+
}
|
|
105
|
+
function displayedSrgb(color) {
|
|
106
|
+
return { mode: "rgb", ...displayed(color, "srgb") };
|
|
107
|
+
}
|
|
77
108
|
function parseToOklch(color) {
|
|
78
109
|
const result = toOklch(color);
|
|
79
110
|
if (!result) {
|
|
@@ -82,7 +113,7 @@ function parseToOklch(color) {
|
|
|
82
113
|
return result;
|
|
83
114
|
}
|
|
84
115
|
function oklchToHex(color) {
|
|
85
|
-
const hex = formatHex(color);
|
|
116
|
+
const hex = formatHex(displayedSrgb(color));
|
|
86
117
|
return hex || "#000000";
|
|
87
118
|
}
|
|
88
119
|
function computeScaleChroma(scaleValue, baseChroma) {
|
|
@@ -122,6 +153,23 @@ var LIGHTNESS_FACTORS = {
|
|
|
122
153
|
achromatic: [0.98, 0.95, 0.9, 0.8, 0.66, 0.38, 0, -0.26, -0.44, -0.62, -0.8, -0.92]
|
|
123
154
|
}
|
|
124
155
|
};
|
|
156
|
+
function printOklch(l, c, h) {
|
|
157
|
+
return `oklch(${l} ${c.toFixed(4)} ${h})`;
|
|
158
|
+
}
|
|
159
|
+
function formatOklchInGamut(lightness, chroma, hue, gamut = "srgb") {
|
|
160
|
+
const l = (lightness * 100).toFixed(2) + "%";
|
|
161
|
+
const h = hue.toFixed(2);
|
|
162
|
+
const c = Math.max(0, chroma);
|
|
163
|
+
const printed = printOklch(l, c, h);
|
|
164
|
+
if (isInGamut(printed, gamut)) return printed;
|
|
165
|
+
const at = { mode: "oklch", l: Number(l.slice(0, -1)) / 100, c, h: Number(h) };
|
|
166
|
+
const capped = clampChroma(at, "oklch", GAMUT_SPACE[gamut]);
|
|
167
|
+
let fit = Math.floor((capped.c ?? 0) * 1e4) / 1e4;
|
|
168
|
+
while (fit > 0 && !isInGamut(printOklch(l, fit, h), gamut)) {
|
|
169
|
+
fit = Math.round((fit - 1e-4) * 1e4) / 1e4;
|
|
170
|
+
}
|
|
171
|
+
return printOklch(l, fit, h);
|
|
172
|
+
}
|
|
125
173
|
function buildScale(baseColor, strategy, options) {
|
|
126
174
|
const base = parseToOklch(baseColor);
|
|
127
175
|
const baseLightness = base.l;
|
|
@@ -130,6 +178,7 @@ function buildScale(baseColor, strategy, options) {
|
|
|
130
178
|
const isAchromatic = baseChroma < 0.01;
|
|
131
179
|
const seedShade = options?.seedShade ?? DEFAULT_SEED_SHADE;
|
|
132
180
|
const emitted = resolveShades(options?.shades);
|
|
181
|
+
const gamut = options?.gamut ?? "srgb";
|
|
133
182
|
const maxLightness = Math.max(0.98, baseLightness);
|
|
134
183
|
const minLightness = Math.min(0.15, baseLightness);
|
|
135
184
|
const headroomUp = maxLightness - baseLightness;
|
|
@@ -144,7 +193,7 @@ function buildScale(baseColor, strategy, options) {
|
|
|
144
193
|
const shade = VALID_SHADES[i];
|
|
145
194
|
if (!emitted.includes(shade)) continue;
|
|
146
195
|
if (shade === seedShade) {
|
|
147
|
-
scale[shade] =
|
|
196
|
+
scale[shade] = formatOklchInGamut(baseLightness, baseChroma, baseHue, gamut);
|
|
148
197
|
continue;
|
|
149
198
|
}
|
|
150
199
|
const factor = factors[i];
|
|
@@ -153,10 +202,7 @@ function buildScale(baseColor, strategy, options) {
|
|
|
153
202
|
const chromaShade = chromaShadeFor(i, seedShade);
|
|
154
203
|
const normalizedPosition = (chromaShade - 25) / 925;
|
|
155
204
|
const chroma = isAchromatic ? baseChroma : computeChroma(normalizedPosition, baseChroma);
|
|
156
|
-
|
|
157
|
-
const c = Math.max(0, chroma).toFixed(4);
|
|
158
|
-
const h = baseHue.toFixed(2);
|
|
159
|
-
scale[shade] = `oklch(${l} ${c} ${h})`;
|
|
205
|
+
scale[shade] = formatOklchInGamut(lightness, chroma, baseHue, gamut);
|
|
160
206
|
}
|
|
161
207
|
return scale;
|
|
162
208
|
}
|
|
@@ -180,30 +226,19 @@ function adjustChroma(color, chroma) {
|
|
|
180
226
|
c: Math.max(0, chroma)
|
|
181
227
|
});
|
|
182
228
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
const rsRGB = rgb.r;
|
|
197
|
-
const gsRGB = rgb.g;
|
|
198
|
-
const bsRGB = rgb.b;
|
|
199
|
-
const r = rsRGB <= 0.03928 ? rsRGB / 12.92 : Math.pow((rsRGB + 0.055) / 1.055, 2.4);
|
|
200
|
-
const g = gsRGB <= 0.03928 ? gsRGB / 12.92 : Math.pow((gsRGB + 0.055) / 1.055, 2.4);
|
|
201
|
-
const b = bsRGB <= 0.03928 ? bsRGB / 12.92 : Math.pow((bsRGB + 0.055) / 1.055, 2.4);
|
|
202
|
-
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
203
|
-
}
|
|
204
|
-
function getContrastRatio(color1, color2) {
|
|
205
|
-
const lum1 = getRelativeLuminance(color1);
|
|
206
|
-
const lum2 = getRelativeLuminance(color2);
|
|
229
|
+
var LUMINANCE_WEIGHTS = {
|
|
230
|
+
srgb: [0.2126, 0.7152, 0.0722],
|
|
231
|
+
p3: [0.2289746, 0.6917385, 0.0792869]
|
|
232
|
+
};
|
|
233
|
+
function getRelativeLuminance(color, gamut = "srgb") {
|
|
234
|
+
const rgb = displayed(color, gamut);
|
|
235
|
+
const [wr, wg, wb] = LUMINANCE_WEIGHTS[gamut];
|
|
236
|
+
const linear = (c) => c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
237
|
+
return wr * linear(rgb.r) + wg * linear(rgb.g) + wb * linear(rgb.b);
|
|
238
|
+
}
|
|
239
|
+
function getContrastRatio(color1, color2, gamut = "srgb") {
|
|
240
|
+
const lum1 = getRelativeLuminance(color1, gamut);
|
|
241
|
+
const lum2 = getRelativeLuminance(color2, gamut);
|
|
207
242
|
const lighter = Math.max(lum1, lum2);
|
|
208
243
|
const darker = Math.min(lum1, lum2);
|
|
209
244
|
return (lighter + 0.05) / (darker + 0.05);
|
|
@@ -214,12 +249,11 @@ function meetsWCAG_AA(ratio, isLargeText = false) {
|
|
|
214
249
|
function meetsWCAG_AAA(ratio, isLargeText = false) {
|
|
215
250
|
return isLargeText ? ratio >= 4.5 : ratio >= 7;
|
|
216
251
|
}
|
|
217
|
-
function getAPCAContrast(foreground, background) {
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
return Math.abs(APCAcontrast(fgY, bgY));
|
|
252
|
+
function getAPCAContrast(foreground, background, gamut = "srgb") {
|
|
253
|
+
const fg = displayed(foreground, gamut);
|
|
254
|
+
const bg = displayed(background, gamut);
|
|
255
|
+
const toY = (c) => gamut === "p3" ? displayP3toY([c.r, c.g, c.b]) : sRGBtoY([c.r * 255, c.g * 255, c.b * 255]);
|
|
256
|
+
return Math.abs(APCAcontrast(toY(fg), toY(bg)));
|
|
223
257
|
}
|
|
224
258
|
function findContrastingColorAPCA(background, lightColor, darkColor) {
|
|
225
259
|
const lightLc = getAPCAContrast(lightColor, background);
|
|
@@ -235,7 +269,7 @@ function simulateCVD(color, cvd) {
|
|
|
235
269
|
if (cvd === "none") return color;
|
|
236
270
|
const parsed = parse(color);
|
|
237
271
|
if (!parsed) return color;
|
|
238
|
-
const simulated = cvdFilters[cvd](parsed);
|
|
272
|
+
const simulated = cvdFilters[cvd](displayedSrgb(parsed));
|
|
239
273
|
return formatHex(simulated) || color;
|
|
240
274
|
}
|
|
241
275
|
function classifyAPCALevel(lcValue) {
|
|
@@ -284,14 +318,13 @@ function getOklchString(color) {
|
|
|
284
318
|
function formatColorAs(color, format) {
|
|
285
319
|
switch (format) {
|
|
286
320
|
case "hex":
|
|
287
|
-
return formatHex(color) || "#000000";
|
|
321
|
+
return formatHex(displayedSrgb(color)) || "#000000";
|
|
288
322
|
case "rgb": {
|
|
289
|
-
|
|
290
|
-
if (!rgb) return "rgb(0, 0, 0)";
|
|
291
|
-
return formatRgb(rgb) || "rgb(0, 0, 0)";
|
|
323
|
+
return formatRgb(displayedSrgb(color)) || "rgb(0, 0, 0)";
|
|
292
324
|
}
|
|
293
325
|
case "hsl": {
|
|
294
|
-
const hsl = toHslConverter(color);
|
|
326
|
+
const hsl = toHslConverter(displayedSrgb(color));
|
|
327
|
+
if (hsl && Math.round((hsl.s ?? 0) * 1e4) === 0) hsl.h = 0;
|
|
295
328
|
if (!hsl) return "hsl(0, 0%, 0%)";
|
|
296
329
|
return formatHsl(hsl) || "hsl(0, 0%, 0%)";
|
|
297
330
|
}
|
|
@@ -303,6 +336,23 @@ function formatColorAs(color, format) {
|
|
|
303
336
|
|
|
304
337
|
// src/primitives.ts
|
|
305
338
|
var DEFAULT_NEUTRAL_COLOR = "#747474";
|
|
339
|
+
var ALIAS_PAIRS = [
|
|
340
|
+
["success", "green"],
|
|
341
|
+
["destructive", "red"],
|
|
342
|
+
["warning", "yellow"]
|
|
343
|
+
];
|
|
344
|
+
function aliasOf(name) {
|
|
345
|
+
for (const [semantic, alias] of ALIAS_PAIRS) {
|
|
346
|
+
if (name === semantic) return alias;
|
|
347
|
+
if (name === alias) return semantic;
|
|
348
|
+
}
|
|
349
|
+
return void 0;
|
|
350
|
+
}
|
|
351
|
+
function familySeedShade(name, seedShade, seedShades) {
|
|
352
|
+
if (name === "primary") return seedShade ?? seedShades?.primary;
|
|
353
|
+
const alias = aliasOf(name);
|
|
354
|
+
return seedShades?.[name] ?? (alias ? seedShades?.[alias] : void 0);
|
|
355
|
+
}
|
|
306
356
|
function clamp(v, min, max) {
|
|
307
357
|
return Math.min(max, Math.max(min, v));
|
|
308
358
|
}
|
|
@@ -339,17 +389,21 @@ function deriveWarningColor(primaryColor) {
|
|
|
339
389
|
};
|
|
340
390
|
return oklchToHex(warning);
|
|
341
391
|
}
|
|
342
|
-
function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed",
|
|
392
|
+
function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", options) {
|
|
343
393
|
const gen = lightnessStrategy === "perceptual" ? generateColorScaleAPCA : generateColorScale;
|
|
344
|
-
const genScale = (color) => gen(color,
|
|
394
|
+
const genScale = (name, color) => gen(color, {
|
|
395
|
+
shades: options?.shades,
|
|
396
|
+
gamut: options?.gamut,
|
|
397
|
+
seedShade: familySeedShade(name, options?.seedShade, options?.seedShades)
|
|
398
|
+
});
|
|
345
399
|
const palette = {};
|
|
346
400
|
palette.black = "oklch(0 0 0)";
|
|
347
401
|
palette.white = "oklch(1 0 0)";
|
|
348
|
-
palette.primary = genScale(brandColors.primary);
|
|
349
|
-
palette.neutral = genScale(brandColors.neutral);
|
|
350
|
-
const successScale = genScale(deriveSuccessColor(brandColors.primary));
|
|
351
|
-
const destructiveScale = genScale(deriveDestructiveColor(brandColors.primary));
|
|
352
|
-
const warningScale = genScale(deriveWarningColor(brandColors.primary));
|
|
402
|
+
palette.primary = genScale("primary", brandColors.primary);
|
|
403
|
+
palette.neutral = genScale("neutral", brandColors.neutral);
|
|
404
|
+
const successScale = genScale("success", deriveSuccessColor(brandColors.primary));
|
|
405
|
+
const destructiveScale = genScale("destructive", deriveDestructiveColor(brandColors.primary));
|
|
406
|
+
const warningScale = genScale("warning", deriveWarningColor(brandColors.primary));
|
|
353
407
|
palette.success = successScale;
|
|
354
408
|
palette.destructive = destructiveScale;
|
|
355
409
|
palette.warning = warningScale;
|
|
@@ -358,7 +412,13 @@ function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", scal
|
|
|
358
412
|
palette.yellow = warningScale;
|
|
359
413
|
if (brandColors.additionalColors) {
|
|
360
414
|
for (const [name, color] of Object.entries(brandColors.additionalColors)) {
|
|
361
|
-
palette[name] = genScale(color);
|
|
415
|
+
palette[name] = genScale(name, color);
|
|
416
|
+
}
|
|
417
|
+
for (const [semantic, alias] of ALIAS_PAIRS) {
|
|
418
|
+
const overrodeSemantic = semantic in brandColors.additionalColors;
|
|
419
|
+
const overrodeAlias = alias in brandColors.additionalColors;
|
|
420
|
+
if (overrodeSemantic && !overrodeAlias) palette[alias] = palette[semantic];
|
|
421
|
+
else if (overrodeAlias && !overrodeSemantic) palette[semantic] = palette[alias];
|
|
362
422
|
}
|
|
363
423
|
}
|
|
364
424
|
return palette;
|
|
@@ -371,6 +431,36 @@ function getShade(palette, colorName, shade) {
|
|
|
371
431
|
return scale[shade];
|
|
372
432
|
}
|
|
373
433
|
|
|
434
|
+
// src/bridge.ts
|
|
435
|
+
var BRIDGE_OFFSETS = {
|
|
436
|
+
light: {
|
|
437
|
+
extended: { DEFAULT: 0, subdued: -5, highlight: 2 },
|
|
438
|
+
ring: -1
|
|
439
|
+
},
|
|
440
|
+
dark: {
|
|
441
|
+
extended: { DEFAULT: 1, subdued: -3, highlight: -1 },
|
|
442
|
+
ring: 2
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
function bridgeShade(offset, seedShade, active) {
|
|
446
|
+
const anchor = VALID_SHADES.indexOf(seedShade ?? DEFAULT_SEED_SHADE);
|
|
447
|
+
const index = Math.min(VALID_SHADES.length - 1, Math.max(0, anchor + offset));
|
|
448
|
+
return snapShade(VALID_SHADES[index], active);
|
|
449
|
+
}
|
|
450
|
+
function extendedBridgeShades(mode, seedShade, active, overrides) {
|
|
451
|
+
const offsets = BRIDGE_OFFSETS[mode].extended;
|
|
452
|
+
const at = (offset) => bridgeShade(offset, seedShade, active);
|
|
453
|
+
const snap = (shade) => snapShade(shade, active);
|
|
454
|
+
return {
|
|
455
|
+
DEFAULT: overrides?.DEFAULT ? snap(overrides.DEFAULT) : at(offsets.DEFAULT),
|
|
456
|
+
subdued: overrides?.subdued ? snap(overrides.subdued) : at(offsets.subdued),
|
|
457
|
+
highlight: overrides?.highlight ? snap(overrides.highlight) : at(offsets.highlight)
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function ringBridgeShade(mode, seedShade, active) {
|
|
461
|
+
return bridgeShade(BRIDGE_OFFSETS[mode].ring, seedShade, active);
|
|
462
|
+
}
|
|
463
|
+
|
|
374
464
|
// src/semantic.ts
|
|
375
465
|
function shadeKeys(shades) {
|
|
376
466
|
return resolveShades(shades);
|
|
@@ -396,14 +486,18 @@ function getContrastThresholds(algorithm, target) {
|
|
|
396
486
|
}
|
|
397
487
|
}
|
|
398
488
|
}
|
|
399
|
-
function findBestForeground(
|
|
489
|
+
function findBestForeground(views, white, black, algorithm, contrastTarget, colorName, activeShades) {
|
|
400
490
|
const { floor, target } = getContrastThresholds(algorithm, contrastTarget);
|
|
401
|
-
const getContrast = (
|
|
402
|
-
|
|
491
|
+
const getContrast = (fgOf) => Math.min(
|
|
492
|
+
...views.map(
|
|
493
|
+
(view) => algorithm === "apca" ? getAPCAContrast(fgOf(view), view.background, view.gamut) : getContrastRatio(view.background, fgOf(view), view.gamut)
|
|
494
|
+
)
|
|
495
|
+
);
|
|
496
|
+
function searchScale(which, refColorName) {
|
|
403
497
|
let bestPassing = null;
|
|
404
498
|
let bestOverall = null;
|
|
405
499
|
for (const shade of activeShades) {
|
|
406
|
-
const c = getContrast(
|
|
500
|
+
const c = getContrast((view) => view[which][shade]);
|
|
407
501
|
if (c >= floor) {
|
|
408
502
|
if (!bestPassing || Math.abs(c - target) < Math.abs(bestPassing.contrast - target)) {
|
|
409
503
|
bestPassing = { shade, contrast: c };
|
|
@@ -416,17 +510,17 @@ function findBestForeground(background, scale, neutralScale, white, black, algor
|
|
|
416
510
|
const pick = bestPassing ?? bestOverall;
|
|
417
511
|
if (!pick) return null;
|
|
418
512
|
return {
|
|
419
|
-
value:
|
|
513
|
+
value: views[0][which][pick.shade],
|
|
420
514
|
ref: { type: "shade", colorName: refColorName, shade: pick.shade },
|
|
421
515
|
contrast: pick.contrast,
|
|
422
516
|
passes: pick === bestPassing
|
|
423
517
|
};
|
|
424
518
|
}
|
|
425
|
-
const ownResult = searchScale(scale, colorName);
|
|
519
|
+
const ownResult = searchScale("scale", colorName);
|
|
426
520
|
if (ownResult?.passes) {
|
|
427
521
|
return { value: ownResult.value, ref: ownResult.ref };
|
|
428
522
|
}
|
|
429
|
-
const neutralResult = searchScale(
|
|
523
|
+
const neutralResult = searchScale("neutral", "neutral");
|
|
430
524
|
if (neutralResult?.passes) {
|
|
431
525
|
if (!ownResult?.passes) {
|
|
432
526
|
return { value: neutralResult.value, ref: neutralResult.ref };
|
|
@@ -442,25 +536,30 @@ function findBestForeground(background, scale, neutralScale, white, black, algor
|
|
|
442
536
|
if (neutralResult) {
|
|
443
537
|
return { value: neutralResult.value, ref: neutralResult.ref };
|
|
444
538
|
}
|
|
445
|
-
const whiteC = getContrast(white);
|
|
446
|
-
const blackC = getContrast(black);
|
|
539
|
+
const whiteC = getContrast(() => white);
|
|
540
|
+
const blackC = getContrast(() => black);
|
|
447
541
|
if (whiteC >= blackC) return { value: white, ref: { type: "white" } };
|
|
448
542
|
return { value: black, ref: { type: "black" } };
|
|
449
543
|
}
|
|
450
|
-
function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades) {
|
|
544
|
+
function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades, seedShade, gamuts = { own: "srgb" }) {
|
|
451
545
|
const activeShades = shadeKeys(shades);
|
|
452
|
-
const findFg = (
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
546
|
+
const findFg = (bgShade) => {
|
|
547
|
+
const views = [
|
|
548
|
+
{ background: scale[bgShade], scale, neutral, gamut: gamuts.own }
|
|
549
|
+
];
|
|
550
|
+
if (gamuts.other) {
|
|
551
|
+
const o = gamuts.other;
|
|
552
|
+
views.push({ background: o.scale[bgShade], scale: o.scale, neutral: o.neutral, gamut: o.gamut });
|
|
553
|
+
}
|
|
554
|
+
return findBestForeground(views, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades);
|
|
555
|
+
};
|
|
556
|
+
const { DEFAULT: defaultShade, subdued: subduedShade, highlight: highlightShade } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
|
|
458
557
|
const DEFAULT = scale[defaultShade];
|
|
459
558
|
const subdued = scale[subduedShade];
|
|
460
559
|
const highlight = scale[highlightShade];
|
|
461
|
-
const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(
|
|
462
|
-
const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : findFg(
|
|
463
|
-
const hfgResult = overrides?.["on-highlight"] ? { value: scale[overrides["on-highlight"]], ref: { type: "shade", colorName, shade: overrides["on-highlight"] } } : findFg(
|
|
560
|
+
const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(defaultShade);
|
|
561
|
+
const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : findFg(subduedShade);
|
|
562
|
+
const hfgResult = overrides?.["on-highlight"] ? { value: scale[overrides["on-highlight"]], ref: { type: "shade", colorName, shade: overrides["on-highlight"] } } : findFg(highlightShade);
|
|
464
563
|
return {
|
|
465
564
|
DEFAULT,
|
|
466
565
|
foreground: fgResult.value,
|
|
@@ -475,11 +574,12 @@ function createExtendedSemanticColor(scale, neutral, white, black, mode, contras
|
|
|
475
574
|
}
|
|
476
575
|
};
|
|
477
576
|
}
|
|
478
|
-
function generateSemanticTokens(palette, mode, options) {
|
|
577
|
+
function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }) {
|
|
479
578
|
const contrastAlgorithm = options?.contrastAlgorithm ?? "wcag";
|
|
480
579
|
const contrastTarget = options?.contrastTarget ?? "aa";
|
|
481
580
|
const shadeOverrides = options?.shadeOverrides ?? {};
|
|
482
581
|
const activeShades = shadeKeys(options?.shades);
|
|
582
|
+
const anchor = (name) => familySeedShade(name, options?.seedShade, options?.seedShades);
|
|
483
583
|
const neutral = palette.neutral;
|
|
484
584
|
if (!neutral) {
|
|
485
585
|
throw new Error("Neutral palette not found");
|
|
@@ -493,6 +593,17 @@ function generateSemanticTokens(palette, mode, options) {
|
|
|
493
593
|
if (!primary || !success || !destructive || !warning) {
|
|
494
594
|
throw new Error("Required color palettes not found");
|
|
495
595
|
}
|
|
596
|
+
const other = gamuts.other;
|
|
597
|
+
const gamutsFor = (name) => ({
|
|
598
|
+
own: gamuts.own,
|
|
599
|
+
...other ? {
|
|
600
|
+
other: {
|
|
601
|
+
gamut: other.gamut,
|
|
602
|
+
scale: other.palette[name],
|
|
603
|
+
neutral: other.palette.neutral
|
|
604
|
+
}
|
|
605
|
+
} : {}
|
|
606
|
+
});
|
|
496
607
|
const muted = mode === "light" ? {
|
|
497
608
|
DEFAULT: neutral[snapShade(25, activeShades)],
|
|
498
609
|
foreground: neutral[snapShade(500, activeShades)]
|
|
@@ -501,11 +612,11 @@ function generateSemanticTokens(palette, mode, options) {
|
|
|
501
612
|
foreground: neutral[snapShade(200, activeShades)]
|
|
502
613
|
};
|
|
503
614
|
const tokens = {
|
|
504
|
-
primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades),
|
|
505
|
-
neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades),
|
|
506
|
-
success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades),
|
|
507
|
-
destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades),
|
|
508
|
-
warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades),
|
|
615
|
+
primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, anchor("primary"), gamutsFor("primary")),
|
|
616
|
+
neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades, anchor("neutral"), gamutsFor("neutral")),
|
|
617
|
+
success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, anchor("success"), gamutsFor("success")),
|
|
618
|
+
destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, anchor("destructive"), gamutsFor("destructive")),
|
|
619
|
+
warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning")),
|
|
509
620
|
muted
|
|
510
621
|
};
|
|
511
622
|
const coreColors = [
|
|
@@ -532,7 +643,9 @@ function generateSemanticTokens(palette, mode, options) {
|
|
|
532
643
|
contrastTarget,
|
|
533
644
|
shadeOverrides[colorName]?.[mode],
|
|
534
645
|
colorName,
|
|
535
|
-
activeShades
|
|
646
|
+
activeShades,
|
|
647
|
+
anchor(colorName),
|
|
648
|
+
gamutsFor(colorName)
|
|
536
649
|
);
|
|
537
650
|
}
|
|
538
651
|
}
|
|
@@ -578,7 +691,7 @@ function generateSurfaceTokens(palette, mode, overrides, shades) {
|
|
|
578
691
|
}
|
|
579
692
|
};
|
|
580
693
|
}
|
|
581
|
-
function generateUtilityTokens(palette, mode, overrides, shades) {
|
|
694
|
+
function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
|
|
582
695
|
const activeShades = shadeKeys(shades);
|
|
583
696
|
const snap = (shade) => snapShade(shade, activeShades);
|
|
584
697
|
const neutral = palette.neutral;
|
|
@@ -586,7 +699,8 @@ function generateUtilityTokens(palette, mode, overrides, shades) {
|
|
|
586
699
|
if (!neutral || !primary) {
|
|
587
700
|
throw new Error("Required palettes not found");
|
|
588
701
|
}
|
|
589
|
-
const
|
|
702
|
+
const ring = ringBridgeShade(mode, seedShade, activeShades);
|
|
703
|
+
const defaults = mode === "light" ? { border: snap(200), input: snap(200), ring, shadow: snap(950) } : { border: snap(800), input: snap(800), ring, shadow: snap(950) };
|
|
590
704
|
return {
|
|
591
705
|
border: neutral[overrides?.border ? snap(overrides.border) : defaults.border],
|
|
592
706
|
input: neutral[overrides?.input ? snap(overrides.input) : defaults.input],
|
|
@@ -594,7 +708,7 @@ function generateUtilityTokens(palette, mode, overrides, shades) {
|
|
|
594
708
|
shadow: neutral[overrides?.shadow ? snap(overrides.shadow) : defaults.shadow]
|
|
595
709
|
};
|
|
596
710
|
}
|
|
597
|
-
function checkSemanticContrast(tokens) {
|
|
711
|
+
function checkSemanticContrast(tokens, gamut = "srgb") {
|
|
598
712
|
const results = {};
|
|
599
713
|
for (const [name, color] of Object.entries(tokens)) {
|
|
600
714
|
if (name === "muted") {
|
|
@@ -603,7 +717,8 @@ function checkSemanticContrast(tokens) {
|
|
|
603
717
|
const extendedColor = color;
|
|
604
718
|
const ratio = getContrastRatio(
|
|
605
719
|
extendedColor.DEFAULT,
|
|
606
|
-
extendedColor.foreground
|
|
720
|
+
extendedColor.foreground,
|
|
721
|
+
gamut
|
|
607
722
|
);
|
|
608
723
|
results[`${name}/default`] = {
|
|
609
724
|
ratio: Math.round(ratio * 100) / 100,
|
|
@@ -612,7 +727,8 @@ function checkSemanticContrast(tokens) {
|
|
|
612
727
|
};
|
|
613
728
|
const subduedRatio = getContrastRatio(
|
|
614
729
|
extendedColor.subdued,
|
|
615
|
-
extendedColor["subdued-foreground"]
|
|
730
|
+
extendedColor["subdued-foreground"],
|
|
731
|
+
gamut
|
|
616
732
|
);
|
|
617
733
|
results[`${name}/subdued`] = {
|
|
618
734
|
ratio: Math.round(subduedRatio * 100) / 100,
|
|
@@ -621,7 +737,8 @@ function checkSemanticContrast(tokens) {
|
|
|
621
737
|
};
|
|
622
738
|
const highlightRatio = getContrastRatio(
|
|
623
739
|
extendedColor.highlight,
|
|
624
|
-
extendedColor["highlight-foreground"]
|
|
740
|
+
extendedColor["highlight-foreground"],
|
|
741
|
+
gamut
|
|
625
742
|
);
|
|
626
743
|
results[`${name}/highlight`] = {
|
|
627
744
|
ratio: Math.round(highlightRatio * 100) / 100,
|
|
@@ -631,7 +748,8 @@ function checkSemanticContrast(tokens) {
|
|
|
631
748
|
}
|
|
632
749
|
const mutedRatio = getContrastRatio(
|
|
633
750
|
tokens.muted.DEFAULT,
|
|
634
|
-
tokens.muted.foreground
|
|
751
|
+
tokens.muted.foreground,
|
|
752
|
+
gamut
|
|
635
753
|
);
|
|
636
754
|
results["muted"] = {
|
|
637
755
|
ratio: Math.round(mutedRatio * 100) / 100,
|
|
@@ -640,43 +758,86 @@ function checkSemanticContrast(tokens) {
|
|
|
640
758
|
};
|
|
641
759
|
return results;
|
|
642
760
|
}
|
|
643
|
-
function checkSemanticContrastAPCA(tokens) {
|
|
761
|
+
function checkSemanticContrastAPCA(tokens, gamut = "srgb") {
|
|
644
762
|
const results = {};
|
|
645
763
|
for (const [name, color] of Object.entries(tokens)) {
|
|
646
764
|
if (name === "muted") {
|
|
647
765
|
continue;
|
|
648
766
|
}
|
|
649
767
|
const ext = color;
|
|
650
|
-
const defaultLc = getAPCAContrast(ext.foreground, ext.DEFAULT);
|
|
768
|
+
const defaultLc = getAPCAContrast(ext.foreground, ext.DEFAULT, gamut);
|
|
651
769
|
results[`${name}/default`] = {
|
|
652
770
|
lcValue: Math.round(defaultLc * 10) / 10,
|
|
653
771
|
level: classifyAPCALevel(defaultLc)
|
|
654
772
|
};
|
|
655
|
-
const subduedLc = getAPCAContrast(ext["subdued-foreground"], ext.subdued);
|
|
773
|
+
const subduedLc = getAPCAContrast(ext["subdued-foreground"], ext.subdued, gamut);
|
|
656
774
|
results[`${name}/subdued`] = {
|
|
657
775
|
lcValue: Math.round(subduedLc * 10) / 10,
|
|
658
776
|
level: classifyAPCALevel(subduedLc)
|
|
659
777
|
};
|
|
660
|
-
const highlightLc = getAPCAContrast(ext["highlight-foreground"], ext.highlight);
|
|
778
|
+
const highlightLc = getAPCAContrast(ext["highlight-foreground"], ext.highlight, gamut);
|
|
661
779
|
results[`${name}/highlight`] = {
|
|
662
780
|
lcValue: Math.round(highlightLc * 10) / 10,
|
|
663
781
|
level: classifyAPCALevel(highlightLc)
|
|
664
782
|
};
|
|
665
783
|
}
|
|
666
|
-
const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT);
|
|
784
|
+
const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT, gamut);
|
|
667
785
|
results["muted"] = {
|
|
668
786
|
lcValue: Math.round(mutedLc * 10) / 10,
|
|
669
787
|
level: classifyAPCALevel(mutedLc)
|
|
670
788
|
};
|
|
671
789
|
return results;
|
|
672
790
|
}
|
|
673
|
-
|
|
791
|
+
var NON_TEXT_CONTRAST_MIN = 3;
|
|
792
|
+
function checkSurfaceContrast(surface, utility, gamut = "srgb") {
|
|
793
|
+
const pair = (bg, fg, nonText = false, advisory = false) => {
|
|
794
|
+
const ratio = getContrastRatio(bg, fg, gamut);
|
|
795
|
+
return {
|
|
796
|
+
ratio: Math.round(ratio * 100) / 100,
|
|
797
|
+
passesAA: meetsWCAG_AA(ratio),
|
|
798
|
+
passesAAA: ratio >= 7,
|
|
799
|
+
nonText,
|
|
800
|
+
passes: nonText ? ratio >= NON_TEXT_CONTRAST_MIN : meetsWCAG_AA(ratio),
|
|
801
|
+
advisory
|
|
802
|
+
};
|
|
803
|
+
};
|
|
804
|
+
return {
|
|
805
|
+
"canvas/foreground": pair(surface.canvas, surface["canvas-foreground"]),
|
|
806
|
+
"surface/foreground": pair(surface.surface, surface["surface-foreground"]),
|
|
807
|
+
"overlay/foreground": pair(surface.overlay, surface["overlay-foreground"]),
|
|
808
|
+
"ring/canvas": pair(surface.canvas, utility.ring, true),
|
|
809
|
+
"border/canvas": pair(surface.canvas, utility.border, true, true)
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
var LOW_CHROMA_THRESHOLD = 0.04;
|
|
813
|
+
function checkRampChroma(palette) {
|
|
814
|
+
const results = {};
|
|
815
|
+
for (const [name, scale] of Object.entries(palette)) {
|
|
816
|
+
if (typeof scale === "string" || !scale) continue;
|
|
817
|
+
let peak = 0;
|
|
818
|
+
let atShade = 500;
|
|
819
|
+
for (const [shade, color] of Object.entries(scale)) {
|
|
820
|
+
const { c } = parseToOklch(color);
|
|
821
|
+
if (c > peak) {
|
|
822
|
+
peak = c;
|
|
823
|
+
atShade = Number(shade);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
results[name] = {
|
|
827
|
+
peak: Math.round(peak * 1e4) / 1e4,
|
|
828
|
+
atShade,
|
|
829
|
+
low: peak < LOW_CHROMA_THRESHOLD
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
return results;
|
|
833
|
+
}
|
|
834
|
+
function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa", gamut = "srgb") {
|
|
674
835
|
const { floor } = getContrastThresholds(algorithm, contrastTarget);
|
|
675
836
|
const skipKeys = /* @__PURE__ */ new Set(["muted"]);
|
|
676
837
|
let passed = 0;
|
|
677
838
|
let total = 0;
|
|
678
839
|
if (algorithm === "wcag") {
|
|
679
|
-
const results = checkSemanticContrast(tokens);
|
|
840
|
+
const results = checkSemanticContrast(tokens, gamut);
|
|
680
841
|
for (const [key, result] of Object.entries(results)) {
|
|
681
842
|
const colorName = key.split("/")[0];
|
|
682
843
|
if (skipKeys.has(colorName)) continue;
|
|
@@ -686,7 +847,7 @@ function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa") {
|
|
|
686
847
|
}
|
|
687
848
|
}
|
|
688
849
|
} else {
|
|
689
|
-
const results = checkSemanticContrastAPCA(tokens);
|
|
850
|
+
const results = checkSemanticContrastAPCA(tokens, gamut);
|
|
690
851
|
for (const [key, result] of Object.entries(results)) {
|
|
691
852
|
const colorName = key.split("/")[0];
|
|
692
853
|
if (skipKeys.has(colorName)) continue;
|
|
@@ -775,35 +936,39 @@ var defaultShadows = {
|
|
|
775
936
|
}
|
|
776
937
|
};
|
|
777
938
|
function generateTokens(brandColors, options) {
|
|
778
|
-
const
|
|
779
|
-
|
|
780
|
-
options?.
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
const
|
|
785
|
-
const
|
|
786
|
-
const
|
|
787
|
-
|
|
788
|
-
const darkUtility = generateUtilityTokens(primitives, "dark", options?.utilityShadeOverrides?.dark, options?.shades);
|
|
789
|
-
return {
|
|
790
|
-
primitives,
|
|
939
|
+
const strategy = options?.lightnessStrategy ?? "fixed";
|
|
940
|
+
const scaleOptions = {
|
|
941
|
+
seedShade: options?.seedShade,
|
|
942
|
+
seedShades: options?.seedShades,
|
|
943
|
+
shades: options?.shades
|
|
944
|
+
};
|
|
945
|
+
const primitives = generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "srgb" });
|
|
946
|
+
const wide = options?.gamut === "p3" ? generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "p3" }) : void 0;
|
|
947
|
+
const colourTiers = (palette, gamuts) => ({
|
|
948
|
+
primitives: palette,
|
|
791
949
|
semantic: {
|
|
792
|
-
light:
|
|
793
|
-
dark:
|
|
950
|
+
light: generateSemanticTokens(palette, "light", options, gamuts),
|
|
951
|
+
dark: generateSemanticTokens(palette, "dark", options, gamuts)
|
|
794
952
|
},
|
|
795
953
|
surface: {
|
|
796
|
-
light:
|
|
797
|
-
dark:
|
|
954
|
+
light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades),
|
|
955
|
+
dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades)
|
|
798
956
|
},
|
|
799
957
|
utility: {
|
|
800
|
-
light:
|
|
801
|
-
dark:
|
|
802
|
-
}
|
|
958
|
+
light: generateUtilityTokens(palette, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade),
|
|
959
|
+
dark: generateUtilityTokens(palette, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade)
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
return {
|
|
963
|
+
...colourTiers(primitives, {
|
|
964
|
+
own: "srgb",
|
|
965
|
+
...wide ? { other: { gamut: "p3", palette: wide } } : {}
|
|
966
|
+
}),
|
|
803
967
|
spacing: {},
|
|
804
968
|
typography: defaultTypography,
|
|
805
969
|
radii: defaultRadii,
|
|
806
|
-
shadows: defaultShadows
|
|
970
|
+
shadows: defaultShadows,
|
|
971
|
+
...wide ? { p3: colourTiers(wide, { own: "p3", other: { gamut: "srgb", palette: primitives } }) } : {}
|
|
807
972
|
};
|
|
808
973
|
}
|
|
809
974
|
|
|
@@ -816,64 +981,57 @@ function colorScaleToCssVars(name, scale, colorFormat = "hex", indent = " ") {
|
|
|
816
981
|
([shade, color]) => `${indent}--color-${name}-${shade}: ${formatColor(color, colorFormat)};`
|
|
817
982
|
);
|
|
818
983
|
}
|
|
819
|
-
function emitPrimitiveVars(tokens, colorFormat, want, indent) {
|
|
984
|
+
function emitPrimitiveVars(tokens, colorFormat, want, indent, inFamily, scoped) {
|
|
820
985
|
const lines = [];
|
|
821
986
|
const rampsOnly = !want.has("primitives");
|
|
822
987
|
for (const [name, scale] of Object.entries(tokens.primitives)) {
|
|
823
988
|
if (typeof scale === "string") {
|
|
824
|
-
if (rampsOnly) continue;
|
|
989
|
+
if (rampsOnly || scoped) continue;
|
|
825
990
|
lines.push(`${indent}--color-${name}: ${formatColor(scale, colorFormat)};`);
|
|
826
991
|
} else {
|
|
992
|
+
if (!inFamily(name)) continue;
|
|
827
993
|
lines.push(...colorScaleToCssVars(name, scale, colorFormat, indent));
|
|
828
994
|
}
|
|
829
995
|
}
|
|
830
996
|
return lines;
|
|
831
997
|
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
const
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
998
|
+
var WIDE_GAMUT_QUERY = "@media (color-gamut: p3)";
|
|
999
|
+
function emitWideGamutBlock(tokens, inFamily) {
|
|
1000
|
+
if (!tokens.p3) return [];
|
|
1001
|
+
const lines = [];
|
|
1002
|
+
for (const [name, scale] of Object.entries(tokens.p3.primitives)) {
|
|
1003
|
+
if (typeof scale === "string" || !inFamily(name)) continue;
|
|
1004
|
+
lines.push(...colorScaleToCssVars(name, scale, "oklch", " "));
|
|
1005
|
+
}
|
|
1006
|
+
if (lines.length === 0) return [];
|
|
1007
|
+
return [
|
|
1008
|
+
"",
|
|
1009
|
+
"/* Wide gamut: the same ramps with the chroma a P3 display can show. Every",
|
|
1010
|
+
" semantic pairing is contrast-checked on these values and on the sRGB ones. */",
|
|
1011
|
+
`${WIDE_GAMUT_QUERY} {`,
|
|
1012
|
+
" :root {",
|
|
1013
|
+
...lines,
|
|
1014
|
+
" }",
|
|
1015
|
+
"}"
|
|
1016
|
+
];
|
|
841
1017
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
muted: {
|
|
850
|
-
DEFAULT: 25,
|
|
851
|
-
foreground: 500
|
|
852
|
-
}
|
|
853
|
-
},
|
|
854
|
-
dark: {
|
|
855
|
-
extended: {
|
|
856
|
-
DEFAULT: 600,
|
|
857
|
-
subdued: 200,
|
|
858
|
-
highlight: 400
|
|
859
|
-
},
|
|
860
|
-
muted: {
|
|
861
|
-
DEFAULT: 800,
|
|
862
|
-
foreground: 200
|
|
863
|
-
}
|
|
864
|
-
}
|
|
1018
|
+
function getExtendedMappings(mode, colorName, overrides, active, anchors) {
|
|
1019
|
+
const seedShade = familySeedShade(colorName, anchors.seedShade, anchors.seedShades);
|
|
1020
|
+
return extendedBridgeShades(mode, seedShade, active, overrides?.[colorName]?.[mode]);
|
|
1021
|
+
}
|
|
1022
|
+
var MUTED_SHADE_MAPPINGS = {
|
|
1023
|
+
light: { DEFAULT: 25, foreground: 500 },
|
|
1024
|
+
dark: { DEFAULT: 800, foreground: 200 }
|
|
865
1025
|
};
|
|
866
1026
|
var UTILITY_SHADE_MAPPINGS = {
|
|
867
1027
|
light: {
|
|
868
1028
|
border: "neutral-200",
|
|
869
1029
|
input: "neutral-200",
|
|
870
|
-
ring: "primary-400",
|
|
871
1030
|
shadow: "neutral-950"
|
|
872
1031
|
},
|
|
873
1032
|
dark: {
|
|
874
1033
|
border: "neutral-800",
|
|
875
1034
|
input: "neutral-800",
|
|
876
|
-
ring: "primary-700",
|
|
877
1035
|
shadow: "neutral-950"
|
|
878
1036
|
}
|
|
879
1037
|
};
|
|
@@ -919,12 +1077,13 @@ function emitSurfaceVars(surface, colorFormat, indent) {
|
|
|
919
1077
|
}
|
|
920
1078
|
return lines;
|
|
921
1079
|
}
|
|
922
|
-
function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active) {
|
|
1080
|
+
function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors) {
|
|
923
1081
|
const lines = [];
|
|
924
1082
|
const extendedSemanticNames = ["primary", "neutral", "success", "destructive", "warning"];
|
|
925
1083
|
for (const name of extendedSemanticNames) {
|
|
1084
|
+
if (!inFamily(name)) continue;
|
|
926
1085
|
const color = tokens.semantic[mode][name];
|
|
927
|
-
const mappings = getExtendedMappings(mode, name, shadeOverrides, active);
|
|
1086
|
+
const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
|
|
928
1087
|
lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
|
|
929
1088
|
lines.push(`${indent}--${name}-foreground: ${color._refs ? refToVar(color._refs.foreground) : formatColor(color.foreground, colorFormat)};`);
|
|
930
1089
|
lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
|
|
@@ -933,9 +1092,9 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
|
|
|
933
1092
|
lines.push(`${indent}--${name}-highlight-foreground: ${color._refs ? refToVar(color._refs["highlight-foreground"]) : formatColor(color["highlight-foreground"], colorFormat)};`);
|
|
934
1093
|
}
|
|
935
1094
|
for (const [name, color] of Object.entries(tokens.semantic[mode])) {
|
|
936
|
-
if (!extendedSemanticNames.includes(name) && name !== "muted") {
|
|
1095
|
+
if (!extendedSemanticNames.includes(name) && name !== "muted" && inFamily(name)) {
|
|
937
1096
|
const ext = color;
|
|
938
|
-
const mappings = getExtendedMappings(mode, name, shadeOverrides, active);
|
|
1097
|
+
const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
|
|
939
1098
|
lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
|
|
940
1099
|
lines.push(`${indent}--${name}-foreground: ${ext._refs ? refToVar(ext._refs.foreground) : formatColor(ext.foreground, colorFormat)};`);
|
|
941
1100
|
lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
|
|
@@ -944,41 +1103,53 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
|
|
|
944
1103
|
lines.push(`${indent}--${name}-highlight-foreground: ${ext._refs ? refToVar(ext._refs["highlight-foreground"]) : formatColor(ext["highlight-foreground"], colorFormat)};`);
|
|
945
1104
|
}
|
|
946
1105
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1106
|
+
if (!scoped || inFamily("neutral")) {
|
|
1107
|
+
const muted = MUTED_SHADE_MAPPINGS[mode];
|
|
1108
|
+
lines.push(`${indent}--muted: var(--color-neutral-${snapShade(muted.DEFAULT, active)});`);
|
|
1109
|
+
lines.push(`${indent}--muted-foreground: var(--color-neutral-${snapShade(muted.foreground, active)});`);
|
|
1110
|
+
}
|
|
950
1111
|
return lines;
|
|
951
1112
|
}
|
|
952
|
-
function emitUtilityVars(mode, indent, active) {
|
|
1113
|
+
function emitUtilityVars(mode, indent, active, seedShade) {
|
|
1114
|
+
const fixed = UTILITY_SHADE_MAPPINGS[mode];
|
|
1115
|
+
const refs = [
|
|
1116
|
+
["border", fixed.border],
|
|
1117
|
+
["input", fixed.input],
|
|
1118
|
+
["ring", `primary-${ringBridgeShade(mode, seedShade, active)}`],
|
|
1119
|
+
["shadow", fixed.shadow]
|
|
1120
|
+
];
|
|
953
1121
|
const lines = [];
|
|
954
|
-
for (const [token, ref] of
|
|
1122
|
+
for (const [token, ref] of refs) {
|
|
955
1123
|
const [scale, shade] = ref.split(/-(?=\d+$)/);
|
|
956
1124
|
const snapped = `${scale}-${snapShade(Number(shade), active)}`;
|
|
957
1125
|
lines.push(`${indent}--${token}: ${primitiveToVar(snapped)};`);
|
|
958
1126
|
}
|
|
959
1127
|
return lines;
|
|
960
1128
|
}
|
|
961
|
-
function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades) {
|
|
1129
|
+
function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades, families, seedShade, seedShades) {
|
|
962
1130
|
const lines = [];
|
|
1131
|
+
const anchors = { seedShade, seedShades };
|
|
963
1132
|
const active = resolveShades(shades);
|
|
964
1133
|
const want = sectionSet(sections);
|
|
1134
|
+
const scoped = !!families && families.length > 0;
|
|
1135
|
+
const inFamily = familyFilter(families);
|
|
965
1136
|
const wantPrimitives = want.has("primitives") || want.has("ramp");
|
|
966
|
-
const wantModes = want.has("semantic") || want.has("surface") || want.has("utility");
|
|
1137
|
+
const wantModes = want.has("semantic") || !scoped && (want.has("surface") || want.has("utility"));
|
|
967
1138
|
const modeBody = (mode, indent, withComments) => {
|
|
968
1139
|
const body = [];
|
|
969
|
-
if (want.has("surface")) {
|
|
1140
|
+
if (want.has("surface") && !scoped) {
|
|
970
1141
|
if (withComments) body.push(`${indent}/* Surface */`);
|
|
971
1142
|
body.push(...emitSurfaceVars(tokens.surface[mode], colorFormat, indent));
|
|
972
1143
|
if (withComments) body.push("");
|
|
973
1144
|
}
|
|
974
|
-
if (want.has("utility")) {
|
|
1145
|
+
if (want.has("utility") && !scoped) {
|
|
975
1146
|
if (withComments) body.push(`${indent}/* Utility */`);
|
|
976
|
-
body.push(...emitUtilityVars(mode, indent, active));
|
|
1147
|
+
body.push(...emitUtilityVars(mode, indent, active, seedShade));
|
|
977
1148
|
if (withComments) body.push("");
|
|
978
1149
|
}
|
|
979
1150
|
if (want.has("semantic")) {
|
|
980
1151
|
if (withComments) body.push(`${indent}/* Semantic Colors */`);
|
|
981
|
-
body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active));
|
|
1152
|
+
body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
982
1153
|
}
|
|
983
1154
|
while (body.length > 0 && body[body.length - 1] === "") body.pop();
|
|
984
1155
|
return body;
|
|
@@ -986,30 +1157,58 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
986
1157
|
if (wantPrimitives) {
|
|
987
1158
|
lines.push(":root {");
|
|
988
1159
|
lines.push(" /* Primitive Colors */");
|
|
989
|
-
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " "));
|
|
1160
|
+
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " ", inFamily, scoped));
|
|
990
1161
|
lines.push("}");
|
|
1162
|
+
lines.push(...emitWideGamutBlock(tokens, inFamily));
|
|
991
1163
|
}
|
|
992
1164
|
if (wantModes) {
|
|
993
1165
|
if (wantPrimitives) lines.push("");
|
|
994
1166
|
lines.push("/* Light Mode */");
|
|
995
|
-
lines.push(
|
|
1167
|
+
lines.push(`${MODE_SELECTORS.light} {`);
|
|
996
1168
|
lines.push(...modeBody("light", " ", true));
|
|
997
1169
|
lines.push("}");
|
|
998
1170
|
lines.push("");
|
|
999
1171
|
lines.push("/* Dark Mode */");
|
|
1000
|
-
lines.push(
|
|
1172
|
+
lines.push(`${MODE_SELECTORS.dark} {`);
|
|
1001
1173
|
lines.push(...modeBody("dark", " ", true));
|
|
1002
1174
|
lines.push("}");
|
|
1003
1175
|
lines.push("");
|
|
1004
1176
|
lines.push("/* System Preference: Dark Mode */");
|
|
1005
1177
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
1006
|
-
lines.push(
|
|
1178
|
+
lines.push(` ${MODE_SELECTORS.systemDark} {`);
|
|
1007
1179
|
lines.push(...modeBody("dark", " ", false));
|
|
1008
1180
|
lines.push(" }");
|
|
1009
1181
|
lines.push("}");
|
|
1010
1182
|
}
|
|
1011
1183
|
return lines.join("\n");
|
|
1012
1184
|
}
|
|
1185
|
+
var MODE_SELECTORS = {
|
|
1186
|
+
light: ':root, [data-theme="light"]',
|
|
1187
|
+
dark: '[data-theme="dark"], .dark',
|
|
1188
|
+
systemDark: ':root:not([data-theme="light"]):not(.light)'
|
|
1189
|
+
};
|
|
1190
|
+
function overrideContract() {
|
|
1191
|
+
return [
|
|
1192
|
+
"/*",
|
|
1193
|
+
" * Overriding these tokens: match the selector, not just the layer.",
|
|
1194
|
+
" *",
|
|
1195
|
+
" * These blocks are UNLAYERED. An override inside @layer loses to them",
|
|
1196
|
+
" * regardless of import order. An unlayered override still loses if it is",
|
|
1197
|
+
" * less specific, so match the selector you are overriding:",
|
|
1198
|
+
" *",
|
|
1199
|
+
` * ${MODE_SELECTORS.light.split(", ")[0]},`,
|
|
1200
|
+
` * ${MODE_SELECTORS.systemDark} { --canvas: ...; }`,
|
|
1201
|
+
" *",
|
|
1202
|
+
" * Then check BOTH colour schemes. A headless browser defaults to light and",
|
|
1203
|
+
" * will report a broken dark mode as passing.",
|
|
1204
|
+
" *",
|
|
1205
|
+
" * Do not override a --color-<name>-<step> ramp step by hand: the contrast",
|
|
1206
|
+
" * search never saw it, --check cannot detect it, and Tailwind still inlines",
|
|
1207
|
+
" * its old value into opacity-modifier fallbacks. Change the seed and",
|
|
1208
|
+
" * regenerate instead.",
|
|
1209
|
+
" */"
|
|
1210
|
+
].join("\n");
|
|
1211
|
+
}
|
|
1013
1212
|
var TOKEN_SECTIONS = [
|
|
1014
1213
|
"primitives",
|
|
1015
1214
|
"ramp",
|
|
@@ -1018,11 +1217,23 @@ var TOKEN_SECTIONS = [
|
|
|
1018
1217
|
"utility"
|
|
1019
1218
|
];
|
|
1020
1219
|
var ALL_SECTIONS = [...TOKEN_SECTIONS];
|
|
1220
|
+
function familyFilter(families) {
|
|
1221
|
+
if (!families || families.length === 0) return () => true;
|
|
1222
|
+
const want = /* @__PURE__ */ new Set();
|
|
1223
|
+
for (const name of families) {
|
|
1224
|
+
want.add(name);
|
|
1225
|
+
const other = aliasOf(name);
|
|
1226
|
+
if (other) want.add(other);
|
|
1227
|
+
}
|
|
1228
|
+
return (name) => want.has(name);
|
|
1229
|
+
}
|
|
1021
1230
|
function sectionSet(sections) {
|
|
1022
1231
|
return new Set(sections && sections.length > 0 ? sections : ALL_SECTIONS);
|
|
1023
1232
|
}
|
|
1024
|
-
function semanticAliasNames(tokens, sections) {
|
|
1233
|
+
function semanticAliasNames(tokens, sections, families) {
|
|
1025
1234
|
const want = sectionSet(sections);
|
|
1235
|
+
const scoped = !!families && families.length > 0;
|
|
1236
|
+
const inFamily = familyFilter(families);
|
|
1026
1237
|
const extended = ["primary", "neutral", "success", "destructive", "warning"];
|
|
1027
1238
|
const names = [];
|
|
1028
1239
|
const pushExtended = (n) => {
|
|
@@ -1036,13 +1247,13 @@ function semanticAliasNames(tokens, sections) {
|
|
|
1036
1247
|
);
|
|
1037
1248
|
};
|
|
1038
1249
|
if (want.has("semantic")) {
|
|
1039
|
-
for (const name of extended) pushExtended(name);
|
|
1250
|
+
for (const name of extended) if (inFamily(name)) pushExtended(name);
|
|
1040
1251
|
for (const name of Object.keys(tokens.semantic.light)) {
|
|
1041
|
-
if (!extended.includes(name) && name !== "muted") pushExtended(name);
|
|
1252
|
+
if (!extended.includes(name) && name !== "muted" && inFamily(name)) pushExtended(name);
|
|
1042
1253
|
}
|
|
1043
|
-
names.push("muted", "muted-foreground");
|
|
1254
|
+
if (!scoped || inFamily("neutral")) names.push("muted", "muted-foreground");
|
|
1044
1255
|
}
|
|
1045
|
-
if (want.has("surface")) {
|
|
1256
|
+
if (want.has("surface") && !scoped) {
|
|
1046
1257
|
names.push(
|
|
1047
1258
|
"canvas",
|
|
1048
1259
|
"canvas-foreground",
|
|
@@ -1052,7 +1263,7 @@ function semanticAliasNames(tokens, sections) {
|
|
|
1052
1263
|
"overlay-foreground"
|
|
1053
1264
|
);
|
|
1054
1265
|
}
|
|
1055
|
-
if (want.has("utility")) {
|
|
1266
|
+
if (want.has("utility") && !scoped) {
|
|
1056
1267
|
names.push("border", "input", "ring", "shadow");
|
|
1057
1268
|
}
|
|
1058
1269
|
return names;
|
|
@@ -1066,35 +1277,39 @@ function emitThemeRebinding(aliases, indent) {
|
|
|
1066
1277
|
}
|
|
1067
1278
|
return lines;
|
|
1068
1279
|
}
|
|
1069
|
-
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active) {
|
|
1280
|
+
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, anchors) {
|
|
1070
1281
|
const lines = [];
|
|
1071
|
-
|
|
1282
|
+
const seedShade = anchors.seedShade;
|
|
1283
|
+
if (want.has("surface") && !scoped) {
|
|
1072
1284
|
lines.push(`${indent}/* Surface */`);
|
|
1073
1285
|
lines.push(...emitSurfaceVars(tokens.surface[mode], colorFormat, indent));
|
|
1074
1286
|
lines.push("");
|
|
1075
1287
|
}
|
|
1076
|
-
if (want.has("utility")) {
|
|
1288
|
+
if (want.has("utility") && !scoped) {
|
|
1077
1289
|
lines.push(`${indent}/* Utility */`);
|
|
1078
|
-
lines.push(...emitUtilityVars(mode, indent, active));
|
|
1290
|
+
lines.push(...emitUtilityVars(mode, indent, active, seedShade));
|
|
1079
1291
|
lines.push("");
|
|
1080
1292
|
}
|
|
1081
1293
|
if (want.has("semantic")) {
|
|
1082
1294
|
lines.push(`${indent}/* Semantic Colors */`);
|
|
1083
|
-
lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active));
|
|
1295
|
+
lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1084
1296
|
}
|
|
1085
1297
|
while (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
|
|
1086
1298
|
lines.push(...emitThemeRebinding(aliases, indent));
|
|
1087
1299
|
return lines;
|
|
1088
1300
|
}
|
|
1089
|
-
function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades) {
|
|
1301
|
+
function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades, families, seedShade, seedShades) {
|
|
1090
1302
|
const lines = [];
|
|
1303
|
+
const anchors = { seedShade, seedShades };
|
|
1091
1304
|
const active = resolveShades(shades);
|
|
1092
1305
|
const want = sectionSet(sections);
|
|
1093
|
-
const
|
|
1306
|
+
const scoped = !!families && families.length > 0;
|
|
1307
|
+
const inFamily = familyFilter(families);
|
|
1308
|
+
const aliases = semanticAliasNames(tokens, sections, families);
|
|
1094
1309
|
const wantPrimitives = want.has("primitives") || want.has("ramp");
|
|
1095
1310
|
const wantModes = aliases.length > 0;
|
|
1096
1311
|
lines.push("@theme static {");
|
|
1097
|
-
if (replacePalette) {
|
|
1312
|
+
if (replacePalette && !scoped) {
|
|
1098
1313
|
lines.push(" /* Clear Tailwind's stock palette, so a colour class this file does not");
|
|
1099
1314
|
lines.push(" define fails to compile instead of silently rendering off-brand. */");
|
|
1100
1315
|
lines.push(" --color-*: initial;");
|
|
@@ -1103,7 +1318,7 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1103
1318
|
if (wantPrimitives) {
|
|
1104
1319
|
lines.push(" /* Primitives \u2014 real values, mode-invariant. Deliberately override");
|
|
1105
1320
|
lines.push(" Tailwind's stock palette: bg-red-500 becomes the project's red. */");
|
|
1106
|
-
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " "));
|
|
1321
|
+
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " ", inFamily, scoped));
|
|
1107
1322
|
}
|
|
1108
1323
|
if (wantModes) {
|
|
1109
1324
|
if (wantPrimitives) lines.push("");
|
|
@@ -1113,22 +1328,23 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1113
1328
|
}
|
|
1114
1329
|
}
|
|
1115
1330
|
lines.push("}");
|
|
1331
|
+
if (wantPrimitives) lines.push(...emitWideGamutBlock(tokens, inFamily));
|
|
1116
1332
|
if (wantModes) {
|
|
1117
1333
|
lines.push("");
|
|
1118
1334
|
lines.push("/* Light Mode */");
|
|
1119
|
-
lines.push(
|
|
1120
|
-
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active));
|
|
1335
|
+
lines.push(`${MODE_SELECTORS.light} {`);
|
|
1336
|
+
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
1121
1337
|
lines.push("}");
|
|
1122
1338
|
lines.push("");
|
|
1123
1339
|
lines.push("/* Dark Mode */");
|
|
1124
|
-
lines.push(
|
|
1125
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active));
|
|
1340
|
+
lines.push(`${MODE_SELECTORS.dark} {`);
|
|
1341
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
1126
1342
|
lines.push("}");
|
|
1127
1343
|
lines.push("");
|
|
1128
1344
|
lines.push("/* System Preference: Dark Mode */");
|
|
1129
1345
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
1130
|
-
lines.push(
|
|
1131
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active));
|
|
1346
|
+
lines.push(` ${MODE_SELECTORS.systemDark} {`);
|
|
1347
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
1132
1348
|
lines.push(" }");
|
|
1133
1349
|
lines.push("}");
|
|
1134
1350
|
}
|
|
@@ -1296,22 +1512,55 @@ function exportToDtcg(tokens) {
|
|
|
1296
1512
|
return JSON.stringify(dtcg, null, 2);
|
|
1297
1513
|
}
|
|
1298
1514
|
var SECTIONED_FORMATS = ["css", "tailwind-v4"];
|
|
1299
|
-
function
|
|
1300
|
-
|
|
1515
|
+
function tokenFamilies(tokens) {
|
|
1516
|
+
return Object.entries(tokens.primitives).filter(([, scale]) => typeof scale !== "string").map(([name]) => name);
|
|
1517
|
+
}
|
|
1518
|
+
function externalReferences(css) {
|
|
1519
|
+
const defined = /* @__PURE__ */ new Set();
|
|
1520
|
+
for (const line of css.split("\n")) {
|
|
1521
|
+
const m = /^\s*(--[a-z0-9-]+)\s*:/i.exec(line);
|
|
1522
|
+
if (m) defined.add(m[1]);
|
|
1523
|
+
}
|
|
1524
|
+
const external = /* @__PURE__ */ new Set();
|
|
1525
|
+
for (const m of css.matchAll(/var\(\s*(--[a-z0-9-]+)/gi)) {
|
|
1526
|
+
if (!defined.has(m[1])) external.add(m[1]);
|
|
1527
|
+
}
|
|
1528
|
+
return [...external].sort();
|
|
1529
|
+
}
|
|
1530
|
+
function selectTokenSections(tokens, sections, families) {
|
|
1531
|
+
const scoped = !!families && families.length > 0;
|
|
1532
|
+
if ((!sections || sections.length === 0) && !scoped) return tokens;
|
|
1301
1533
|
const want = sectionSet(sections);
|
|
1534
|
+
const inFamily = familyFilter(families);
|
|
1302
1535
|
const out = {};
|
|
1303
1536
|
if (want.has("primitives") || want.has("ramp")) {
|
|
1304
1537
|
const rampsOnly = !want.has("primitives");
|
|
1305
1538
|
const primitives = {};
|
|
1306
1539
|
for (const [name, scale] of Object.entries(tokens.primitives)) {
|
|
1307
|
-
if (typeof scale === "string" && rampsOnly) continue;
|
|
1540
|
+
if (typeof scale === "string" && (rampsOnly || scoped)) continue;
|
|
1541
|
+
if (typeof scale !== "string" && !inFamily(name)) continue;
|
|
1308
1542
|
primitives[name] = scale;
|
|
1309
1543
|
}
|
|
1310
1544
|
out.primitives = primitives;
|
|
1311
1545
|
}
|
|
1312
|
-
if (want.has("semantic"))
|
|
1313
|
-
|
|
1314
|
-
|
|
1546
|
+
if (want.has("semantic")) {
|
|
1547
|
+
out.semantic = scoped ? {
|
|
1548
|
+
light: Object.fromEntries(
|
|
1549
|
+
Object.entries(tokens.semantic.light).filter(([n]) => inFamily(n))
|
|
1550
|
+
),
|
|
1551
|
+
dark: Object.fromEntries(
|
|
1552
|
+
Object.entries(tokens.semantic.dark).filter(([n]) => inFamily(n))
|
|
1553
|
+
)
|
|
1554
|
+
} : tokens.semantic;
|
|
1555
|
+
}
|
|
1556
|
+
if (want.has("surface") && !scoped) out.surface = tokens.surface;
|
|
1557
|
+
if (want.has("utility") && !scoped) out.utility = tokens.utility;
|
|
1558
|
+
if (tokens.p3) {
|
|
1559
|
+
const wide = selectTokenSections({ ...tokens, ...tokens.p3, p3: void 0 }, sections, families);
|
|
1560
|
+
out.p3 = Object.fromEntries(
|
|
1561
|
+
["primitives", "semantic", "surface", "utility"].filter((tier) => wide[tier] !== void 0).map((tier) => [tier, wide[tier]])
|
|
1562
|
+
);
|
|
1563
|
+
}
|
|
1315
1564
|
return out;
|
|
1316
1565
|
}
|
|
1317
1566
|
function exportTokens(tokens, format, options) {
|
|
@@ -1320,6 +1569,14 @@ function exportTokens(tokens, format, options) {
|
|
|
1320
1569
|
const replacePalette = options?.replacePalette !== false;
|
|
1321
1570
|
const sections = options?.sections;
|
|
1322
1571
|
const shades = options?.shades;
|
|
1572
|
+
const families = options?.families;
|
|
1573
|
+
const seedShade = options?.seedShade;
|
|
1574
|
+
const seedShades = options?.seedShades;
|
|
1575
|
+
if (families && families.length > 0 && !SECTIONED_FORMATS.includes(format)) {
|
|
1576
|
+
throw new Error(
|
|
1577
|
+
`Format "${format}" cannot be scoped to colour families; supported: ${SECTIONED_FORMATS.join(", ")}`
|
|
1578
|
+
);
|
|
1579
|
+
}
|
|
1323
1580
|
if (sections && sections.length > 0 && !SECTIONED_FORMATS.includes(format)) {
|
|
1324
1581
|
throw new Error(
|
|
1325
1582
|
`Format "${format}" cannot be filtered by section; supported: ${SECTIONED_FORMATS.join(", ")}`
|
|
@@ -1327,11 +1584,11 @@ function exportTokens(tokens, format, options) {
|
|
|
1327
1584
|
}
|
|
1328
1585
|
switch (format) {
|
|
1329
1586
|
case "css":
|
|
1330
|
-
return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades);
|
|
1587
|
+
return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades, families, seedShade, seedShades);
|
|
1331
1588
|
case "tailwind-v3":
|
|
1332
1589
|
return exportToTailwindV3(tokens, colorFormat);
|
|
1333
1590
|
case "tailwind-v4":
|
|
1334
|
-
return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades);
|
|
1591
|
+
return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades, families, seedShade, seedShades);
|
|
1335
1592
|
case "dtcg":
|
|
1336
1593
|
return exportToDtcg(tokens);
|
|
1337
1594
|
default:
|
|
@@ -1339,6 +1596,132 @@ function exportTokens(tokens, format, options) {
|
|
|
1339
1596
|
}
|
|
1340
1597
|
}
|
|
1341
1598
|
|
|
1599
|
+
// src/merge.ts
|
|
1600
|
+
function keyOf(block, property) {
|
|
1601
|
+
return `${block} ${property}`;
|
|
1602
|
+
}
|
|
1603
|
+
function normaliseSelector(raw) {
|
|
1604
|
+
return raw.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\s+/g, " ").trim();
|
|
1605
|
+
}
|
|
1606
|
+
function walk(css, onDeclaration) {
|
|
1607
|
+
const lines = css.split("\n");
|
|
1608
|
+
const stack = [];
|
|
1609
|
+
let pending = "";
|
|
1610
|
+
lines.forEach((line, index) => {
|
|
1611
|
+
const trimmed = line.trim();
|
|
1612
|
+
const declaration = /^(--[a-z0-9-]+)\s*:\s*(.+?);\s*$/i.exec(trimmed);
|
|
1613
|
+
if (declaration && stack.length > 0) {
|
|
1614
|
+
onDeclaration(line, index, stack.join(" > "), declaration[1], declaration[2]);
|
|
1615
|
+
}
|
|
1616
|
+
for (const char of trimmed) {
|
|
1617
|
+
if (char === "{") {
|
|
1618
|
+
stack.push(normaliseSelector(pending));
|
|
1619
|
+
pending = "";
|
|
1620
|
+
} else if (char === "}") {
|
|
1621
|
+
stack.pop();
|
|
1622
|
+
pending = "";
|
|
1623
|
+
} else {
|
|
1624
|
+
pending += char;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
if (trimmed.endsWith("{") || trimmed.endsWith("}")) pending = "";
|
|
1628
|
+
});
|
|
1629
|
+
}
|
|
1630
|
+
function readDeclarations(css) {
|
|
1631
|
+
const out = [];
|
|
1632
|
+
walk(
|
|
1633
|
+
css,
|
|
1634
|
+
(_line, _index, block, property, value) => out.push({ block, property, value })
|
|
1635
|
+
);
|
|
1636
|
+
return out;
|
|
1637
|
+
}
|
|
1638
|
+
function mergeSlice(target, slice) {
|
|
1639
|
+
const incoming = /* @__PURE__ */ new Map();
|
|
1640
|
+
for (const declaration of readDeclarations(slice)) {
|
|
1641
|
+
incoming.set(keyOf(declaration.block, declaration.property), declaration);
|
|
1642
|
+
}
|
|
1643
|
+
const lines = target.split("\n");
|
|
1644
|
+
const updated = [];
|
|
1645
|
+
const unchanged = [];
|
|
1646
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1647
|
+
walk(target, (line, index, block, property, value) => {
|
|
1648
|
+
const key = keyOf(block, property);
|
|
1649
|
+
const wanted = incoming.get(key);
|
|
1650
|
+
if (!wanted) return;
|
|
1651
|
+
seen.add(key);
|
|
1652
|
+
if (wanted.value === value) {
|
|
1653
|
+
unchanged.push(wanted);
|
|
1654
|
+
return;
|
|
1655
|
+
}
|
|
1656
|
+
lines[index] = line.replace(`${property}: ${value};`, `${property}: ${wanted.value};`);
|
|
1657
|
+
updated.push(wanted);
|
|
1658
|
+
});
|
|
1659
|
+
const additions = [...incoming.entries()].filter(([key]) => !seen.has(key));
|
|
1660
|
+
const added = [];
|
|
1661
|
+
const missingBlocks = /* @__PURE__ */ new Set();
|
|
1662
|
+
if (additions.length > 0) {
|
|
1663
|
+
const lastLineOfBlock = /* @__PURE__ */ new Map();
|
|
1664
|
+
const indentOfBlock = /* @__PURE__ */ new Map();
|
|
1665
|
+
walk(lines.join("\n"), (line, index, block) => {
|
|
1666
|
+
lastLineOfBlock.set(block, index);
|
|
1667
|
+
if (!indentOfBlock.has(block)) {
|
|
1668
|
+
indentOfBlock.set(block, /^\s*/.exec(line)?.[0] ?? " ");
|
|
1669
|
+
}
|
|
1670
|
+
});
|
|
1671
|
+
const byBlock = /* @__PURE__ */ new Map();
|
|
1672
|
+
for (const [, declaration] of additions) {
|
|
1673
|
+
if (!lastLineOfBlock.has(declaration.block)) {
|
|
1674
|
+
missingBlocks.add(declaration.block);
|
|
1675
|
+
continue;
|
|
1676
|
+
}
|
|
1677
|
+
const list = byBlock.get(declaration.block) ?? [];
|
|
1678
|
+
list.push(declaration);
|
|
1679
|
+
byBlock.set(declaration.block, list);
|
|
1680
|
+
}
|
|
1681
|
+
const ordered = [...byBlock.entries()].sort(
|
|
1682
|
+
(a, b) => (lastLineOfBlock.get(b[0]) ?? 0) - (lastLineOfBlock.get(a[0]) ?? 0)
|
|
1683
|
+
);
|
|
1684
|
+
for (const [block, declarations] of ordered) {
|
|
1685
|
+
const at = lastLineOfBlock.get(block);
|
|
1686
|
+
const indent = indentOfBlock.get(block) ?? " ";
|
|
1687
|
+
lines.splice(
|
|
1688
|
+
at + 1,
|
|
1689
|
+
0,
|
|
1690
|
+
...declarations.map((d) => `${indent}${d.property}: ${d.value};`)
|
|
1691
|
+
);
|
|
1692
|
+
added.push(...declarations);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
return {
|
|
1696
|
+
css: lines.join("\n"),
|
|
1697
|
+
updated,
|
|
1698
|
+
added,
|
|
1699
|
+
unchanged,
|
|
1700
|
+
missingBlocks: [...missingBlocks].sort()
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
function diffSlice(target, slice) {
|
|
1704
|
+
const actual = /* @__PURE__ */ new Map();
|
|
1705
|
+
for (const declaration of readDeclarations(target)) {
|
|
1706
|
+
actual.set(keyOf(declaration.block, declaration.property), declaration.value);
|
|
1707
|
+
}
|
|
1708
|
+
const differing = [];
|
|
1709
|
+
const missing = [];
|
|
1710
|
+
for (const declaration of readDeclarations(slice)) {
|
|
1711
|
+
const found = actual.get(keyOf(declaration.block, declaration.property));
|
|
1712
|
+
if (found === void 0) missing.push(declaration);
|
|
1713
|
+
else if (found !== declaration.value) {
|
|
1714
|
+
differing.push({
|
|
1715
|
+
block: declaration.block,
|
|
1716
|
+
property: declaration.property,
|
|
1717
|
+
expected: declaration.value,
|
|
1718
|
+
actual: found
|
|
1719
|
+
});
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
return { matches: differing.length === 0 && missing.length === 0, differing, missing };
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1342
1725
|
// src/seed.ts
|
|
1343
1726
|
var GENERATOR_NAME = "@designtools/tokens";
|
|
1344
1727
|
function stableStringify(value) {
|
|
@@ -1389,8 +1772,10 @@ export {
|
|
|
1389
1772
|
snapShade,
|
|
1390
1773
|
reanchorFactors,
|
|
1391
1774
|
chromaShadeFor,
|
|
1775
|
+
isInGamut,
|
|
1392
1776
|
parseToOklch,
|
|
1393
1777
|
oklchToHex,
|
|
1778
|
+
formatOklchInGamut,
|
|
1394
1779
|
generateColorScale,
|
|
1395
1780
|
generateColorScaleAPCA,
|
|
1396
1781
|
adjustLightness,
|
|
@@ -1407,23 +1792,37 @@ export {
|
|
|
1407
1792
|
getOklchString,
|
|
1408
1793
|
formatColorAs,
|
|
1409
1794
|
DEFAULT_NEUTRAL_COLOR,
|
|
1795
|
+
ALIAS_PAIRS,
|
|
1796
|
+
aliasOf,
|
|
1797
|
+
familySeedShade,
|
|
1410
1798
|
generatePrimitivePalette,
|
|
1411
1799
|
getShade,
|
|
1800
|
+
extendedBridgeShades,
|
|
1801
|
+
ringBridgeShade,
|
|
1412
1802
|
generateSemanticTokens,
|
|
1413
1803
|
generateSurfaceTokens,
|
|
1414
1804
|
generateUtilityTokens,
|
|
1415
1805
|
checkSemanticContrast,
|
|
1416
1806
|
checkSemanticContrastAPCA,
|
|
1807
|
+
checkSurfaceContrast,
|
|
1808
|
+
checkRampChroma,
|
|
1417
1809
|
computeAccessibilityScore,
|
|
1418
1810
|
generateTokens,
|
|
1811
|
+
WIDE_GAMUT_QUERY,
|
|
1419
1812
|
exportToCss,
|
|
1813
|
+
overrideContract,
|
|
1420
1814
|
TOKEN_SECTIONS,
|
|
1421
1815
|
exportToTailwindV4,
|
|
1422
1816
|
exportToTailwindV3,
|
|
1423
1817
|
exportToDtcg,
|
|
1424
1818
|
SECTIONED_FORMATS,
|
|
1819
|
+
tokenFamilies,
|
|
1820
|
+
externalReferences,
|
|
1425
1821
|
selectTokenSections,
|
|
1426
1822
|
exportTokens,
|
|
1823
|
+
readDeclarations,
|
|
1824
|
+
mergeSlice,
|
|
1825
|
+
diffSlice,
|
|
1427
1826
|
GENERATOR_NAME,
|
|
1428
1827
|
stableStringify,
|
|
1429
1828
|
fingerprint,
|