figdriver 0.0.0-stage → 0.4.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/CHANGELOG.md +86 -0
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/cli/result.ts +231 -0
- package/dist/cli.js +4125 -0
- package/index.d.ts +12 -0
- package/index.js +8 -0
- package/package.json +78 -4
- package/src/core/color.ts +435 -0
- package/src/core/css.ts +668 -0
- package/src/core/foundations.ts +389 -0
- package/src/core/json.d.ts +4 -0
- package/src/core/layout.ts +684 -0
- package/src/core/naming.ts +129 -0
- package/src/core/protocol.ts +328 -0
- package/src/core/rerun.ts +130 -0
- package/src/core/reuse.ts +149 -0
- package/src/core/snapshot.ts +88 -0
- package/src/core/spec.ts +167 -0
- package/src/core/text.ts +37 -0
- package/src/core/tokens.ts +77 -0
- package/src/core/types.ts +181 -0
- package/src/core/users.ts +17 -0
- package/src/figma/helpers.ts +238 -0
- package/src/figma/import.ts +962 -0
- package/src/figma/inspect.ts +254 -0
- package/src/figma/kit.ts +562 -0
- package/src/figma/migrate.ts +301 -0
- package/src/figma/owned.ts +56 -0
- package/src/figma/read.ts +618 -0
- package/src/figma/rerun.ts +387 -0
- package/src/figma/review.ts +481 -0
- package/src/figma/tokens.ts +97 -0
- package/src/page/capture.ts +999 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The types a design's index.ts imports, re-exported from src/core/types.ts so they cannot drift from the engine.
|
|
2
|
+
import type { ImportSpec as CoreImportSpec } from "./src/core/types.js";
|
|
3
|
+
|
|
4
|
+
export type { Box, ComponentRule, El, Match, Pseudo, Run, Style, Vars } from "./src/core/types.js";
|
|
5
|
+
|
|
6
|
+
export type ImportSpec = Omit<CoreImportSpec, "snapshot"> & {
|
|
7
|
+
/** The captured page, `import snapshot from "./snapshot.json"`. Loosely typed, since a JSON import has no tuples or literal kinds. */
|
|
8
|
+
snapshot: { url: string; viewport: number[]; tokens: Record<string, string>; root: object; assets: object };
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** Builds the design in the open Figma file. Runs only in a bundle from `figdriver import plugin` or `figdriver import run`. */
|
|
12
|
+
export declare function importDesign(spec: ImportSpec): void;
|
package/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const message =
|
|
2
|
+
"figdriver runs inside a Figma file, not here. Bundle a spec with `npx figdriver import plugin <dir>`, or build it in Figma with `npx figdriver import run <dir>`.";
|
|
3
|
+
|
|
4
|
+
export function importDesign() {
|
|
5
|
+
throw new Error(message);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
throw new Error(message);
|
package/package.json
CHANGED
|
@@ -1,6 +1,80 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "figdriver",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Playwright for Figma: drive a Figma file from the terminal or a coding agent, and rebuild web pages in it as design systems.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agent-skill",
|
|
7
|
+
"automation",
|
|
8
|
+
"cli",
|
|
9
|
+
"coding-agent",
|
|
10
|
+
"design-system",
|
|
11
|
+
"figma",
|
|
12
|
+
"figma-plugin",
|
|
13
|
+
"html-to-figma",
|
|
14
|
+
"plugin-api"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/MahmoudOmiesh/figdriver#readme",
|
|
17
|
+
"bugs": "https://github.com/MahmoudOmiesh/figdriver/issues",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/MahmoudOmiesh/figdriver.git"
|
|
22
|
+
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"figdriver": "dist/cli.js"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/cli.js",
|
|
28
|
+
"index.js",
|
|
29
|
+
"index.d.ts",
|
|
30
|
+
"cli/result.ts",
|
|
31
|
+
"src",
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"type": "module",
|
|
36
|
+
"types": "./index.d.ts",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./index.d.ts",
|
|
40
|
+
"default": "./index.js"
|
|
41
|
+
},
|
|
42
|
+
"./result": {
|
|
43
|
+
"types": "./cli/result.ts"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"figdriver": "bun cli/main.ts",
|
|
52
|
+
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.core.json && tsc --noEmit -p tsconfig.page.json && tsc --noEmit -p tsconfig.cli.json && tsc --noEmit -p tsconfig.dev.json",
|
|
53
|
+
"lint": "oxlint",
|
|
54
|
+
"format": "oxfmt",
|
|
55
|
+
"check": "bun run typecheck && oxlint && oxfmt --check && bun test",
|
|
56
|
+
"pin": "bun scripts/pin.ts",
|
|
57
|
+
"e2e": "bun scripts/e2e.ts",
|
|
58
|
+
"prepack": "esbuild cli/main.ts --bundle --platform=node --format=esm --target=node22 --packages=external --outfile=dist/cli.js",
|
|
59
|
+
"smoke": "bun scripts/smoke.ts",
|
|
60
|
+
"release": "bun scripts/release.ts"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"acorn": "^8.19.0",
|
|
64
|
+
"esbuild-wasm": "^0.28.2",
|
|
65
|
+
"pngjs": "^7.0.0"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@figma/plugin-typings": "^1.142.0",
|
|
69
|
+
"@oxlint/plugins": "1.95.0",
|
|
70
|
+
"@types/bun": "^1.4.5",
|
|
71
|
+
"@types/node": "^22",
|
|
72
|
+
"@types/pngjs": "^6.0.5",
|
|
73
|
+
"oxfmt": "0.80.0",
|
|
74
|
+
"oxlint": "1.95.0",
|
|
75
|
+
"typescript": "^7.0.2"
|
|
76
|
+
},
|
|
77
|
+
"engines": {
|
|
78
|
+
"node": ">=22.1"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS Color 4 colors: every color space CSS names, parsing a computed color into one model, and interpolating between two colors
|
|
3
|
+
* the way a CSS gradient does.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
type Vec = [number, number, number];
|
|
7
|
+
|
|
8
|
+
type Matrix = [Vec, Vec, Vec];
|
|
9
|
+
|
|
10
|
+
/** A color as coordinates in the space it was written in, so it converts only when drawn or blended elsewhere, and alpha. */
|
|
11
|
+
export type Color = { space: Space; coords: Vec; alpha: number };
|
|
12
|
+
|
|
13
|
+
export type RGBA = { r: number; g: number; b: number; a: number };
|
|
14
|
+
|
|
15
|
+
function times(m: Matrix, v: Vec): Vec {
|
|
16
|
+
const dot = (row: Vec) => row[0] * v[0] + row[1] * v[1] + row[2] * v[2];
|
|
17
|
+
|
|
18
|
+
return [dot(m[0]), dot(m[1]), dot(m[2])];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const product = (a: Matrix, b: Matrix): Matrix => {
|
|
22
|
+
const column = (j: number): Vec => [b[0][j], b[1][j], b[2][j]];
|
|
23
|
+
const [c0, c1, c2] = [times(a, column(0)), times(a, column(1)), times(a, column(2))];
|
|
24
|
+
|
|
25
|
+
return [
|
|
26
|
+
[c0[0], c1[0], c2[0]],
|
|
27
|
+
[c0[1], c1[1], c2[1]],
|
|
28
|
+
[c0[2], c1[2], c2[2]],
|
|
29
|
+
];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function inverse([[a, b, c], [d, e, f], [g, h, i]]: Matrix): Matrix {
|
|
33
|
+
const det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
|
|
34
|
+
|
|
35
|
+
return [
|
|
36
|
+
[(e * i - f * h) / det, (c * h - b * i) / det, (b * f - c * e) / det],
|
|
37
|
+
[(f * g - d * i) / det, (a * i - c * g) / det, (c * d - a * f) / det],
|
|
38
|
+
[(d * h - e * g) / det, (b * g - a * h) / det, (a * e - b * d) / det],
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const map = (v: Vec, f: (c: number) => number): Vec => [f(v[0]), f(v[1]), f(v[2])];
|
|
43
|
+
|
|
44
|
+
/** A transfer function applied to a channel's magnitude, mirrored for negative channels, which a wide-gamut color has. */
|
|
45
|
+
const mirrored = (f: (c: number) => number) => (c: number) => Math.sign(c) * f(Math.abs(c));
|
|
46
|
+
|
|
47
|
+
const srgbLinear = mirrored((c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
|
|
48
|
+
|
|
49
|
+
const srgbGamma = mirrored((c) => (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055));
|
|
50
|
+
|
|
51
|
+
const same = (c: number) => c;
|
|
52
|
+
|
|
53
|
+
// Matrices from CSS Color 4, each from a space's linear channels into XYZ with a D65 white point.
|
|
54
|
+
const XYZ_FROM_SRGB: Matrix = [
|
|
55
|
+
[0.41239079926595934, 0.357584339383878, 0.1804807884018343],
|
|
56
|
+
[0.21263900587151027, 0.715168678767756, 0.07219231536073371],
|
|
57
|
+
[0.01933081871559182, 0.11919477979462598, 0.9505321522496607],
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
const SRGB_FROM_XYZ = inverse(XYZ_FROM_SRGB);
|
|
61
|
+
|
|
62
|
+
const D65_FROM_D50: Matrix = [
|
|
63
|
+
[0.955473421488075, -0.02309845494876471, 0.06325924320057072],
|
|
64
|
+
[-0.0283697093338637, 1.0099953980813041, 0.021041441191917323],
|
|
65
|
+
[0.012314014864481998, -0.020507649298898964, 1.330365926242124],
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const D50_FROM_D65 = inverse(D65_FROM_D50);
|
|
69
|
+
|
|
70
|
+
const IDENTITY: Matrix = [
|
|
71
|
+
[1, 0, 0],
|
|
72
|
+
[0, 1, 0],
|
|
73
|
+
[0, 0, 1],
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
/** How a space's coordinates map to and from linear sRGB, and which coordinate is a hue, for a polar space. */
|
|
77
|
+
type Space = { from: (v: Vec) => Vec; to: (linear: Vec) => Vec; hue?: { at: 0 | 2; powerless: (v: Vec) => boolean } };
|
|
78
|
+
|
|
79
|
+
const clean = ([x, y, z]: Vec): Vec => [x || 0, y || 0, z || 0];
|
|
80
|
+
|
|
81
|
+
/** Linear sRGB is the hub every space converts through. It is unclamped, so a wide-gamut color keeps its gamut until it is drawn. */
|
|
82
|
+
function inSpace(c: Color, space: Space): Vec {
|
|
83
|
+
if (c.space === space) return c.coords;
|
|
84
|
+
const out = space.to(c.space.from(clean(c.coords)));
|
|
85
|
+
|
|
86
|
+
if (space.hue && space.hue.powerless(out)) out[space.hue.at] = NaN;
|
|
87
|
+
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** An RGB or XYZ space: a transfer function each way and the matrix from its linear channels to XYZ D65. */
|
|
92
|
+
function rgbSpace(toXyz: Matrix, linear: (c: number) => number, gamma: (c: number) => number): Space {
|
|
93
|
+
const toSrgb = product(SRGB_FROM_XYZ, toXyz);
|
|
94
|
+
const fromSrgb = inverse(toSrgb);
|
|
95
|
+
|
|
96
|
+
return { from: (v) => times(toSrgb, map(v, linear)), to: (v) => map(times(fromSrgb, v), gamma) };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const REC2020 = { alpha: 1.09929682680944, beta: 0.018053968510807 };
|
|
100
|
+
|
|
101
|
+
const D50_WHITE: Vec = [0.3457 / 0.3585, 1, (1 - 0.3457 - 0.3585) / 0.3585];
|
|
102
|
+
|
|
103
|
+
const LAB = { k: 24389 / 27, e: 216 / 24389 };
|
|
104
|
+
|
|
105
|
+
const lab: Space = {
|
|
106
|
+
from([L, a, b]) {
|
|
107
|
+
const fy = (L + 16) / 116;
|
|
108
|
+
const cube = (f: number) => (f ** 3 > LAB.e ? f ** 3 : (116 * f - 16) / LAB.k);
|
|
109
|
+
const xyz: Vec = [cube(a / 500 + fy), L > LAB.k * LAB.e ? fy ** 3 : L / LAB.k, cube(fy - b / 200)];
|
|
110
|
+
|
|
111
|
+
return times(SRGB_FROM_XYZ, times(D65_FROM_D50, [xyz[0] * D50_WHITE[0], xyz[1], xyz[2] * D50_WHITE[2]]));
|
|
112
|
+
},
|
|
113
|
+
to(v) {
|
|
114
|
+
const xyz = times(D50_FROM_D65, times(XYZ_FROM_SRGB, v));
|
|
115
|
+
const f = (c: number) => (c > LAB.e ? Math.cbrt(c) : (LAB.k * c + 16) / 116);
|
|
116
|
+
const [fx, fy, fz] = [f(xyz[0] / D50_WHITE[0]), f(xyz[1]), f(xyz[2] / D50_WHITE[2])];
|
|
117
|
+
|
|
118
|
+
return [116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz)];
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const LMS_FROM_OKLAB: Matrix = [
|
|
123
|
+
[1, 0.3963377774, 0.2158037573],
|
|
124
|
+
[1, -0.1055613458, -0.0638541728],
|
|
125
|
+
[1, -0.0894841775, -1.291485548],
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
const OKLAB_FROM_LMS = inverse(LMS_FROM_OKLAB);
|
|
129
|
+
|
|
130
|
+
const SRGB_FROM_LMS: Matrix = [
|
|
131
|
+
[4.0767416621, -3.3077115913, 0.2309699292],
|
|
132
|
+
[-1.2684380046, 2.6097574011, -0.3413193965],
|
|
133
|
+
[-0.0041960863, -0.7034186147, 1.707614701],
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
const LMS_FROM_SRGB = inverse(SRGB_FROM_LMS);
|
|
137
|
+
|
|
138
|
+
const oklab: Space = {
|
|
139
|
+
from: (v) =>
|
|
140
|
+
times(
|
|
141
|
+
SRGB_FROM_LMS,
|
|
142
|
+
map(times(LMS_FROM_OKLAB, v), (c) => c ** 3),
|
|
143
|
+
),
|
|
144
|
+
to: (v) => times(OKLAB_FROM_LMS, map(times(LMS_FROM_SRGB, v), Math.cbrt)),
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const radians = (deg: number) => (deg * Math.PI) / 180;
|
|
148
|
+
const turn = (h: number) => ((h % 360) + 360) % 360;
|
|
149
|
+
|
|
150
|
+
/** The polar form of a Lab-like space, its hue powerless once chroma falls under `gray`. */
|
|
151
|
+
function polar(base: Space, gray: number): Space {
|
|
152
|
+
return {
|
|
153
|
+
from: ([L, C, H]) => base.from([L, C * Math.cos(radians(H)), C * Math.sin(radians(H))]),
|
|
154
|
+
to(v) {
|
|
155
|
+
const [L, a, b] = base.to(v);
|
|
156
|
+
|
|
157
|
+
return [L, Math.hypot(a, b), ((Math.atan2(b, a) * 180) / Math.PI + 360) % 360];
|
|
158
|
+
},
|
|
159
|
+
hue: { at: 2, powerless: ([, C]) => C < gray },
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** HSL and HWB, which CSS defines over gamma-encoded sRGB with saturation and lightness from 0 to 1. */
|
|
164
|
+
function fromHsl([h, s, l]: Vec): Vec {
|
|
165
|
+
const hue = turn(h);
|
|
166
|
+
const k = (n: number) => (n + hue / 30) % 12;
|
|
167
|
+
const f = (n: number) => l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
|
|
168
|
+
|
|
169
|
+
return [f(0), f(8), f(4)];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function toHsl([r, g, b]: Vec): Vec {
|
|
173
|
+
const max = Math.max(r, g, b),
|
|
174
|
+
min = Math.min(r, g, b),
|
|
175
|
+
d = max - min,
|
|
176
|
+
l = (max + min) / 2;
|
|
177
|
+
|
|
178
|
+
const s = d === 0 || l === 0 || l === 1 ? 0 : (max - l) / Math.min(l, 1 - l);
|
|
179
|
+
const h = d === 0 ? 0 : max === r ? ((g - b) / d + 6) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
|
|
180
|
+
|
|
181
|
+
return [h * 60, s, l];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const hsl: Space = {
|
|
185
|
+
from: (v) => map(fromHsl(v), srgbLinear),
|
|
186
|
+
to: (v) => toHsl(map(v, srgbGamma)),
|
|
187
|
+
hue: { at: 0, powerless: ([, s]) => s < 1e-6 },
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const hwb: Space = {
|
|
191
|
+
from([h, w, b]) {
|
|
192
|
+
if (w + b >= 1) return map([w / (w + b), w / (w + b), w / (w + b)], srgbLinear);
|
|
193
|
+
|
|
194
|
+
return map(
|
|
195
|
+
map(fromHsl([h, 1, 0.5]), (c) => c * (1 - w - b) + w),
|
|
196
|
+
srgbLinear,
|
|
197
|
+
);
|
|
198
|
+
},
|
|
199
|
+
to(v) {
|
|
200
|
+
const rgb = map(v, srgbGamma);
|
|
201
|
+
|
|
202
|
+
return [toHsl(rgb)[0], Math.min(...rgb), 1 - Math.max(...rgb)];
|
|
203
|
+
},
|
|
204
|
+
hue: { at: 0, powerless: ([, w, b]) => w + b >= 1 - 1e-6 },
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const SRGB: Space = { from: (v) => map(v, srgbLinear), to: (v) => map(v, srgbGamma) };
|
|
208
|
+
|
|
209
|
+
const SPACES = new Map<string, Space>([
|
|
210
|
+
["srgb", SRGB],
|
|
211
|
+
["srgb-linear", { from: (v) => v, to: (v) => v }],
|
|
212
|
+
[
|
|
213
|
+
"display-p3",
|
|
214
|
+
rgbSpace(
|
|
215
|
+
[
|
|
216
|
+
[0.4865709486482162, 0.26566769316909306, 0.1982172852343625],
|
|
217
|
+
[0.2289745640697488, 0.6917385218365064, 0.079286914093745],
|
|
218
|
+
[0, 0.04511338185890264, 1.043944368900976],
|
|
219
|
+
],
|
|
220
|
+
srgbLinear,
|
|
221
|
+
srgbGamma,
|
|
222
|
+
),
|
|
223
|
+
],
|
|
224
|
+
[
|
|
225
|
+
"a98-rgb",
|
|
226
|
+
rgbSpace(
|
|
227
|
+
[
|
|
228
|
+
[0.5766690429101305, 0.1855582379065463, 0.1882286462349947],
|
|
229
|
+
[0.29734497525053605, 0.6273635662554661, 0.0752914584939979],
|
|
230
|
+
[0.02703136138641234, 0.07068885253582723, 0.9913375368376388],
|
|
231
|
+
],
|
|
232
|
+
mirrored((c) => c ** (563 / 256)),
|
|
233
|
+
mirrored((c) => c ** (256 / 563)),
|
|
234
|
+
),
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
"prophoto-rgb",
|
|
238
|
+
rgbSpace(
|
|
239
|
+
product(D65_FROM_D50, [
|
|
240
|
+
[0.7977604896723027, 0.13518583717574031, 0.0313493495815248],
|
|
241
|
+
[0.2880711282292934, 0.7118432178101014, 0.00008565396060525902],
|
|
242
|
+
[0, 0, 0.8251046025104602],
|
|
243
|
+
]),
|
|
244
|
+
mirrored((c) => (c <= 16 / 512 ? c / 16 : c ** 1.8)),
|
|
245
|
+
mirrored((c) => (c < 1 / 512 ? 16 * c : c ** (1 / 1.8))),
|
|
246
|
+
),
|
|
247
|
+
],
|
|
248
|
+
[
|
|
249
|
+
"rec2020",
|
|
250
|
+
rgbSpace(
|
|
251
|
+
[
|
|
252
|
+
[0.6369580483012914, 0.14461690358620832, 0.1688809751641721],
|
|
253
|
+
[0.2627002120112671, 0.6779980715188708, 0.05930171646986196],
|
|
254
|
+
[0, 0.028072693049087428, 1.060985057710791],
|
|
255
|
+
],
|
|
256
|
+
mirrored((c) => (c < REC2020.beta * 4.5 ? c / 4.5 : ((c + REC2020.alpha - 1) / REC2020.alpha) ** (1 / 0.45))),
|
|
257
|
+
mirrored((c) => (c < REC2020.beta ? 4.5 * c : REC2020.alpha * c ** 0.45 - (REC2020.alpha - 1))),
|
|
258
|
+
),
|
|
259
|
+
],
|
|
260
|
+
["xyz", rgbSpace(IDENTITY, same, same)],
|
|
261
|
+
["xyz-d65", rgbSpace(IDENTITY, same, same)],
|
|
262
|
+
["xyz-d50", rgbSpace(D65_FROM_D50, same, same)],
|
|
263
|
+
["lab", lab],
|
|
264
|
+
["lch", polar(lab, 0.0015)],
|
|
265
|
+
["oklab", oklab],
|
|
266
|
+
["oklch", polar(oklab, 0.000004)],
|
|
267
|
+
["hsl", hsl],
|
|
268
|
+
["hwb", hwb],
|
|
269
|
+
]);
|
|
270
|
+
|
|
271
|
+
const CHANNEL = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|turn|grad|rad)?$/i;
|
|
272
|
+
|
|
273
|
+
/** Reads one CSS channel; returns NaN for the missing keyword `none`, or undefined for invalid syntax. */
|
|
274
|
+
function channel(v: string, percent = 1): number | undefined {
|
|
275
|
+
if (v.toLowerCase() === "none") return NaN;
|
|
276
|
+
const m = v.match(CHANNEL);
|
|
277
|
+
|
|
278
|
+
if (!m) return;
|
|
279
|
+
const n = parseFloat(m[1]);
|
|
280
|
+
const unit = m[2]?.toLowerCase();
|
|
281
|
+
|
|
282
|
+
return unit === "%"
|
|
283
|
+
? (n / 100) * percent
|
|
284
|
+
: unit === "turn"
|
|
285
|
+
? n * 360
|
|
286
|
+
: unit === "grad"
|
|
287
|
+
? n * 0.9
|
|
288
|
+
: unit === "rad"
|
|
289
|
+
? (n * 180) / Math.PI
|
|
290
|
+
: n;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
type Channels = (x: string, y: string, z: string) => Vec | undefined;
|
|
294
|
+
|
|
295
|
+
const vec = (
|
|
296
|
+
x: number | undefined,
|
|
297
|
+
y: number | undefined,
|
|
298
|
+
z: number | undefined,
|
|
299
|
+
scale: Vec = [1, 1, 1],
|
|
300
|
+
): Vec | undefined =>
|
|
301
|
+
x === undefined || y === undefined || z === undefined ? undefined : [x / scale[0], y / scale[1], z / scale[2]];
|
|
302
|
+
|
|
303
|
+
const rgbChannels: Channels = (x, y, z) => vec(channel(x, 255), channel(y, 255), channel(z, 255), [255, 255, 255]);
|
|
304
|
+
|
|
305
|
+
const hslChannels: Channels = (x, y, z) => vec(channel(x), channel(y, 100), channel(z, 100), [1, 100, 100]);
|
|
306
|
+
|
|
307
|
+
/** Each color function: the space its coordinates are in, and how it reads them, a percentage of each channel's reference range. */
|
|
308
|
+
const FUNCTIONS = new Map<string, [space: string, read: Channels]>([
|
|
309
|
+
["rgb", ["srgb", rgbChannels]],
|
|
310
|
+
["rgba", ["srgb", rgbChannels]],
|
|
311
|
+
["hsl", ["hsl", hslChannels]],
|
|
312
|
+
["hsla", ["hsl", hslChannels]],
|
|
313
|
+
["hwb", ["hwb", hslChannels]],
|
|
314
|
+
["lab", ["lab", (x, y, z) => vec(channel(x, 100), channel(y, 125), channel(z, 125))]],
|
|
315
|
+
["lch", ["lch", (x, y, z) => vec(channel(x, 100), channel(y, 150), channel(z))]],
|
|
316
|
+
["oklab", ["oklab", (x, y, z) => vec(channel(x), channel(y, 0.4), channel(z, 0.4))]],
|
|
317
|
+
["oklch", ["oklch", (x, y, z) => vec(channel(x), channel(y, 0.4), channel(z))]],
|
|
318
|
+
]);
|
|
319
|
+
|
|
320
|
+
const hex = (h: string): Vec => {
|
|
321
|
+
const byte = (i: number) => parseInt(h.slice(i, i + 2), 16) / 255;
|
|
322
|
+
|
|
323
|
+
return [byte(0), byte(2), byte(4)];
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/** A CSS color: any function or `color()` space CSS Color 4 defines, a hex color, or `transparent`. */
|
|
327
|
+
export function colorOf(css: string): Color | undefined {
|
|
328
|
+
const fn = css.match(/^([a-z]+)\(([^)]+)\)$/i);
|
|
329
|
+
|
|
330
|
+
if (fn) {
|
|
331
|
+
const args = fn[2].split(/[\s,/]+/).filter(Boolean);
|
|
332
|
+
const kind = fn[1].toLowerCase();
|
|
333
|
+
|
|
334
|
+
const [name, read] =
|
|
335
|
+
kind === "color"
|
|
336
|
+
? [
|
|
337
|
+
args.shift()?.toLowerCase() ?? "",
|
|
338
|
+
(x: string, y: string, z: string) => vec(channel(x), channel(y), channel(z)),
|
|
339
|
+
]
|
|
340
|
+
: (FUNCTIONS.get(kind) ?? []);
|
|
341
|
+
|
|
342
|
+
const space = name && SPACES.get(name);
|
|
343
|
+
|
|
344
|
+
if (!space || !read || args.length < 3 || args.length > 4) return;
|
|
345
|
+
const [x, y, z, rawAlpha = "1"] = args;
|
|
346
|
+
const coords = read(x, y, z);
|
|
347
|
+
const alpha = channel(rawAlpha);
|
|
348
|
+
|
|
349
|
+
if (!coords || alpha === undefined) return;
|
|
350
|
+
|
|
351
|
+
return { space, coords, alpha };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const short = css.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i)?.[1];
|
|
355
|
+
|
|
356
|
+
if (short)
|
|
357
|
+
return { space: SRGB, coords: hex(short.length === 3 ? [...short].map((c) => c + c).join("") : short), alpha: 1 };
|
|
358
|
+
|
|
359
|
+
if (css.toLowerCase() === "transparent") return { space: SRGB, coords: [0, 0, 0], alpha: 0 };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** A color as gamma-encoded sRGB from 0 to 1, each channel clipped to the sRGB gamut, as Chromium draws it on an sRGB display. */
|
|
363
|
+
export function srgbOf(c: Color): RGBA {
|
|
364
|
+
const [r, g, b] = map(inSpace(c, SRGB), (v) => Math.min(1, Math.max(0, v || 0)));
|
|
365
|
+
|
|
366
|
+
return { r, g, b, a: Math.min(1, Math.max(0, c.alpha || 0)) };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const HUE_METHODS = ["shorter", "longer", "increasing", "decreasing"] as const;
|
|
370
|
+
|
|
371
|
+
type HueMethod = (typeof HUE_METHODS)[number];
|
|
372
|
+
|
|
373
|
+
/** A color interpolation method, such as `in oklch longer hue`, or nothing for a space or hue method CSS does not define. */
|
|
374
|
+
export function methodOf(space: string, hue = "shorter") {
|
|
375
|
+
const s = SPACES.get(space.toLowerCase());
|
|
376
|
+
const h = HUE_METHODS.find((m) => m === hue.toLowerCase());
|
|
377
|
+
|
|
378
|
+
return s && h && { space: s, hue: h };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export type Method = NonNullable<ReturnType<typeof methodOf>>;
|
|
382
|
+
|
|
383
|
+
/** Hue angles adjusted so a straight line between them takes the path the hue method names. */
|
|
384
|
+
function hues(h1: number, h2: number, method: HueMethod): [number, number] {
|
|
385
|
+
let [a, b] = [turn(h1), turn(h2)];
|
|
386
|
+
const d = b - a;
|
|
387
|
+
|
|
388
|
+
if (method === "shorter") {
|
|
389
|
+
if (d > 180) a += 360;
|
|
390
|
+
else if (d < -180) b += 360;
|
|
391
|
+
} else if (method === "longer") {
|
|
392
|
+
if (d > 0 && d < 180) a += 360;
|
|
393
|
+
else if (d > -180 && d <= 0) b += 360;
|
|
394
|
+
} else if (method === "increasing") {
|
|
395
|
+
if (b < a) b += 360;
|
|
396
|
+
} else if (a < b) a += 360;
|
|
397
|
+
|
|
398
|
+
return [a, b];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* A function from 0 to 1 that blends `from` into `to` as CSS does: in the method's space, with alpha premultiplied, and a hue
|
|
403
|
+
* along the method's path. Missing or powerless channels carry forward from the other color.
|
|
404
|
+
*/
|
|
405
|
+
export function blend(from: Color, to: Color, { space, hue }: Method) {
|
|
406
|
+
const [a, b]: [Vec, Vec] = [[...inSpace(from, space)], [...inSpace(to, space)]];
|
|
407
|
+
const at = space.hue?.at;
|
|
408
|
+
|
|
409
|
+
if (space.hue && at !== undefined) {
|
|
410
|
+
if (space.hue.powerless(a)) a[at] = NaN;
|
|
411
|
+
|
|
412
|
+
if (space.hue.powerless(b)) b[at] = NaN;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
for (let i = 0; i < 3; i++) {
|
|
416
|
+
if (Number.isNaN(a[i]) && !Number.isNaN(b[i])) a[i] = b[i];
|
|
417
|
+
else if (Number.isNaN(b[i]) && !Number.isNaN(a[i])) b[i] = a[i];
|
|
418
|
+
else if (Number.isNaN(a[i])) a[i] = b[i] = 0;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (at !== undefined) [a[at], b[at]] = hues(a[at], b[at], hue);
|
|
422
|
+
const fa = Number.isNaN(from.alpha) ? (Number.isNaN(to.alpha) ? 0 : to.alpha) : from.alpha;
|
|
423
|
+
const ta = Number.isNaN(to.alpha) ? fa : to.alpha;
|
|
424
|
+
|
|
425
|
+
return (t: number): Color => {
|
|
426
|
+
const alpha = fa + (ta - fa) * t;
|
|
427
|
+
|
|
428
|
+
// A clear end has no color of its own, so the blend keeps the other end's color as it fades.
|
|
429
|
+
if (alpha === 0) return { ...(fa ? from : to), alpha: 0 };
|
|
430
|
+
|
|
431
|
+
const mix = (i: number) => (i === at ? a[i] + (b[i] - a[i]) * t : (a[i] * fa * (1 - t) + b[i] * ta * t) / alpha);
|
|
432
|
+
|
|
433
|
+
return { space, coords: [mix(0), mix(1), mix(2)], alpha };
|
|
434
|
+
};
|
|
435
|
+
}
|