@weasel-js/paint 1.5.0 → 1.5.1

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/dist/index.d.ts CHANGED
@@ -9,6 +9,36 @@ interface TextureHandle {
9
9
  readonly id: string;
10
10
  }
11
11
 
12
+ /** Convert an 8-bit sRGB triple to OKLab. */
13
+ declare function srgbU8ToOklab(r: number, g: number, b: number): [number, number, number];
14
+ /** Convert OKLab back to an 8-bit sRGB triple, clamped into gamut. */
15
+ declare function oklabToSrgbU8(L: number, A: number, B: number): [number, number, number];
16
+ /** Blend two 8-bit sRGB colors through OKLab. Perceptually even, and it does
17
+ * not pass through the muddy midpoints an sRGB blend produces between
18
+ * complementary hues. */
19
+ declare function lerpOklab(from: readonly [number, number, number], to: readonly [number, number, number], t: number): [number, number, number];
20
+ /** Convert OKLab (L, a, b) to OKLCh (L, C, h). h is in radians, range [-π, π]. */
21
+ declare function oklabToOklch(L: number, A: number, B: number): [number, number, number];
22
+ /** Convert OKLCh (L, C, h) to OKLab (L, a, b). h is in radians. */
23
+ declare function oklchToOklab(L: number, C: number, h: number): [number, number, number];
24
+ /** Lerp OKLCh values with shortest-arc hue interpolation. Lightness and
25
+ * chroma lerp linearly. When chroma is near zero on either endpoint, hue
26
+ * is taken from the other endpoint (avoids hue snap-from-undefined).
27
+ * h inputs are in radians. */
28
+ declare function lerpOklch(from: readonly [number, number, number], to: readonly [number, number, number], t: number): [number, number, number];
29
+ /** Which space a color blend is computed in. `'rgb'` is cheapest; `'oklab'`
30
+ * is perceptually even; `'oklch'` additionally travels around the hue wheel
31
+ * rather than through it. */
32
+ type ColorSpace = 'rgb' | 'oklab' | 'oklch';
33
+ /** Lerp a flat RGBA color array `from` toward `to`. Inputs are 0..1
34
+ * floats (the renderer's color space); outputs are 0..1 floats too.
35
+ * Alpha is always linearly lerped; RGB channels are lerped in the
36
+ * requested color space. OKLab / OKLCh paths bounce through u8 because
37
+ * the gamma-correction LUT and gamut-clip routines are byte-keyed —
38
+ * the round-trip is lossy at <1% per channel, well under the visible
39
+ * threshold for these animations. */
40
+ declare function lerpColorArray(from: readonly number[], to: readonly number[], t: number, space?: ColorSpace): number[];
41
+
12
42
  /**
13
43
  * FillStyle and Stroke types — the unified shape for "what color or texture
14
44
  * paints these pixels," modeled on SVG's paint-server concept.
@@ -39,6 +69,12 @@ interface TextureHandle {
39
69
  * `createTilePattern()`. A handle is a session-scoped registry key, so a
40
70
  * paint carrying one cannot be persisted or exported; prefer the spec.
41
71
  *
72
+ * `interpolate` on a gradient names the space its stops blend through, and
73
+ * defaults to `'rgb'` — the channel-wise sRGB lerp every other vector format
74
+ * means by a gradient. `'oklab'` spaces the blend perceptually; `'oklch'`
75
+ * additionally travels around the hue wheel, so red to blue stays saturated
76
+ * instead of passing through a muddy purple. Alpha is linear in every space.
77
+ *
42
78
  * `units` on a pattern names the space the tile's origin and scale live in,
43
79
  * not the space of any geometry (a pattern has none). Under `'bounds'` the
44
80
  * tile anchors to the painted node's box, so the pattern travels with the
@@ -71,6 +107,7 @@ type FillStyle = {
71
107
  };
72
108
  stops: GradStop[];
73
109
  units?: GradientUnits;
110
+ interpolate?: ColorSpace;
74
111
  opacity?: number;
75
112
  } | {
76
113
  fill: 'radial-gradient';
@@ -81,6 +118,7 @@ type FillStyle = {
81
118
  radius: number;
82
119
  stops: GradStop[];
83
120
  units?: GradientUnits;
121
+ interpolate?: ColorSpace;
84
122
  opacity?: number;
85
123
  } | {
86
124
  fill: 'conic-gradient';
@@ -91,6 +129,7 @@ type FillStyle = {
91
129
  angle: number;
92
130
  stops: GradStop[];
93
131
  units?: GradientUnits;
132
+ interpolate?: ColorSpace;
94
133
  opacity?: number;
95
134
  };
96
135
  /**
@@ -184,10 +223,23 @@ type MarkerKey = KitMarkerKey | (string & {});
184
223
  */
185
224
  type MarkerRef = MarkerKey | {
186
225
  key: MarkerKey;
187
- size?: number | {
188
- px: number;
189
- };
226
+ size?: ScreenLength;
227
+ };
228
+ /**
229
+ * A length the author states either in world units (a plain number) or in
230
+ * screen pixels (`{ px }`), resolved against the accumulated transform scale
231
+ * wherever it is consumed — so a `{ px }` length holds its on-screen size as
232
+ * the view zooms, with no call-site arithmetic.
233
+ */
234
+ type ScreenLength = number | {
235
+ px: number;
190
236
  };
237
+ /**
238
+ * Resolve a {@link ScreenLength} to world units. A non-finite or zero scale
239
+ * has no world to divide into, so the pixel count passes through unchanged
240
+ * rather than becoming `Infinity`.
241
+ */
242
+ declare function resolveScreenLength(value: ScreenLength, scale: number): number;
191
243
  /** Stroke style: a FillStyle plus structural line parameters. */
192
244
  interface Stroke {
193
245
  /**
@@ -200,9 +252,7 @@ interface Stroke {
200
252
  /** World units, or `{ px }` for screen pixels — resolved against the
201
253
  * accumulated transform scale at draw time, so it holds its on-screen
202
254
  * thickness as the view zooms. */
203
- width?: number | {
204
- px: number;
205
- };
255
+ width?: ScreenLength;
206
256
  /** Per `CanvasRenderingContext2D.setLineDash` — empty/omitted = solid. */
207
257
  dash?: number[];
208
258
  /** Marker at the first vertex of each open subpath, rotated to point back
@@ -319,4 +369,4 @@ interface Region {
319
369
  shape: 'rectangle' | 'circle';
320
370
  }
321
371
 
322
- export { type FillStyle, type GradStop, type GradientFill, type GradientKind, type GradientUnits, type KitMarkerKey, type MarkerKey, type MarkerRef, type Region, STROKE_DASH_RATIOS, type Stroke, type StrokeAlign, type StrokeDashStyle, type TextureHandle, type TilePatternSpec, alignedStrokeRect, dashForStrokeStyle, strokeDashStyleOf };
372
+ export { type ColorSpace, type FillStyle, type GradStop, type GradientFill, type GradientKind, type GradientUnits, type KitMarkerKey, type MarkerKey, type MarkerRef, type Region, STROKE_DASH_RATIOS, type ScreenLength, type Stroke, type StrokeAlign, type StrokeDashStyle, type TextureHandle, type TilePatternSpec, alignedStrokeRect, dashForStrokeStyle, lerpColorArray, lerpOklab, lerpOklch, oklabToOklch, oklabToSrgbU8, oklchToOklab, resolveScreenLength, srgbU8ToOklab, strokeDashStyleOf };
package/dist/index.js CHANGED
@@ -1,4 +1,9 @@
1
1
  // src/paint.ts
2
+ function resolveScreenLength(value, scale) {
3
+ if (typeof value === "number") return value;
4
+ if (!Number.isFinite(scale) || scale === 0) return value.px;
5
+ return value.px / scale;
6
+ }
2
7
  function alignedStrokeRect(rect, align, width = 1) {
3
8
  if (align === "center") return rect;
4
9
  const sign = align === "inner" ? -1 : 1;
@@ -35,6 +40,151 @@ function strokeDashStyleOf(dash, width) {
35
40
  return "custom";
36
41
  }
37
42
 
38
- export { STROKE_DASH_RATIOS, alignedStrokeRect, dashForStrokeStyle, strokeDashStyleOf };
43
+ // src/colorSpaces.ts
44
+ var SRGB_TO_LINEAR = (() => {
45
+ const lut = new Float64Array(256);
46
+ for (let i = 0; i < 256; i++) {
47
+ const c = i / 255;
48
+ lut[i] = c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
49
+ }
50
+ return lut;
51
+ })();
52
+ function linearToSrgbByte(c) {
53
+ if (c <= 0) return 0;
54
+ if (c >= 1) return 255;
55
+ const v = c <= 31308e-7 ? c * 12.92 : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
56
+ return Math.round(v * 255);
57
+ }
58
+ function srgbU8ToOklab(r, g, b) {
59
+ const rl = SRGB_TO_LINEAR[r & 255];
60
+ const gl = SRGB_TO_LINEAR[g & 255];
61
+ const bl = SRGB_TO_LINEAR[b & 255];
62
+ const l = 0.4122214708 * rl + 0.5363325363 * gl + 0.0514459929 * bl;
63
+ const m = 0.2119034982 * rl + 0.6806995451 * gl + 0.1073969566 * bl;
64
+ const s = 0.0883024619 * rl + 0.2817188376 * gl + 0.6299787005 * bl;
65
+ const lp = Math.cbrt(l);
66
+ const mp = Math.cbrt(m);
67
+ const sp = Math.cbrt(s);
68
+ return [
69
+ 0.2104542553 * lp + 0.793617785 * mp - 0.0040720468 * sp,
70
+ 1.9779984951 * lp - 2.428592205 * mp + 0.4505937099 * sp,
71
+ 0.0259040371 * lp + 0.7827717662 * mp - 0.808675766 * sp
72
+ ];
73
+ }
74
+ function oklabToLinearSrgb(L, A, B) {
75
+ const lp = L + 0.3963377774 * A + 0.2158037573 * B;
76
+ const mp = L - 0.1055613458 * A - 0.0638541728 * B;
77
+ const sp = L - 0.0894841775 * A - 1.291485548 * B;
78
+ const l = lp * lp * lp;
79
+ const m = mp * mp * mp;
80
+ const s = sp * sp * sp;
81
+ return [
82
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
83
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
84
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s
85
+ ];
86
+ }
87
+ function inGamut(rl, gl, bl) {
88
+ const eps = 1e-6;
89
+ return rl >= -eps && rl <= 1 + eps && gl >= -eps && gl <= 1 + eps && bl >= -eps && bl <= 1 + eps;
90
+ }
91
+ function clipToGamut(L, A, B) {
92
+ let [rl, gl, bl] = oklabToLinearSrgb(L, A, B);
93
+ if (inGamut(rl, gl, bl)) return [rl, gl, bl];
94
+ let lo = 0;
95
+ let hi = 1;
96
+ for (let i = 0; i < 5; i++) {
97
+ const t = (lo + hi) / 2;
98
+ [rl, gl, bl] = oklabToLinearSrgb(L, A * t, B * t);
99
+ if (inGamut(rl, gl, bl)) lo = t;
100
+ else hi = t;
101
+ }
102
+ [rl, gl, bl] = oklabToLinearSrgb(L, A * lo, B * lo);
103
+ return [rl, gl, bl];
104
+ }
105
+ function oklabToSrgbU8(L, A, B) {
106
+ const [rl, gl, bl] = clipToGamut(L, A, B);
107
+ return [linearToSrgbByte(rl), linearToSrgbByte(gl), linearToSrgbByte(bl)];
108
+ }
109
+ function lerpOklab(from, to, t) {
110
+ return [
111
+ from[0] + (to[0] - from[0]) * t,
112
+ from[1] + (to[1] - from[1]) * t,
113
+ from[2] + (to[2] - from[2]) * t
114
+ ];
115
+ }
116
+ function oklabToOklch(L, A, B) {
117
+ const C = Math.hypot(A, B);
118
+ const h = Math.atan2(B, A);
119
+ return [L, C, h];
120
+ }
121
+ function oklchToOklab(L, C, h) {
122
+ return [L, C * Math.cos(h), C * Math.sin(h)];
123
+ }
124
+ function lerpOklch(from, to, t) {
125
+ const [L1, C1, h1Raw] = from;
126
+ const [L2, C2, h2Raw] = to;
127
+ const C_EPS = 1e-4;
128
+ const h1 = C1 < C_EPS ? h2Raw : h1Raw;
129
+ const h2 = C2 < C_EPS ? h1Raw : h2Raw;
130
+ const TWO_PI = Math.PI * 2;
131
+ let dh = h2 - h1;
132
+ while (dh > Math.PI) dh -= TWO_PI;
133
+ while (dh < -Math.PI) dh += TWO_PI;
134
+ return [
135
+ L1 + (L2 - L1) * t,
136
+ C1 + (C2 - C1) * t,
137
+ h1 + dh * t
138
+ ];
139
+ }
140
+ var f2u = (v) => Math.max(0, Math.min(255, Math.round(v * 255)));
141
+ var u2f = (v) => v / 255;
142
+ function lerpColorArray(from, to, t, space = "rgb") {
143
+ if (from.length !== to.length) {
144
+ throw new Error(`lerpColorArray: length mismatch (from=${from.length}, to=${to.length})`);
145
+ }
146
+ if (from.length % 4 !== 0) {
147
+ throw new Error(`lerpColorArray: length ${from.length} not divisible by 4`);
148
+ }
149
+ const out = new Array(from.length);
150
+ const n = from.length / 4;
151
+ if (space === "rgb") {
152
+ for (let i = 0; i < from.length; i++) {
153
+ out[i] = from[i] + (to[i] - from[i]) * t;
154
+ }
155
+ return out;
156
+ }
157
+ if (space === "oklch") {
158
+ for (let i = 0; i < n; i++) {
159
+ const k = i * 4;
160
+ const fLab = srgbU8ToOklab(f2u(from[k]), f2u(from[k + 1]), f2u(from[k + 2]));
161
+ const tLab = srgbU8ToOklab(f2u(to[k]), f2u(to[k + 1]), f2u(to[k + 2]));
162
+ const fLch = oklabToOklch(fLab[0], fLab[1], fLab[2]);
163
+ const tLch = oklabToOklch(tLab[0], tLab[1], tLab[2]);
164
+ const midLch = lerpOklch(fLch, tLch, t);
165
+ const midLab = oklchToOklab(midLch[0], midLch[1], midLch[2]);
166
+ const [r, g, b] = oklabToSrgbU8(midLab[0], midLab[1], midLab[2]);
167
+ out[k] = u2f(r);
168
+ out[k + 1] = u2f(g);
169
+ out[k + 2] = u2f(b);
170
+ out[k + 3] = from[k + 3] + (to[k + 3] - from[k + 3]) * t;
171
+ }
172
+ return out;
173
+ }
174
+ for (let i = 0; i < n; i++) {
175
+ const k = i * 4;
176
+ const fLab = srgbU8ToOklab(f2u(from[k]), f2u(from[k + 1]), f2u(from[k + 2]));
177
+ const tLab = srgbU8ToOklab(f2u(to[k]), f2u(to[k + 1]), f2u(to[k + 2]));
178
+ const mid = lerpOklab(fLab, tLab, t);
179
+ const [r, g, b] = oklabToSrgbU8(mid[0], mid[1], mid[2]);
180
+ out[k] = u2f(r);
181
+ out[k + 1] = u2f(g);
182
+ out[k + 2] = u2f(b);
183
+ out[k + 3] = from[k + 3] + (to[k + 3] - from[k + 3]) * t;
184
+ }
185
+ return out;
186
+ }
187
+
188
+ export { STROKE_DASH_RATIOS, alignedStrokeRect, dashForStrokeStyle, lerpColorArray, lerpOklab, lerpOklch, oklabToOklch, oklabToSrgbU8, oklchToOklab, resolveScreenLength, srgbU8ToOklab, strokeDashStyleOf };
39
189
  //# sourceMappingURL=index.js.map
40
190
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/paint.ts"],"names":[],"mappings":";AA+NO,SAAS,iBAAA,CACd,IAAA,EACA,KAAA,EACA,KAAA,GAAQ,CAAA,EACiD;AACzD,EAAA,IAAI,KAAA,KAAU,UAAU,OAAO,IAAA;AAG/B,EAAA,MAAM,IAAA,GAAO,KAAA,KAAU,OAAA,GAAU,EAAA,GAAK,CAAA;AACtC,EAAA,MAAM,CAAA,GAAK,OAAO,KAAA,GAAS,CAAA;AAC3B,EAAA,OAAO;AAAA,IACL,CAAA,EAAG,KAAK,CAAA,GAAI,CAAA;AAAA,IACZ,CAAA,EAAG,KAAK,CAAA,GAAI,CAAA;AAAA,IACZ,KAAA,EAAO,IAAA,C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,IAAA,CAAK,CAAC,CAAA,GAAI,CAAA,GAAI,CAAC,CAAA,IAAK,CAAA,GAAI,IAAI,CAAA,EAAG;AAClG,MAAA,OAAO,KAAA;AAAA,IACT;AAAA,EACF;AACA,EAAA,OAAO,QAAA;AACT","file":"index.js","sourcesContent":["/**\n * FillStyle and Stroke types — the unified shape for \"what color or texture\n * paints these pixels,\" modeled on SVG's paint-server concept.\n *\n * - `FillStyle` is a tagged union: solid color, pattern, or gradient. Used\n * wherever a kit option previously took `fillStyle: string`.\n * - `Stroke` pairs a `FillStyle` with structural stroke parameters (width, dash,\n * line cap/join, alignment).\n * - These types are consumed by the GL renderer's DrawCommand path fills\n * and strokes.\n *\n * The 2D `applyPaint` / `applyStroke` / `renderFilledRegion` helpers that\n * formerly lived alongside these types were deleted with the 2D backend in\n * Step 10. `alignedStrokeRect` survives as a pure geometry helper used by\n * path tessellation and the selection overlay.\n */\n\nimport type { TextureHandle } from './texture';\n\n/**\n * Color/texture strategy for fills (and, via `Stroke.paint`, strokes).\n *\n * `fill` is optional and defaults to `'solid'` — `{ color: '#abc' }` is\n * equivalent to `{ fill: 'solid', color: '#abc' }`. Pattern paints must set\n * `fill: 'pattern'` explicitly.\n *\n * The `'pattern'` variant's payload is either a `TilePatternSpec` — plain\n * data naming one of the built-in tiles, which survives serialization — or a\n * `TextureHandle` for a tile the consumer built itself via\n * `createTilePattern()`. A handle is a session-scoped registry key, so a\n * paint carrying one cannot be persisted or exported; prefer the spec.\n *\n * `units` on a pattern names the space the tile's origin and scale live in,\n * not the space of any geometry (a pattern has none). Under `'bounds'` the\n * tile anchors to the painted node's box, so the pattern travels with the\n * node and a resize reveals more tiles rather than stretching them;\n * `fillInPoseFrame` resolves that to a `'local'` paint with an explicit\n * `origin` before the renderer sees it.\n */\nexport type FillStyle =\n | { fill?: 'solid'; color: string; opacity?: number }\n | { fill: 'pattern'; pattern: TextureHandle | TilePatternSpec; units?: GradientUnits; origin?: { x: number; y: number }; opacity?: number }\n | { fill: 'linear-gradient'; from: { x: number; y: number }; to: { x: number; y: number }; stops: GradStop[]; units?: GradientUnits; opacity?: number }\n | { fill: 'radial-gradient'; center: { x: number; y: number }; radius: number; stops: GradStop[]; units?: GradientUnits; opacity?: number }\n | { fill: 'conic-gradient'; center: { x: number; y: number }; angle: number; stops: GradStop[]; units?: GradientUnits; opacity?: number };\n\n/**\n * Which coordinate space a gradient's geometry (`from`/`to`, `center`,\n * `radius`) is expressed in. SVG's `gradientUnits`, plus an option for\n * paints that are themselves viewport furniture.\n *\n * - `'bounds'`: fractions of the painted node's bounding box, `0..1` on each\n * axis — SVG `objectBoundingBox`. Resolved by the node painter, before the\n * renderer sees it, so the paint follows the node through moves, resizes\n * and rotation. What a gradient on a scene node wants.\n * - `'local'`: the coordinate space the geometry was handed to the renderer\n * in — the enclosing group's frame. For draw commands a consumer builds\n * itself, where \"the coordinates I just wrote\" is the useful frame.\n * - `'world'`: scene coordinates. The paint stays put under pan and zoom\n * while the geometry moves through it. SVG `userSpaceOnUse`. Requires the\n * renderer to have been handed a view matrix; falls back to `'screen'`\n * when it has not.\n * - `'screen'`: CSS pixels of the drawing surface. For overlays and\n * viewport-fixed washes that should not move with the content at all.\n *\n * Defaults to `'screen'`, which is the behavior every gradient had before\n * this field existed.\n */\nexport type GradientUnits = 'bounds' | 'local' | 'world' | 'screen';\n\n/**\n * A built-in tile, described as plain data. The serializable half of the\n * pattern paint: `resolvePatternSpec()` turns one into a `TextureHandle`\n * at paint time, memoized so identical specs share a texture.\n *\n * `size` is the tile's edge length, and doubles as its extent in paint\n * space — a bigger `size` rasterizes a bigger tile rather than magnifying\n * a small one, which is why there is no separate scale field.\n */\nexport interface TilePatternSpec {\n tile: 'hatch' | 'crosshatch' | 'dots' | 'chunks';\n color: string;\n /** `chunks` only — omit for a transparent tile background. */\n bg?: string;\n size?: number;\n /** `hatch` / `crosshatch`. */\n lineWidth?: number;\n /** `dots`. */\n radius?: number;\n /** `chunks`. */\n density?: number;\n chunkSize?: number;\n seed?: number;\n}\n\n/** A single color stop within a gradient. `offset` is in 0..1. */\nexport interface GradStop {\n offset: number;\n color: string;\n}\n\n/** The gradient members of `FillStyle`, as one type. What a gradient editor\n * edits, and what the gradient-specific helpers accept. */\nexport type GradientFill = Extract<\n FillStyle,\n { fill: 'linear-gradient' | 'radial-gradient' | 'conic-gradient' }\n>;\n\n/** `GradientFill['fill']` — the three gradient discriminants on their own. */\nexport type GradientKind = GradientFill['fill'];\n\n/**\n * Where a stroke sits relative to the geometric edge it strokes.\n *\n * - `'center'` (default): canvas-native — half the stroke width sits inside\n * the geometry, half outside.\n * - `'inner'`: the entire stroke lies inside the geometry. The outer edge of\n * the stroke coincides with the geometric edge.\n * - `'outer'`: the entire stroke lies outside the geometry. The inner edge\n * of the stroke coincides with the geometric edge.\n *\n * Mirrors the (proposed) SVG `stroke-alignment` property. Honoring `inner`\n * or `outer` is the renderer's responsibility — for axis-aligned rects, the\n * kit shifts coordinates by `width / 2`. For arbitrary paths, renderers\n * typically use a stencil mask of the stroked path against the geometry.\n */\nexport type StrokeAlign = 'center' | 'inner' | 'outer';\n\n/**\n * The kit's built-in marker vocabulary, open on the string the way\n * `PaintKind` is so a consumer's registered key typechecks.\n */\nexport type KitMarkerKey =\n | 'arrow' | 'arrow-open' | 'arrow-concave'\n | 'diamond' | 'diamond-hollow'\n | 'circle' | 'square' | 'bar';\nexport type MarkerKey = KitMarkerKey | (string & {});\n\n/**\n * A marker on one end (or every interior vertex) of a stroke.\n *\n * `size` reuses `Stroke.width`'s unit system: a bare number scales with the\n * resolved stroke width, `{ px }` pins screen pixels. That is SVG's\n * `markerUnits` in the idiom this codebase already resolves at draw time.\n * Omitted, the marker is one stroke width per unit.\n */\nexport type MarkerRef = MarkerKey | { key: MarkerKey; size?: number | { px: number } };\n\n/** Stroke style: a FillStyle plus structural line parameters. */\nexport interface Stroke {\n /**\n * Omitted, the stroke paints nothing — the same \"no paint\" a `fill: null`\n * says for a node's fill. Documents written before mid-2026 can hold a\n * stroke whose other fields were seeded without one, and the type is\n * optional so every reader has to answer for that rather than throw on it.\n */\n paint?: FillStyle;\n /** World units, or `{ px }` for screen pixels — resolved against the\n * accumulated transform scale at draw time, so it holds its on-screen\n * thickness as the view zooms. */\n width?: number | { px: number };\n /** Per `CanvasRenderingContext2D.setLineDash` — empty/omitted = solid. */\n dash?: number[];\n /** Marker at the first vertex of each open subpath, rotated to point back\n * along the line (SVG's `auto-start-reverse`, as the only behavior). */\n markerStart?: MarkerRef;\n /** Marker at every interior authored vertex, on the bisector of the\n * incoming and outgoing directions. Never insets the stroke. */\n markerMid?: MarkerRef;\n /** Marker at the last vertex of each open subpath. */\n markerEnd?: MarkerRef;\n cap?: 'butt' | 'round' | 'square';\n join?: 'miter' | 'round' | 'bevel';\n /**\n * Miter join fallback threshold. When the miter length exceeds\n * `miterLimit * width / 2`, the join falls back to a bevel. Default 4,\n * matching SVG — which is also what the kit's own serializer implies when\n * it omits the attribute for an unset field. Canvas2D's 10 lets an acute\n * corner throw a spike four times the half-width.\n */\n miterLimit?: number;\n /** Where the stroke sits relative to the geometric edge. Default `'center'`. */\n align?: StrokeAlign;\n /**\n * Per-anchor RGBA, flat (length = 4 × countPathAnchors(path)). Each\n * value in 0..1. Arc-length interpolated across the tessellated ribbon\n * between consecutive anchors. When set, `paint` is still required —\n * its `opacity` (and color, as a placeholder) flow through the shader.\n */\n vertexColors?: number[];\n /**\n * Per-anchor stroke width (length = `countPathAnchors(path)`). When set,\n * the tessellator interpolates half-widths along each segment to produce\n * a tapered ribbon. `width` is used as the fallback for any anchor whose\n * entry is missing or non-finite. Pressure-driven pencil strokes use\n * this; pair with `pressureToWidth` to derive widths from stylus input.\n *\n * Joins between adjacent segments whose widths differ by more than\n * `varyingWidthJoinThreshold` (default 1.5×) are forced to bevel\n * regardless of the `join` setting — miter math is unstable when widths\n * vary across the corner; smooth round joins with mismatched widths\n * are a future enhancement.\n */\n vertexWidths?: number[];\n /**\n * Max width ratio (greater / lesser) at which a non-bevel join is\n * preserved when `vertexWidths` causes adjacent segments to differ.\n * Beyond this ratio the join falls back to bevel. Default 1.5. Ignored\n * when `vertexWidths` is absent.\n */\n varyingWidthJoinThreshold?: number;\n}\n\n/**\n * Inflate (positive) or deflate (negative) a rect to honor `align` when\n * stroking it. Returns the rect to pass to a stroked-rect renderer. `width`\n * is the stroke width (defaults to 1 to match canvas).\n *\n * Pure geometry helper — no rendering side effects. Used by path\n * tessellation and the selection overlay to produce a rect whose\n * center-aligned stroke visually coincides with the requested\n * inner/outer-aligned stroke of the original rect.\n */\nexport function alignedStrokeRect(\n rect: { x: number; y: number; width: number; height: number },\n align: StrokeAlign,\n width = 1,\n): { x: number; y: number; width: number; height: number } {\n if (align === 'center') return rect;\n // For 'inner', shift inward by width/2 so the stroke's outer edge coincides\n // with the geometric edge. For 'outer', shift outward.\n const sign = align === 'inner' ? -1 : 1;\n const d = (sign * width) / 2;\n return {\n x: rect.x - d,\n y: rect.y - d,\n width: rect.width + 2 * d,\n height: rect.height + 2 * d,\n };\n}\n\n/**\n * The named line styles a `Stroke.dash` array reads as.\n *\n * `custom` is what an imported array that matches no preset reads as — it is\n * reportable, not authorable: there is no array it maps back to.\n */\nexport type StrokeDashStyle = 'solid' | 'dashed' | 'dotted' | 'custom';\n\n/**\n * Dash and gap lengths of the presets, **as multiples of the stroke width**.\n *\n * SVG dash lengths are absolute, so a fixed pattern is a different style at\n * every width: `[6, 3]` is dots on a hairline and a railroad on a 20px\n * stroke. Scaling by the width is what makes \"dashed\" one style.\n */\nexport const STROKE_DASH_RATIOS = {\n dashed: [3, 2],\n dotted: [1, 2],\n} as const satisfies Record<'dashed' | 'dotted', readonly [number, number]>;\n\n/** `width` as a plain number — a `{ px }` width is read at scale 1, matching\n * what an unresolved stroke reaching the tessellator gets. */\nfunction dashWidth(width: number | { px: number } | undefined): number {\n const w = typeof width === 'object' ? width.px : (width ?? 1);\n return Number.isFinite(w) && w > 0 ? w : 1;\n}\n\n/**\n * The `Stroke.dash` array for a named style at `width`, or `undefined` for\n * `solid` — which is stored as no dash at all, not as an empty pattern.\n *\n * `custom` has no array of its own and returns `undefined`; a caller that\n * offers it as a choice should refuse the choice rather than call this.\n */\nexport function dashForStrokeStyle(\n style: StrokeDashStyle,\n width: number | { px: number } | undefined,\n): number[] | undefined {\n if (style !== 'dashed' && style !== 'dotted') return undefined;\n const w = dashWidth(width);\n return STROKE_DASH_RATIOS[style].map((r) => r * w);\n}\n\n/**\n * The style a stored `dash` reads as at `width`. Absent or empty is `solid`;\n * an array matching neither preset is `custom`.\n */\nexport function strokeDashStyleOf(\n dash: readonly number[] | undefined,\n width: number | { px: number } | undefined,\n): StrokeDashStyle {\n if (dash === undefined || dash.length === 0 || dash.every((v) => v === 0)) return 'solid';\n const w = dashWidth(width);\n for (const style of ['dashed', 'dotted'] as const) {\n const preset = STROKE_DASH_RATIOS[style];\n // Tolerance is relative to the width: the presets are multiples of it, and\n // a round-tripped array carries the serializer's decimal trimming.\n if (dash.length === preset.length && preset.every((r, i) => Math.abs(dash[i] - r * w) <= w * 1e-3)) {\n return style;\n }\n }\n return 'custom';\n}\n\n/** Region a fill is clipped to. */\nexport interface Region {\n x: number;\n y: number;\n w: number;\n h: number;\n shape: 'rectangle' | 'circle';\n}\n"]}
1
+ {"version":3,"sources":["../src/paint.ts","../src/colorSpaces.ts"],"names":[],"mappings":";AAwKO,SAAS,mBAAA,CAAoB,OAAqB,KAAA,EAAuB;AAC9E,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,EAAU,OAAO,KAAA;AACtC,EAAA,IAAI,CAAC,OAAO,QAAA,CAAS,KAAK,KAAK,KAAA,KAAU,CAAA,SAAU,KAAA,CAAM,EAAA;AACzD,EAAA,OAAO,MAAM,EAAA,GAAK,KAAA;AACpB;AA6EO,SAAS,iBAAA,CACd,IAAA,EACA,KAAA,EACA,KAAA,GAAQ,CAAA,EACiD;AACzD,EAAA,IAAI,KAAA,KAAU,UAAU,OAAO,IAAA;AAG/B,EAAA,MAAM,IA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B,IAAA,GAAA,CAAI,CAAA,GAAI,CAAC,CAAA,GAAI,GAAA,CAAI,CAAC,CAAA;AAClB,IAAA,GAAA,CAAI,CAAA,GAAI,CAAC,CAAA,GAAI,IAAA,CAAK,IAAI,CAAC,CAAA,GAAA,CAAK,EAAA,CAAG,CAAA,GAAI,CAAC,CAAA,GAAI,IAAA,CAAK,CAAA,GAAI,CAAC,CAAA,IAAK,CAAA;AAAA,EACzD;AACA,EAAA,OAAO,GAAA;AACT","file":"index.js","sourcesContent":["/**\n * FillStyle and Stroke types — the unified shape for \"what color or texture\n * paints these pixels,\" modeled on SVG's paint-server concept.\n *\n * - `FillStyle` is a tagged union: solid color, pattern, or gradient. Used\n * wherever a kit option previously took `fillStyle: string`.\n * - `Stroke` pairs a `FillStyle` with structural stroke parameters (width, dash,\n * line cap/join, alignment).\n * - These types are consumed by the GL renderer's DrawCommand path fills\n * and strokes.\n *\n * The 2D `applyPaint` / `applyStroke` / `renderFilledRegion` helpers that\n * formerly lived alongside these types were deleted with the 2D backend in\n * Step 10. `alignedStrokeRect` survives as a pure geometry helper used by\n * path tessellation and the selection overlay.\n */\n\nimport type { TextureHandle } from './texture';\nimport type { ColorSpace } from './colorSpaces';\n\n/**\n * Color/texture strategy for fills (and, via `Stroke.paint`, strokes).\n *\n * `fill` is optional and defaults to `'solid'` — `{ color: '#abc' }` is\n * equivalent to `{ fill: 'solid', color: '#abc' }`. Pattern paints must set\n * `fill: 'pattern'` explicitly.\n *\n * The `'pattern'` variant's payload is either a `TilePatternSpec` — plain\n * data naming one of the built-in tiles, which survives serialization — or a\n * `TextureHandle` for a tile the consumer built itself via\n * `createTilePattern()`. A handle is a session-scoped registry key, so a\n * paint carrying one cannot be persisted or exported; prefer the spec.\n *\n * `interpolate` on a gradient names the space its stops blend through, and\n * defaults to `'rgb'` — the channel-wise sRGB lerp every other vector format\n * means by a gradient. `'oklab'` spaces the blend perceptually; `'oklch'`\n * additionally travels around the hue wheel, so red to blue stays saturated\n * instead of passing through a muddy purple. Alpha is linear in every space.\n *\n * `units` on a pattern names the space the tile's origin and scale live in,\n * not the space of any geometry (a pattern has none). Under `'bounds'` the\n * tile anchors to the painted node's box, so the pattern travels with the\n * node and a resize reveals more tiles rather than stretching them;\n * `fillInPoseFrame` resolves that to a `'local'` paint with an explicit\n * `origin` before the renderer sees it.\n */\nexport type FillStyle =\n | { fill?: 'solid'; color: string; opacity?: number }\n | { fill: 'pattern'; pattern: TextureHandle | TilePatternSpec; units?: GradientUnits; origin?: { x: number; y: number }; opacity?: number }\n | { fill: 'linear-gradient'; from: { x: number; y: number }; to: { x: number; y: number }; stops: GradStop[]; units?: GradientUnits; interpolate?: ColorSpace; opacity?: number }\n | { fill: 'radial-gradient'; center: { x: number; y: number }; radius: number; stops: GradStop[]; units?: GradientUnits; interpolate?: ColorSpace; opacity?: number }\n | { fill: 'conic-gradient'; center: { x: number; y: number }; angle: number; stops: GradStop[]; units?: GradientUnits; interpolate?: ColorSpace; opacity?: number };\n\n/**\n * Which coordinate space a gradient's geometry (`from`/`to`, `center`,\n * `radius`) is expressed in. SVG's `gradientUnits`, plus an option for\n * paints that are themselves viewport furniture.\n *\n * - `'bounds'`: fractions of the painted node's bounding box, `0..1` on each\n * axis — SVG `objectBoundingBox`. Resolved by the node painter, before the\n * renderer sees it, so the paint follows the node through moves, resizes\n * and rotation. What a gradient on a scene node wants.\n * - `'local'`: the coordinate space the geometry was handed to the renderer\n * in — the enclosing group's frame. For draw commands a consumer builds\n * itself, where \"the coordinates I just wrote\" is the useful frame.\n * - `'world'`: scene coordinates. The paint stays put under pan and zoom\n * while the geometry moves through it. SVG `userSpaceOnUse`. Requires the\n * renderer to have been handed a view matrix; falls back to `'screen'`\n * when it has not.\n * - `'screen'`: CSS pixels of the drawing surface. For overlays and\n * viewport-fixed washes that should not move with the content at all.\n *\n * Defaults to `'screen'`, which is the behavior every gradient had before\n * this field existed.\n */\nexport type GradientUnits = 'bounds' | 'local' | 'world' | 'screen';\n\n/**\n * A built-in tile, described as plain data. The serializable half of the\n * pattern paint: `resolvePatternSpec()` turns one into a `TextureHandle`\n * at paint time, memoized so identical specs share a texture.\n *\n * `size` is the tile's edge length, and doubles as its extent in paint\n * space — a bigger `size` rasterizes a bigger tile rather than magnifying\n * a small one, which is why there is no separate scale field.\n */\nexport interface TilePatternSpec {\n tile: 'hatch' | 'crosshatch' | 'dots' | 'chunks';\n color: string;\n /** `chunks` only — omit for a transparent tile background. */\n bg?: string;\n size?: number;\n /** `hatch` / `crosshatch`. */\n lineWidth?: number;\n /** `dots`. */\n radius?: number;\n /** `chunks`. */\n density?: number;\n chunkSize?: number;\n seed?: number;\n}\n\n/** A single color stop within a gradient. `offset` is in 0..1. */\nexport interface GradStop {\n offset: number;\n color: string;\n}\n\n/** The gradient members of `FillStyle`, as one type. What a gradient editor\n * edits, and what the gradient-specific helpers accept. */\nexport type GradientFill = Extract<\n FillStyle,\n { fill: 'linear-gradient' | 'radial-gradient' | 'conic-gradient' }\n>;\n\n/** `GradientFill['fill']` — the three gradient discriminants on their own. */\nexport type GradientKind = GradientFill['fill'];\n\n/**\n * Where a stroke sits relative to the geometric edge it strokes.\n *\n * - `'center'` (default): canvas-native — half the stroke width sits inside\n * the geometry, half outside.\n * - `'inner'`: the entire stroke lies inside the geometry. The outer edge of\n * the stroke coincides with the geometric edge.\n * - `'outer'`: the entire stroke lies outside the geometry. The inner edge\n * of the stroke coincides with the geometric edge.\n *\n * Mirrors the (proposed) SVG `stroke-alignment` property. Honoring `inner`\n * or `outer` is the renderer's responsibility — for axis-aligned rects, the\n * kit shifts coordinates by `width / 2`. For arbitrary paths, renderers\n * typically use a stencil mask of the stroked path against the geometry.\n */\nexport type StrokeAlign = 'center' | 'inner' | 'outer';\n\n/**\n * The kit's built-in marker vocabulary, open on the string the way\n * `PaintKind` is so a consumer's registered key typechecks.\n */\nexport type KitMarkerKey =\n | 'arrow' | 'arrow-open' | 'arrow-concave'\n | 'diamond' | 'diamond-hollow'\n | 'circle' | 'square' | 'bar';\nexport type MarkerKey = KitMarkerKey | (string & {});\n\n/**\n * A marker on one end (or every interior vertex) of a stroke.\n *\n * `size` reuses `Stroke.width`'s unit system: a bare number scales with the\n * resolved stroke width, `{ px }` pins screen pixels. That is SVG's\n * `markerUnits` in the idiom this codebase already resolves at draw time.\n * Omitted, the marker is one stroke width per unit.\n */\nexport type MarkerRef = MarkerKey | { key: MarkerKey; size?: ScreenLength };\n\n/**\n * A length the author states either in world units (a plain number) or in\n * screen pixels (`{ px }`), resolved against the accumulated transform scale\n * wherever it is consumed — so a `{ px }` length holds its on-screen size as\n * the view zooms, with no call-site arithmetic.\n */\nexport type ScreenLength = number | { px: number };\n\n/**\n * Resolve a {@link ScreenLength} to world units. A non-finite or zero scale\n * has no world to divide into, so the pixel count passes through unchanged\n * rather than becoming `Infinity`.\n */\nexport function resolveScreenLength(value: ScreenLength, scale: number): number {\n if (typeof value === 'number') return value;\n if (!Number.isFinite(scale) || scale === 0) return value.px;\n return value.px / scale;\n}\n\n/** Stroke style: a FillStyle plus structural line parameters. */\nexport interface Stroke {\n /**\n * Omitted, the stroke paints nothing — the same \"no paint\" a `fill: null`\n * says for a node's fill. Documents written before mid-2026 can hold a\n * stroke whose other fields were seeded without one, and the type is\n * optional so every reader has to answer for that rather than throw on it.\n */\n paint?: FillStyle;\n /** World units, or `{ px }` for screen pixels — resolved against the\n * accumulated transform scale at draw time, so it holds its on-screen\n * thickness as the view zooms. */\n width?: ScreenLength;\n /** Per `CanvasRenderingContext2D.setLineDash` — empty/omitted = solid. */\n dash?: number[];\n /** Marker at the first vertex of each open subpath, rotated to point back\n * along the line (SVG's `auto-start-reverse`, as the only behavior). */\n markerStart?: MarkerRef;\n /** Marker at every interior authored vertex, on the bisector of the\n * incoming and outgoing directions. Never insets the stroke. */\n markerMid?: MarkerRef;\n /** Marker at the last vertex of each open subpath. */\n markerEnd?: MarkerRef;\n cap?: 'butt' | 'round' | 'square';\n join?: 'miter' | 'round' | 'bevel';\n /**\n * Miter join fallback threshold. When the miter length exceeds\n * `miterLimit * width / 2`, the join falls back to a bevel. Default 4,\n * matching SVG — which is also what the kit's own serializer implies when\n * it omits the attribute for an unset field. Canvas2D's 10 lets an acute\n * corner throw a spike four times the half-width.\n */\n miterLimit?: number;\n /** Where the stroke sits relative to the geometric edge. Default `'center'`. */\n align?: StrokeAlign;\n /**\n * Per-anchor RGBA, flat (length = 4 × countPathAnchors(path)). Each\n * value in 0..1. Arc-length interpolated across the tessellated ribbon\n * between consecutive anchors. When set, `paint` is still required —\n * its `opacity` (and color, as a placeholder) flow through the shader.\n */\n vertexColors?: number[];\n /**\n * Per-anchor stroke width (length = `countPathAnchors(path)`). When set,\n * the tessellator interpolates half-widths along each segment to produce\n * a tapered ribbon. `width` is used as the fallback for any anchor whose\n * entry is missing or non-finite. Pressure-driven pencil strokes use\n * this; pair with `pressureToWidth` to derive widths from stylus input.\n *\n * Joins between adjacent segments whose widths differ by more than\n * `varyingWidthJoinThreshold` (default 1.5×) are forced to bevel\n * regardless of the `join` setting — miter math is unstable when widths\n * vary across the corner; smooth round joins with mismatched widths\n * are a future enhancement.\n */\n vertexWidths?: number[];\n /**\n * Max width ratio (greater / lesser) at which a non-bevel join is\n * preserved when `vertexWidths` causes adjacent segments to differ.\n * Beyond this ratio the join falls back to bevel. Default 1.5. Ignored\n * when `vertexWidths` is absent.\n */\n varyingWidthJoinThreshold?: number;\n}\n\n/**\n * Inflate (positive) or deflate (negative) a rect to honor `align` when\n * stroking it. Returns the rect to pass to a stroked-rect renderer. `width`\n * is the stroke width (defaults to 1 to match canvas).\n *\n * Pure geometry helper — no rendering side effects. Used by path\n * tessellation and the selection overlay to produce a rect whose\n * center-aligned stroke visually coincides with the requested\n * inner/outer-aligned stroke of the original rect.\n */\nexport function alignedStrokeRect(\n rect: { x: number; y: number; width: number; height: number },\n align: StrokeAlign,\n width = 1,\n): { x: number; y: number; width: number; height: number } {\n if (align === 'center') return rect;\n // For 'inner', shift inward by width/2 so the stroke's outer edge coincides\n // with the geometric edge. For 'outer', shift outward.\n const sign = align === 'inner' ? -1 : 1;\n const d = (sign * width) / 2;\n return {\n x: rect.x - d,\n y: rect.y - d,\n width: rect.width + 2 * d,\n height: rect.height + 2 * d,\n };\n}\n\n/**\n * The named line styles a `Stroke.dash` array reads as.\n *\n * `custom` is what an imported array that matches no preset reads as — it is\n * reportable, not authorable: there is no array it maps back to.\n */\nexport type StrokeDashStyle = 'solid' | 'dashed' | 'dotted' | 'custom';\n\n/**\n * Dash and gap lengths of the presets, **as multiples of the stroke width**.\n *\n * SVG dash lengths are absolute, so a fixed pattern is a different style at\n * every width: `[6, 3]` is dots on a hairline and a railroad on a 20px\n * stroke. Scaling by the width is what makes \"dashed\" one style.\n */\nexport const STROKE_DASH_RATIOS = {\n dashed: [3, 2],\n dotted: [1, 2],\n} as const satisfies Record<'dashed' | 'dotted', readonly [number, number]>;\n\n/** `width` as a plain number — a `{ px }` width is read at scale 1, matching\n * what an unresolved stroke reaching the tessellator gets. */\nfunction dashWidth(width: number | { px: number } | undefined): number {\n const w = typeof width === 'object' ? width.px : (width ?? 1);\n return Number.isFinite(w) && w > 0 ? w : 1;\n}\n\n/**\n * The `Stroke.dash` array for a named style at `width`, or `undefined` for\n * `solid` — which is stored as no dash at all, not as an empty pattern.\n *\n * `custom` has no array of its own and returns `undefined`; a caller that\n * offers it as a choice should refuse the choice rather than call this.\n */\nexport function dashForStrokeStyle(\n style: StrokeDashStyle,\n width: number | { px: number } | undefined,\n): number[] | undefined {\n if (style !== 'dashed' && style !== 'dotted') return undefined;\n const w = dashWidth(width);\n return STROKE_DASH_RATIOS[style].map((r) => r * w);\n}\n\n/**\n * The style a stored `dash` reads as at `width`. Absent or empty is `solid`;\n * an array matching neither preset is `custom`.\n */\nexport function strokeDashStyleOf(\n dash: readonly number[] | undefined,\n width: number | { px: number } | undefined,\n): StrokeDashStyle {\n if (dash === undefined || dash.length === 0 || dash.every((v) => v === 0)) return 'solid';\n const w = dashWidth(width);\n for (const style of ['dashed', 'dotted'] as const) {\n const preset = STROKE_DASH_RATIOS[style];\n // Tolerance is relative to the width: the presets are multiples of it, and\n // a round-tripped array carries the serializer's decimal trimming.\n if (dash.length === preset.length && preset.every((r, i) => Math.abs(dash[i] - r * w) <= w * 1e-3)) {\n return style;\n }\n }\n return 'custom';\n}\n\n/** Region a fill is clipped to. */\nexport interface Region {\n x: number;\n y: number;\n w: number;\n h: number;\n shape: 'rectangle' | 'circle';\n}\n","// sRGB ↔ OKLab conversion. Reference: https://bottosson.github.io/posts/oklab/\n\nconst SRGB_TO_LINEAR: Float64Array = (() => {\n const lut = new Float64Array(256);\n for (let i = 0; i < 256; i++) {\n const c = i / 255;\n lut[i] = c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);\n }\n return lut;\n})();\n\nfunction linearToSrgbByte(c: number): number {\n if (c <= 0) return 0;\n if (c >= 1) return 255;\n const v = c <= 0.0031308 ? c * 12.92 : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;\n return Math.round(v * 255);\n}\n\n/** Convert an 8-bit sRGB triple to OKLab. */\nexport function srgbU8ToOklab(r: number, g: number, b: number): [number, number, number] {\n const rl = SRGB_TO_LINEAR[r & 0xff];\n const gl = SRGB_TO_LINEAR[g & 0xff];\n const bl = SRGB_TO_LINEAR[b & 0xff];\n const l = 0.4122214708 * rl + 0.5363325363 * gl + 0.0514459929 * bl;\n const m = 0.2119034982 * rl + 0.6806995451 * gl + 0.1073969566 * bl;\n const s = 0.0883024619 * rl + 0.2817188376 * gl + 0.6299787005 * bl;\n const lp = Math.cbrt(l);\n const mp = Math.cbrt(m);\n const sp = Math.cbrt(s);\n return [\n 0.2104542553 * lp + 0.7936177850 * mp - 0.0040720468 * sp,\n 1.9779984951 * lp - 2.4285922050 * mp + 0.4505937099 * sp,\n 0.0259040371 * lp + 0.7827717662 * mp - 0.8086757660 * sp,\n ];\n}\n\nfunction oklabToLinearSrgb(L: number, A: number, B: number): [number, number, number] {\n const lp = L + 0.3963377774 * A + 0.2158037573 * B;\n const mp = L - 0.1055613458 * A - 0.0638541728 * B;\n const sp = L - 0.0894841775 * A - 1.2914855480 * B;\n const l = lp * lp * lp;\n const m = mp * mp * mp;\n const s = sp * sp * sp;\n return [\n +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,\n -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,\n -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,\n ];\n}\n\nfunction inGamut(rl: number, gl: number, bl: number): boolean {\n // Small epsilon absorbs float-roundtrip noise on pure-primary colors.\n const eps = 1e-6;\n return (\n rl >= -eps && rl <= 1 + eps &&\n gl >= -eps && gl <= 1 + eps &&\n bl >= -eps && bl <= 1 + eps\n );\n}\n\nfunction clipToGamut(L: number, A: number, B: number): [number, number, number] {\n let [rl, gl, bl] = oklabToLinearSrgb(L, A, B);\n if (inGamut(rl, gl, bl)) return [rl, gl, bl];\n let lo = 0;\n let hi = 1;\n // 5 iterations → ~3% chroma precision; sufficient for u8 output.\n for (let i = 0; i < 5; i++) {\n const t = (lo + hi) / 2;\n [rl, gl, bl] = oklabToLinearSrgb(L, A * t, B * t);\n if (inGamut(rl, gl, bl)) lo = t; else hi = t;\n }\n [rl, gl, bl] = oklabToLinearSrgb(L, A * lo, B * lo);\n return [rl, gl, bl];\n}\n\n/** Convert OKLab back to an 8-bit sRGB triple, clamped into gamut. */\nexport function oklabToSrgbU8(L: number, A: number, B: number): [number, number, number] {\n const [rl, gl, bl] = clipToGamut(L, A, B);\n return [linearToSrgbByte(rl), linearToSrgbByte(gl), linearToSrgbByte(bl)];\n}\n\n/** Blend two 8-bit sRGB colors through OKLab. Perceptually even, and it does\n * not pass through the muddy midpoints an sRGB blend produces between\n * complementary hues. */\nexport function lerpOklab(\n from: readonly [number, number, number],\n to: readonly [number, number, number],\n t: number,\n): [number, number, number] {\n return [\n from[0] + (to[0] - from[0]) * t,\n from[1] + (to[1] - from[1]) * t,\n from[2] + (to[2] - from[2]) * t,\n ];\n}\n\n/** Convert OKLab (L, a, b) to OKLCh (L, C, h). h is in radians, range [-π, π]. */\nexport function oklabToOklch(L: number, A: number, B: number): [number, number, number] {\n const C = Math.hypot(A, B);\n const h = Math.atan2(B, A);\n return [L, C, h];\n}\n\n/** Convert OKLCh (L, C, h) to OKLab (L, a, b). h is in radians. */\nexport function oklchToOklab(L: number, C: number, h: number): [number, number, number] {\n return [L, C * Math.cos(h), C * Math.sin(h)];\n}\n\n/** Lerp OKLCh values with shortest-arc hue interpolation. Lightness and\n * chroma lerp linearly. When chroma is near zero on either endpoint, hue\n * is taken from the other endpoint (avoids hue snap-from-undefined).\n * h inputs are in radians. */\nexport function lerpOklch(\n from: readonly [number, number, number],\n to: readonly [number, number, number],\n t: number,\n): [number, number, number] {\n const [L1, C1, h1Raw] = from;\n const [L2, C2, h2Raw] = to;\n const C_EPS = 1e-4;\n // When one endpoint is near-achromatic, take hue from the other endpoint\n // to avoid spurious arc rotation through undefined hue space.\n const h1 = C1 < C_EPS ? h2Raw : h1Raw;\n const h2 = C2 < C_EPS ? h1Raw : h2Raw;\n const TWO_PI = Math.PI * 2;\n // Shortest-arc delta in [-π, π].\n let dh = h2 - h1;\n while (dh > Math.PI) dh -= TWO_PI;\n while (dh < -Math.PI) dh += TWO_PI;\n return [\n L1 + (L2 - L1) * t,\n C1 + (C2 - C1) * t,\n h1 + dh * t,\n ];\n}\n\n/** Which space a color blend is computed in. `'rgb'` is cheapest; `'oklab'`\n * is perceptually even; `'oklch'` additionally travels around the hue wheel\n * rather than through it. */\nexport type ColorSpace = 'rgb' | 'oklab' | 'oklch';\n\nconst f2u = (v: number): number => Math.max(0, Math.min(255, Math.round(v * 255)));\nconst u2f = (v: number): number => v / 255;\n\n/** Lerp a flat RGBA color array `from` toward `to`. Inputs are 0..1\n * floats (the renderer's color space); outputs are 0..1 floats too.\n * Alpha is always linearly lerped; RGB channels are lerped in the\n * requested color space. OKLab / OKLCh paths bounce through u8 because\n * the gamma-correction LUT and gamut-clip routines are byte-keyed —\n * the round-trip is lossy at <1% per channel, well under the visible\n * threshold for these animations. */\nexport function lerpColorArray(\n from: readonly number[],\n to: readonly number[],\n t: number,\n space: ColorSpace = 'rgb',\n): number[] {\n if (from.length !== to.length) {\n throw new Error(`lerpColorArray: length mismatch (from=${from.length}, to=${to.length})`);\n }\n if (from.length % 4 !== 0) {\n throw new Error(`lerpColorArray: length ${from.length} not divisible by 4`);\n }\n const out = new Array<number>(from.length);\n const n = from.length / 4;\n if (space === 'rgb') {\n // Pure float lerp — no rounding (which previously discretized 0..1\n // inputs to 0/1, giving the cycle its 'choppy 8-corner' look).\n for (let i = 0; i < from.length; i++) {\n out[i] = from[i] + (to[i] - from[i]) * t;\n }\n return out;\n }\n if (space === 'oklch') {\n for (let i = 0; i < n; i++) {\n const k = i * 4;\n const fLab = srgbU8ToOklab(f2u(from[k]), f2u(from[k + 1]), f2u(from[k + 2]));\n const tLab = srgbU8ToOklab(f2u(to[k]), f2u(to[k + 1]), f2u(to[k + 2]));\n const fLch = oklabToOklch(fLab[0], fLab[1], fLab[2]);\n const tLch = oklabToOklch(tLab[0], tLab[1], tLab[2]);\n const midLch = lerpOklch(fLch, tLch, t);\n const midLab = oklchToOklab(midLch[0], midLch[1], midLch[2]);\n const [r, g, b] = oklabToSrgbU8(midLab[0], midLab[1], midLab[2]);\n out[k] = u2f(r);\n out[k + 1] = u2f(g);\n out[k + 2] = u2f(b);\n out[k + 3] = from[k + 3] + (to[k + 3] - from[k + 3]) * t;\n }\n return out;\n }\n for (let i = 0; i < n; i++) {\n const k = i * 4;\n const fLab = srgbU8ToOklab(f2u(from[k]), f2u(from[k + 1]), f2u(from[k + 2]));\n const tLab = srgbU8ToOklab(f2u(to[k]), f2u(to[k + 1]), f2u(to[k + 2]));\n const mid = lerpOklab(fLab, tLab, t);\n const [r, g, b] = oklabToSrgbU8(mid[0], mid[1], mid[2]);\n out[k] = u2f(r);\n out[k + 1] = u2f(g);\n out[k + 2] = u2f(b);\n out[k + 3] = from[k + 3] + (to[k + 3] - from[k + 3]) * t;\n }\n return out;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weasel-js/paint",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Paint vocabulary for weasel: FillStyle, Stroke, gradients, dashes. Plain data, no renderer.",
5
5
  "license": "MIT",
6
6
  "type": "module",