@weasel-js/paint-kinds 1.19.1 → 2.0.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.
@@ -13,18 +13,24 @@ function invertible(norm, inverseNorm) {
13
13
  * an attribute. A non-finite `n` writes `'0'`. */
14
14
  function trimNumber(n, places = 6) {
15
15
  if (!Number.isFinite(n)) return "0";
16
- const k = 10 ** places;
17
- const rounded = Math.round(n * k) / k;
18
- return String(Number.isFinite(rounded) ? rounded : n);
16
+ return String(Number(n.toFixed(Math.min(100, Math.max(0, places)))) + 0);
17
+ }
18
+ /** The number `t` of the way from `a` to `b`; `t` outside [0, 1] extrapolates. Exactly `b` at `t = 1`, which
19
+ * `a + (b - a) * t` can miss by an ulp, and finite where `b - a` would overflow. */
20
+ function lerp$1(a, b, t) {
21
+ if (t === 1) return b;
22
+ if (a === b) return a;
23
+ const d = b - a;
24
+ return Number.isFinite(d) ? a + d * t : a * (1 - t) + b * t;
19
25
  }
20
26
  function minOf(items, of) {
21
- let least = Infinity;
27
+ let least = 1e400;
22
28
  let i = 0;
23
29
  for (const item of items) least = Math.min(least, of ? of(item, i++) : item);
24
30
  return least;
25
31
  }
26
32
  function maxOf(items, of) {
27
- let most = -Infinity;
33
+ let most = -1e400;
28
34
  let i = 0;
29
35
  for (const item of items) most = Math.max(most, of ? of(item, i++) : item);
30
36
  return most;
@@ -62,6 +68,32 @@ function invert$1(m) {
62
68
  -(m[4] * b + m[5] * d)
63
69
  ];
64
70
  }
71
+ // ../registry/src/isDev.ts
72
+ /**
73
+ * Whether to emit development diagnostics. Dev is the default when nothing
74
+ * says otherwise.
75
+ *
76
+ * The read is the bare member expression `process.env.NODE_ENV` because that is
77
+ * the only form a bundler's define replaces; a production build then folds this
78
+ * to `false`. A `typeof process` guard or an optional chain would survive the
79
+ * define and read as dev in every browser. Where nothing defined it (published
80
+ * ESM loaded from a CDN, say) `process` is absent, the read throws, and the
81
+ * answer is dev.
82
+ */
83
+ function isDev() {
84
+ try {
85
+ return true;
86
+ } catch {
87
+ return true;
88
+ }
89
+ }
90
+ // ../registry/src/getOwn.ts
91
+ /** `record[key]` when `record` holds `key` itself, else `undefined`, as it is for no record at all: a key read from
92
+ * outside may name an `Object.prototype` member (`constructor`, `toString`), which a bare read finds on the
93
+ * prototype. */
94
+ function getOwn(record, key) {
95
+ return record != null && Object.hasOwn(record, key) ? record[key] : void 0;
96
+ }
65
97
  // ../geom/src/glMat3.ts
66
98
  /**
67
99
  * 2D affine matrix utilities. Column-major 9 elements, matching
@@ -89,14 +121,15 @@ function create(a, b, c, d, tx, ty) {
89
121
  function identity() {
90
122
  return create(1, 0, 0, 1, 0, 0);
91
123
  }
92
- function multiply(out, m) {
93
- const a = out[0], b = out[1];
94
- const c = out[3], d = out[4];
95
- const tx = out[6], ty = out[7];
96
- const ma = m[0], mb = m[1];
97
- const mc = m[3], md = m[4];
98
- const mtx = m[6], mty = m[7];
99
- return create(a * ma + c * mb, b * ma + d * mb, a * mc + c * md, b * mc + d * md, a * mtx + c * mty + tx, b * mtx + d * mty + ty);
124
+ /** `m · n`, as a new matrix; neither argument is written. */
125
+ function multiply(m, n) {
126
+ const a = m[0], b = m[1];
127
+ const c = m[3], d = m[4];
128
+ const tx = m[6], ty = m[7];
129
+ const na = n[0], nb = n[1];
130
+ const nc = n[3], nd = n[4];
131
+ const ntx = n[6], nty = n[7];
132
+ return create(a * na + c * nb, b * na + d * nb, a * nc + c * nd, b * nc + d * nd, a * ntx + c * nty + tx, b * ntx + d * nty + ty);
100
133
  }
101
134
  /** `m · T(tx, ty)`: composes onto `m`, where `@weasel-js/geom`'s `translate`
102
135
  * constructs a fresh matrix. */
@@ -133,7 +166,8 @@ function invert(m) {
133
166
  function toUniform(m) {
134
167
  return Float32Array.from(m);
135
168
  }
136
- function apply(m, x, y) {
169
+ /** The point `(x, y)` mapped through `m`. */
170
+ function transformPoint(m, x, y) {
137
171
  const a = m[0], b = m[1];
138
172
  const c = m[3], d = m[4];
139
173
  const tx = m[6], ty = m[7];
@@ -176,7 +210,7 @@ const mat3 = {
176
210
  scaled,
177
211
  invert,
178
212
  toUniform,
179
- apply,
213
+ transformPoint,
180
214
  screenToClip,
181
215
  meanScaleOf,
182
216
  minScaleOf
@@ -184,7 +218,7 @@ const mat3 = {
184
218
  // ../paint/src/hue.ts
185
219
  /** `h` degrees in `[0, 360)`. */
186
220
  const wrapDeg = (h) => (h % 360 + 360) % 360;
187
- // ../paint/src/lerp.ts
221
+ // ../paint/src/scalar.ts
188
222
  /** geom's `lerp`, which this package cannot import: exactly `b` at `t = 1`, and finite where `b - a` would overflow. */
189
223
  function lerp(a, b, t) {
190
224
  if (t === 1) return b;
@@ -192,6 +226,10 @@ function lerp(a, b, t) {
192
226
  const d = b - a;
193
227
  return Number.isFinite(d) ? a + d * t : a * (1 - t) + b * t;
194
228
  }
229
+ /** geom's `clamp01`, which this package cannot import: `v` held to `0`..`1`, `NaN` passing through. */
230
+ function clamp01(v) {
231
+ return v < 0 ? 0 : v > 1 ? 1 : v;
232
+ }
195
233
  // ../paint/src/hex.ts
196
234
  const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
197
235
  /**
@@ -344,9 +382,8 @@ function oklabToSrgbU8(L, A, B) {
344
382
  linearToSrgbByte(bl)
345
383
  ];
346
384
  }
347
- /** Blend two 8-bit sRGB colors through OKLab. Perceptually even, and it does
348
- * not pass through the muddy midpoints an sRGB blend produces between
349
- * complementary hues. */
385
+ /** Blend two OKLab colors, component by component, `t` of the way from `from` to `to`. Perceptually even,
386
+ * and it does not pass through the muddy midpoints an sRGB blend produces between complementary hues. */
350
387
  function lerpOklab(from, to, t) {
351
388
  return [
352
389
  lerp(from[0], to[0], t),
@@ -464,13 +501,22 @@ function parseColor(input) {
464
501
  CACHE.set(input, result);
465
502
  return result;
466
503
  }
504
+ /** {@link parseColor}'s reading of `input`, or null where it would throw. The tuple is read-only, as there. */
505
+ function tryParseColor(input) {
506
+ try {
507
+ return parseColor(input);
508
+ } catch {
509
+ return null;
510
+ }
511
+ }
467
512
  const CACHE = /* @__PURE__ */ new Map();
468
513
  const CACHE_MAX = 1024;
469
514
  function parseColorUncached(input) {
470
515
  const s = input.trim();
471
516
  if (s.startsWith("#")) return parseHex(s);
472
517
  const lower = s.toLowerCase();
473
- if (Object.hasOwn(NAMED_COLORS, lower)) return NAMED_COLORS[lower];
518
+ const named = getOwn(NAMED_COLORS, lower);
519
+ if (named) return named;
474
520
  if (lower.startsWith("rgb")) {
475
521
  const parsed = parseRgbFunction(lower);
476
522
  if (parsed) return parsed;
@@ -548,7 +594,7 @@ function splitChannels(body) {
548
594
  alpha
549
595
  };
550
596
  }
551
- const CSS_NUMBER = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/;
597
+ const CSS_NUMBER = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/;
552
598
  /** A CSS `<number>` token, or null: `Number` alone reads `''` as 0 and accepts `0x10`. */
553
599
  function cssNumber(tok) {
554
600
  if (!CSS_NUMBER.test(tok)) return null;
@@ -655,159 +701,156 @@ function parseHue(tok) {
655
701
  function parsePercentOrNumber(tok) {
656
702
  return parseChannel(tok, 1);
657
703
  }
658
- function clamp01(n) {
659
- return n < 0 ? 0 : n > 1 ? 1 : n;
660
- }
661
704
  const NAMED_COLORS = (() => {
662
705
  const hex = {
663
- aliceblue: 15792383,
664
- antiquewhite: 16444375,
706
+ aliceblue: 0xf0f8ff,
707
+ antiquewhite: 0xfaebd7,
665
708
  aqua: 65535,
666
709
  aquamarine: 8388564,
667
- azure: 15794175,
668
- beige: 16119260,
669
- bisque: 16770244,
710
+ azure: 0xf0ffff,
711
+ beige: 0xf5f5dc,
712
+ bisque: 0xffe4c4,
670
713
  black: 0,
671
- blanchedalmond: 16772045,
714
+ blanchedalmond: 0xffebcd,
672
715
  blue: 255,
673
716
  blueviolet: 9055202,
674
- brown: 10824234,
675
- burlywood: 14596231,
717
+ brown: 0xa52a2a,
718
+ burlywood: 0xdeb887,
676
719
  cadetblue: 6266528,
677
720
  chartreuse: 8388352,
678
- chocolate: 13789470,
679
- coral: 16744272,
721
+ chocolate: 0xd2691e,
722
+ coral: 0xff7f50,
680
723
  cornflowerblue: 6591981,
681
- cornsilk: 16775388,
682
- crimson: 14423100,
724
+ cornsilk: 0xfff8dc,
725
+ crimson: 0xdc143c,
683
726
  cyan: 65535,
684
727
  darkblue: 139,
685
728
  darkcyan: 35723,
686
- darkgoldenrod: 12092939,
687
- darkgray: 11119017,
729
+ darkgoldenrod: 0xb8860b,
730
+ darkgray: 0xa9a9a9,
688
731
  darkgreen: 25600,
689
- darkgrey: 11119017,
690
- darkkhaki: 12433259,
732
+ darkgrey: 0xa9a9a9,
733
+ darkkhaki: 0xbdb76b,
691
734
  darkmagenta: 9109643,
692
735
  darkolivegreen: 5597999,
693
- darkorange: 16747520,
694
- darkorchid: 10040012,
736
+ darkorange: 0xff8c00,
737
+ darkorchid: 0x9932cc,
695
738
  darkred: 9109504,
696
- darksalmon: 15308410,
739
+ darksalmon: 0xe9967a,
697
740
  darkseagreen: 9419919,
698
741
  darkslateblue: 4734347,
699
742
  darkslategray: 3100495,
700
743
  darkslategrey: 3100495,
701
744
  darkturquoise: 52945,
702
745
  darkviolet: 9699539,
703
- deeppink: 16716947,
746
+ deeppink: 0xff1493,
704
747
  deepskyblue: 49151,
705
748
  dimgray: 6908265,
706
749
  dimgrey: 6908265,
707
750
  dodgerblue: 2003199,
708
- firebrick: 11674146,
709
- floralwhite: 16775920,
751
+ firebrick: 0xb22222,
752
+ floralwhite: 0xfffaf0,
710
753
  forestgreen: 2263842,
711
- fuchsia: 16711935,
712
- gainsboro: 14474460,
713
- ghostwhite: 16316671,
714
- gold: 16766720,
715
- goldenrod: 14329120,
754
+ fuchsia: 0xff00ff,
755
+ gainsboro: 0xdcdcdc,
756
+ ghostwhite: 0xf8f8ff,
757
+ gold: 0xffd700,
758
+ goldenrod: 0xdaa520,
716
759
  gray: 8421504,
717
760
  green: 32768,
718
- greenyellow: 11403055,
761
+ greenyellow: 0xadff2f,
719
762
  grey: 8421504,
720
- honeydew: 15794160,
721
- hotpink: 16738740,
722
- indianred: 13458524,
763
+ honeydew: 0xf0fff0,
764
+ hotpink: 0xff69b4,
765
+ indianred: 0xcd5c5c,
723
766
  indigo: 4915330,
724
- ivory: 16777200,
725
- khaki: 15787660,
726
- lavender: 15132410,
727
- lavenderblush: 16773365,
767
+ ivory: 0xfffff0,
768
+ khaki: 0xf0e68c,
769
+ lavender: 0xe6e6fa,
770
+ lavenderblush: 0xfff0f5,
728
771
  lawngreen: 8190976,
729
- lemonchiffon: 16775885,
730
- lightblue: 11393254,
731
- lightcoral: 15761536,
732
- lightcyan: 14745599,
733
- lightgoldenrodyellow: 16448210,
734
- lightgray: 13882323,
772
+ lemonchiffon: 0xfffacd,
773
+ lightblue: 0xadd8e6,
774
+ lightcoral: 0xf08080,
775
+ lightcyan: 0xe0ffff,
776
+ lightgoldenrodyellow: 0xfafad2,
777
+ lightgray: 0xd3d3d3,
735
778
  lightgreen: 9498256,
736
- lightgrey: 13882323,
737
- lightpink: 16758465,
738
- lightsalmon: 16752762,
779
+ lightgrey: 0xd3d3d3,
780
+ lightpink: 0xffb6c1,
781
+ lightsalmon: 0xffa07a,
739
782
  lightseagreen: 2142890,
740
783
  lightskyblue: 8900346,
741
784
  lightslategray: 7833753,
742
785
  lightslategrey: 7833753,
743
- lightsteelblue: 11584734,
744
- lightyellow: 16777184,
786
+ lightsteelblue: 0xb0c4de,
787
+ lightyellow: 0xffffe0,
745
788
  lime: 65280,
746
789
  limegreen: 3329330,
747
- linen: 16445670,
748
- magenta: 16711935,
790
+ linen: 0xfaf0e6,
791
+ magenta: 0xff00ff,
749
792
  maroon: 8388608,
750
793
  mediumaquamarine: 6737322,
751
794
  mediumblue: 205,
752
- mediumorchid: 12211667,
795
+ mediumorchid: 0xba55d3,
753
796
  mediumpurple: 9662683,
754
797
  mediumseagreen: 3978097,
755
798
  mediumslateblue: 8087790,
756
799
  mediumspringgreen: 64154,
757
800
  mediumturquoise: 4772300,
758
- mediumvioletred: 13047173,
801
+ mediumvioletred: 0xc71585,
759
802
  midnightblue: 1644912,
760
- mintcream: 16121850,
761
- mistyrose: 16770273,
762
- moccasin: 16770229,
763
- navajowhite: 16768685,
803
+ mintcream: 0xf5fffa,
804
+ mistyrose: 0xffe4e1,
805
+ moccasin: 0xffe4b5,
806
+ navajowhite: 0xffdead,
764
807
  navy: 128,
765
- oldlace: 16643558,
808
+ oldlace: 0xfdf5e6,
766
809
  olive: 8421376,
767
810
  olivedrab: 7048739,
768
- orange: 16753920,
769
- orangered: 16729344,
770
- orchid: 14315734,
771
- palegoldenrod: 15657130,
772
- palegreen: 10025880,
773
- paleturquoise: 11529966,
774
- palevioletred: 14381203,
775
- papayawhip: 16773077,
776
- peachpuff: 16767673,
777
- peru: 13468991,
778
- pink: 16761035,
779
- plum: 14524637,
780
- powderblue: 11591910,
811
+ orange: 0xffa500,
812
+ orangered: 0xff4500,
813
+ orchid: 0xda70d6,
814
+ palegoldenrod: 0xeee8aa,
815
+ palegreen: 0x98fb98,
816
+ paleturquoise: 0xafeeee,
817
+ palevioletred: 0xdb7093,
818
+ papayawhip: 0xffefd5,
819
+ peachpuff: 0xffdab9,
820
+ peru: 0xcd853f,
821
+ pink: 0xffc0cb,
822
+ plum: 0xdda0dd,
823
+ powderblue: 0xb0e0e6,
781
824
  purple: 8388736,
782
825
  rebeccapurple: 6697881,
783
- red: 16711680,
784
- rosybrown: 12357519,
826
+ red: 0xff0000,
827
+ rosybrown: 0xbc8f8f,
785
828
  royalblue: 4286945,
786
829
  saddlebrown: 9127187,
787
- salmon: 16416882,
788
- sandybrown: 16032864,
830
+ salmon: 0xfa8072,
831
+ sandybrown: 0xf4a460,
789
832
  seagreen: 3050327,
790
- seashell: 16774638,
791
- sienna: 10506797,
792
- silver: 12632256,
833
+ seashell: 0xfff5ee,
834
+ sienna: 0xa0522d,
835
+ silver: 0xc0c0c0,
793
836
  skyblue: 8900331,
794
837
  slateblue: 6970061,
795
838
  slategray: 7372944,
796
839
  slategrey: 7372944,
797
- snow: 16775930,
840
+ snow: 0xfffafa,
798
841
  springgreen: 65407,
799
842
  steelblue: 4620980,
800
- tan: 13808780,
843
+ tan: 0xd2b48c,
801
844
  teal: 32896,
802
- thistle: 14204888,
803
- tomato: 16737095,
845
+ thistle: 0xd8bfd8,
846
+ tomato: 0xff6347,
804
847
  turquoise: 4251856,
805
- violet: 15631086,
806
- wheat: 16113331,
807
- white: 16777215,
808
- whitesmoke: 16119285,
809
- yellow: 16776960,
810
- yellowgreen: 10145074
848
+ violet: 0xee82ee,
849
+ wheat: 0xf5deb3,
850
+ white: 0xffffff,
851
+ whitesmoke: 0xf5f5f5,
852
+ yellow: 0xffff00,
853
+ yellowgreen: 0x9acd32
811
854
  };
812
855
  const rgba = (n) => [
813
856
  (n >> 16 & 255) / 255,
@@ -825,6 +868,14 @@ const NAMED_COLORS = (() => {
825
868
  ...Object.fromEntries(Object.entries(hex).map(([name, n]) => [name, rgba(n)]))
826
869
  };
827
870
  })();
871
+ // ../renderer/src/warnOnce.ts
872
+ const warned = /* @__PURE__ */ new Set();
873
+ /** Print `message` in development the first time `key` is seen. */
874
+ function warnOnce(key, message) {
875
+ if (warned.has(key)) return;
876
+ warned.add(key);
877
+ if (isDev()) console.warn(message);
878
+ }
828
879
  // ../renderer/src/meshPaint/surface.ts
829
880
  /** Corner `i` of the boundary walk. */
830
881
  function patchCorner(patch, i) {
@@ -834,10 +885,10 @@ function patchCorner(patch, i) {
834
885
  function isTensorPatch(patch) {
835
886
  return patch.points.length >= 16;
836
887
  }
837
- /** A patch is twelve boundary points, or sixteen with the interior. Anything
838
- * else cannot be evaluated, and saying so here beats painting nonsense. */
888
+ /** A patch is twelve boundary points, or sixteen with the interior, and four
889
+ * corner colors. Anything else cannot be evaluated, and saying so here beats painting nonsense. */
839
890
  function isValidPatch(patch) {
840
- return patch.points.length === 12 || patch.points.length === 16;
891
+ return (patch.points.length === 12 || patch.points.length === 16) && patch.colors.length === 4;
841
892
  }
842
893
  function bezier(p0, p1, p2, p3, t) {
843
894
  const s = 1 - t;
@@ -948,19 +999,41 @@ function evalTensor(patch, u, v) {
948
999
  y
949
1000
  };
950
1001
  }
951
- /** Axis-aligned bounds of a patch, sampled on a grid: control points alone
952
- * bound a Bézier but not tightly. */
1002
+ /** Axis-aligned bounds of a patch: the union, over a `steps` × `steps` split of it, of each piece's control
1003
+ * net. A piece's net encloses the piece, so the box encloses the surface, and it closes in on it as the
1004
+ * pieces shrink, where the control points of the whole patch alone bound it loosely. */
953
1005
  function patchBounds(patch, steps = 8) {
954
- let minX = Infinity;
955
- let minY = Infinity;
956
- let maxX = -Infinity;
957
- let maxY = -Infinity;
958
- for (let i = 0; i <= steps; i++) for (let j = 0; j <= steps; j++) {
959
- const p = evalPatch(patch, i / steps, j / steps);
960
- if (p.x < minX) minX = p.x;
961
- if (p.y < minY) minY = p.y;
962
- if (p.x > maxX) maxX = p.x;
963
- if (p.y > maxY) maxY = p.y;
1006
+ const net = bicubicNet(patch);
1007
+ let minX = 1e400;
1008
+ let minY = 1e400;
1009
+ let maxX = -1e400;
1010
+ let maxY = -1e400;
1011
+ for (let i = 0; i < steps; i++) {
1012
+ const s0 = i / steps, s1 = (i + 1) / steps;
1013
+ const rows = net.map((row) => [
1014
+ 0,
1015
+ 1,
1016
+ 2,
1017
+ 3
1018
+ ].map((k) => blossomAt(row, k, s0, s1)));
1019
+ for (let j = 0; j < steps; j++) {
1020
+ const t0 = j / steps, t1 = (j + 1) / steps;
1021
+ for (let a = 0; a < 4; a++) {
1022
+ const column = [
1023
+ rows[0][a],
1024
+ rows[1][a],
1025
+ rows[2][a],
1026
+ rows[3][a]
1027
+ ];
1028
+ for (let b = 0; b < 4; b++) {
1029
+ const p = blossomAt(column, b, t0, t1);
1030
+ if (p.x < minX) minX = p.x;
1031
+ if (p.y < minY) minY = p.y;
1032
+ if (p.x > maxX) maxX = p.x;
1033
+ if (p.y > maxY) maxY = p.y;
1034
+ }
1035
+ }
1036
+ }
964
1037
  }
965
1038
  return {
966
1039
  x: minX,
@@ -969,6 +1042,65 @@ function patchBounds(patch, steps = 8) {
969
1042
  height: maxY - minY
970
1043
  };
971
1044
  }
1045
+ /** The bicubic net a patch evaluates as, indexed `[u][v]` as `tensorNet` is: a Coons patch's interior is the
1046
+ * one that makes the tensor surface equal the Coons one (PDF's type 6 to type 7 rule). */
1047
+ function bicubicNet(patch) {
1048
+ if (isTensorPatch(patch)) return tensorNet(patch);
1049
+ const p = patch.points;
1050
+ const n = [
1051
+ [
1052
+ p[0],
1053
+ p[11],
1054
+ p[10],
1055
+ p[9]
1056
+ ],
1057
+ [
1058
+ p[1],
1059
+ p[1],
1060
+ p[1],
1061
+ p[8]
1062
+ ],
1063
+ [
1064
+ p[2],
1065
+ p[2],
1066
+ p[2],
1067
+ p[7]
1068
+ ],
1069
+ [
1070
+ p[3],
1071
+ p[4],
1072
+ p[5],
1073
+ p[6]
1074
+ ]
1075
+ ];
1076
+ for (const i of [1, 2]) for (const j of [1, 2]) {
1077
+ const I = i === 1 ? 0 : 3, J = j === 1 ? 0 : 3, fI = 3 - I, fJ = 3 - J;
1078
+ const mix = (k) => (-4 * n[I][J][k] + 6 * (n[I][j][k] + n[i][J][k]) - 2 * (n[I][fJ][k] + n[fI][J][k]) + 3 * (n[fI][j][k] + n[i][fJ][k]) - n[fI][fJ][k]) / 9;
1079
+ n[i][j] = {
1080
+ x: mix("x"),
1081
+ y: mix("y")
1082
+ };
1083
+ }
1084
+ return n;
1085
+ }
1086
+ /** Control point `k` of the cubic `c` cut to `[a, b]`: its blossom at `a` taken `3 − k` times and `b` `k` times. */
1087
+ function blossomAt(c, k, a, b) {
1088
+ const ts = [
1089
+ k > 2 ? b : a,
1090
+ k > 1 ? b : a,
1091
+ k > 0 ? b : a
1092
+ ];
1093
+ let pts = c;
1094
+ for (const t of ts) {
1095
+ const next = [];
1096
+ for (let m = 0; m + 1 < pts.length; m++) next.push({
1097
+ x: lerp$1(pts[m].x, pts[m + 1].x, t),
1098
+ y: lerp$1(pts[m].y, pts[m + 1].y, t)
1099
+ });
1100
+ pts = next;
1101
+ }
1102
+ return pts[0];
1103
+ }
972
1104
  /** Texels a rasterized cell spans at most. The rasterizer interpolates color
973
1105
  * linearly inside a cell and draws a curved edge as its chord, so this is
974
1106
  * what keeps a perceptual blend perceptual and an edge curved at any size. */
@@ -979,10 +1111,10 @@ function gridFor(size) {
979
1111
  }
980
1112
  /** The union of every patch's bounds, or `null` when nothing is drawable. */
981
1113
  function meshBounds(patches) {
982
- let minX = Infinity;
983
- let minY = Infinity;
984
- let maxX = -Infinity;
985
- let maxY = -Infinity;
1114
+ let minX = 1e400;
1115
+ let minY = 1e400;
1116
+ let maxX = -1e400;
1117
+ let maxY = -1e400;
986
1118
  for (const patch of patches) {
987
1119
  if (!isValidPatch(patch)) continue;
988
1120
  const b = patchBounds(patch);
@@ -1022,7 +1154,13 @@ function bakeMesh(patches, space = "rgb", size = 256) {
1022
1154
  });
1023
1155
  for (const patch of patches) {
1024
1156
  if (!isValidPatch(patch)) continue;
1025
- const corners = patch.colors.map((c) => parseColor(c));
1157
+ const corners = patch.colors.map((c) => tryParseColor(c));
1158
+ const unread = patch.colors.find((_, i) => corners[i] === null);
1159
+ if (unread !== void 0) {
1160
+ warnOnce(`mesh-color:${unread}`, `weasel: a mesh patch's color '${unread}' does not parse; the patch is not drawn`);
1161
+ continue;
1162
+ }
1163
+ const rgba = corners;
1026
1164
  for (let i = 0; i < grid; i++) for (let j = 0; j < grid; j++) {
1027
1165
  const u0 = i / grid;
1028
1166
  const u1 = (i + 1) / grid;
@@ -1031,19 +1169,19 @@ function bakeMesh(patches, space = "rgb", size = 256) {
1031
1169
  const quad = [
1032
1170
  {
1033
1171
  at: toTexel(evalPatch(patch, u0, v0)),
1034
- color: colorAt(corners, u0, v0, space)
1172
+ color: colorAt(rgba, u0, v0, space)
1035
1173
  },
1036
1174
  {
1037
1175
  at: toTexel(evalPatch(patch, u1, v0)),
1038
- color: colorAt(corners, u1, v0, space)
1176
+ color: colorAt(rgba, u1, v0, space)
1039
1177
  },
1040
1178
  {
1041
1179
  at: toTexel(evalPatch(patch, u1, v1)),
1042
- color: colorAt(corners, u1, v1, space)
1180
+ color: colorAt(rgba, u1, v1, space)
1043
1181
  },
1044
1182
  {
1045
1183
  at: toTexel(evalPatch(patch, u0, v1)),
1046
- color: colorAt(corners, u0, v1, space)
1184
+ color: colorAt(rgba, u0, v1, space)
1047
1185
  }
1048
1186
  ];
1049
1187
  fillTriangle(pixels, size, quad[0], quad[1], quad[2]);
@@ -1077,7 +1215,9 @@ function fillTriangle(pixels, size, a, b, c) {
1077
1215
  const w2 = 1 - w0 - w1;
1078
1216
  if (w0 < -1e-6 || w1 < -1e-6 || w2 < -1e-6) continue;
1079
1217
  const k = (y * size + x) * 4;
1080
- for (let ch = 0; ch < 4; ch++) pixels[k + ch] = (w0 * a.color[ch] + w1 * b.color[ch] + w2 * c.color[ch]) * 255;
1218
+ const alpha = w0 * a.color[3] + w1 * b.color[3] + w2 * c.color[3];
1219
+ for (let ch = 0; ch < 3; ch++) pixels[k + ch] = (w0 * a.color[ch] + w1 * b.color[ch] + w2 * c.color[ch]) * alpha * 255;
1220
+ pixels[k + 3] = alpha * 255;
1081
1221
  }
1082
1222
  }
1083
1223
  /** The largest bake, whatever the context allows: 16 MiB of RGBA, and a bake
@@ -1223,8 +1363,7 @@ void main() {
1223
1363
  vec2 texels = vec2(textureSize(u_sampler, 0));
1224
1364
  vec2 uv = (t * (texels - 1.0) + 0.5) / texels;
1225
1365
  vec4 texel = texture(u_sampler, uv);
1226
- float a = texel.a * u_opacity * u_alpha;
1227
- outColor = vec4(texel.rgb * a, a);
1366
+ outColor = texel * (u_opacity * u_alpha);
1228
1367
  }
1229
1368
  `;
1230
1369
  // ../renderer/src/gpu/webgpu/wgsl/common.ts
@@ -1344,8 +1483,7 @@ ${PAINT_VERTEX_WGSL}${textureWgsl("u_sampler", 0)}
1344
1483
  // Sampled before the box test: WGSL takes no implicit derivative past a branch on a varying.
1345
1484
  let texel = textureSample(u_sampler, u_sampler_sampler, uv);
1346
1485
  let outside = any(t < vec2f(0.0)) || any(t > vec2f(1.0));
1347
- let a = texel.a * u.u_opacity * u.u_alpha;
1348
- return weasel_out(select(vec4f(texel.rgb * a, a), vec4f(0.0), outside), u.u_weaselId);
1486
+ return weasel_out(select(texel * (u.u_opacity * u.u_alpha), vec4f(0.0), outside), u.u_weaselId);
1349
1487
  }
1350
1488
  `;
1351
1489
  // ../renderer/src/meshPaint/meshPaint.ts
@@ -1469,17 +1607,27 @@ function bandPatch(x0, x1, left, right) {
1469
1607
  }
1470
1608
  /**
1471
1609
  * A stop list as a mesh: one full-height band per gap between neighboring
1472
- * stops, colored left to right. The bands blend exactly the way the ramp
1473
- * does, so a horizontal gradient survives the switch unchanged. A zero-width
1474
- * gap is a hard break and gets no band.
1610
+ * stops, colored left to right, and a band in the end color from 0 to the
1611
+ * first stop and from the last to 1, as a ramp pads. The bands blend exactly
1612
+ * the way the ramp does, so a horizontal gradient survives the switch
1613
+ * unchanged. A zero-width gap is a hard break and gets no band.
1475
1614
  */
1476
1615
  function meshFromStops(stops) {
1477
- const sorted = [...stops].sort((a, b) => a.offset - b.offset);
1478
- if (sorted.length === 1) sorted.push({
1616
+ if (stops.length === 0) return seedMeshPatch("#000000ff");
1617
+ const sorted = stops.map((s) => ({
1618
+ offset: Math.min(1, Math.max(0, s.offset)),
1619
+ color: s.color
1620
+ })).sort((a, b) => a.offset - b.offset);
1621
+ const first = sorted[0];
1622
+ const last = sorted[sorted.length - 1];
1623
+ if (first.offset > 0) sorted.unshift({
1624
+ offset: 0,
1625
+ color: first.color
1626
+ });
1627
+ if (last.offset < 1) sorted.push({
1479
1628
  offset: 1,
1480
- color: sorted[0].color
1629
+ color: last.color
1481
1630
  });
1482
- if (sorted.length === 0) return seedMeshPatch("#000000ff");
1483
1631
  const patches = [];
1484
1632
  for (let i = 0; i < sorted.length - 1; i++) {
1485
1633
  const a = sorted[i];
@@ -1516,7 +1664,10 @@ function meshStops(mesh) {
1516
1664
  color
1517
1665
  });
1518
1666
  }
1519
- return stops.sort((a, b) => a.offset - b.offset);
1667
+ stops.sort((a, b) => a.offset - b.offset);
1668
+ while (stops.length > 1 && stops[0].color === stops[1].color) stops.shift();
1669
+ while (stops.length > 1 && stops[stops.length - 1].color === stops[stops.length - 2].color) stops.pop();
1670
+ return stops;
1520
1671
  }
1521
1672
  /** Every patch mapped through `move`. */
1522
1673
  function mapPatches(mesh, move) {
@@ -1548,7 +1699,7 @@ function devicePixelsOf(ctx, mesh, box) {
1548
1699
  if (!m) return NaN;
1549
1700
  return Math.max(box.width * Math.hypot(m[0], m[1]), box.height * Math.hypot(m[3], m[4]));
1550
1701
  }
1551
- function bakeFor(ctx, mesh) {
1702
+ function ensureBake(ctx, mesh) {
1552
1703
  const box = meshBounds(mesh.patches);
1553
1704
  if (!box) return null;
1554
1705
  const size = meshBakeSize(devicePixelsOf(ctx, mesh, box), ctx.maxTextureSize);
@@ -1571,7 +1722,7 @@ function bakeFor(ctx, mesh) {
1571
1722
  function bindMesh(ctx, fill) {
1572
1723
  const mesh = asMesh$1(fill);
1573
1724
  if (!mesh.patches.some(isValidPatch)) return null;
1574
- const entry = bakeFor(ctx, mesh);
1725
+ const entry = ensureBake(ctx, mesh);
1575
1726
  if (!entry) return null;
1576
1727
  const { box } = entry;
1577
1728
  const inverse = ctx.spaceInverse(mesh.units, box);
@@ -1599,7 +1750,7 @@ const meshDrawKind = {
1599
1750
  wgsl: { frag: MESH_WGSL }
1600
1751
  } },
1601
1752
  bind: bindMesh,
1602
- inPoseFrame: (fill, box) => {
1753
+ toLocalUnits: (fill, box) => {
1603
1754
  const mesh = asMesh$1(fill);
1604
1755
  if (mesh.units !== "bounds") return fill;
1605
1756
  const posed = asFill$1({
@@ -1613,7 +1764,7 @@ const meshDrawKind = {
1613
1764
  BAKE_SOURCE.set(posed, fill);
1614
1765
  return posed;
1615
1766
  },
1616
- toBoundsFrame: (fill, box) => {
1767
+ toBoundsUnits: (fill, box) => {
1617
1768
  const mesh = asMesh$1(fill);
1618
1769
  if (mesh.units === "bounds" || box.width === 0 || box.height === 0) return fill;
1619
1770
  return asFill$1({
@@ -1640,15 +1791,16 @@ function meshGradientXml(id, mesh) {
1640
1791
  const P = "wzl";
1641
1792
  return `<${P}:meshGradient id="${id}" gradientUnits="${mesh.units === "bounds" ? "objectBoundingBox" : "userSpaceOnUse"}"${mesh.interpolate && mesh.interpolate !== "rgb" ? ` interpolate="${mesh.interpolate}"` : ""}>${mesh.patches.map((patch) => {
1642
1793
  const points = patch.points.map((p) => `${trimNumber(p.x)},${trimNumber(p.y)}`).join(" ");
1643
- const colors = patch.colors.join(" ");
1794
+ const colors = patch.colors.join(";");
1644
1795
  return `<${P}:patch points="${points}" colors="${colors}"/>`;
1645
1796
  }).join("")}</${P}:meshGradient>`;
1646
1797
  }
1647
1798
  // src/meshKind.ts
1648
1799
  const asMesh = (fill) => fill;
1649
1800
  const asFill = (mesh) => mesh;
1650
- /** The `mesh-gradient` kind's registry entry. `./registerMesh` registers it;
1651
- * the paint-kind registry loads it on demand otherwise. */
1801
+ /** The `mesh-gradient` kind's registry entry. Importing
1802
+ * `@weasel-js/paint-kinds/mesh` registers it; the paint-kind registry loads
1803
+ * it on demand otherwise. */
1652
1804
  const meshGradientKind = {
1653
1805
  ...meshDrawKind,
1654
1806
  label: "Mesh",