@polyfen/polykit-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 +172 -2
- package/bin/polykit-brand.mjs +83 -0
- package/contrast.pairs.mjs +417 -0
- package/dist/_tokens.scss +492 -0
- package/dist/brand-contract.json +71 -0
- package/dist/brands/coral.css +14 -0
- package/dist/brands/coral.json +109 -0
- package/dist/brands/indigo.css +14 -0
- package/dist/brands/indigo.json +109 -0
- package/dist/brands/purple.css +14 -0
- package/dist/brands/purple.json +109 -0
- package/dist/brands/sky.css +14 -0
- package/dist/brands/sky.json +103 -0
- package/dist/brands/teal.css +14 -0
- package/dist/brands/teal.json +127 -0
- package/dist/brands/violet.css +20 -0
- package/dist/brands/violet.json +157 -0
- package/dist/index.d.ts +1439 -0
- package/dist/index.js +1436 -0
- package/dist/media.css +9 -0
- package/dist/shadcn.css +105 -0
- package/dist/tailwind.css +293 -0
- package/dist/themes/neutral.css +44 -0
- package/dist/tokens-studio.json +2405 -0
- package/dist/tokens.css +742 -0
- package/dist/tokens.dark.css +519 -0
- package/dist/tokens.json +928 -0
- package/dist/tokens.light.css +519 -0
- package/lib/brand.mjs +328 -0
- package/lib/contrast.mjs +130 -0
- package/package.json +54 -3
package/lib/brand.mjs
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brands (ADR 0020, ADR 0025): a brand is a small JSON file that sets the accent tokens for the
|
|
3
|
+
* light and dark themes. This module reads one, checks it, writes its CSS, and lays it over the
|
|
4
|
+
* built tokens so the contrast check can measure it. Pure: no file access, so the build, the
|
|
5
|
+
* `polykit-brand` command and the docs site all run the same code.
|
|
6
|
+
*
|
|
7
|
+
* A brand file:
|
|
8
|
+
*
|
|
9
|
+
* {
|
|
10
|
+
* "name": "atlas",
|
|
11
|
+
* "$description": "What the brand is for.",
|
|
12
|
+
* "color": {
|
|
13
|
+
* "light": { "accent": { "$value": "#0e8a7e" }, ... },
|
|
14
|
+
* "dark": { "accent": { "$value": "{palette.teal.500}" }, ... }
|
|
15
|
+
* },
|
|
16
|
+
* "font": {
|
|
17
|
+
* "sans": ["Archivo", "ui-sans-serif", "system-ui", "sans-serif"],
|
|
18
|
+
* "mono": ["DM Mono", "ui-monospace", "monospace"],
|
|
19
|
+
* "display": ["Clash Display", "Archivo", "sans-serif"]
|
|
20
|
+
* },
|
|
21
|
+
* "weight": { "heading": 600 },
|
|
22
|
+
* "exceptions": {
|
|
23
|
+
* "color.accent on color.canvas": { "themes": ["light"], "reason": "..." }
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* Colors are hex or rgb() colors, or {palette.*} aliases to Polykit's raw palette. Typography is
|
|
28
|
+
* optional: font stacks end in a generic family, and the display family goes to the headings.
|
|
29
|
+
*/
|
|
30
|
+
import { parseColor } from './contrast.mjs';
|
|
31
|
+
|
|
32
|
+
/** The primary accent: every brand sets all of these, in both themes. */
|
|
33
|
+
export const PRIMARY = [
|
|
34
|
+
'accent',
|
|
35
|
+
'accent-hover',
|
|
36
|
+
'accent-tint',
|
|
37
|
+
'accent-tint-strong',
|
|
38
|
+
'on-accent-tint',
|
|
39
|
+
'on-accent',
|
|
40
|
+
'focus-field',
|
|
41
|
+
'focus-halo',
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/** The secondary accent: optional, all or nothing. Without it, each falls back to its primary. */
|
|
45
|
+
export const SECONDARY = [
|
|
46
|
+
'accent-secondary',
|
|
47
|
+
'accent-secondary-hover',
|
|
48
|
+
'accent-secondary-tint',
|
|
49
|
+
'accent-secondary-tint-strong',
|
|
50
|
+
'on-accent-secondary-tint',
|
|
51
|
+
'on-accent-secondary',
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
export const CONTRACT = [...PRIMARY, ...SECONDARY];
|
|
55
|
+
export const THEMES = ['light', 'dark'];
|
|
56
|
+
|
|
57
|
+
/** The variable a brand sets for a contract token: accent -> --pk-brand-accent. */
|
|
58
|
+
export const brandVar = (name) => `--pk-brand-${name}`;
|
|
59
|
+
|
|
60
|
+
const NAME = /^[a-z][a-z0-9-]*$/;
|
|
61
|
+
|
|
62
|
+
/** The roles a display family and the heading weight apply to (the rest follow the sans). */
|
|
63
|
+
export const HEADING_ROLES = ['display', 'heading-lg', 'heading-md', 'heading-sm'];
|
|
64
|
+
const FONT_SLOTS = ['sans', 'mono', 'display'];
|
|
65
|
+
const GENERIC = new Set([
|
|
66
|
+
'serif',
|
|
67
|
+
'sans-serif',
|
|
68
|
+
'monospace',
|
|
69
|
+
'cursive',
|
|
70
|
+
'fantasy',
|
|
71
|
+
'system-ui',
|
|
72
|
+
'ui-serif',
|
|
73
|
+
'ui-sans-serif',
|
|
74
|
+
'ui-monospace',
|
|
75
|
+
'ui-rounded',
|
|
76
|
+
'math',
|
|
77
|
+
'emoji',
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
/** A font stack as CSS: family names quoted, keywords and -apple-system left bare. */
|
|
81
|
+
export function fontStack(families) {
|
|
82
|
+
return families
|
|
83
|
+
.map((family) => (GENERIC.has(family) || family.startsWith('-') ? family : `'${family}'`))
|
|
84
|
+
.join(', ');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readTypography(json, problems) {
|
|
88
|
+
const font = {};
|
|
89
|
+
for (const [slot, stack] of Object.entries(json?.font ?? {})) {
|
|
90
|
+
if (!FONT_SLOTS.includes(slot)) {
|
|
91
|
+
problems.push(`font.${slot}: not a font slot (sans, mono, display)`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (!Array.isArray(stack) || stack.length === 0 || stack.some((f) => typeof f !== 'string')) {
|
|
95
|
+
problems.push(`font.${slot}: a list of family names`);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (!GENERIC.has(stack.at(-1))) {
|
|
99
|
+
problems.push(`font.${slot}: end the list with a generic family, such as sans-serif`);
|
|
100
|
+
}
|
|
101
|
+
font[slot] = stack;
|
|
102
|
+
}
|
|
103
|
+
const heading = json?.weight?.heading;
|
|
104
|
+
if (heading !== undefined && !(Number.isInteger(heading) && heading >= 100 && heading <= 900)) {
|
|
105
|
+
problems.push('weight.heading: a weight from 100 to 900');
|
|
106
|
+
}
|
|
107
|
+
return { font, headingWeight: heading ?? null };
|
|
108
|
+
}
|
|
109
|
+
const PALETTE_ALIAS = /^\{palette\.([a-z0-9-]+)\.([a-z0-9-]+)\}$/;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* One value from a brand file: the CSS to write and the color it resolves to.
|
|
113
|
+
* `palette` maps raw palette keys (palette.teal.500) to their values.
|
|
114
|
+
*/
|
|
115
|
+
function brandValue(raw, palette, where) {
|
|
116
|
+
if (typeof raw !== 'string') throw new Error(`${where}: the value must be a string`);
|
|
117
|
+
const alias = PALETTE_ALIAS.exec(raw);
|
|
118
|
+
if (alias) {
|
|
119
|
+
const key = `palette.${alias[1]}.${alias[2]}`;
|
|
120
|
+
if (!(key in palette)) throw new Error(`${where}: ${raw} is not in Polykit's palette`);
|
|
121
|
+
return { css: `var(--pk-${alias[1]}-${alias[2]})`, value: palette[key] };
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
parseColor(raw);
|
|
125
|
+
} catch {
|
|
126
|
+
throw new Error(`${where}: "${raw}" is not a hex or rgb() color or a {palette.*} alias`);
|
|
127
|
+
}
|
|
128
|
+
return { css: raw, value: raw };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Checks a parsed brand file and returns it normalized:
|
|
133
|
+
* { name, description, secondary, colors: { light: { accent: { css, value } }, dark }, exceptions }.
|
|
134
|
+
* Throws with every problem listed when the file breaks the contract.
|
|
135
|
+
*/
|
|
136
|
+
export function readBrand(json, palette) {
|
|
137
|
+
const problems = [];
|
|
138
|
+
const name = json?.name;
|
|
139
|
+
if (typeof name !== 'string' || !NAME.test(name)) {
|
|
140
|
+
problems.push('name: lowercase letters, digits and dashes, starting with a letter');
|
|
141
|
+
}
|
|
142
|
+
if (name === 'default') problems.push('name: "default" is the built-in brand');
|
|
143
|
+
const colors = { light: {}, dark: {} };
|
|
144
|
+
let secondary = null;
|
|
145
|
+
for (const theme of THEMES) {
|
|
146
|
+
const given = json?.color?.[theme];
|
|
147
|
+
if (!given || typeof given !== 'object') {
|
|
148
|
+
problems.push(`color.${theme}: missing`);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
for (const key of Object.keys(given)) {
|
|
152
|
+
if (key.startsWith('$')) continue;
|
|
153
|
+
if (!CONTRACT.includes(key)) problems.push(`color.${theme}.${key}: not a brand token`);
|
|
154
|
+
}
|
|
155
|
+
for (const key of PRIMARY) {
|
|
156
|
+
if (!given[key]) problems.push(`color.${theme}.${key}: missing (the primary is required)`);
|
|
157
|
+
}
|
|
158
|
+
const has = SECONDARY.filter((key) => given[key]);
|
|
159
|
+
if (has.length > 0 && has.length < SECONDARY.length) {
|
|
160
|
+
const missing = SECONDARY.filter((key) => !given[key]).join(', ');
|
|
161
|
+
problems.push(`color.${theme}: a secondary accent needs all six tokens; missing ${missing}`);
|
|
162
|
+
}
|
|
163
|
+
const themeHasSecondary = has.length === SECONDARY.length;
|
|
164
|
+
if (secondary === null) secondary = themeHasSecondary;
|
|
165
|
+
else if (secondary !== themeHasSecondary) {
|
|
166
|
+
problems.push('color: set the secondary accent in both themes or in neither');
|
|
167
|
+
}
|
|
168
|
+
for (const key of CONTRACT) {
|
|
169
|
+
if (!given[key]) continue;
|
|
170
|
+
try {
|
|
171
|
+
colors[theme][key] = brandValue(given[key].$value, palette, `color.${theme}.${key}`);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
problems.push(error.message);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const typography = readTypography(json, problems);
|
|
178
|
+
const exceptions = json?.exceptions ?? {};
|
|
179
|
+
for (const [pair, exception] of Object.entries(exceptions)) {
|
|
180
|
+
if (!exception?.reason) problems.push(`exceptions["${pair}"]: give a reason`);
|
|
181
|
+
const themes = exception?.themes ?? [];
|
|
182
|
+
if (!themes.length || themes.some((t) => !THEMES.includes(t))) {
|
|
183
|
+
problems.push(`exceptions["${pair}"]: themes must list light, dark or both`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (problems.length > 0) {
|
|
187
|
+
throw new Error(`Brand ${name ?? '(no name)'}:\n ${problems.join('\n ')}`);
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
name,
|
|
191
|
+
description: json.$description ?? '',
|
|
192
|
+
secondary: Boolean(secondary),
|
|
193
|
+
colors,
|
|
194
|
+
...typography,
|
|
195
|
+
exceptions,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The CSS for a brand: its colors, chosen per theme with light-dark(), and its typography.
|
|
201
|
+
* `typeRoles` (from dist/brand-contract.json) says which font each type role reads, so the roles
|
|
202
|
+
* can be worked out again on the brand's element: they resolve where they are declared.
|
|
203
|
+
*/
|
|
204
|
+
export function brandCss(brand, typeRoles = {}) {
|
|
205
|
+
const lines = CONTRACT.filter((key) => brand.colors.light[key]).map((key) => {
|
|
206
|
+
const light = brand.colors.light[key].css;
|
|
207
|
+
const dark = brand.colors.dark[key].css;
|
|
208
|
+
return ` ${brandVar(key)}: ${light === dark ? light : `light-dark(${light}, ${dark})`};`;
|
|
209
|
+
});
|
|
210
|
+
const font = brand.font ?? {};
|
|
211
|
+
const typed = Object.keys(font).length > 0 || brand.headingWeight;
|
|
212
|
+
if (typed) {
|
|
213
|
+
lines.push('', ' /* Typography */');
|
|
214
|
+
if (font.sans) lines.push(` --pk-font-sans: ${fontStack(font.sans)};`);
|
|
215
|
+
if (font.mono) lines.push(` --pk-font-mono: ${fontStack(font.mono)};`);
|
|
216
|
+
for (const [role, family] of Object.entries(typeRoles)) {
|
|
217
|
+
const heading = HEADING_ROLES.includes(role);
|
|
218
|
+
const value = heading && font.display ? fontStack(font.display) : `var(--pk-font-${family})`;
|
|
219
|
+
lines.push(` --pk-type-${role}-font-family: ${value};`);
|
|
220
|
+
if (heading && brand.headingWeight) {
|
|
221
|
+
lines.push(` --pk-type-${role}-font-weight: ${brand.headingWeight};`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const about = brand.description ? `\n * ${brand.description}` : '';
|
|
226
|
+
return `/* Polykit brand "${brand.name}".${about}
|
|
227
|
+
* Opt in with data-brand="${brand.name}" on <html> or any element, after tokens.css.
|
|
228
|
+
* Needs light-dark() (Baseline 2024). */
|
|
229
|
+
[data-brand="${brand.name}"] {
|
|
230
|
+
${lines.join('\n')}
|
|
231
|
+
}
|
|
232
|
+
`;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The built tokens (dist/tokens.json) with a brand laid over them, for the contrast check.
|
|
237
|
+
* `dependents` maps, per theme, each color token that aliases a brand token to what it aliases
|
|
238
|
+
* (dist/brand-contract.json): chart-1 follows the accent, the secondary follows the primary when
|
|
239
|
+
* a brand has no secondary of its own.
|
|
240
|
+
*/
|
|
241
|
+
export function brandTokens(tokens, brand, dependents) {
|
|
242
|
+
const layer = (theme) => {
|
|
243
|
+
const map = { ...tokens[theme] };
|
|
244
|
+
for (const [key, { value }] of Object.entries(brand.colors[theme])) map[`color.${key}`] = value;
|
|
245
|
+
const own = new Set(Object.keys(brand.colors[theme]).map((key) => `color.${key}`));
|
|
246
|
+
const pending = Object.entries(dependents[theme]).filter(([key]) => !own.has(key));
|
|
247
|
+
// Aliases can chain (a badge follows the tint, which a brand sets), so settle them in passes.
|
|
248
|
+
for (let pass = 0; pass < pending.length + 1; pass++) {
|
|
249
|
+
for (const [key, target] of pending) map[key] = map[target];
|
|
250
|
+
}
|
|
251
|
+
return map;
|
|
252
|
+
};
|
|
253
|
+
return { light: layer('light'), dark: layer('dark'), neutral: tokens.neutral };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The key a brand file uses to excuse a pair: "color.accent on color.canvas". */
|
|
257
|
+
export const pairKey = (pair) => `${pair.fg} on ${pair.mix ? 'mix' : pair.bg}`;
|
|
258
|
+
|
|
259
|
+
/** Whether a pair measures something a brand changes. */
|
|
260
|
+
export function touchesBrand(pair, dependents) {
|
|
261
|
+
const keys = new Set([
|
|
262
|
+
...CONTRACT.map((key) => `color.${key}`),
|
|
263
|
+
...Object.keys(dependents.light),
|
|
264
|
+
...Object.keys(dependents.dark),
|
|
265
|
+
]);
|
|
266
|
+
return [pair.fg, pair.bg, pair.over, pair.mix?.color, pair.mix?.over].some((k) => keys.has(k));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The pairs to measure for a brand: every pair that touches a brand token, with the brand's own
|
|
271
|
+
* exceptions in place of the default brand's.
|
|
272
|
+
*/
|
|
273
|
+
export function brandPairs(pairs, brand, dependents) {
|
|
274
|
+
return pairs
|
|
275
|
+
.filter((pair) => touchesBrand(pair, dependents))
|
|
276
|
+
.map((pair) => {
|
|
277
|
+
const excused = brand.exceptions[pairKey(pair)];
|
|
278
|
+
const exceptions = excused
|
|
279
|
+
? Object.fromEntries(
|
|
280
|
+
excused.themes.map((theme) => [theme, { ref: brand.name, reason: excused.reason }]),
|
|
281
|
+
)
|
|
282
|
+
: undefined;
|
|
283
|
+
return { ...pair, exceptions };
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** The hue of a color in degrees (0 to 360), and its saturation (0 to 1), as HSL gives them. */
|
|
288
|
+
export function hueOf(input) {
|
|
289
|
+
const { r, g, b } = parseColor(input);
|
|
290
|
+
const [R, G, B] = [r, g, b].map((c) => c / 255);
|
|
291
|
+
const max = Math.max(R, G, B);
|
|
292
|
+
const min = Math.min(R, G, B);
|
|
293
|
+
const delta = max - min;
|
|
294
|
+
if (delta === 0) return { hue: 0, saturation: 0 };
|
|
295
|
+
const light = (max + min) / 2;
|
|
296
|
+
const saturation = delta / (1 - Math.abs(2 * light - 1));
|
|
297
|
+
let hue;
|
|
298
|
+
if (max === R) hue = ((G - B) / delta) % 6;
|
|
299
|
+
else if (max === G) hue = (B - R) / delta + 2;
|
|
300
|
+
else hue = (R - G) / delta + 4;
|
|
301
|
+
return { hue: (hue * 60 + 360) % 360, saturation };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** How close a brand's accent may come to the information blue before the check warns. */
|
|
305
|
+
export const INFO_DISTANCE = 20;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Warnings that never fail the check: a primary accent within INFO_DISTANCE degrees of hue of
|
|
309
|
+
* --pk-color-info, where people may read the accent as information or a link.
|
|
310
|
+
*/
|
|
311
|
+
export function brandWarnings(brand, tokens) {
|
|
312
|
+
const warnings = [];
|
|
313
|
+
for (const theme of THEMES) {
|
|
314
|
+
const accent = hueOf(brand.colors[theme].accent.value);
|
|
315
|
+
const info = hueOf(tokens[theme]['color.info']);
|
|
316
|
+
const apart = Math.abs(accent.hue - info.hue);
|
|
317
|
+
const distance = Math.min(apart, 360 - apart);
|
|
318
|
+
if (accent.saturation > 0.25 && distance < INFO_DISTANCE) {
|
|
319
|
+
warnings.push({
|
|
320
|
+
theme,
|
|
321
|
+
kind: 'near-info',
|
|
322
|
+
distance,
|
|
323
|
+
message: `${theme}: the accent is ${Math.round(distance)}° of hue from the information blue. People may read it as information or a link: see "Close to the information blue" on the Brands page.`,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return warnings;
|
|
328
|
+
}
|
package/lib/contrast.mjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contrast math and evaluation. Pure functions with no Node APIs, so the docs site runs the
|
|
3
|
+
* same code as CI.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const MINIMUM = { text: 4.5, graphic: 3 };
|
|
7
|
+
|
|
8
|
+
/** Parses #rgb, #rgba, #rrggbb, #rrggbbaa, rgb(r g b / a) and rgba(r, g, b, a). */
|
|
9
|
+
export function parseColor(input) {
|
|
10
|
+
const value = String(input).trim().toLowerCase();
|
|
11
|
+
const hex = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/.exec(value);
|
|
12
|
+
if (hex) {
|
|
13
|
+
let digits = hex[1];
|
|
14
|
+
if (digits.length <= 4) digits = [...digits].map((c) => c + c).join('');
|
|
15
|
+
const channel = (i) => Number.parseInt(digits.slice(i, i + 2), 16);
|
|
16
|
+
return {
|
|
17
|
+
r: channel(0),
|
|
18
|
+
g: channel(2),
|
|
19
|
+
b: channel(4),
|
|
20
|
+
a: digits.length === 8 ? channel(6) / 255 : 1,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const fn = /^rgba?\(\s*([^)]+?)\s*\)$/.exec(value);
|
|
24
|
+
if (fn) {
|
|
25
|
+
const parts = fn[1].split(/[\s,/]+/).filter(Boolean);
|
|
26
|
+
if (parts.length === 3 || parts.length === 4) {
|
|
27
|
+
const [r, g, b] = parts.slice(0, 3).map(Number);
|
|
28
|
+
const a = parts[3] === undefined ? 1 : Number(parts[3]);
|
|
29
|
+
if ([r, g, b, a].every(Number.isFinite)) return { r, g, b, a };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
throw new Error(`Cannot parse color: ${input}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `fg` laid over an opaque `bg`. */
|
|
36
|
+
export function composite(fg, bg) {
|
|
37
|
+
const mix = (f, b) => Math.round(f * fg.a + b * (1 - fg.a));
|
|
38
|
+
return { r: mix(fg.r, bg.r), g: mix(fg.g, bg.g), b: mix(fg.b, bg.b), a: 1 };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function luminance({ r, g, b }) {
|
|
42
|
+
const linear = (c) => {
|
|
43
|
+
const s = c / 255;
|
|
44
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
45
|
+
};
|
|
46
|
+
return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function contrastRatio(a, b) {
|
|
50
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
51
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** `a` mixed with `b` in sRGB, `share` of it from `a`: what color-mix(in srgb, a share, b) paints. */
|
|
55
|
+
export function mixSrgb(a, b, share) {
|
|
56
|
+
const channel = (x, y) => Math.round(x * share + y * (1 - share));
|
|
57
|
+
return { r: channel(a.r, b.r), g: channel(a.g, b.g), b: channel(a.b, b.b), a: 1 };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function resolve(map, key) {
|
|
61
|
+
if (!(key in map)) throw new Error(`Token not found: ${key}`);
|
|
62
|
+
return parseColor(map[key]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Measures every pair in every theme it lists.
|
|
67
|
+
* `tokens` is the built tokens.json: { light, dark, neutral: { light, dark } }.
|
|
68
|
+
*/
|
|
69
|
+
export function evaluate(pairs, tokens) {
|
|
70
|
+
const results = [];
|
|
71
|
+
for (const pair of pairs) {
|
|
72
|
+
for (const theme of pair.themes) {
|
|
73
|
+
const map =
|
|
74
|
+
pair.canvas === 'neutral' ? { ...tokens[theme], ...tokens.neutral[theme] } : tokens[theme];
|
|
75
|
+
// A tinted background, as the icon tile paints it: mix.color at mix.share over mix.over.
|
|
76
|
+
// share is a number (0.16) or the name of a token that holds a percentage ('tint').
|
|
77
|
+
let bgLabel = pair.bg;
|
|
78
|
+
let mix = null;
|
|
79
|
+
let bg;
|
|
80
|
+
if (pair.mix) {
|
|
81
|
+
const { color, over, share } = pair.mix;
|
|
82
|
+
const amount = typeof share === 'number' ? share : Number.parseFloat(map[share]) / 100;
|
|
83
|
+
if (!Number.isFinite(amount)) throw new Error(`Mix share not found: ${share}`);
|
|
84
|
+
bg = mixSrgb(resolve(map, color), resolve(map, over), amount);
|
|
85
|
+
mix = { color, over, amount };
|
|
86
|
+
bgLabel = `${color} ${Math.round(amount * 100)}% over ${over}`;
|
|
87
|
+
} else {
|
|
88
|
+
bg = resolve(map, pair.bg);
|
|
89
|
+
}
|
|
90
|
+
if (bg.a < 1) {
|
|
91
|
+
if (!pair.over) {
|
|
92
|
+
throw new Error(`Background ${pair.bg} is translucent; give the pair an "over" token`);
|
|
93
|
+
}
|
|
94
|
+
const base = resolve(map, pair.over);
|
|
95
|
+
if (base.a < 1) throw new Error(`${pair.over} is translucent; "over" must be opaque`);
|
|
96
|
+
bg = composite(bg, base);
|
|
97
|
+
}
|
|
98
|
+
const fg = composite(resolve(map, pair.fg), bg);
|
|
99
|
+
const ratio = contrastRatio(fg, bg);
|
|
100
|
+
const min = pair.min ?? MINIMUM[pair.kind];
|
|
101
|
+
const exception = pair.exceptions?.[theme] ?? null;
|
|
102
|
+
const passes = ratio >= min;
|
|
103
|
+
const status = passes
|
|
104
|
+
? exception
|
|
105
|
+
? 'stale-exception'
|
|
106
|
+
: 'pass'
|
|
107
|
+
: exception
|
|
108
|
+
? 'exception'
|
|
109
|
+
: 'fail';
|
|
110
|
+
results.push({
|
|
111
|
+
group: pair.group ?? 'Text',
|
|
112
|
+
label: pair.label ?? null,
|
|
113
|
+
fg: pair.fg,
|
|
114
|
+
bg: bgLabel,
|
|
115
|
+
mix,
|
|
116
|
+
theme: pair.canvas === 'neutral' ? `${theme} (cool)` : theme,
|
|
117
|
+
kind: pair.kind,
|
|
118
|
+
min,
|
|
119
|
+
ratio,
|
|
120
|
+
status,
|
|
121
|
+
exception,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return results;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function failures(results) {
|
|
129
|
+
return results.filter((r) => r.status === 'fail' || r.status === 'stale-exception');
|
|
130
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polyfen/polykit-tokens",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Polykit design tokens: CSS variables, SCSS, JS, Tailwind v4 theme and a shadcn/ui bridge.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"polykit-brand": "./bin/polykit-brand.mjs"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"sass": "./dist/_tokens.scss",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./tokens.css": "./dist/tokens.css",
|
|
19
|
+
"./tokens.light.css": "./dist/tokens.light.css",
|
|
20
|
+
"./tokens.dark.css": "./dist/tokens.dark.css",
|
|
21
|
+
"./themes/neutral.css": "./dist/themes/neutral.css",
|
|
22
|
+
"./brands/*": "./dist/brands/*",
|
|
23
|
+
"./brand-contract.json": "./dist/brand-contract.json",
|
|
24
|
+
"./brand": "./lib/brand.mjs",
|
|
25
|
+
"./contrast": "./lib/contrast.mjs",
|
|
26
|
+
"./media.css": "./dist/media.css",
|
|
27
|
+
"./tailwind.css": "./dist/tailwind.css",
|
|
28
|
+
"./shadcn.css": "./dist/shadcn.css",
|
|
29
|
+
"./_tokens.scss": "./dist/_tokens.scss",
|
|
30
|
+
"./tokens.json": "./dist/tokens.json",
|
|
31
|
+
"./tokens-studio.json": "./dist/tokens-studio.json",
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"lib",
|
|
37
|
+
"bin",
|
|
38
|
+
"contrast.pairs.mjs",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": [
|
|
43
|
+
"*.css"
|
|
44
|
+
],
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"sass": "^1.108.2",
|
|
50
|
+
"style-dictionary": "^5.8.0",
|
|
51
|
+
"tailwindcss": "^4.3.5"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "node build/build.mjs",
|
|
55
|
+
"test": "node build/build.mjs && node --test test/*.test.mjs"
|
|
56
|
+
}
|
|
6
57
|
}
|