@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/dist/cli.js CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
+ APCAUnavailableError,
3
4
  DEFAULT_NEUTRAL_COLOR,
4
5
  SECTIONED_FORMATS,
5
6
  SHADE_PRESETS,
@@ -15,6 +16,8 @@ import {
15
16
  extendedBridgeShades,
16
17
  externalReferences,
17
18
  generateTokens,
19
+ isAPCAAvailable,
20
+ isInGamut,
18
21
  mergeSlice,
19
22
  overrideContract,
20
23
  parseProvenance,
@@ -27,7 +30,7 @@ import {
27
30
  snapShade,
28
31
  tokenFamilies,
29
32
  withJsonProvenance
30
- } from "./chunk-MCLZSLHE.js";
33
+ } from "./chunk-UW2HXSV4.js";
31
34
 
32
35
  // src/cli.ts
33
36
  import { parseArgs } from "util";
@@ -43,7 +46,10 @@ Seeds:
43
46
  --primary <colour> Brand colour (hex, rgb(), hsl(), oklch(), named). Required
44
47
  unless supplied by --seed.
45
48
  --neutral <colour> Neutral seed. Default: ${DEFAULT_NEUTRAL_COLOR}
46
- --color <name>=<colour> Additional brand colour scale. Repeatable.
49
+ --color <name>=<colour>[@<shade>]
50
+ Additional brand colour scale. Repeatable. Append
51
+ @<shade> to anchor its seed on that step, as
52
+ --seed-shade does for the primary: accent=#fde68a@200.
47
53
  --seed <file.json> Replay a saved configuration (the shape the web tool
48
54
  persists). Any flag given alongside it wins, so a seed
49
55
  file is a set of defaults rather than a lock.
@@ -52,15 +58,28 @@ Generation options:
52
58
  --contrast <wcag|apca> Contrast algorithm for foreground selection. Default: wcag
53
59
  --target <aa-large|aa|aaa> Contrast target level. Default: aa
54
60
  --lightness <fixed|perceptual> Lightness distribution strategy. Default: fixed
55
- --seed-shade <25..950> Which step the seed colour lands on. Default: 500.
61
+ --seed-shade <25..950> Which step the primary seed lands on. Default: 500.
56
62
  Brand colours arrive as whatever they are; a deep
57
63
  navy or a pale tint forced onto 500 leaves no
58
64
  headroom on one side and wrecks the ramp.
65
+ Moves the primary only: neutral and the derived
66
+ status colours stay on 500. (Before 0.5.0 it moved
67
+ every family.) Anchor the others with
68
+ --neutral-seed-shade and --color name=colour@shade.
69
+ --neutral-seed-shade <25..950> Which step the neutral seed lands on. Default: 500.
59
70
  --shades <list|preset> Which steps to emit. Default: all twelve (25-950).
60
71
  Presets: tailwind (50-950, matching Tailwind's
61
72
  stock ramps) and full. Or a list: 50,100,500,900.
62
73
  Tokens naming a dropped step snap to the nearest
63
74
  emitted one, reported on stderr.
75
+ --gamut <srgb|p3> Which displays to generate for. Default: srgb.
76
+ srgb: every colour is one every screen shows exactly,
77
+ so the contrast checks measure what is displayed.
78
+ p3: also emit richer ramps in a
79
+ @media (color-gamut: p3) block for displays that can
80
+ show them (most phones and Macs). Every foreground is
81
+ chosen to pass on both. css and tailwind-v4 only;
82
+ json carries them as "p3".
64
83
  --overrides <file.json> JSON file with { shadeOverrides, surfaceShadeOverrides,
65
84
  utilityShadeOverrides } (same shape as the web tool).
66
85
 
@@ -166,7 +185,9 @@ try {
166
185
  contrast: { type: "string" },
167
186
  target: { type: "string" },
168
187
  lightness: { type: "string" },
188
+ gamut: { type: "string" },
169
189
  "seed-shade": { type: "string" },
190
+ "neutral-seed-shade": { type: "string" },
170
191
  shades: { type: "string" },
171
192
  overrides: { type: "string" },
172
193
  only: { type: "string", multiple: true },
@@ -233,22 +254,33 @@ var neutral = validateColor(
233
254
  values.neutral ?? seed.neutralColor ?? DEFAULT_NEUTRAL_COLOR
234
255
  );
235
256
  var additionalColors = {};
236
- for (const { name, color } of seed.additionalColors ?? []) {
257
+ var additionalAnchors = {};
258
+ for (const { name, color, seedShade: anchor } of seed.additionalColors ?? []) {
237
259
  additionalColors[name] = validateColor(`seed colour ${name}`, color);
260
+ if (anchor !== void 0) additionalAnchors[name] = { flag: `seed (additionalColors ${name})`, raw: anchor };
238
261
  }
239
262
  for (const entry of values.color ?? []) {
240
263
  const eq = entry.indexOf("=");
241
264
  if (eq === -1) fail(`--color expects name=colour (got "${entry}")`);
242
265
  const name = entry.slice(0, eq).trim();
243
- const colour = entry.slice(eq + 1).trim();
266
+ let colour = entry.slice(eq + 1).trim();
244
267
  if (!/^[a-z][a-z0-9-]*$/i.test(name)) {
245
268
  fail(`--color name must be alphanumeric/kebab-case (got "${name}")`);
246
269
  }
270
+ const at = /^(.*)@(\d+)$/.exec(colour);
271
+ if (at) {
272
+ colour = at[1].trim();
273
+ additionalAnchors[name] = { flag: `color ${name}@`, raw: at[2] };
274
+ } else {
275
+ delete additionalAnchors[name];
276
+ }
247
277
  additionalColors[name] = validateColor(`color ${name}`, colour);
248
278
  }
249
279
  var contrastAlgorithm = validateChoice("contrast", values.contrast ?? seed.contrastAlgorithm, ["wcag", "apca"], "wcag");
280
+ if (contrastAlgorithm === "apca" && !isAPCAAvailable()) fail(new APCAUnavailableError().message);
250
281
  var contrastTarget = validateChoice("target", values.target ?? seed.contrastTarget, ["aa-large", "aa", "aaa"], "aa");
251
282
  var lightnessStrategy = validateChoice("lightness", values.lightness ?? seed.lightnessStrategy, ["fixed", "perceptual"], "fixed");
283
+ var gamut = validateChoice("gamut", values.gamut ?? seed.gamut, ["srgb", "p3"], "srgb");
252
284
  var format = validateChoice(
253
285
  "format",
254
286
  values.format,
@@ -257,7 +289,7 @@ var format = validateChoice(
257
289
  );
258
290
  var colorFormat = validateChoice("color-format", values["color-format"] ?? seed.colorFormat, ["oklch", "hex", "rgb", "hsl"], "oklch");
259
291
  function parseShade(flag, raw) {
260
- const n = Number(raw.trim());
292
+ const n = Number(String(raw).trim());
261
293
  if (!VALID_SHADES.includes(n)) {
262
294
  fail(`--${flag} must be one of: ${VALID_SHADES.join(", ")} (got "${raw}")`);
263
295
  }
@@ -271,9 +303,21 @@ if (shadesInput !== void 0) {
271
303
  if (shades.length < 3) fail(`--shades needs at least 3 steps (got ${shades.length})`);
272
304
  }
273
305
  var seedShade = values["seed-shade"] !== void 0 ? parseShade("seed-shade", values["seed-shade"]) : seed.seedShade;
274
- if (seedShade !== void 0 && shades !== void 0 && !shades.includes(seedShade)) {
275
- const emitted = shades;
276
- fail(`--seed-shade ${seedShade} is not in --shades (${emitted.join(", ")})`);
306
+ var neutralSeedShade = values["neutral-seed-shade"] !== void 0 ? parseShade("neutral-seed-shade", values["neutral-seed-shade"]) : seed.neutralSeedShade;
307
+ var seedShades = {};
308
+ if (neutralSeedShade !== void 0) seedShades.neutral = neutralSeedShade;
309
+ for (const [name, { flag, raw }] of Object.entries(additionalAnchors)) {
310
+ seedShades[name] = parseShade(flag, raw);
311
+ }
312
+ for (const [flag, anchor] of [
313
+ ["--seed-shade", seedShade],
314
+ ...Object.entries(seedShades).map(
315
+ ([name, a]) => [name === "neutral" && neutralSeedShade !== void 0 ? "--neutral-seed-shade" : `--color ${name}@`, a]
316
+ )
317
+ ]) {
318
+ if (anchor !== void 0 && shades !== void 0 && !shades.includes(anchor)) {
319
+ fail(`${flag} ${anchor} is not in --shades (${shades.join(", ")})`);
320
+ }
277
321
  }
278
322
  var sections = (values.only ?? []).flatMap((entry) => entry.split(",")).map((s) => s.trim()).filter((s) => s.length > 0).map((s) => {
279
323
  if (!TOKEN_SECTIONS.includes(s)) {
@@ -322,11 +366,40 @@ var options = {
322
366
  contrastTarget,
323
367
  lightnessStrategy,
324
368
  seedShade,
369
+ ...Object.keys(seedShades).length > 0 ? { seedShades } : {},
325
370
  shades,
326
371
  shadeOverrides: overrides.shadeOverrides ?? {},
327
372
  surfaceShadeOverrides: overrides.surfaceShadeOverrides,
328
- utilityShadeOverrides: overrides.utilityShadeOverrides
373
+ utilityShadeOverrides: overrides.utilityShadeOverrides,
374
+ gamut
329
375
  };
376
+ {
377
+ const seeds = [
378
+ ["--primary", primary],
379
+ ["--neutral", neutral],
380
+ ...Object.entries(additionalColors).map(([name, c]) => [`--color ${name}`, c])
381
+ ];
382
+ for (const [label, colour] of seeds) {
383
+ if (isInGamut(colour, "srgb")) continue;
384
+ const kept = gamut === "p3" && isInGamut(colour, "p3");
385
+ process.stderr.write(
386
+ `generate-tokens: ${label} ${colour} is outside sRGB; its chroma is reduced to fit, keeping lightness and hue.` + (kept ? ` The p3 layer keeps it as given.
387
+ ` : `
388
+ `)
389
+ );
390
+ }
391
+ }
392
+ if (gamut === "p3") {
393
+ const flat = (format === "all" ? ["tailwind-v3", "dtcg"] : [format]).filter(
394
+ (f) => f === "tailwind-v3" || f === "dtcg"
395
+ );
396
+ for (const f of flat) {
397
+ process.stderr.write(
398
+ `generate-tokens: --format ${f} has no way to carry a wide-gamut layer; it gets the sRGB values only.
399
+ `
400
+ );
401
+ }
402
+ }
330
403
  for (const [flag, why] of [
331
404
  ["vanilla", "the shadcn/ui aliases it switched off no longer exist, so this is already the only behaviour"],
332
405
  ["replace-palette", "clearing Tailwind's stock palette is the default; use --no-replace-palette to opt out"]
@@ -378,16 +451,24 @@ var replacePalette = values["no-replace-palette"] !== true;
378
451
  var resolvedSeed = {
379
452
  primaryColor: primary,
380
453
  neutralColor: neutral,
381
- additionalColors: Object.entries(additionalColors).map(([name, color]) => ({ name, color })),
454
+ additionalColors: Object.entries(additionalColors).map(([name, color]) => ({
455
+ name,
456
+ color,
457
+ seedShade: seedShades[name]
458
+ })),
382
459
  colorFormat,
383
460
  contrastAlgorithm,
384
461
  contrastTarget,
385
462
  lightnessStrategy,
386
463
  seedShade,
464
+ neutralSeedShade,
387
465
  shades,
388
466
  shadeOverrides: options.shadeOverrides ?? {},
389
467
  surfaceShadeOverrides: options.surfaceShadeOverrides,
390
- utilityShadeOverrides: options.utilityShadeOverrides
468
+ utilityShadeOverrides: options.utilityShadeOverrides,
469
+ // srgb is the default and was the only behaviour before --gamut existed, so
470
+ // it is left out: an sRGB seed keeps the fingerprint it always had.
471
+ gamut: gamut === "p3" ? gamut : void 0
391
472
  };
392
473
  var provenance = {
393
474
  version: VERSION,
@@ -405,7 +486,8 @@ function serialise(fmt) {
405
486
  sections: SECTIONED_FORMATS.includes(fmt) ? sections : void 0,
406
487
  shades,
407
488
  families: SECTIONED_FORMATS.includes(fmt) ? families : void 0,
408
- seedShade: options.seedShade
489
+ seedShade: options.seedShade,
490
+ seedShades: options.seedShades
409
491
  });
410
492
  if (!stampProvenance) return body;
411
493
  if (JSON_FORMATS.includes(fmt)) return withJsonProvenance(body, provenance);
@@ -456,10 +538,13 @@ function check(fmt, target) {
456
538
  return false;
457
539
  }
458
540
  function reportBridge() {
459
- process.stderr.write(`
460
- semantic bridge (seed anchored at ${options.seedShade ?? 500})
461
- `);
462
541
  const active = resolveShades(shades);
542
+ const anchored = Object.entries(seedShades);
543
+ process.stderr.write(
544
+ `
545
+ semantic bridge for primary, anchored at ${options.seedShade ?? 500} (${anchored.map(([n, a]) => `${n} at ${a}; `).join("")}other families at 500)
546
+ `
547
+ );
463
548
  for (const mode of ["light", "dark"]) {
464
549
  const e = extendedBridgeShades(mode, options.seedShade, active);
465
550
  process.stderr.write(
@@ -488,19 +573,28 @@ ramp chroma (peak C per family)
488
573
  );
489
574
  }
490
575
  }
576
+ function reportGamuts() {
577
+ const out = [
578
+ { gamut: "srgb", label: "", tiers: tokens }
579
+ ];
580
+ if (tokens.p3) out.push({ gamut: "p3", label: " on p3 displays", tiers: tokens.p3 });
581
+ return out;
582
+ }
491
583
  function reportSurfaces() {
492
584
  const width = 34;
493
- for (const mode of ["light", "dark"]) {
494
- process.stderr.write(`
495
- ${mode} surfaces and utilities (wcag, not counted in the score)
585
+ for (const { gamut: g, label, tiers } of reportGamuts()) {
586
+ for (const mode of ["light", "dark"]) {
587
+ process.stderr.write(`
588
+ ${mode} surfaces and utilities${label} (wcag, not counted in the score)
496
589
  `);
497
- for (const [pair, r] of Object.entries(
498
- checkSurfaceContrast(tokens.surface[mode], tokens.utility[mode])
499
- )) {
500
- const mark = r.advisory ? " " : r.nonText ? r.passes ? "3:1" : "- " : r.passesAAA ? "AAA" : r.passesAA ? "AA " : "- ";
501
- const note = r.advisory ? " (advisory: 3:1 only where it bounds a control)" : r.nonText ? " (non-text, 1.4.11)" : "";
502
- process.stderr.write(` ${mark} ${pair.padEnd(width)} ${r.ratio.toFixed(2)}:1${note}
590
+ for (const [pair, r] of Object.entries(
591
+ checkSurfaceContrast(tiers.surface[mode], tiers.utility[mode], g)
592
+ )) {
593
+ const mark = r.advisory ? " " : r.nonText ? r.passes ? "3:1" : "- " : r.passesAAA ? "AAA" : r.passesAA ? "AA " : "- ";
594
+ const note = r.advisory ? " (advisory: 3:1 only where it bounds a control)" : r.nonText ? " (non-text, 1.4.11)" : "";
595
+ process.stderr.write(` ${mark} ${pair.padEnd(width)} ${r.ratio.toFixed(2)}:1${note}
503
596
  `);
597
+ }
504
598
  }
505
599
  }
506
600
  }
@@ -508,32 +602,35 @@ function report() {
508
602
  const width = 34;
509
603
  reportBridge();
510
604
  reportChroma();
511
- for (const mode of ["light", "dark"]) {
512
- const semantic = tokens.semantic[mode];
513
- process.stderr.write(`
514
- ${mode} mode (${contrastAlgorithm}, target ${contrastTarget})
605
+ for (const { gamut: g, label, tiers } of reportGamuts()) {
606
+ for (const mode of ["light", "dark"]) {
607
+ const semantic = tiers.semantic[mode];
608
+ process.stderr.write(`
609
+ ${mode} mode${label} (${contrastAlgorithm}, target ${contrastTarget})
515
610
  `);
516
- if (contrastAlgorithm === "wcag") {
517
- for (const [pair, r] of Object.entries(checkSemanticContrast(semantic))) {
518
- const mark = r.passesAAA ? "AAA" : r.passesAA ? "AA " : "- ";
519
- process.stderr.write(` ${mark} ${pair.padEnd(width)} ${r.ratio.toFixed(2)}:1
611
+ if (contrastAlgorithm === "wcag") {
612
+ for (const [pair, r] of Object.entries(checkSemanticContrast(semantic, g))) {
613
+ const mark = r.passesAAA ? "AAA" : r.passesAA ? "AA " : "- ";
614
+ process.stderr.write(` ${mark} ${pair.padEnd(width)} ${r.ratio.toFixed(2)}:1
520
615
  `);
521
- }
522
- } else {
523
- for (const [pair, r] of Object.entries(checkSemanticContrastAPCA(semantic))) {
524
- process.stderr.write(
525
- ` ${r.level.padEnd(10)} ${pair.padEnd(width)} Lc ${r.lcValue.toFixed(1)}
616
+ }
617
+ } else {
618
+ for (const [pair, r] of Object.entries(checkSemanticContrastAPCA(semantic, g))) {
619
+ process.stderr.write(
620
+ ` ${r.level.padEnd(10)} ${pair.padEnd(width)} Lc ${r.lcValue.toFixed(1)}
526
621
  `
527
- );
622
+ );
623
+ }
528
624
  }
529
- }
530
- const { score, passed, total } = computeAccessibilityScore(
531
- semantic,
532
- contrastAlgorithm,
533
- contrastTarget
534
- );
535
- process.stderr.write(` score: ${score}% (${passed}/${total} pairings)
625
+ const { score, passed, total } = computeAccessibilityScore(
626
+ semantic,
627
+ contrastAlgorithm,
628
+ contrastTarget,
629
+ g
630
+ );
631
+ process.stderr.write(` score: ${score}% (${passed}/${total} pairings)
536
632
  `);
633
+ }
537
634
  }
538
635
  reportSurfaces();
539
636
  }