synthesisui 0.16.36 → 0.16.37

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.
@@ -1,6 +1,7 @@
1
1
  import { mkdir, readFile, writeFile } from "node:fs/promises";
2
2
  import { join, relative } from "node:path";
3
3
  import { readToken, resolveRegistry } from "../config.js";
4
+ import { isNearDuplicate } from "../doctor/color-distance.js";
4
5
  import { diagnose, scanSource } from "../doctor/scan.js";
5
6
  import { buildTable } from "../doctor/tokens.js";
6
7
  import { body, paint, section } from "../output.js";
@@ -24,6 +25,9 @@ import { walk, walkAll } from "./doctor.js";
24
25
  * worst case is ~380 entries, about 30KB of json - cheap for the one payload
25
26
  * that describes somebody's entire vocabulary.
26
27
  */
28
+ /** How many rare colours may ride along on similarity. Bounded so a
29
+ * pathological palette cannot turn the payload into a log. */
30
+ const TWIN_BUDGET = 250;
27
31
  const MAX_PER_KIND = {
28
32
  color: 250,
29
33
  radius: 60,
@@ -135,18 +139,47 @@ function distinctValues(d) {
135
139
  });
136
140
  }
137
141
  // Per-kind budgets, each family ranked inside its own: colour never starves
138
- // radius, and the low-frequency tail of colour (where near-duplicates live)
139
- // survives a cut that a global cap would have made first.
142
+ // radius, and the low-frequency tail of colour survives a cut a global cap
143
+ // would have made first.
140
144
  const kept = [];
141
145
  const dropped = new Map();
146
+ let ridealong = 0;
142
147
  for (const kind of Object.keys(MAX_PER_KIND)) {
143
148
  const family = [...by.values()]
144
149
  .filter((v) => v.kind === kind)
145
150
  .sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
146
151
  const budget = MAX_PER_KIND[kind];
147
- if (family.length > budget)
148
- dropped.set(kind, family.length - budget);
149
- for (const v of family.slice(0, budget)) {
152
+ const take = family.slice(0, budget);
153
+ let rest = family.slice(budget);
154
+ /**
155
+ * THE TWIN RIDES ALONG (measured 30/07 on a 2876-file project: 899 distinct
156
+ * colours, 250 carried, 649 left behind).
157
+ *
158
+ * Frequency is the wrong sole criterion for colour. A near-duplicate is BY
159
+ * DEFINITION rare - the twin of a brand blue used three times - so ranking
160
+ * by count discards precisely the evidence the normalisation pass exists to
161
+ * find. Raising the budget does not fix it either; it just moves the wall.
162
+ *
163
+ * So after the budget is spent, any colour sitting within the
164
+ * just-noticeable difference of one we already kept comes too. It costs a
165
+ * little payload and it is the whole point of the payload.
166
+ */
167
+ if (kind === "color") {
168
+ const anchors = take.map((v) => v.value);
169
+ const near = [];
170
+ const far = [];
171
+ for (const v of rest) {
172
+ (anchors.some((a) => isNearDuplicate(a, v.value)) ? near : far).push(v);
173
+ }
174
+ // Bounded: a pathological palette could put thousands here, and a payload
175
+ // that large stops being a vocabulary.
176
+ take.push(...near.slice(0, TWIN_BUDGET));
177
+ ridealong += Math.min(near.length, TWIN_BUDGET);
178
+ rest = [...far, ...near.slice(TWIN_BUDGET)];
179
+ }
180
+ if (rest.length > 0)
181
+ dropped.set(kind, rest.length);
182
+ for (const v of take) {
150
183
  kept.push({
151
184
  kind: v.kind,
152
185
  value: v.value,
@@ -156,10 +189,15 @@ function distinctValues(d) {
156
189
  });
157
190
  }
158
191
  }
192
+ if (ridealong > 0) {
193
+ console.log(body(paint.dim(`(+${ridealong} rare colour${ridealong === 1 ? "" : "s"} carried anyway: each sits within the just-noticeable difference of one above, which is what normalizing is about)`)));
194
+ }
159
195
  // NO SILENT CAPS: a payload that quietly stops at a budget reads as "this is
160
196
  // everything you have", which is the one thing a census must never imply.
161
197
  for (const [kind, n] of dropped) {
162
- console.log(body(paint.faint(`(${n} more ${kind} value${n === 1 ? "" : "s"} exist below the ${MAX_PER_KIND[kind]} we carry - each used less often than the ones above)`)));
198
+ console.log(body(paint.faint(kind === "color"
199
+ ? `(${n} more colour${n === 1 ? "" : "s"} left behind: rarer than the ${MAX_PER_KIND[kind]} above AND not close enough to any of them to matter)`
200
+ : `(${n} more ${kind} value${n === 1 ? "" : "s"} exist below the ${MAX_PER_KIND[kind]} we carry - each used less often than the ones above)`)));
163
201
  }
164
202
  return kept.sort((a, b) => b.count - a.count || a.value.localeCompare(b.value));
165
203
  }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * PERCEPTUAL COLOUR DISTANCE, in the CLI.
3
+ *
4
+ * ΔE76 in CIE Lab: the cheapest metric that matches what an eye calls "the same
5
+ * colour". Landmarks - under 1 nobody sees it, 2.3 is the just-noticeable
6
+ * difference, past 5 a designer defends the difference.
7
+ *
8
+ * DELIBERATE DUPLICATION. The platform has this same maths in
9
+ * `apps/web/src/lib/engine/near-colors.ts`, and the CLI is a standalone npm
10
+ * package that cannot import from the app. The two are kept honest by specs on
11
+ * both sides asserting the SAME landmark numbers, so a change to one that drifts
12
+ * from the other turns red rather than quietly producing two different opinions
13
+ * about whether two greys are the same grey.
14
+ *
15
+ * Why the CLI needs it at all: the census picks which values travel, and
16
+ * frequency alone picks wrong. A near-duplicate is by definition rare - the twin
17
+ * of a brand colour used three times - so a frequency-ranked cut throws away
18
+ * exactly the evidence the normalisation pass is looking for. With this, a rare
19
+ * value rides along when it sits close to one we already kept.
20
+ */
21
+ const HEX6 = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
22
+ /** The just-noticeable difference for ΔE76. Same constant as the platform's. */
23
+ export const JND = 2.3;
24
+ function toRgb(hex) {
25
+ const h = hex.trim().replace("#", "");
26
+ if (!HEX6.test(`#${h}`))
27
+ return null;
28
+ const full = h.length === 3
29
+ ? h
30
+ .split("")
31
+ .map((c) => c + c)
32
+ .join("")
33
+ : h;
34
+ const n = Number.parseInt(full, 16);
35
+ if (Number.isNaN(n))
36
+ return null;
37
+ return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
38
+ }
39
+ /** sRGB → CIE Lab (D65). The gamma expansion and the white point matter: skip
40
+ * them and dark values read as far closer together than they look. */
41
+ export function toLab(hex) {
42
+ const rgb = toRgb(hex);
43
+ if (!rgb)
44
+ return null;
45
+ const lin = rgb.map((c) => {
46
+ const s = c / 255;
47
+ return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
48
+ });
49
+ const x = (lin[0] * 0.4124 + lin[1] * 0.3576 + lin[2] * 0.1805) / 0.95047;
50
+ const y = lin[0] * 0.2126 + lin[1] * 0.7152 + lin[2] * 0.0722;
51
+ const z = (lin[0] * 0.0193 + lin[1] * 0.1192 + lin[2] * 0.9505) / 1.08883;
52
+ const f = (t) => (t > 0.008856 ? Math.cbrt(t) : 7.787 * t + 16 / 116);
53
+ const fx = f(x);
54
+ const fy = f(y);
55
+ const fz = f(z);
56
+ return [116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz)];
57
+ }
58
+ /** ΔE76 between two colours, or null when either cannot be read. */
59
+ export function deltaE(a, b) {
60
+ const la = toLab(a);
61
+ const lb = toLab(b);
62
+ if (!la || !lb)
63
+ return null;
64
+ return Math.sqrt((la[0] - lb[0]) ** 2 + (la[1] - lb[1]) ** 2 + (la[2] - lb[2]) ** 2);
65
+ }
66
+ /** True when two colours are the same decision typed twice. */
67
+ export function isNearDuplicate(a, b, threshold = JND) {
68
+ const d = deltaE(a, b);
69
+ return d != null && d <= threshold;
70
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "synthesisui",
3
- "version": "0.16.36",
3
+ "version": "0.16.37",
4
4
  "description": "Bring SynthesisUI design systems into any project - tokens, typed components, whole pages and an agent-ready CLAUDE.md manifest.",
5
5
  "type": "module",
6
6
  "bin": {