@designtools/tokens 0.1.7 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1338,11 +1338,56 @@ function exportTokens(tokens, format, options) {
1338
1338
  }
1339
1339
  }
1340
1340
 
1341
+ // src/seed.ts
1342
+ var GENERATOR_NAME = "@designtools/tokens";
1343
+ function stableStringify(value) {
1344
+ if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
1345
+ if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
1346
+ const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`);
1347
+ return `{${entries.join(",")}}`;
1348
+ }
1349
+ function fingerprint(input) {
1350
+ let h = 2166136261;
1351
+ for (let i = 0; i < input.length; i++) {
1352
+ h ^= input.charCodeAt(i);
1353
+ h = Math.imul(h, 16777619) >>> 0;
1354
+ }
1355
+ return h.toString(16).padStart(8, "0");
1356
+ }
1357
+ function seedFingerprint(config) {
1358
+ return fingerprint(stableStringify(config));
1359
+ }
1360
+ function provenanceComment(p) {
1361
+ return `/* Generated by ${GENERATOR_NAME} v${p.version} (seed ${p.seed}) - do not edit by hand */`;
1362
+ }
1363
+ var PROVENANCE_RE = new RegExp(
1364
+ `${GENERATOR_NAME.replace("/", "\\/")} v(\\S+) \\(seed ([0-9a-f]{8})\\)`
1365
+ );
1366
+ function parseProvenance(text) {
1367
+ const comment = PROVENANCE_RE.exec(text);
1368
+ if (comment) return { version: comment[1], seed: comment[2] };
1369
+ const json = /"\$designtools"\s*:\s*\{[^}]*"version"\s*:\s*"([^"]+)"[^}]*"seed"\s*:\s*"([0-9a-f]{8})"/.exec(text);
1370
+ if (json) return { version: json[1], seed: json[2] };
1371
+ return null;
1372
+ }
1373
+ function withJsonProvenance(json, p) {
1374
+ const parsed = JSON.parse(json);
1375
+ const stamped = {
1376
+ $designtools: { generator: GENERATOR_NAME, version: p.version, seed: p.seed },
1377
+ ...parsed
1378
+ };
1379
+ return JSON.stringify(stamped, null, 2);
1380
+ }
1381
+
1341
1382
  export {
1342
1383
  VALID_SHADES,
1384
+ DEFAULT_SEED_SHADE,
1385
+ TAILWIND_SHADES,
1343
1386
  SHADE_PRESETS,
1344
1387
  resolveShades,
1345
1388
  snapShade,
1389
+ reanchorFactors,
1390
+ chromaShadeFor,
1346
1391
  parseToOklch,
1347
1392
  oklchToHex,
1348
1393
  generateColorScale,
@@ -1377,5 +1422,12 @@ export {
1377
1422
  exportToDtcg,
1378
1423
  SECTIONED_FORMATS,
1379
1424
  selectTokenSections,
1380
- exportTokens
1425
+ exportTokens,
1426
+ GENERATOR_NAME,
1427
+ stableStringify,
1428
+ fingerprint,
1429
+ seedFingerprint,
1430
+ provenanceComment,
1431
+ parseProvenance,
1432
+ withJsonProvenance
1381
1433
  };
package/dist/cli.js CHANGED
@@ -10,60 +10,21 @@ import {
10
10
  computeAccessibilityScore,
11
11
  exportTokens,
12
12
  generateTokens,
13
+ parseProvenance,
13
14
  parseToOklch,
15
+ provenanceComment,
14
16
  resolveShades,
17
+ seedFingerprint,
15
18
  selectTokenSections,
16
- snapShade
17
- } from "./chunk-T62MMGGQ.js";
19
+ snapShade,
20
+ withJsonProvenance
21
+ } from "./chunk-757P7AQA.js";
18
22
 
19
23
  // src/cli.ts
20
24
  import { parseArgs } from "util";
21
25
  import { mkdirSync, writeFileSync, readFileSync, statSync } from "fs";
22
26
  import { join, dirname } from "path";
23
27
  import { fileURLToPath } from "url";
24
-
25
- // src/seed.ts
26
- var GENERATOR_NAME = "@designtools/tokens";
27
- function stableStringify(value) {
28
- if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
29
- if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
30
- const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`);
31
- return `{${entries.join(",")}}`;
32
- }
33
- function fingerprint(input) {
34
- let h = 2166136261;
35
- for (let i = 0; i < input.length; i++) {
36
- h ^= input.charCodeAt(i);
37
- h = Math.imul(h, 16777619) >>> 0;
38
- }
39
- return h.toString(16).padStart(8, "0");
40
- }
41
- function seedFingerprint(config) {
42
- return fingerprint(stableStringify(config));
43
- }
44
- function provenanceComment(p) {
45
- return `/* Generated by ${GENERATOR_NAME} v${p.version} (seed ${p.seed}) - do not edit by hand */`;
46
- }
47
- var PROVENANCE_RE = new RegExp(
48
- `${GENERATOR_NAME.replace("/", "\\/")} v(\\S+) \\(seed ([0-9a-f]{8})\\)`
49
- );
50
- function parseProvenance(text) {
51
- const comment = PROVENANCE_RE.exec(text);
52
- if (comment) return { version: comment[1], seed: comment[2] };
53
- const json = /"\$designtools"\s*:\s*\{[^}]*"version"\s*:\s*"([^"]+)"[^}]*"seed"\s*:\s*"([0-9a-f]{8})"/.exec(text);
54
- if (json) return { version: json[1], seed: json[2] };
55
- return null;
56
- }
57
- function withJsonProvenance(json, p) {
58
- const parsed = JSON.parse(json);
59
- const stamped = {
60
- $designtools: { generator: GENERATOR_NAME, version: p.version, seed: p.seed },
61
- ...parsed
62
- };
63
- return JSON.stringify(stamped, null, 2);
64
- }
65
-
66
- // src/cli.ts
67
28
  var HELP = `generate-tokens \u2014 brand colour in, design tokens out
68
29
 
69
30
  Usage:
package/dist/index.d.ts CHANGED
@@ -439,9 +439,114 @@ interface ExportOptions {
439
439
  */
440
440
  shades?: readonly ShadeNumber[];
441
441
  }
442
+ /** Formats whose structure lets a section filter be applied meaningfully */
443
+ declare const SECTIONED_FORMATS: ExportFormat[];
444
+ /**
445
+ * Narrow a TokenSystem to the requested sections, for the `json` output.
446
+ *
447
+ * Colour only: the spacing, typography, radii and shadow tiers are carried
448
+ * through untouched when the full system is requested, and dropped otherwise.
449
+ */
450
+ declare function selectTokenSections(tokens: TokenSystem, sections?: TokenSection[]): Partial<TokenSystem>;
442
451
  /**
443
452
  * Export tokens in the specified format
444
453
  */
445
454
  declare function exportTokens(tokens: TokenSystem, format: ExportFormat, options?: ExportOptions): string;
446
455
 
447
- export { type APCAContrastResult, type BrandColors, type CVDType, type ColorFormat, type ColorScale, type ContrastAlgorithm, type ContrastTarget, DEFAULT_NEUTRAL_COLOR, type ExportFormat, type ExportOptions, type ExtendedSemanticColor, type ForegroundRef, type GenerationOptions, type LightnessStrategy, type PrimitivePalette, type SavedConfig, type SemanticColorPair, type SemanticShadeOverrides, type SemanticTokens, type ShadeNumber, type SurfaceRef, type SurfaceShadeOverrides, type SurfaceTokens, type TokenSystem, type UtilityShadeOverrides, type UtilityTokens, VALID_SHADES, type VariantShadeOverrides, type WCAGContrastResult, adjustChroma, adjustLightness, checkSemanticContrast, checkSemanticContrastAPCA, classifyAPCALevel, computeAccessibilityScore, exportToCss, exportToDtcg, exportToTailwindV3, exportToTailwindV4, exportTokens, findContrastingColorAPCA, formatColorAs, formatColorShort, generateColorScale, generateColorScaleAPCA, generatePrimitivePalette, generateSemanticTokens, generateSurfaceTokens, generateTokens, generateUtilityTokens, getAPCAContrast, getContrastRatio, getOklchString, getRelativeLuminance, getShade, meetsWCAG_AA, meetsWCAG_AAA, oklchToHex, parseToOklch, simulateCVD };
456
+ /**
457
+ * Shade-set utilities: which steps a ramp emits, and where the seed sits on it.
458
+ *
459
+ * Two independent knobs:
460
+ * - `seedShade` — which step the input colour lands on. It was pinned to 500,
461
+ * which wrecks the ramp when a brand colour arrives as a deep navy or a pale
462
+ * tint, since everything else is derived from headroom either side of it.
463
+ * - `shades` — which steps get emitted at all. Ours is 25–950; Tailwind's stock
464
+ * ramps are 50–950, and matching that makes the palette override cleaner.
465
+ */
466
+
467
+ /** Where the seed sits when nobody says otherwise */
468
+ declare const DEFAULT_SEED_SHADE: ShadeNumber;
469
+ /** Tailwind's stock ramp: 50–950, no 25 */
470
+ declare const TAILWIND_SHADES: ShadeNumber[];
471
+ /** Named shade-set presets accepted by --shades */
472
+ declare const SHADE_PRESETS: Record<string, ShadeNumber[]>;
473
+ /** Normalise a requested shade set: valid, de-duplicated, light-to-dark */
474
+ declare function resolveShades(requested?: readonly ShadeNumber[]): ShadeNumber[];
475
+ /**
476
+ * Nearest emitted shade to the one a token asked for.
477
+ *
478
+ * Semantic, surface and utility tokens name specific steps (muted sits on 25,
479
+ * borders on neutral-200). When --shades drops one of those, the reference has
480
+ * to move somewhere real or the output ships a var() pointing at nothing.
481
+ * Distance is measured in ramp positions, not shade numbers, so the steps count
482
+ * evenly. Ties go to the darker step, which is the safer direction for the
483
+ * foregrounds and borders this mostly affects.
484
+ */
485
+ declare function snapShade(shade: ShadeNumber, active: readonly ShadeNumber[]): ShadeNumber;
486
+ /**
487
+ * Re-anchor a lightness-factor table so the seed lands on `seedShade`.
488
+ *
489
+ * The table is signed: positive entries are a fraction of the headroom above the
490
+ * seed, negative a fraction of the headroom below, and the anchor is 0. Moving
491
+ * the anchor changes how many steps sit either side of it, so each side's curve
492
+ * is resampled onto its new slot count. That keeps the ramp's perceptual shape —
493
+ * gentle at the light end, larger jumps at the dark end for text contrast —
494
+ * rather than stretching one uniform gradient.
495
+ *
496
+ * A side left with a single slot reaches that curve's extreme, since that one
497
+ * step stands in for the whole remaining range.
498
+ */
499
+ declare function reanchorFactors(base: readonly number[], seedShade: ShadeNumber): number[];
500
+ /**
501
+ * Shade whose chroma envelope a given ramp position should borrow.
502
+ *
503
+ * Chroma peaks around the middle of the ramp. With the anchor moved, using each
504
+ * step's own shade number would put peak chroma at 500 while the seed's exact
505
+ * colour sits elsewhere, leaving a visible discontinuity at the anchor. Shifting
506
+ * the envelope by the same amount as the anchor keeps the seed at the peak.
507
+ */
508
+ declare function chromaShadeFor(index: number, seedShade: ShadeNumber): ShadeNumber;
509
+
510
+ /**
511
+ * Seed capture and provenance.
512
+ *
513
+ * A seed file is a `SavedConfig` — the same shape the web tool persists — so the
514
+ * two can hand work to each other without a second format. The provenance header
515
+ * records the generator version and a fingerprint of the resolved seed, which is
516
+ * what lets `--check` tell "you upgraded the generator" apart from "someone
517
+ * hand-edited the output".
518
+ *
519
+ * No imports on purpose: this stays usable in a browser alongside the rest of
520
+ * the library, and the dependency list stays culori + apca-w3.
521
+ */
522
+
523
+ declare const GENERATOR_NAME = "@designtools/tokens";
524
+ /** A seed file is a partial SavedConfig — every field has a CLI default. */
525
+ type SeedFile = Partial<SavedConfig>;
526
+ /**
527
+ * Deterministic JSON with object keys sorted, so two configs that differ only
528
+ * in key order fingerprint identically.
529
+ */
530
+ declare function stableStringify(value: unknown): string;
531
+ /**
532
+ * FNV-1a, 32-bit, as eight hex digits.
533
+ *
534
+ * A change-detection fingerprint, not a security hash — it only has to make an
535
+ * accidental edit to the seed visible, and it keeps this module dependency-free
536
+ * and identical across platforms.
537
+ */
538
+ declare function fingerprint(input: string): string;
539
+ /** Fingerprint of a resolved seed configuration */
540
+ declare function seedFingerprint(config: SeedFile): string;
541
+ interface Provenance {
542
+ version: string;
543
+ seed: string;
544
+ }
545
+ /** The one-line header stamped onto generated CSS and JS output */
546
+ declare function provenanceComment(p: Provenance): string;
547
+ /** Read provenance back out of previously generated output, in any format */
548
+ declare function parseProvenance(text: string): Provenance | null;
549
+ /** Attach provenance to a JSON document as a `$designtools` key */
550
+ declare function withJsonProvenance(json: string, p: Provenance): string;
551
+
552
+ export { type APCAContrastResult, type BrandColors, type CVDType, type ColorFormat, type ColorScale, type ContrastAlgorithm, type ContrastTarget, DEFAULT_NEUTRAL_COLOR, DEFAULT_SEED_SHADE, type ExportFormat, type ExportOptions, type ExtendedSemanticColor, type ForegroundRef, GENERATOR_NAME, type GenerationOptions, type LightnessStrategy, type PrimitivePalette, type Provenance, SECTIONED_FORMATS, SHADE_PRESETS, type SavedConfig, type ScaleOptions, type SeedFile, type SemanticColorPair, type SemanticShadeOverrides, type SemanticTokens, type ShadeNumber, type SurfaceRef, type SurfaceShadeOverrides, type SurfaceTokens, TAILWIND_SHADES, TOKEN_SECTIONS, type TokenSection, type TokenSystem, type UtilityShadeOverrides, type UtilityTokens, VALID_SHADES, type VariantShadeOverrides, type WCAGContrastResult, adjustChroma, adjustLightness, checkSemanticContrast, checkSemanticContrastAPCA, chromaShadeFor, classifyAPCALevel, computeAccessibilityScore, exportToCss, exportToDtcg, exportToTailwindV3, exportToTailwindV4, exportTokens, findContrastingColorAPCA, fingerprint, formatColorAs, formatColorShort, generateColorScale, generateColorScaleAPCA, generatePrimitivePalette, generateSemanticTokens, generateSurfaceTokens, generateTokens, generateUtilityTokens, getAPCAContrast, getContrastRatio, getOklchString, getRelativeLuminance, getShade, meetsWCAG_AA, meetsWCAG_AAA, oklchToHex, parseProvenance, parseToOklch, provenanceComment, reanchorFactors, resolveShades, seedFingerprint, selectTokenSections, simulateCVD, snapShade, stableStringify, withJsonProvenance };
package/dist/index.js CHANGED
@@ -1,10 +1,17 @@
1
1
  import {
2
2
  DEFAULT_NEUTRAL_COLOR,
3
+ DEFAULT_SEED_SHADE,
4
+ GENERATOR_NAME,
5
+ SECTIONED_FORMATS,
6
+ SHADE_PRESETS,
7
+ TAILWIND_SHADES,
8
+ TOKEN_SECTIONS,
3
9
  VALID_SHADES,
4
10
  adjustChroma,
5
11
  adjustLightness,
6
12
  checkSemanticContrast,
7
13
  checkSemanticContrastAPCA,
14
+ chromaShadeFor,
8
15
  classifyAPCALevel,
9
16
  computeAccessibilityScore,
10
17
  exportToCss,
@@ -13,6 +20,7 @@ import {
13
20
  exportToTailwindV4,
14
21
  exportTokens,
15
22
  findContrastingColorAPCA,
23
+ fingerprint,
16
24
  formatColorAs,
17
25
  formatColorShort,
18
26
  generateColorScale,
@@ -30,16 +38,32 @@ import {
30
38
  meetsWCAG_AA,
31
39
  meetsWCAG_AAA,
32
40
  oklchToHex,
41
+ parseProvenance,
33
42
  parseToOklch,
34
- simulateCVD
35
- } from "./chunk-T62MMGGQ.js";
43
+ provenanceComment,
44
+ reanchorFactors,
45
+ resolveShades,
46
+ seedFingerprint,
47
+ selectTokenSections,
48
+ simulateCVD,
49
+ snapShade,
50
+ stableStringify,
51
+ withJsonProvenance
52
+ } from "./chunk-757P7AQA.js";
36
53
  export {
37
54
  DEFAULT_NEUTRAL_COLOR,
55
+ DEFAULT_SEED_SHADE,
56
+ GENERATOR_NAME,
57
+ SECTIONED_FORMATS,
58
+ SHADE_PRESETS,
59
+ TAILWIND_SHADES,
60
+ TOKEN_SECTIONS,
38
61
  VALID_SHADES,
39
62
  adjustChroma,
40
63
  adjustLightness,
41
64
  checkSemanticContrast,
42
65
  checkSemanticContrastAPCA,
66
+ chromaShadeFor,
43
67
  classifyAPCALevel,
44
68
  computeAccessibilityScore,
45
69
  exportToCss,
@@ -48,6 +72,7 @@ export {
48
72
  exportToTailwindV4,
49
73
  exportTokens,
50
74
  findContrastingColorAPCA,
75
+ fingerprint,
51
76
  formatColorAs,
52
77
  formatColorShort,
53
78
  generateColorScale,
@@ -65,6 +90,15 @@ export {
65
90
  meetsWCAG_AA,
66
91
  meetsWCAG_AAA,
67
92
  oklchToHex,
93
+ parseProvenance,
68
94
  parseToOklch,
69
- simulateCVD
95
+ provenanceComment,
96
+ reanchorFactors,
97
+ resolveShades,
98
+ seedFingerprint,
99
+ selectTokenSections,
100
+ simulateCVD,
101
+ snapShade,
102
+ stableStringify,
103
+ withJsonProvenance
70
104
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@designtools/tokens",
3
- "version": "0.1.7",
3
+ "version": "0.2.1",
4
4
  "description": "Headless colour token generation from brand colours: OKLCH primitive scales, contrast-checked semantic intents, light/dark modes, and serialisers for CSS custom properties (lead), Tailwind v4 (@theme static) and v3, and DTCG. Pure TypeScript, deterministic.",
5
5
  "type": "module",
6
6
  "license": "CC-BY-NC-4.0",