@weasel-js/theme 1.5.0 → 1.5.2

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.
@@ -0,0 +1,293 @@
1
+ import { OklchDeg, oklchDegToHex } from '@weasel-js/paint';
2
+ import { f as ThemeDefinition, L as Lookup, F as FlatTokens, R as RawToken, S as Selection, B as BakedTheme, T as Theme } from './theme-DAZcQi_L.js';
3
+ export { C as CategoricalRampDef, l as ContrastRule, m as LightnessRampDef, n as LiteralRule, N as NumberParam, O as OffsetRule, P as PinObject, e as PinValue, o as RampDef, p as RefRule, q as ScaleDef, r as SemanticRule, t as StepRule, u as bake, v as bakeChain, x as mergeChain } from './theme-DAZcQi_L.js';
4
+
5
+ /** A color in OKLCH: lightness 0–1, chroma, hue in degrees. paint's own type,
6
+ * under the name this engine has always called it. */
7
+ type Lch = OklchDeg;
8
+ declare function hexToRgb(hex: string): [number, number, number];
9
+ declare const toHex: typeof oklchDegToHex;
10
+ declare const toLch: (hex: string) => Lch;
11
+ /** The most chroma this hue can carry at this lightness, inside sRGB. */
12
+ declare function chromaCap(L: number, H: number): number;
13
+ /** WCAG relative luminance. */
14
+ declare function luminance(hex: string): number;
15
+ declare function contrast(a: string, b: string): number;
16
+ /** Shortest angular distance between two hues, in degrees. */
17
+ declare function hueGap(a: number, b: number): number;
18
+ /** OKLab coordinates, for distance work. */
19
+ declare function toLab(hex: string): [number, number, number];
20
+ /**
21
+ * How far apart two colors look, with chroma weighted up.
22
+ *
23
+ * Two things this is not. It is not WCAG contrast, which reads lightness only
24
+ * and so scores a vivid yellow on white at 1.2:1 and calls it illegible when
25
+ * what separates them is chroma. And it is not plain euclidean OKLab distance:
26
+ * OKLab's chroma range is small beside its lightness range, so an unweighted
27
+ * distance is almost all lightness, and it ranks a mid-gray as further from
28
+ * white than a pale blue is. Measured against CIELAB dE on the same colors,
29
+ * unweighted OKLab put `#c1c1c1` at 0.160 and `#abd9ff` at 0.126 while CIELAB
30
+ * put them at 21.9 and 28.5 — opposite orders.
31
+ *
32
+ * `CHROMA_WEIGHT` restores CIELAB's ordering. It is a weight, not a unit
33
+ * conversion, so these numbers do not transfer to or from CIELAB dE.
34
+ *
35
+ * Rough calibration in the weighted space: 0.17 is a pale wash against paper,
36
+ * 0.25 is a real color, 0.5 is unmistakable.
37
+ */
38
+ declare const CHROMA_WEIGHT = 3;
39
+ declare function deltaE(a: string, b: string): number;
40
+ /** The most chroma this hue can carry at this lightness, as a hex. */
41
+ declare function vividAt(L: number, H: number): string;
42
+
43
+ /**
44
+ * A hue pinned into the palette with its own lightness.
45
+ *
46
+ * Anchors exist because the lightness law excludes some colors entirely. Yellow
47
+ * is the case that forces it: its chroma peaks at L 0.95 — higher than any other
48
+ * hue — so a law that picks lightness for legibility silently yields gold, then
49
+ * olive, and never reports that yellow was dropped.
50
+ */
51
+ interface Anchor {
52
+ readonly name: string;
53
+ readonly hue: number;
54
+ readonly lightness: number;
55
+ /**
56
+ * Absolute chroma. Omitted means "as much as the gamut allows here", which is
57
+ * what a bright color wants. A muted color has to say so: hue and lightness
58
+ * alone can only describe vivid colors, so a tan asked for at max chroma
59
+ * comes back as an amber.
60
+ */
61
+ readonly chroma?: number;
62
+ }
63
+ /** Every constraint the generator honors. A zero disables the ones that gate. */
64
+ interface Constraints {
65
+ readonly count: number;
66
+ /**
67
+ * Minimum hue separation, as a share of the even `360 / count` spacing an
68
+ * ideal set would have. 0 disables the check.
69
+ *
70
+ * A share rather than degrees, because absolute degrees mean different things
71
+ * at different cardinalities: 30 degrees is generous for 8 colors and
72
+ * impossible for 16, so a floor in degrees silently becomes unsatisfiable as
73
+ * the set grows.
74
+ */
75
+ readonly hueFloor: number;
76
+ /** Minimum WCAG contrast every swatch must clear against `surface`. 0 disables. */
77
+ readonly minContrast: number;
78
+ /**
79
+ * Minimum perceptual distance from `surface`. 0 disables.
80
+ *
81
+ * The gate WCAG contrast cannot express. Contrast reads only lightness, so it
82
+ * scores a vivid yellow on white at 1.2:1 and calls it illegible, when what
83
+ * separates them is chroma. Gate on this instead of on lightness and a yellow
84
+ * can be a yellow; gate on contrast alone and the search quietly returns gold.
85
+ */
86
+ readonly minSurfaceDistance: number;
87
+ /** Minimum perceptual distance between any two swatches. 0 disables. */
88
+ readonly minDistance: number;
89
+ readonly surface: string;
90
+ /** Where `lightnessPull` drags each hue, away from its own chroma peak. */
91
+ readonly lightnessTarget: number;
92
+ /** 0 leaves every hue at its chroma peak; 1 puts them all at the target. */
93
+ readonly lightnessPull: number;
94
+ /** Fraction of the in-gamut chroma ceiling to take. */
95
+ readonly chromaFraction: number;
96
+ /** 0 leaves chroma alone; 1 pulls every hue to the set mean. */
97
+ readonly equalize: number;
98
+ readonly anchors: readonly Anchor[];
99
+ readonly order: 'hue' | 'farthest';
100
+ }
101
+ interface Swatch {
102
+ readonly name: string;
103
+ readonly hex: string;
104
+ readonly lch: Lch;
105
+ readonly anchored: boolean;
106
+ readonly contrast: number;
107
+ }
108
+ interface Stats {
109
+ readonly meanChroma: number;
110
+ readonly chromaSpread: number;
111
+ readonly lightnessSpread: number;
112
+ readonly minHueGap: number;
113
+ /** The realized floor as a share of even spacing, comparable across sizes. */
114
+ readonly hueFloorShare: number;
115
+ readonly minContrast: number;
116
+ readonly minSurfaceDistance: number;
117
+ readonly minDistance: number;
118
+ }
119
+ interface Palette {
120
+ readonly swatches: readonly Swatch[];
121
+ readonly stats: Stats;
122
+ /** False when no hue arrangement satisfied the gates; `swatches` is then the
123
+ * best unconstrained attempt, so the lab still has something to draw. */
124
+ readonly feasible: boolean;
125
+ }
126
+ declare function hueName(H: number): string;
127
+ /**
128
+ * Search hue positions that maximize mean chroma subject to the gates.
129
+ *
130
+ * Seeds matter: a uniform 360/n ring can never open a `2 * minHueGap` hole
131
+ * around a pinned hue, so with anchors the seeds spread across the arc the
132
+ * anchors leave free instead.
133
+ */
134
+ /** The share-of-even floor, in degrees, for a set of this size. */
135
+ declare function floorDegrees(c: Pick<Constraints, 'hueFloor' | 'count'>): number;
136
+ declare function generate(c: Constraints): Palette;
137
+ /** What the lab opens with: the set this palette work landed on. */
138
+ declare const DEFAULT_CONSTRAINTS: Constraints;
139
+ /**
140
+ * An anchor taken from an existing color — a brand hex, or a color lifted from
141
+ * somewhere else in the theme. Lightness comes along with the hue, which is the
142
+ * point: the reason to pin a color is usually that the lightness law would not
143
+ * have chosen its lightness.
144
+ */
145
+ /** The color an anchor will actually contribute, for a swatch beside its controls. */
146
+ declare function toHexPreview(a: Pick<Anchor, 'hue' | 'lightness' | 'chroma'>): string;
147
+ declare function anchorFromHex(hex: string, name?: string): Anchor;
148
+
149
+ interface AxisDependency {
150
+ /** Axes this token's own entry varies on. */
151
+ readonly own: readonly string[];
152
+ /** `own` plus everything reachable through references and rule inputs. */
153
+ readonly all: readonly string[];
154
+ }
155
+ declare function axisDependencies(definition: ThemeDefinition, lookup?: Lookup): Record<string, AxisDependency>;
156
+
157
+ type Layer = 'ramps' | 'scales' | 'semantics' | 'components' | 'pins';
158
+ interface Provenance {
159
+ readonly layer: Layer;
160
+ /** `lightness`, `categorical`, `linear`, `geometric`, `step`, `offset`, `contrast`, `ref`, `value`. */
161
+ readonly rule: string;
162
+ /** A pin replaced what the rule produced. Only these count as overridden. */
163
+ readonly pinned: boolean;
164
+ /** What the rule alone produced, when `pinned`. */
165
+ readonly generated?: RawToken;
166
+ }
167
+ type Issue = {
168
+ readonly kind: 'missing-axis-value';
169
+ readonly path: string;
170
+ readonly axis: string;
171
+ readonly value: string;
172
+ } | {
173
+ readonly kind: 'untyped-pin';
174
+ readonly token: string;
175
+ } | {
176
+ readonly kind: 'infeasible-ramp';
177
+ readonly ramp: string;
178
+ } | {
179
+ readonly kind: 'contrast-unmet';
180
+ readonly token: string;
181
+ readonly min: number;
182
+ readonly against: readonly string[];
183
+ /** The step used anyway: the one with the best worst-case ratio across the whole ramp. */
184
+ readonly picked: string;
185
+ readonly ratio: number;
186
+ } | {
187
+ readonly kind: 'check-failed';
188
+ readonly token: string;
189
+ readonly against: string;
190
+ readonly min: number;
191
+ readonly ratio: number;
192
+ } | {
193
+ readonly kind: 'invalid';
194
+ readonly path: string;
195
+ readonly message: string;
196
+ };
197
+ interface DeriveResult {
198
+ /** In layer order, then definition order within a layer. */
199
+ readonly tokens: FlatTokens;
200
+ readonly provenance: Readonly<Record<string, Provenance>>;
201
+ readonly issues: readonly Issue[];
202
+ }
203
+
204
+ /** Derive every token of `definition` for one selection. Unmet rules are reported in `issues`; cycles and dangling references throw. */
205
+ declare function derive(definition: ThemeDefinition, selection?: Selection, lookup?: Lookup): DeriveResult;
206
+
207
+ interface LightnessParams {
208
+ readonly steps: readonly string[];
209
+ readonly lightness: readonly [number, number];
210
+ readonly curve: number;
211
+ readonly hue: number;
212
+ readonly peak: number;
213
+ /** Lifts the first step's chroma off zero, as `darkBias` lifts the last. Default 0. */
214
+ readonly lightBias?: number;
215
+ readonly darkBias: number;
216
+ readonly anchor?: Readonly<Record<string, string>>;
217
+ }
218
+ /**
219
+ * Step name → hex. Lightness walks from `lightness[0]` to `lightness[1]`, `curve` blending an even walk toward a
220
+ * smoothstep; chroma is `peak` scaled by the envelope `sin(πt) + lightBias·(1−t) + darkBias·t` normalized to a maximum of 1.
221
+ */
222
+ declare function lightnessRamp(p: LightnessParams): Record<string, string>;
223
+ declare function categoricalRamp(steps: readonly string[], gates: Partial<Constraints>, anchors: readonly Anchor[]): {
224
+ colors: Record<string, string>;
225
+ feasible: boolean;
226
+ };
227
+
228
+ interface ScaleParams {
229
+ readonly base: number;
230
+ readonly step?: number;
231
+ readonly ratio?: number;
232
+ readonly factors?: readonly number[];
233
+ }
234
+ declare function scale(steps: readonly string[], p: ScaleParams): Record<string, string>;
235
+
236
+ interface EmitInput {
237
+ readonly baked: BakedTheme;
238
+ readonly deps: Readonly<Record<string, AxisDependency>>;
239
+ /** The default theme alone gets `:root` and the unscoped selectors. */
240
+ readonly isDefault: boolean;
241
+ }
242
+ /** A reference becomes `var()` (or `color-mix()` with an alpha); a literal is resolved. */
243
+ declare function cssValue(name: string, token: RawToken): string;
244
+ declare function emitCss(themes: readonly EmitInput[]): string;
245
+
246
+ type Group = Record<string, unknown> & {
247
+ $type: string;
248
+ };
249
+ interface DtcgExport {
250
+ readonly name: string;
251
+ readonly defaultMode?: string;
252
+ readonly primitives: Record<string, Group>;
253
+ readonly modes: Record<string, Record<string, Group>>;
254
+ }
255
+ /**
256
+ * A theme's own tokens as a DTCG document `loadDTCG` reads back. `extends` is
257
+ * not carried; pass it to `loadDTCG`. Every mode the chain declares is written,
258
+ * so a token that leaves one out still falls through to the parent on the way back.
259
+ *
260
+ * DTCG has one variant dimension and no standard way to name a second, so mode
261
+ * is the only axis exported: a token varying by any other axis is written at
262
+ * that axis's default value and its other branches are dropped. Round-tripping
263
+ * a theme through DTCG therefore flattens it to the default selection of every
264
+ * non-mode axis.
265
+ */
266
+ declare function toDTCG(theme: Theme): DtcgExport;
267
+
268
+ declare function emitManifest({ baked }: EmitInput): string;
269
+
270
+ interface ThemesInput {
271
+ readonly definition: ThemeDefinition;
272
+ readonly baked: BakedTheme;
273
+ }
274
+ declare function emitThemes(themes: readonly ThemesInput[]): string;
275
+
276
+ /** Every step name a ramp or scale entry declares, across every `by` branch of the entry and of its `steps`, first seen first. */
277
+ declare function declaredSteps(entry: unknown): string[];
278
+
279
+ type GeneratedTokens = {
280
+ readonly ok: true;
281
+ readonly files: {
282
+ readonly 'tokens.css': string;
283
+ readonly 'themes.ts': string;
284
+ readonly 'manifest.ts': string;
285
+ };
286
+ } | {
287
+ readonly ok: false;
288
+ readonly problems: readonly string[];
289
+ };
290
+ /** Every generated token file for a set of definitions, the one that extends nothing being the default. Any derive issue refuses the whole set. */
291
+ declare function generateTokens(definitions: readonly ThemeDefinition[]): GeneratedTokens;
292
+
293
+ export { type Anchor, type AxisDependency, BakedTheme, CHROMA_WEIGHT, type Constraints, DEFAULT_CONSTRAINTS, type DeriveResult, type DtcgExport, type EmitInput, type GeneratedTokens, type Issue, type Layer, type Lch, type LightnessParams, Lookup, type Palette, type Provenance, type ScaleParams, type Stats, type Swatch, ThemeDefinition, type ThemesInput, anchorFromHex, axisDependencies, categoricalRamp, chromaCap, contrast, cssValue, declaredSteps, deltaE, derive, emitCss, emitManifest, emitThemes, floorDegrees, generate, generateTokens, hexToRgb, hueGap, hueName, lightnessRamp, luminance, scale, toDTCG, toHex, toHexPreview, toLab, toLch, vividAt };