@noix-dev/brix-tokens 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +58 -2
- package/package.json +36 -3
- package/src/canvasColor.ts +39 -0
- package/src/contract.json +683 -0
- package/src/contrast.ts +319 -0
- package/src/density.css +565 -0
- package/src/fonts.css +10 -0
- package/src/index.css +125 -0
- package/src/perceptual.ts +99 -0
- package/src/themes/graphite.css +265 -0
- package/src/themes/kingfisher.css +397 -0
- package/src/themes/noix.css +266 -0
- package/src/themes/registry.json +5 -0
- package/src/variants.css +94 -0
package/src/contrast.ts
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The colour maths behind the contrast gate and the playground's own measuring: enough of
|
|
3
|
+
* CSS Color to turn the values a theme writes, and the values a browser answers with, into a
|
|
4
|
+
* WCAG contrast ratio, and nothing more. It is shipped source (`@noix-dev/brix-tokens/contrast`)
|
|
5
|
+
* so that the gate in `tests/` and the playground read colour with one instrument rather than
|
|
6
|
+
* two that can drift apart.
|
|
7
|
+
*
|
|
8
|
+
* It is written out here rather than taken from a library because the gate's job is to be
|
|
9
|
+
* trustworthy: a short stretch of conversion, pinned by tests against values that can be
|
|
10
|
+
* checked by hand, says more about whether the gate means anything than a dependency would.
|
|
11
|
+
* Everything it cannot measure it refuses, loudly — a gate that guesses at a value is worse
|
|
12
|
+
* than no gate, because it reports headroom a theme does not have.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** An sRGB colour: three gamma-encoded channels and an alpha, each in 0..1. */
|
|
16
|
+
export interface Srgb {
|
|
17
|
+
r: number;
|
|
18
|
+
g: number;
|
|
19
|
+
b: number;
|
|
20
|
+
alpha: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const HEX = /^#([0-9a-f]{3,8})$/i;
|
|
24
|
+
const OKLCH = /^oklch\(([^)]*)\)$/i;
|
|
25
|
+
const OKLAB = /^oklab\(([^)]*)\)$/i;
|
|
26
|
+
const RGB = /^rgba?\(([^)]*)\)$/i;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* How far outside 0..1 a converted channel may land before the colour is refused rather
|
|
30
|
+
* than clamped. A colour sRGB cannot show is refused, because the clipped colour is not the
|
|
31
|
+
* one the theme asked for and the gate would be measuring something else.
|
|
32
|
+
*
|
|
33
|
+
* Half an 8-bit step, which is what the rounding in a written value costs. The sRGB
|
|
34
|
+
* primaries sit exactly on the gamut surface, so a five-decimal oklch coordinate for one of
|
|
35
|
+
* them lands a hair outside: the three in the test file miss by at most 0.00127. Anything
|
|
36
|
+
* genuinely outside the gamut misses by far more — a vivid oklch green by 4.2 of a channel,
|
|
37
|
+
* and Tailwind's oklch red by 0.12, which this refuses and the test file pins.
|
|
38
|
+
*
|
|
39
|
+
* The check is on the encoded channel, where the transfer function's slope does the talking:
|
|
40
|
+
* 0.002 of encoded overshoot is about 0.0046 of linear light at the bright end and 0.00015 at
|
|
41
|
+
* the dark end. It is loosest exactly where a channel moves luminance fastest, which is the
|
|
42
|
+
* argument for keeping it this tight.
|
|
43
|
+
*/
|
|
44
|
+
const GAMUT_TOLERANCE = 0.002;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A colour as written in a stylesheet or answered by a browser: hex, `oklch()`, `oklab()`, and
|
|
48
|
+
* the `rgb()` / `rgba()` that `getComputedStyle` reports. A browser answers `oklab()` for every
|
|
49
|
+
* translucent wash (`bg-primary/10`), so a reading of a wash goes through that branch.
|
|
50
|
+
*/
|
|
51
|
+
export function parseColor(value: string): Srgb {
|
|
52
|
+
const text = value.trim();
|
|
53
|
+
const hex = HEX.exec(text);
|
|
54
|
+
if (hex) {
|
|
55
|
+
return fromHex(hex[1]!, text);
|
|
56
|
+
}
|
|
57
|
+
const oklch = OKLCH.exec(text);
|
|
58
|
+
if (oklch) {
|
|
59
|
+
return fromOklch(oklch[1]!, text);
|
|
60
|
+
}
|
|
61
|
+
const oklab = OKLAB.exec(text);
|
|
62
|
+
if (oklab) {
|
|
63
|
+
return fromOklab(oklab[1]!, text);
|
|
64
|
+
}
|
|
65
|
+
const rgb = RGB.exec(text);
|
|
66
|
+
if (rgb) {
|
|
67
|
+
return fromRgb(rgb[1]!, text);
|
|
68
|
+
}
|
|
69
|
+
throw new Error(
|
|
70
|
+
`cannot measure "${text}": the gate reads hex, oklch(), oklab() and rgb() colours. A role in a ` +
|
|
71
|
+
`contrast pair has to be written as a plain colour, so that what the gate ` +
|
|
72
|
+
`measures is what the screen shows.`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** `#rrggbb` for an opaque colour. Alpha is refused: it would be silently dropped. */
|
|
77
|
+
export function formatHex(color: Srgb): string {
|
|
78
|
+
if (color.alpha !== 1) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
'a translucent colour has no hex form here: composite it with wash() first, so ' +
|
|
81
|
+
'that the alpha is spent on a surface rather than thrown away',
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const channel = (value: number): string =>
|
|
85
|
+
Math.round(clampChannel(value) * 255)
|
|
86
|
+
.toString(16)
|
|
87
|
+
.padStart(2, '0');
|
|
88
|
+
return `#${channel(color.r)}${channel(color.g)}${channel(color.b)}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The WCAG 2.1 relative luminance of an opaque colour. */
|
|
92
|
+
export function relativeLuminance(value: string): number {
|
|
93
|
+
const { r, g, b } = opaque(parseColor(value), value);
|
|
94
|
+
return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The WCAG 2.1 contrast ratio of two opaque colours, between 1 and 21. */
|
|
98
|
+
export function contrastRatio(a: string, b: string): number {
|
|
99
|
+
const first = relativeLuminance(a);
|
|
100
|
+
const second = relativeLuminance(b);
|
|
101
|
+
return (Math.max(first, second) + 0.05) / (Math.min(first, second) + 0.05);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* `mix` of `on` laid over `over`, as `#rrggbb`.
|
|
106
|
+
*
|
|
107
|
+
* The share is mixed in gamma-encoded sRGB, which is where a browser composites a
|
|
108
|
+
* translucent layer over an opaque backdrop — half black over white renders as the mid grey
|
|
109
|
+
* #808080, not as the much lighter colour a linear-light mix would give. A gate that mixed
|
|
110
|
+
* in linear light would wave through washes that a screen renders darker than it measured.
|
|
111
|
+
*
|
|
112
|
+
* A translucent `on` thins the wash further, the way two stacked layers would; `over` has to
|
|
113
|
+
* be opaque, because what is behind a translucent surface is not knowable from a stylesheet.
|
|
114
|
+
*/
|
|
115
|
+
export function wash(on: string, over: string, mix: number): string {
|
|
116
|
+
if (!(mix >= 0 && mix <= 1)) {
|
|
117
|
+
throw new Error(`a wash of ${mix} is not a share between 0 and 1`);
|
|
118
|
+
}
|
|
119
|
+
const top = parseColor(on);
|
|
120
|
+
const bottom = opaque(parseColor(over), over);
|
|
121
|
+
const share = mix * top.alpha;
|
|
122
|
+
const blend = (a: number, b: number): number => a * share + b * (1 - share);
|
|
123
|
+
return formatHex({
|
|
124
|
+
r: blend(top.r, bottom.r),
|
|
125
|
+
g: blend(top.g, bottom.g),
|
|
126
|
+
b: blend(top.b, bottom.b),
|
|
127
|
+
alpha: 1,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function opaque(color: Srgb, text: string): Srgb {
|
|
132
|
+
if (color.alpha !== 1) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`"${text}" is translucent, and a translucent colour has no contrast of its own: ` +
|
|
135
|
+
`composite it over its surface with wash() first`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
return color;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function fromHex(digits: string, text: string): Srgb {
|
|
142
|
+
const size = digits.length === 3 || digits.length === 4 ? 1 : 2;
|
|
143
|
+
if (digits.length !== 3 * size && digits.length !== 4 * size) {
|
|
144
|
+
throw new Error(`cannot measure "${text}": a hex colour has 3, 4, 6 or 8 digits`);
|
|
145
|
+
}
|
|
146
|
+
const parts = digits.match(size === 1 ? /./g : /../g) ?? [];
|
|
147
|
+
const channel = (part: string): number => parseInt(size === 1 ? part + part : part, 16) / 255;
|
|
148
|
+
return {
|
|
149
|
+
r: channel(parts[0]!),
|
|
150
|
+
g: channel(parts[1]!),
|
|
151
|
+
b: channel(parts[2]!),
|
|
152
|
+
alpha: parts[3] === undefined ? 1 : channel(parts[3]),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* `rgb()` and `rgba()` in both of their spellings: legacy commas with an optional fourth
|
|
158
|
+
* value for alpha, and modern spaces with alpha after a slash. A channel is 0..255 or a
|
|
159
|
+
* percentage, alpha a number or a percentage. The colour is already sRGB, so unlike oklch()
|
|
160
|
+
* there is no gamut to refuse; a value outside the ranges is refused instead of clamped.
|
|
161
|
+
*/
|
|
162
|
+
function fromRgb(args: string, text: string): Srgb {
|
|
163
|
+
const [coords, slashAlpha] = splitTwo(args, '/', text);
|
|
164
|
+
let parts: string[];
|
|
165
|
+
let alpha = slashAlpha;
|
|
166
|
+
if (coords.includes(',')) {
|
|
167
|
+
if (slashAlpha !== undefined) {
|
|
168
|
+
throw new Error(`cannot measure "${text}": commas and "/" are two spellings of rgb()`);
|
|
169
|
+
}
|
|
170
|
+
parts = coords.split(',');
|
|
171
|
+
if (parts.length === 4) {
|
|
172
|
+
alpha = parts.pop();
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
parts = coords.trim().split(/\s+/).filter(Boolean);
|
|
176
|
+
}
|
|
177
|
+
if (parts.length !== 3) {
|
|
178
|
+
throw new Error(`cannot measure "${text}": rgb() takes three channels, red green blue`);
|
|
179
|
+
}
|
|
180
|
+
const channel = (token: string): number => {
|
|
181
|
+
const value = number(token, 255, text) / 255;
|
|
182
|
+
if (value < 0 || value > 1) {
|
|
183
|
+
throw new Error(
|
|
184
|
+
`cannot measure "${text}": the channel "${token.trim()}" is out of range`,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return value;
|
|
188
|
+
};
|
|
189
|
+
const opacity = alpha === undefined ? 1 : number(alpha, 1, text);
|
|
190
|
+
if (opacity < 0 || opacity > 1) {
|
|
191
|
+
throw new Error(`cannot measure "${text}": the alpha "${alpha!.trim()}" is out of range`);
|
|
192
|
+
}
|
|
193
|
+
return { r: channel(parts[0]!), g: channel(parts[1]!), b: channel(parts[2]!), alpha: opacity };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function fromOklch(args: string, text: string): Srgb {
|
|
197
|
+
const [coords, alpha] = splitTwo(args, '/', text);
|
|
198
|
+
const parts = coords.trim().split(/\s+/).filter(Boolean);
|
|
199
|
+
if (parts.length !== 3) {
|
|
200
|
+
throw new Error(
|
|
201
|
+
`cannot measure "${text}": oklch() takes three coordinates, lightness chroma hue`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
// Percentages are referenced to 1 for lightness and 0.4 for chroma, per CSS Color 4.
|
|
205
|
+
return toSrgb(
|
|
206
|
+
number(parts[0]!, 1, text),
|
|
207
|
+
number(parts[1]!, 0.4, text),
|
|
208
|
+
degrees(parts[2]!, text),
|
|
209
|
+
alpha === undefined ? 1 : number(alpha, 1, text),
|
|
210
|
+
text,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function fromOklab(args: string, text: string): Srgb {
|
|
215
|
+
const [coords, alpha] = splitTwo(args, '/', text);
|
|
216
|
+
const parts = coords.trim().split(/\s+/).filter(Boolean);
|
|
217
|
+
if (parts.length !== 3) {
|
|
218
|
+
throw new Error(`cannot measure "${text}": oklab() takes three coordinates, lightness a b`);
|
|
219
|
+
}
|
|
220
|
+
// Percentages are referenced to 1 for lightness and 0.4 for a and b, per CSS Color 4.
|
|
221
|
+
return oklabToSrgb(
|
|
222
|
+
number(parts[0]!, 1, text),
|
|
223
|
+
number(parts[1]!, 0.4, text),
|
|
224
|
+
number(parts[2]!, 0.4, text),
|
|
225
|
+
alpha === undefined ? 1 : number(alpha, 1, text),
|
|
226
|
+
text,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* oklch to sRGB: polar to oklab, oklab to the cone responses, those to linear sRGB, and the
|
|
232
|
+
* gamma transfer function last. The two matrices are Ottosson's, and the test file pins them
|
|
233
|
+
* by converting the sRGB primaries back from their published oklch coordinates.
|
|
234
|
+
*/
|
|
235
|
+
function toSrgb(lightness: number, chroma: number, hue: number, alpha: number, text: string): Srgb {
|
|
236
|
+
const radians = (hue * Math.PI) / 180;
|
|
237
|
+
return oklabToSrgb(
|
|
238
|
+
lightness,
|
|
239
|
+
chroma * Math.cos(radians),
|
|
240
|
+
chroma * Math.sin(radians),
|
|
241
|
+
alpha,
|
|
242
|
+
text,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** oklab to sRGB, the same path oklch takes after its polar step, with the same gamut check. */
|
|
247
|
+
function oklabToSrgb(lightness: number, a: number, b: number, alpha: number, text: string): Srgb {
|
|
248
|
+
const long = (lightness + 0.3963377774 * a + 0.2158037573 * b) ** 3;
|
|
249
|
+
const medium = (lightness - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
250
|
+
const short = (lightness - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
r: toGamma(4.0767416621 * long - 3.3077115913 * medium + 0.2309699292 * short, text),
|
|
254
|
+
g: toGamma(-1.2684380046 * long + 2.6097574011 * medium - 0.3413193965 * short, text),
|
|
255
|
+
b: toGamma(-0.0041960863 * long - 0.7034186147 * medium + 1.707614701 * short, text),
|
|
256
|
+
alpha,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** The sRGB transfer function, and the gamut check that goes with it. */
|
|
261
|
+
function toGamma(linear: number, text: string): number {
|
|
262
|
+
// The low branch takes every negative value too, so the power is only ever applied to a
|
|
263
|
+
// positive channel.
|
|
264
|
+
const encoded = linear <= 0.0031308 ? linear * 12.92 : 1.055 * linear ** (1 / 2.4) - 0.055;
|
|
265
|
+
if (encoded < -GAMUT_TOLERANCE || encoded > 1 + GAMUT_TOLERANCE) {
|
|
266
|
+
throw new Error(
|
|
267
|
+
`cannot measure "${text}": it is outside the sRGB gamut (a channel comes out at ` +
|
|
268
|
+
`${encoded.toFixed(3)}), so the colour a screen would show is not the colour ` +
|
|
269
|
+
`the value names. Write the role as a hex value, or lower the chroma until it ` +
|
|
270
|
+
`fits sRGB.`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
return clampChannel(encoded);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Its inverse, as WCAG 2.1 writes it. */
|
|
277
|
+
export function toLinear(channel: number): number {
|
|
278
|
+
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** A channel into the 0..1 an sRGB channel can hold. */
|
|
282
|
+
export function clampChannel(value: number): number {
|
|
283
|
+
return Math.min(1, Math.max(0, value));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* One number, with or without a percentage sign. The whole token has to be the number:
|
|
288
|
+
* `parseFloat` would read "0.5abc" as 0.5 and hand back a colour, which is the one way this
|
|
289
|
+
* file could accept a value it cannot actually measure.
|
|
290
|
+
*/
|
|
291
|
+
function number(token: string, reference: number, text: string): number {
|
|
292
|
+
const trimmed = token.trim();
|
|
293
|
+
const percentage = trimmed.endsWith('%');
|
|
294
|
+
const body = percentage ? trimmed.slice(0, -1) : trimmed;
|
|
295
|
+
if (!/^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i.test(body)) {
|
|
296
|
+
throw new Error(`cannot measure "${text}": "${token.trim()}" is not a number`);
|
|
297
|
+
}
|
|
298
|
+
const parsed = Number.parseFloat(body);
|
|
299
|
+
return percentage ? (parsed / 100) * reference : parsed;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function degrees(token: string, text: string): number {
|
|
303
|
+
const bare = token.toLowerCase().endsWith('deg') ? token.slice(0, -3) : token;
|
|
304
|
+
if (/[a-z%]/i.test(bare)) {
|
|
305
|
+
throw new Error(
|
|
306
|
+
`cannot measure "${text}": the hue "${token}" is not in degrees, and only ` +
|
|
307
|
+
`degrees are read here`,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
return number(bare, 1, text);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function splitTwo(text: string, separator: string, where: string): [string, string | undefined] {
|
|
314
|
+
const parts = text.split(separator);
|
|
315
|
+
if (parts.length > 2) {
|
|
316
|
+
throw new Error(`cannot measure "${where}": more than one "${separator}"`);
|
|
317
|
+
}
|
|
318
|
+
return [parts[0]!, parts[1]];
|
|
319
|
+
}
|