@designtools/tokens 0.4.0 → 0.6.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 +95 -0
- package/LICENSE +21 -0
- package/README.md +125 -25
- package/dist/{chunk-MCLZSLHE.js → chunk-UW2HXSV4.js} +272 -129
- package/dist/cli.js +143 -46
- package/dist/index.d.ts +204 -21
- package/dist/index.js +17 -1
- package/package.json +28 -15
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
// src/apca.ts
|
|
2
|
+
var APCA_PACKAGE = "apca-w3";
|
|
3
|
+
var APCAUnavailableError = class extends Error {
|
|
4
|
+
constructor() {
|
|
5
|
+
super(
|
|
6
|
+
`APCA contrast needs the ${APCA_PACKAGE} package, which is not installed. Install it beside @designtools/tokens (npm install ${APCA_PACKAGE}), or with npx: npx -p ${APCA_PACKAGE} -p @designtools/tokens generate-tokens --contrast apca \u2026. ${APCA_PACKAGE} has its own licence, which permits WCAG contrast checking of web content only: https://github.com/Myndex/apca-w3/blob/master/LICENSE.md`
|
|
7
|
+
);
|
|
8
|
+
this.name = "APCAUnavailableError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
function isMissing(error) {
|
|
12
|
+
const { code, message } = error ?? {};
|
|
13
|
+
return code === "ERR_MODULE_NOT_FOUND" && typeof message === "string" && message.includes(APCA_PACKAGE);
|
|
14
|
+
}
|
|
15
|
+
var apca = null;
|
|
16
|
+
try {
|
|
17
|
+
apca = await import("apca-w3");
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (!isMissing(error)) throw error;
|
|
20
|
+
}
|
|
21
|
+
function isAPCAAvailable() {
|
|
22
|
+
return apca !== null;
|
|
23
|
+
}
|
|
24
|
+
function requireAPCA() {
|
|
25
|
+
if (!apca) throw new APCAUnavailableError();
|
|
26
|
+
return apca;
|
|
27
|
+
}
|
|
28
|
+
|
|
1
29
|
// src/types.ts
|
|
2
30
|
var VALID_SHADES = [25, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
|
|
3
31
|
|
|
@@ -68,12 +96,42 @@ import {
|
|
|
68
96
|
filterDeficiencyProt,
|
|
69
97
|
filterDeficiencyDeuter,
|
|
70
98
|
filterDeficiencyTrit,
|
|
71
|
-
parse
|
|
99
|
+
parse,
|
|
100
|
+
toGamut,
|
|
101
|
+
inGamut,
|
|
102
|
+
clampChroma
|
|
72
103
|
} from "culori";
|
|
73
|
-
import { APCAcontrast, sRGBtoY } from "apca-w3";
|
|
74
104
|
var toOklch = converter("oklch");
|
|
75
|
-
var toRgbConverter = converter("rgb");
|
|
76
105
|
var toHslConverter = converter("hsl");
|
|
106
|
+
var GAMUT_SPACE = { srgb: "rgb", p3: "p3" };
|
|
107
|
+
var GAMUT_MAPPERS = {
|
|
108
|
+
srgb: toGamut("rgb", "oklch"),
|
|
109
|
+
p3: toGamut("p3", "oklch")
|
|
110
|
+
};
|
|
111
|
+
var GAMUT_CHECKS = {
|
|
112
|
+
srgb: inGamut("rgb"),
|
|
113
|
+
p3: inGamut("p3")
|
|
114
|
+
};
|
|
115
|
+
var GAMUT_CONVERTERS = {
|
|
116
|
+
srgb: converter("rgb"),
|
|
117
|
+
p3: converter("p3")
|
|
118
|
+
};
|
|
119
|
+
function isInGamut(color, gamut = "srgb") {
|
|
120
|
+
const parsed = parse(color);
|
|
121
|
+
if (!parsed) throw new Error(`Invalid color: ${color}`);
|
|
122
|
+
return GAMUT_CHECKS[gamut](parsed);
|
|
123
|
+
}
|
|
124
|
+
function displayed(color, gamut = "srgb") {
|
|
125
|
+
const parsed = typeof color === "string" ? parse(color) : color;
|
|
126
|
+
if (!parsed) throw new Error(`Cannot convert ${String(color)} to RGB`);
|
|
127
|
+
const mapped = GAMUT_CHECKS[gamut](parsed) ? GAMUT_CONVERTERS[gamut](parsed) : GAMUT_MAPPERS[gamut](parsed);
|
|
128
|
+
if (!mapped) throw new Error(`Cannot convert ${color} to ${GAMUT_SPACE[gamut]}`);
|
|
129
|
+
const unit = (v) => Math.min(1, Math.max(0, v ?? 0));
|
|
130
|
+
return { r: unit(mapped.r), g: unit(mapped.g), b: unit(mapped.b) };
|
|
131
|
+
}
|
|
132
|
+
function displayedSrgb(color) {
|
|
133
|
+
return { mode: "rgb", ...displayed(color, "srgb") };
|
|
134
|
+
}
|
|
77
135
|
function parseToOklch(color) {
|
|
78
136
|
const result = toOklch(color);
|
|
79
137
|
if (!result) {
|
|
@@ -82,7 +140,7 @@ function parseToOklch(color) {
|
|
|
82
140
|
return result;
|
|
83
141
|
}
|
|
84
142
|
function oklchToHex(color) {
|
|
85
|
-
const hex = formatHex(color);
|
|
143
|
+
const hex = formatHex(displayedSrgb(color));
|
|
86
144
|
return hex || "#000000";
|
|
87
145
|
}
|
|
88
146
|
function computeScaleChroma(scaleValue, baseChroma) {
|
|
@@ -122,6 +180,23 @@ var LIGHTNESS_FACTORS = {
|
|
|
122
180
|
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
181
|
}
|
|
124
182
|
};
|
|
183
|
+
function printOklch(l, c, h) {
|
|
184
|
+
return `oklch(${l} ${c.toFixed(4)} ${h})`;
|
|
185
|
+
}
|
|
186
|
+
function formatOklchInGamut(lightness, chroma, hue, gamut = "srgb") {
|
|
187
|
+
const l = (lightness * 100).toFixed(2) + "%";
|
|
188
|
+
const h = hue.toFixed(2);
|
|
189
|
+
const c = Math.max(0, chroma);
|
|
190
|
+
const printed = printOklch(l, c, h);
|
|
191
|
+
if (isInGamut(printed, gamut)) return printed;
|
|
192
|
+
const at = { mode: "oklch", l: Number(l.slice(0, -1)) / 100, c, h: Number(h) };
|
|
193
|
+
const capped = clampChroma(at, "oklch", GAMUT_SPACE[gamut]);
|
|
194
|
+
let fit = Math.floor((capped.c ?? 0) * 1e4) / 1e4;
|
|
195
|
+
while (fit > 0 && !isInGamut(printOklch(l, fit, h), gamut)) {
|
|
196
|
+
fit = Math.round((fit - 1e-4) * 1e4) / 1e4;
|
|
197
|
+
}
|
|
198
|
+
return printOklch(l, fit, h);
|
|
199
|
+
}
|
|
125
200
|
function buildScale(baseColor, strategy, options) {
|
|
126
201
|
const base = parseToOklch(baseColor);
|
|
127
202
|
const baseLightness = base.l;
|
|
@@ -130,6 +205,7 @@ function buildScale(baseColor, strategy, options) {
|
|
|
130
205
|
const isAchromatic = baseChroma < 0.01;
|
|
131
206
|
const seedShade = options?.seedShade ?? DEFAULT_SEED_SHADE;
|
|
132
207
|
const emitted = resolveShades(options?.shades);
|
|
208
|
+
const gamut = options?.gamut ?? "srgb";
|
|
133
209
|
const maxLightness = Math.max(0.98, baseLightness);
|
|
134
210
|
const minLightness = Math.min(0.15, baseLightness);
|
|
135
211
|
const headroomUp = maxLightness - baseLightness;
|
|
@@ -144,7 +220,7 @@ function buildScale(baseColor, strategy, options) {
|
|
|
144
220
|
const shade = VALID_SHADES[i];
|
|
145
221
|
if (!emitted.includes(shade)) continue;
|
|
146
222
|
if (shade === seedShade) {
|
|
147
|
-
scale[shade] =
|
|
223
|
+
scale[shade] = formatOklchInGamut(baseLightness, baseChroma, baseHue, gamut);
|
|
148
224
|
continue;
|
|
149
225
|
}
|
|
150
226
|
const factor = factors[i];
|
|
@@ -153,10 +229,7 @@ function buildScale(baseColor, strategy, options) {
|
|
|
153
229
|
const chromaShade = chromaShadeFor(i, seedShade);
|
|
154
230
|
const normalizedPosition = (chromaShade - 25) / 925;
|
|
155
231
|
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})`;
|
|
232
|
+
scale[shade] = formatOklchInGamut(lightness, chroma, baseHue, gamut);
|
|
160
233
|
}
|
|
161
234
|
return scale;
|
|
162
235
|
}
|
|
@@ -180,30 +253,19 @@ function adjustChroma(color, chroma) {
|
|
|
180
253
|
c: Math.max(0, chroma)
|
|
181
254
|
});
|
|
182
255
|
}
|
|
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);
|
|
256
|
+
var LUMINANCE_WEIGHTS = {
|
|
257
|
+
srgb: [0.2126, 0.7152, 0.0722],
|
|
258
|
+
p3: [0.2289746, 0.6917385, 0.0792869]
|
|
259
|
+
};
|
|
260
|
+
function getRelativeLuminance(color, gamut = "srgb") {
|
|
261
|
+
const rgb = displayed(color, gamut);
|
|
262
|
+
const [wr, wg, wb] = LUMINANCE_WEIGHTS[gamut];
|
|
263
|
+
const linear = (c) => c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
264
|
+
return wr * linear(rgb.r) + wg * linear(rgb.g) + wb * linear(rgb.b);
|
|
265
|
+
}
|
|
266
|
+
function getContrastRatio(color1, color2, gamut = "srgb") {
|
|
267
|
+
const lum1 = getRelativeLuminance(color1, gamut);
|
|
268
|
+
const lum2 = getRelativeLuminance(color2, gamut);
|
|
207
269
|
const lighter = Math.max(lum1, lum2);
|
|
208
270
|
const darker = Math.min(lum1, lum2);
|
|
209
271
|
return (lighter + 0.05) / (darker + 0.05);
|
|
@@ -214,12 +276,12 @@ function meetsWCAG_AA(ratio, isLargeText = false) {
|
|
|
214
276
|
function meetsWCAG_AAA(ratio, isLargeText = false) {
|
|
215
277
|
return isLargeText ? ratio >= 4.5 : ratio >= 7;
|
|
216
278
|
}
|
|
217
|
-
function getAPCAContrast(foreground, background) {
|
|
218
|
-
const
|
|
219
|
-
const
|
|
220
|
-
const
|
|
221
|
-
const
|
|
222
|
-
return Math.abs(APCAcontrast(
|
|
279
|
+
function getAPCAContrast(foreground, background, gamut = "srgb") {
|
|
280
|
+
const { APCAcontrast, sRGBtoY, displayP3toY } = requireAPCA();
|
|
281
|
+
const fg = displayed(foreground, gamut);
|
|
282
|
+
const bg = displayed(background, gamut);
|
|
283
|
+
const toY = (c) => gamut === "p3" ? displayP3toY([c.r, c.g, c.b]) : sRGBtoY([c.r * 255, c.g * 255, c.b * 255]);
|
|
284
|
+
return Math.abs(APCAcontrast(toY(fg), toY(bg)));
|
|
223
285
|
}
|
|
224
286
|
function findContrastingColorAPCA(background, lightColor, darkColor) {
|
|
225
287
|
const lightLc = getAPCAContrast(lightColor, background);
|
|
@@ -235,7 +297,7 @@ function simulateCVD(color, cvd) {
|
|
|
235
297
|
if (cvd === "none") return color;
|
|
236
298
|
const parsed = parse(color);
|
|
237
299
|
if (!parsed) return color;
|
|
238
|
-
const simulated = cvdFilters[cvd](parsed);
|
|
300
|
+
const simulated = cvdFilters[cvd](displayedSrgb(parsed));
|
|
239
301
|
return formatHex(simulated) || color;
|
|
240
302
|
}
|
|
241
303
|
function classifyAPCALevel(lcValue) {
|
|
@@ -284,14 +346,13 @@ function getOklchString(color) {
|
|
|
284
346
|
function formatColorAs(color, format) {
|
|
285
347
|
switch (format) {
|
|
286
348
|
case "hex":
|
|
287
|
-
return formatHex(color) || "#000000";
|
|
349
|
+
return formatHex(displayedSrgb(color)) || "#000000";
|
|
288
350
|
case "rgb": {
|
|
289
|
-
|
|
290
|
-
if (!rgb) return "rgb(0, 0, 0)";
|
|
291
|
-
return formatRgb(rgb) || "rgb(0, 0, 0)";
|
|
351
|
+
return formatRgb(displayedSrgb(color)) || "rgb(0, 0, 0)";
|
|
292
352
|
}
|
|
293
353
|
case "hsl": {
|
|
294
|
-
const hsl = toHslConverter(color);
|
|
354
|
+
const hsl = toHslConverter(displayedSrgb(color));
|
|
355
|
+
if (hsl && Math.round((hsl.s ?? 0) * 1e4) === 0) hsl.h = 0;
|
|
295
356
|
if (!hsl) return "hsl(0, 0%, 0%)";
|
|
296
357
|
return formatHsl(hsl) || "hsl(0, 0%, 0%)";
|
|
297
358
|
}
|
|
@@ -315,6 +376,11 @@ function aliasOf(name) {
|
|
|
315
376
|
}
|
|
316
377
|
return void 0;
|
|
317
378
|
}
|
|
379
|
+
function familySeedShade(name, seedShade, seedShades) {
|
|
380
|
+
if (name === "primary") return seedShade ?? seedShades?.primary;
|
|
381
|
+
const alias = aliasOf(name);
|
|
382
|
+
return seedShades?.[name] ?? (alias ? seedShades?.[alias] : void 0);
|
|
383
|
+
}
|
|
318
384
|
function clamp(v, min, max) {
|
|
319
385
|
return Math.min(max, Math.max(min, v));
|
|
320
386
|
}
|
|
@@ -351,17 +417,21 @@ function deriveWarningColor(primaryColor) {
|
|
|
351
417
|
};
|
|
352
418
|
return oklchToHex(warning);
|
|
353
419
|
}
|
|
354
|
-
function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed",
|
|
420
|
+
function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", options) {
|
|
355
421
|
const gen = lightnessStrategy === "perceptual" ? generateColorScaleAPCA : generateColorScale;
|
|
356
|
-
const genScale = (color) => gen(color,
|
|
422
|
+
const genScale = (name, color) => gen(color, {
|
|
423
|
+
shades: options?.shades,
|
|
424
|
+
gamut: options?.gamut,
|
|
425
|
+
seedShade: familySeedShade(name, options?.seedShade, options?.seedShades)
|
|
426
|
+
});
|
|
357
427
|
const palette = {};
|
|
358
428
|
palette.black = "oklch(0 0 0)";
|
|
359
429
|
palette.white = "oklch(1 0 0)";
|
|
360
|
-
palette.primary = genScale(brandColors.primary);
|
|
361
|
-
palette.neutral = genScale(brandColors.neutral);
|
|
362
|
-
const successScale = genScale(deriveSuccessColor(brandColors.primary));
|
|
363
|
-
const destructiveScale = genScale(deriveDestructiveColor(brandColors.primary));
|
|
364
|
-
const warningScale = genScale(deriveWarningColor(brandColors.primary));
|
|
430
|
+
palette.primary = genScale("primary", brandColors.primary);
|
|
431
|
+
palette.neutral = genScale("neutral", brandColors.neutral);
|
|
432
|
+
const successScale = genScale("success", deriveSuccessColor(brandColors.primary));
|
|
433
|
+
const destructiveScale = genScale("destructive", deriveDestructiveColor(brandColors.primary));
|
|
434
|
+
const warningScale = genScale("warning", deriveWarningColor(brandColors.primary));
|
|
365
435
|
palette.success = successScale;
|
|
366
436
|
palette.destructive = destructiveScale;
|
|
367
437
|
palette.warning = warningScale;
|
|
@@ -370,7 +440,7 @@ function generatePrimitivePalette(brandColors, lightnessStrategy = "fixed", scal
|
|
|
370
440
|
palette.yellow = warningScale;
|
|
371
441
|
if (brandColors.additionalColors) {
|
|
372
442
|
for (const [name, color] of Object.entries(brandColors.additionalColors)) {
|
|
373
|
-
palette[name] = genScale(color);
|
|
443
|
+
palette[name] = genScale(name, color);
|
|
374
444
|
}
|
|
375
445
|
for (const [semantic, alias] of ALIAS_PAIRS) {
|
|
376
446
|
const overrodeSemantic = semantic in brandColors.additionalColors;
|
|
@@ -444,14 +514,18 @@ function getContrastThresholds(algorithm, target) {
|
|
|
444
514
|
}
|
|
445
515
|
}
|
|
446
516
|
}
|
|
447
|
-
function findBestForeground(
|
|
517
|
+
function findBestForeground(views, white, black, algorithm, contrastTarget, colorName, activeShades) {
|
|
448
518
|
const { floor, target } = getContrastThresholds(algorithm, contrastTarget);
|
|
449
|
-
const getContrast = (
|
|
450
|
-
|
|
519
|
+
const getContrast = (fgOf) => Math.min(
|
|
520
|
+
...views.map(
|
|
521
|
+
(view) => algorithm === "apca" ? getAPCAContrast(fgOf(view), view.background, view.gamut) : getContrastRatio(view.background, fgOf(view), view.gamut)
|
|
522
|
+
)
|
|
523
|
+
);
|
|
524
|
+
function searchScale(which, refColorName) {
|
|
451
525
|
let bestPassing = null;
|
|
452
526
|
let bestOverall = null;
|
|
453
527
|
for (const shade of activeShades) {
|
|
454
|
-
const c = getContrast(
|
|
528
|
+
const c = getContrast((view) => view[which][shade]);
|
|
455
529
|
if (c >= floor) {
|
|
456
530
|
if (!bestPassing || Math.abs(c - target) < Math.abs(bestPassing.contrast - target)) {
|
|
457
531
|
bestPassing = { shade, contrast: c };
|
|
@@ -464,17 +538,17 @@ function findBestForeground(background, scale, neutralScale, white, black, algor
|
|
|
464
538
|
const pick = bestPassing ?? bestOverall;
|
|
465
539
|
if (!pick) return null;
|
|
466
540
|
return {
|
|
467
|
-
value:
|
|
541
|
+
value: views[0][which][pick.shade],
|
|
468
542
|
ref: { type: "shade", colorName: refColorName, shade: pick.shade },
|
|
469
543
|
contrast: pick.contrast,
|
|
470
544
|
passes: pick === bestPassing
|
|
471
545
|
};
|
|
472
546
|
}
|
|
473
|
-
const ownResult = searchScale(scale, colorName);
|
|
547
|
+
const ownResult = searchScale("scale", colorName);
|
|
474
548
|
if (ownResult?.passes) {
|
|
475
549
|
return { value: ownResult.value, ref: ownResult.ref };
|
|
476
550
|
}
|
|
477
|
-
const neutralResult = searchScale(
|
|
551
|
+
const neutralResult = searchScale("neutral", "neutral");
|
|
478
552
|
if (neutralResult?.passes) {
|
|
479
553
|
if (!ownResult?.passes) {
|
|
480
554
|
return { value: neutralResult.value, ref: neutralResult.ref };
|
|
@@ -490,21 +564,30 @@ function findBestForeground(background, scale, neutralScale, white, black, algor
|
|
|
490
564
|
if (neutralResult) {
|
|
491
565
|
return { value: neutralResult.value, ref: neutralResult.ref };
|
|
492
566
|
}
|
|
493
|
-
const whiteC = getContrast(white);
|
|
494
|
-
const blackC = getContrast(black);
|
|
567
|
+
const whiteC = getContrast(() => white);
|
|
568
|
+
const blackC = getContrast(() => black);
|
|
495
569
|
if (whiteC >= blackC) return { value: white, ref: { type: "white" } };
|
|
496
570
|
return { value: black, ref: { type: "black" } };
|
|
497
571
|
}
|
|
498
|
-
function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades, seedShade) {
|
|
572
|
+
function createExtendedSemanticColor(scale, neutral, white, black, mode, contrastAlgorithm = "wcag", contrastTarget = "aa", overrides, colorName = "primary", shades, seedShade, gamuts = { own: "srgb" }) {
|
|
499
573
|
const activeShades = shadeKeys(shades);
|
|
500
|
-
const findFg = (
|
|
574
|
+
const findFg = (bgShade) => {
|
|
575
|
+
const views = [
|
|
576
|
+
{ background: scale[bgShade], scale, neutral, gamut: gamuts.own }
|
|
577
|
+
];
|
|
578
|
+
if (gamuts.other) {
|
|
579
|
+
const o = gamuts.other;
|
|
580
|
+
views.push({ background: o.scale[bgShade], scale: o.scale, neutral: o.neutral, gamut: o.gamut });
|
|
581
|
+
}
|
|
582
|
+
return findBestForeground(views, white, black, contrastAlgorithm, contrastTarget, colorName, activeShades);
|
|
583
|
+
};
|
|
501
584
|
const { DEFAULT: defaultShade, subdued: subduedShade, highlight: highlightShade } = extendedBridgeShades(mode, seedShade, activeShades, overrides);
|
|
502
585
|
const DEFAULT = scale[defaultShade];
|
|
503
586
|
const subdued = scale[subduedShade];
|
|
504
587
|
const highlight = scale[highlightShade];
|
|
505
|
-
const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(
|
|
506
|
-
const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : findFg(
|
|
507
|
-
const hfgResult = overrides?.["on-highlight"] ? { value: scale[overrides["on-highlight"]], ref: { type: "shade", colorName, shade: overrides["on-highlight"] } } : findFg(
|
|
588
|
+
const fgResult = overrides?.["on-DEFAULT"] ? { value: scale[overrides["on-DEFAULT"]], ref: { type: "shade", colorName, shade: overrides["on-DEFAULT"] } } : findFg(defaultShade);
|
|
589
|
+
const sfgResult = overrides?.["on-subdued"] ? { value: scale[overrides["on-subdued"]], ref: { type: "shade", colorName, shade: overrides["on-subdued"] } } : findFg(subduedShade);
|
|
590
|
+
const hfgResult = overrides?.["on-highlight"] ? { value: scale[overrides["on-highlight"]], ref: { type: "shade", colorName, shade: overrides["on-highlight"] } } : findFg(highlightShade);
|
|
508
591
|
return {
|
|
509
592
|
DEFAULT,
|
|
510
593
|
foreground: fgResult.value,
|
|
@@ -519,12 +602,12 @@ function createExtendedSemanticColor(scale, neutral, white, black, mode, contras
|
|
|
519
602
|
}
|
|
520
603
|
};
|
|
521
604
|
}
|
|
522
|
-
function generateSemanticTokens(palette, mode, options) {
|
|
605
|
+
function generateSemanticTokens(palette, mode, options, gamuts = { own: "srgb" }) {
|
|
523
606
|
const contrastAlgorithm = options?.contrastAlgorithm ?? "wcag";
|
|
524
607
|
const contrastTarget = options?.contrastTarget ?? "aa";
|
|
525
608
|
const shadeOverrides = options?.shadeOverrides ?? {};
|
|
526
609
|
const activeShades = shadeKeys(options?.shades);
|
|
527
|
-
const
|
|
610
|
+
const anchor = (name) => familySeedShade(name, options?.seedShade, options?.seedShades);
|
|
528
611
|
const neutral = palette.neutral;
|
|
529
612
|
if (!neutral) {
|
|
530
613
|
throw new Error("Neutral palette not found");
|
|
@@ -538,6 +621,17 @@ function generateSemanticTokens(palette, mode, options) {
|
|
|
538
621
|
if (!primary || !success || !destructive || !warning) {
|
|
539
622
|
throw new Error("Required color palettes not found");
|
|
540
623
|
}
|
|
624
|
+
const other = gamuts.other;
|
|
625
|
+
const gamutsFor = (name) => ({
|
|
626
|
+
own: gamuts.own,
|
|
627
|
+
...other ? {
|
|
628
|
+
other: {
|
|
629
|
+
gamut: other.gamut,
|
|
630
|
+
scale: other.palette[name],
|
|
631
|
+
neutral: other.palette.neutral
|
|
632
|
+
}
|
|
633
|
+
} : {}
|
|
634
|
+
});
|
|
541
635
|
const muted = mode === "light" ? {
|
|
542
636
|
DEFAULT: neutral[snapShade(25, activeShades)],
|
|
543
637
|
foreground: neutral[snapShade(500, activeShades)]
|
|
@@ -546,11 +640,11 @@ function generateSemanticTokens(palette, mode, options) {
|
|
|
546
640
|
foreground: neutral[snapShade(200, activeShades)]
|
|
547
641
|
};
|
|
548
642
|
const tokens = {
|
|
549
|
-
primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades,
|
|
550
|
-
neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades,
|
|
551
|
-
success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades,
|
|
552
|
-
destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades,
|
|
553
|
-
warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades,
|
|
643
|
+
primary: createExtendedSemanticColor(primary, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.primary?.[mode], "primary", activeShades, anchor("primary"), gamutsFor("primary")),
|
|
644
|
+
neutral: createExtendedSemanticColor(neutral, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.neutral?.[mode], "neutral", activeShades, anchor("neutral"), gamutsFor("neutral")),
|
|
645
|
+
success: createExtendedSemanticColor(success, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.success?.[mode], "success", activeShades, anchor("success"), gamutsFor("success")),
|
|
646
|
+
destructive: createExtendedSemanticColor(destructive, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.destructive?.[mode], "destructive", activeShades, anchor("destructive"), gamutsFor("destructive")),
|
|
647
|
+
warning: createExtendedSemanticColor(warning, neutral, white, black, mode, contrastAlgorithm, contrastTarget, shadeOverrides.warning?.[mode], "warning", activeShades, anchor("warning"), gamutsFor("warning")),
|
|
554
648
|
muted
|
|
555
649
|
};
|
|
556
650
|
const coreColors = [
|
|
@@ -578,7 +672,8 @@ function generateSemanticTokens(palette, mode, options) {
|
|
|
578
672
|
shadeOverrides[colorName]?.[mode],
|
|
579
673
|
colorName,
|
|
580
674
|
activeShades,
|
|
581
|
-
|
|
675
|
+
anchor(colorName),
|
|
676
|
+
gamutsFor(colorName)
|
|
582
677
|
);
|
|
583
678
|
}
|
|
584
679
|
}
|
|
@@ -641,7 +736,7 @@ function generateUtilityTokens(palette, mode, overrides, shades, seedShade) {
|
|
|
641
736
|
shadow: neutral[overrides?.shadow ? snap(overrides.shadow) : defaults.shadow]
|
|
642
737
|
};
|
|
643
738
|
}
|
|
644
|
-
function checkSemanticContrast(tokens) {
|
|
739
|
+
function checkSemanticContrast(tokens, gamut = "srgb") {
|
|
645
740
|
const results = {};
|
|
646
741
|
for (const [name, color] of Object.entries(tokens)) {
|
|
647
742
|
if (name === "muted") {
|
|
@@ -650,7 +745,8 @@ function checkSemanticContrast(tokens) {
|
|
|
650
745
|
const extendedColor = color;
|
|
651
746
|
const ratio = getContrastRatio(
|
|
652
747
|
extendedColor.DEFAULT,
|
|
653
|
-
extendedColor.foreground
|
|
748
|
+
extendedColor.foreground,
|
|
749
|
+
gamut
|
|
654
750
|
);
|
|
655
751
|
results[`${name}/default`] = {
|
|
656
752
|
ratio: Math.round(ratio * 100) / 100,
|
|
@@ -659,7 +755,8 @@ function checkSemanticContrast(tokens) {
|
|
|
659
755
|
};
|
|
660
756
|
const subduedRatio = getContrastRatio(
|
|
661
757
|
extendedColor.subdued,
|
|
662
|
-
extendedColor["subdued-foreground"]
|
|
758
|
+
extendedColor["subdued-foreground"],
|
|
759
|
+
gamut
|
|
663
760
|
);
|
|
664
761
|
results[`${name}/subdued`] = {
|
|
665
762
|
ratio: Math.round(subduedRatio * 100) / 100,
|
|
@@ -668,7 +765,8 @@ function checkSemanticContrast(tokens) {
|
|
|
668
765
|
};
|
|
669
766
|
const highlightRatio = getContrastRatio(
|
|
670
767
|
extendedColor.highlight,
|
|
671
|
-
extendedColor["highlight-foreground"]
|
|
768
|
+
extendedColor["highlight-foreground"],
|
|
769
|
+
gamut
|
|
672
770
|
);
|
|
673
771
|
results[`${name}/highlight`] = {
|
|
674
772
|
ratio: Math.round(highlightRatio * 100) / 100,
|
|
@@ -678,7 +776,8 @@ function checkSemanticContrast(tokens) {
|
|
|
678
776
|
}
|
|
679
777
|
const mutedRatio = getContrastRatio(
|
|
680
778
|
tokens.muted.DEFAULT,
|
|
681
|
-
tokens.muted.foreground
|
|
779
|
+
tokens.muted.foreground,
|
|
780
|
+
gamut
|
|
682
781
|
);
|
|
683
782
|
results["muted"] = {
|
|
684
783
|
ratio: Math.round(mutedRatio * 100) / 100,
|
|
@@ -687,30 +786,30 @@ function checkSemanticContrast(tokens) {
|
|
|
687
786
|
};
|
|
688
787
|
return results;
|
|
689
788
|
}
|
|
690
|
-
function checkSemanticContrastAPCA(tokens) {
|
|
789
|
+
function checkSemanticContrastAPCA(tokens, gamut = "srgb") {
|
|
691
790
|
const results = {};
|
|
692
791
|
for (const [name, color] of Object.entries(tokens)) {
|
|
693
792
|
if (name === "muted") {
|
|
694
793
|
continue;
|
|
695
794
|
}
|
|
696
795
|
const ext = color;
|
|
697
|
-
const defaultLc = getAPCAContrast(ext.foreground, ext.DEFAULT);
|
|
796
|
+
const defaultLc = getAPCAContrast(ext.foreground, ext.DEFAULT, gamut);
|
|
698
797
|
results[`${name}/default`] = {
|
|
699
798
|
lcValue: Math.round(defaultLc * 10) / 10,
|
|
700
799
|
level: classifyAPCALevel(defaultLc)
|
|
701
800
|
};
|
|
702
|
-
const subduedLc = getAPCAContrast(ext["subdued-foreground"], ext.subdued);
|
|
801
|
+
const subduedLc = getAPCAContrast(ext["subdued-foreground"], ext.subdued, gamut);
|
|
703
802
|
results[`${name}/subdued`] = {
|
|
704
803
|
lcValue: Math.round(subduedLc * 10) / 10,
|
|
705
804
|
level: classifyAPCALevel(subduedLc)
|
|
706
805
|
};
|
|
707
|
-
const highlightLc = getAPCAContrast(ext["highlight-foreground"], ext.highlight);
|
|
806
|
+
const highlightLc = getAPCAContrast(ext["highlight-foreground"], ext.highlight, gamut);
|
|
708
807
|
results[`${name}/highlight`] = {
|
|
709
808
|
lcValue: Math.round(highlightLc * 10) / 10,
|
|
710
809
|
level: classifyAPCALevel(highlightLc)
|
|
711
810
|
};
|
|
712
811
|
}
|
|
713
|
-
const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT);
|
|
812
|
+
const mutedLc = getAPCAContrast(tokens.muted.foreground, tokens.muted.DEFAULT, gamut);
|
|
714
813
|
results["muted"] = {
|
|
715
814
|
lcValue: Math.round(mutedLc * 10) / 10,
|
|
716
815
|
level: classifyAPCALevel(mutedLc)
|
|
@@ -718,9 +817,9 @@ function checkSemanticContrastAPCA(tokens) {
|
|
|
718
817
|
return results;
|
|
719
818
|
}
|
|
720
819
|
var NON_TEXT_CONTRAST_MIN = 3;
|
|
721
|
-
function checkSurfaceContrast(surface, utility) {
|
|
820
|
+
function checkSurfaceContrast(surface, utility, gamut = "srgb") {
|
|
722
821
|
const pair = (bg, fg, nonText = false, advisory = false) => {
|
|
723
|
-
const ratio = getContrastRatio(bg, fg);
|
|
822
|
+
const ratio = getContrastRatio(bg, fg, gamut);
|
|
724
823
|
return {
|
|
725
824
|
ratio: Math.round(ratio * 100) / 100,
|
|
726
825
|
passesAA: meetsWCAG_AA(ratio),
|
|
@@ -760,13 +859,13 @@ function checkRampChroma(palette) {
|
|
|
760
859
|
}
|
|
761
860
|
return results;
|
|
762
861
|
}
|
|
763
|
-
function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa") {
|
|
862
|
+
function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa", gamut = "srgb") {
|
|
764
863
|
const { floor } = getContrastThresholds(algorithm, contrastTarget);
|
|
765
864
|
const skipKeys = /* @__PURE__ */ new Set(["muted"]);
|
|
766
865
|
let passed = 0;
|
|
767
866
|
let total = 0;
|
|
768
867
|
if (algorithm === "wcag") {
|
|
769
|
-
const results = checkSemanticContrast(tokens);
|
|
868
|
+
const results = checkSemanticContrast(tokens, gamut);
|
|
770
869
|
for (const [key, result] of Object.entries(results)) {
|
|
771
870
|
const colorName = key.split("/")[0];
|
|
772
871
|
if (skipKeys.has(colorName)) continue;
|
|
@@ -776,7 +875,7 @@ function computeAccessibilityScore(tokens, algorithm, contrastTarget = "aa") {
|
|
|
776
875
|
}
|
|
777
876
|
}
|
|
778
877
|
} else {
|
|
779
|
-
const results = checkSemanticContrastAPCA(tokens);
|
|
878
|
+
const results = checkSemanticContrastAPCA(tokens, gamut);
|
|
780
879
|
for (const [key, result] of Object.entries(results)) {
|
|
781
880
|
const colorName = key.split("/")[0];
|
|
782
881
|
if (skipKeys.has(colorName)) continue;
|
|
@@ -865,35 +964,39 @@ var defaultShadows = {
|
|
|
865
964
|
}
|
|
866
965
|
};
|
|
867
966
|
function generateTokens(brandColors, options) {
|
|
868
|
-
const
|
|
869
|
-
|
|
870
|
-
options?.
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
const
|
|
875
|
-
const
|
|
876
|
-
const
|
|
877
|
-
|
|
878
|
-
const darkUtility = generateUtilityTokens(primitives, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade);
|
|
879
|
-
return {
|
|
880
|
-
primitives,
|
|
967
|
+
const strategy = options?.lightnessStrategy ?? "fixed";
|
|
968
|
+
const scaleOptions = {
|
|
969
|
+
seedShade: options?.seedShade,
|
|
970
|
+
seedShades: options?.seedShades,
|
|
971
|
+
shades: options?.shades
|
|
972
|
+
};
|
|
973
|
+
const primitives = generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "srgb" });
|
|
974
|
+
const wide = options?.gamut === "p3" ? generatePrimitivePalette(brandColors, strategy, { ...scaleOptions, gamut: "p3" }) : void 0;
|
|
975
|
+
const colourTiers = (palette, gamuts) => ({
|
|
976
|
+
primitives: palette,
|
|
881
977
|
semantic: {
|
|
882
|
-
light:
|
|
883
|
-
dark:
|
|
978
|
+
light: generateSemanticTokens(palette, "light", options, gamuts),
|
|
979
|
+
dark: generateSemanticTokens(palette, "dark", options, gamuts)
|
|
884
980
|
},
|
|
885
981
|
surface: {
|
|
886
|
-
light:
|
|
887
|
-
dark:
|
|
982
|
+
light: generateSurfaceTokens(palette, "light", options?.surfaceShadeOverrides?.light, options?.shades),
|
|
983
|
+
dark: generateSurfaceTokens(palette, "dark", options?.surfaceShadeOverrides?.dark, options?.shades)
|
|
888
984
|
},
|
|
889
985
|
utility: {
|
|
890
|
-
light:
|
|
891
|
-
dark:
|
|
892
|
-
}
|
|
986
|
+
light: generateUtilityTokens(palette, "light", options?.utilityShadeOverrides?.light, options?.shades, options?.seedShade),
|
|
987
|
+
dark: generateUtilityTokens(palette, "dark", options?.utilityShadeOverrides?.dark, options?.shades, options?.seedShade)
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
return {
|
|
991
|
+
...colourTiers(primitives, {
|
|
992
|
+
own: "srgb",
|
|
993
|
+
...wide ? { other: { gamut: "p3", palette: wide } } : {}
|
|
994
|
+
}),
|
|
893
995
|
spacing: {},
|
|
894
996
|
typography: defaultTypography,
|
|
895
997
|
radii: defaultRadii,
|
|
896
|
-
shadows: defaultShadows
|
|
998
|
+
shadows: defaultShadows,
|
|
999
|
+
...wide ? { p3: colourTiers(wide, { own: "p3", other: { gamut: "srgb", palette: primitives } }) } : {}
|
|
897
1000
|
};
|
|
898
1001
|
}
|
|
899
1002
|
|
|
@@ -920,7 +1023,28 @@ function emitPrimitiveVars(tokens, colorFormat, want, indent, inFamily, scoped)
|
|
|
920
1023
|
}
|
|
921
1024
|
return lines;
|
|
922
1025
|
}
|
|
923
|
-
|
|
1026
|
+
var WIDE_GAMUT_QUERY = "@media (color-gamut: p3)";
|
|
1027
|
+
function emitWideGamutBlock(tokens, inFamily) {
|
|
1028
|
+
if (!tokens.p3) return [];
|
|
1029
|
+
const lines = [];
|
|
1030
|
+
for (const [name, scale] of Object.entries(tokens.p3.primitives)) {
|
|
1031
|
+
if (typeof scale === "string" || !inFamily(name)) continue;
|
|
1032
|
+
lines.push(...colorScaleToCssVars(name, scale, "oklch", " "));
|
|
1033
|
+
}
|
|
1034
|
+
if (lines.length === 0) return [];
|
|
1035
|
+
return [
|
|
1036
|
+
"",
|
|
1037
|
+
"/* Wide gamut: the same ramps with the chroma a P3 display can show. Every",
|
|
1038
|
+
" semantic pairing is contrast-checked on these values and on the sRGB ones. */",
|
|
1039
|
+
`${WIDE_GAMUT_QUERY} {`,
|
|
1040
|
+
" :root {",
|
|
1041
|
+
...lines,
|
|
1042
|
+
" }",
|
|
1043
|
+
"}"
|
|
1044
|
+
];
|
|
1045
|
+
}
|
|
1046
|
+
function getExtendedMappings(mode, colorName, overrides, active, anchors) {
|
|
1047
|
+
const seedShade = familySeedShade(colorName, anchors.seedShade, anchors.seedShades);
|
|
924
1048
|
return extendedBridgeShades(mode, seedShade, active, overrides?.[colorName]?.[mode]);
|
|
925
1049
|
}
|
|
926
1050
|
var MUTED_SHADE_MAPPINGS = {
|
|
@@ -981,13 +1105,13 @@ function emitSurfaceVars(surface, colorFormat, indent) {
|
|
|
981
1105
|
}
|
|
982
1106
|
return lines;
|
|
983
1107
|
}
|
|
984
|
-
function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped,
|
|
1108
|
+
function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors) {
|
|
985
1109
|
const lines = [];
|
|
986
1110
|
const extendedSemanticNames = ["primary", "neutral", "success", "destructive", "warning"];
|
|
987
1111
|
for (const name of extendedSemanticNames) {
|
|
988
1112
|
if (!inFamily(name)) continue;
|
|
989
1113
|
const color = tokens.semantic[mode][name];
|
|
990
|
-
const mappings = getExtendedMappings(mode, name, shadeOverrides, active,
|
|
1114
|
+
const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
|
|
991
1115
|
lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
|
|
992
1116
|
lines.push(`${indent}--${name}-foreground: ${color._refs ? refToVar(color._refs.foreground) : formatColor(color.foreground, colorFormat)};`);
|
|
993
1117
|
lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
|
|
@@ -998,7 +1122,7 @@ function emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, act
|
|
|
998
1122
|
for (const [name, color] of Object.entries(tokens.semantic[mode])) {
|
|
999
1123
|
if (!extendedSemanticNames.includes(name) && name !== "muted" && inFamily(name)) {
|
|
1000
1124
|
const ext = color;
|
|
1001
|
-
const mappings = getExtendedMappings(mode, name, shadeOverrides, active,
|
|
1125
|
+
const mappings = getExtendedMappings(mode, name, shadeOverrides, active, anchors);
|
|
1002
1126
|
lines.push(`${indent}--${name}: var(--color-${name}-${mappings.DEFAULT});`);
|
|
1003
1127
|
lines.push(`${indent}--${name}-foreground: ${ext._refs ? refToVar(ext._refs.foreground) : formatColor(ext.foreground, colorFormat)};`);
|
|
1004
1128
|
lines.push(`${indent}--${name}-subdued: var(--color-${name}-${mappings.subdued});`);
|
|
@@ -1030,8 +1154,9 @@ function emitUtilityVars(mode, indent, active, seedShade) {
|
|
|
1030
1154
|
}
|
|
1031
1155
|
return lines;
|
|
1032
1156
|
}
|
|
1033
|
-
function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades, families, seedShade) {
|
|
1157
|
+
function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shades, families, seedShade, seedShades) {
|
|
1034
1158
|
const lines = [];
|
|
1159
|
+
const anchors = { seedShade, seedShades };
|
|
1035
1160
|
const active = resolveShades(shades);
|
|
1036
1161
|
const want = sectionSet(sections);
|
|
1037
1162
|
const scoped = !!families && families.length > 0;
|
|
@@ -1052,7 +1177,7 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1052
1177
|
}
|
|
1053
1178
|
if (want.has("semantic")) {
|
|
1054
1179
|
if (withComments) body.push(`${indent}/* Semantic Colors */`);
|
|
1055
|
-
body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped,
|
|
1180
|
+
body.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1056
1181
|
}
|
|
1057
1182
|
while (body.length > 0 && body[body.length - 1] === "") body.pop();
|
|
1058
1183
|
return body;
|
|
@@ -1062,6 +1187,7 @@ function exportToCss(tokens, colorFormat = "hex", shadeOverrides, sections, shad
|
|
|
1062
1187
|
lines.push(" /* Primitive Colors */");
|
|
1063
1188
|
lines.push(...emitPrimitiveVars(tokens, colorFormat, want, " ", inFamily, scoped));
|
|
1064
1189
|
lines.push("}");
|
|
1190
|
+
lines.push(...emitWideGamutBlock(tokens, inFamily));
|
|
1065
1191
|
}
|
|
1066
1192
|
if (wantModes) {
|
|
1067
1193
|
if (wantPrimitives) lines.push("");
|
|
@@ -1104,9 +1230,10 @@ function overrideContract() {
|
|
|
1104
1230
|
" * Then check BOTH colour schemes. A headless browser defaults to light and",
|
|
1105
1231
|
" * will report a broken dark mode as passing.",
|
|
1106
1232
|
" *",
|
|
1107
|
-
" *
|
|
1108
|
-
" *
|
|
1109
|
-
" *
|
|
1233
|
+
" * Do not override a --color-<name>-<step> ramp step by hand: the contrast",
|
|
1234
|
+
" * search never saw it, --check cannot detect it, and Tailwind still inlines",
|
|
1235
|
+
" * its old value into opacity-modifier fallbacks. Change the seed and",
|
|
1236
|
+
" * regenerate instead.",
|
|
1110
1237
|
" */"
|
|
1111
1238
|
].join("\n");
|
|
1112
1239
|
}
|
|
@@ -1178,8 +1305,9 @@ function emitThemeRebinding(aliases, indent) {
|
|
|
1178
1305
|
}
|
|
1179
1306
|
return lines;
|
|
1180
1307
|
}
|
|
1181
|
-
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped,
|
|
1308
|
+
function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases, want, indent, active, inFamily, scoped, anchors) {
|
|
1182
1309
|
const lines = [];
|
|
1310
|
+
const seedShade = anchors.seedShade;
|
|
1183
1311
|
if (want.has("surface") && !scoped) {
|
|
1184
1312
|
lines.push(`${indent}/* Surface */`);
|
|
1185
1313
|
lines.push(...emitSurfaceVars(tokens.surface[mode], colorFormat, indent));
|
|
@@ -1192,14 +1320,15 @@ function emitTailwindV4Mode(tokens, mode, colorFormat, shadeOverrides, aliases,
|
|
|
1192
1320
|
}
|
|
1193
1321
|
if (want.has("semantic")) {
|
|
1194
1322
|
lines.push(`${indent}/* Semantic Colors */`);
|
|
1195
|
-
lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped,
|
|
1323
|
+
lines.push(...emitSemanticVars(tokens, mode, colorFormat, shadeOverrides, indent, active, inFamily, scoped, anchors));
|
|
1196
1324
|
}
|
|
1197
1325
|
while (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
|
|
1198
1326
|
lines.push(...emitThemeRebinding(aliases, indent));
|
|
1199
1327
|
return lines;
|
|
1200
1328
|
}
|
|
1201
|
-
function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades, families, seedShade) {
|
|
1329
|
+
function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replacePalette = true, sections, shades, families, seedShade, seedShades) {
|
|
1202
1330
|
const lines = [];
|
|
1331
|
+
const anchors = { seedShade, seedShades };
|
|
1203
1332
|
const active = resolveShades(shades);
|
|
1204
1333
|
const want = sectionSet(sections);
|
|
1205
1334
|
const scoped = !!families && families.length > 0;
|
|
@@ -1227,22 +1356,23 @@ function exportToTailwindV4(tokens, colorFormat = "hex", shadeOverrides, replace
|
|
|
1227
1356
|
}
|
|
1228
1357
|
}
|
|
1229
1358
|
lines.push("}");
|
|
1359
|
+
if (wantPrimitives) lines.push(...emitWideGamutBlock(tokens, inFamily));
|
|
1230
1360
|
if (wantModes) {
|
|
1231
1361
|
lines.push("");
|
|
1232
1362
|
lines.push("/* Light Mode */");
|
|
1233
1363
|
lines.push(`${MODE_SELECTORS.light} {`);
|
|
1234
|
-
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped,
|
|
1364
|
+
lines.push(...emitTailwindV4Mode(tokens, "light", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
1235
1365
|
lines.push("}");
|
|
1236
1366
|
lines.push("");
|
|
1237
1367
|
lines.push("/* Dark Mode */");
|
|
1238
1368
|
lines.push(`${MODE_SELECTORS.dark} {`);
|
|
1239
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped,
|
|
1369
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
1240
1370
|
lines.push("}");
|
|
1241
1371
|
lines.push("");
|
|
1242
1372
|
lines.push("/* System Preference: Dark Mode */");
|
|
1243
1373
|
lines.push("@media (prefers-color-scheme: dark) {");
|
|
1244
1374
|
lines.push(` ${MODE_SELECTORS.systemDark} {`);
|
|
1245
|
-
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped,
|
|
1375
|
+
lines.push(...emitTailwindV4Mode(tokens, "dark", colorFormat, shadeOverrides, aliases, want, " ", active, inFamily, scoped, anchors));
|
|
1246
1376
|
lines.push(" }");
|
|
1247
1377
|
lines.push("}");
|
|
1248
1378
|
}
|
|
@@ -1453,6 +1583,12 @@ function selectTokenSections(tokens, sections, families) {
|
|
|
1453
1583
|
}
|
|
1454
1584
|
if (want.has("surface") && !scoped) out.surface = tokens.surface;
|
|
1455
1585
|
if (want.has("utility") && !scoped) out.utility = tokens.utility;
|
|
1586
|
+
if (tokens.p3) {
|
|
1587
|
+
const wide = selectTokenSections({ ...tokens, ...tokens.p3, p3: void 0 }, sections, families);
|
|
1588
|
+
out.p3 = Object.fromEntries(
|
|
1589
|
+
["primitives", "semantic", "surface", "utility"].filter((tier) => wide[tier] !== void 0).map((tier) => [tier, wide[tier]])
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1456
1592
|
return out;
|
|
1457
1593
|
}
|
|
1458
1594
|
function exportTokens(tokens, format, options) {
|
|
@@ -1463,6 +1599,7 @@ function exportTokens(tokens, format, options) {
|
|
|
1463
1599
|
const shades = options?.shades;
|
|
1464
1600
|
const families = options?.families;
|
|
1465
1601
|
const seedShade = options?.seedShade;
|
|
1602
|
+
const seedShades = options?.seedShades;
|
|
1466
1603
|
if (families && families.length > 0 && !SECTIONED_FORMATS.includes(format)) {
|
|
1467
1604
|
throw new Error(
|
|
1468
1605
|
`Format "${format}" cannot be scoped to colour families; supported: ${SECTIONED_FORMATS.join(", ")}`
|
|
@@ -1475,11 +1612,11 @@ function exportTokens(tokens, format, options) {
|
|
|
1475
1612
|
}
|
|
1476
1613
|
switch (format) {
|
|
1477
1614
|
case "css":
|
|
1478
|
-
return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades, families, seedShade);
|
|
1615
|
+
return exportToCss(tokens, colorFormat, shadeOverrides, sections, shades, families, seedShade, seedShades);
|
|
1479
1616
|
case "tailwind-v3":
|
|
1480
1617
|
return exportToTailwindV3(tokens, colorFormat);
|
|
1481
1618
|
case "tailwind-v4":
|
|
1482
|
-
return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades, families, seedShade);
|
|
1619
|
+
return exportToTailwindV4(tokens, colorFormat, shadeOverrides, replacePalette, sections, shades, families, seedShade, seedShades);
|
|
1483
1620
|
case "dtcg":
|
|
1484
1621
|
return exportToDtcg(tokens);
|
|
1485
1622
|
default:
|
|
@@ -1502,7 +1639,7 @@ function walk(css, onDeclaration) {
|
|
|
1502
1639
|
const trimmed = line.trim();
|
|
1503
1640
|
const declaration = /^(--[a-z0-9-]+)\s*:\s*(.+?);\s*$/i.exec(trimmed);
|
|
1504
1641
|
if (declaration && stack.length > 0) {
|
|
1505
|
-
onDeclaration(line, index, stack
|
|
1642
|
+
onDeclaration(line, index, stack.join(" > "), declaration[1], declaration[2]);
|
|
1506
1643
|
}
|
|
1507
1644
|
for (const char of trimmed) {
|
|
1508
1645
|
if (char === "{") {
|
|
@@ -1655,6 +1792,8 @@ function withJsonProvenance(json, p) {
|
|
|
1655
1792
|
}
|
|
1656
1793
|
|
|
1657
1794
|
export {
|
|
1795
|
+
APCAUnavailableError,
|
|
1796
|
+
isAPCAAvailable,
|
|
1658
1797
|
VALID_SHADES,
|
|
1659
1798
|
DEFAULT_SEED_SHADE,
|
|
1660
1799
|
TAILWIND_SHADES,
|
|
@@ -1663,8 +1802,10 @@ export {
|
|
|
1663
1802
|
snapShade,
|
|
1664
1803
|
reanchorFactors,
|
|
1665
1804
|
chromaShadeFor,
|
|
1805
|
+
isInGamut,
|
|
1666
1806
|
parseToOklch,
|
|
1667
1807
|
oklchToHex,
|
|
1808
|
+
formatOklchInGamut,
|
|
1668
1809
|
generateColorScale,
|
|
1669
1810
|
generateColorScaleAPCA,
|
|
1670
1811
|
adjustLightness,
|
|
@@ -1683,6 +1824,7 @@ export {
|
|
|
1683
1824
|
DEFAULT_NEUTRAL_COLOR,
|
|
1684
1825
|
ALIAS_PAIRS,
|
|
1685
1826
|
aliasOf,
|
|
1827
|
+
familySeedShade,
|
|
1686
1828
|
generatePrimitivePalette,
|
|
1687
1829
|
getShade,
|
|
1688
1830
|
extendedBridgeShades,
|
|
@@ -1696,6 +1838,7 @@ export {
|
|
|
1696
1838
|
checkRampChroma,
|
|
1697
1839
|
computeAccessibilityScore,
|
|
1698
1840
|
generateTokens,
|
|
1841
|
+
WIDE_GAMUT_QUERY,
|
|
1699
1842
|
exportToCss,
|
|
1700
1843
|
overrideContract,
|
|
1701
1844
|
TOKEN_SECTIONS,
|