@pixerate/editor 0.238.0 → 0.239.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.
@@ -0,0 +1,936 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+
5
+
6
+
7
+ var _chunk7PT4RJL5cjs = require('./chunk-7PT4RJL5.cjs');
8
+
9
+
10
+
11
+
12
+ var _chunkPPFJHTW5cjs = require('./chunk-PPFJHTW5.cjs');
13
+
14
+
15
+
16
+ var _chunkRD7RX52Gcjs = require('./chunk-RD7RX52G.cjs');
17
+
18
+
19
+
20
+ var _chunk7AHBW2CQcjs = require('./chunk-7AHBW2CQ.cjs');
21
+
22
+ // src/gradient/model.ts
23
+ function clamp(v, min, max) {
24
+ return Math.max(min, Math.min(max, v));
25
+ }
26
+ function gradientMidpointRemap(u, m) {
27
+ const mid = clamp(m, 0.05, 0.95);
28
+ if (Math.abs(mid - 0.5) < 1e-6) return u;
29
+ return u <= mid ? 0.5 * u / mid : 0.5 + 0.5 * (u - mid) / (1 - mid);
30
+ }
31
+ function normalizeGradient(input) {
32
+ if (typeof input === "string") {
33
+ const parsed = parseCssGradient(input);
34
+ if (!parsed) {
35
+ return { error: `Could not parse CSS gradient: "${input}".` };
36
+ }
37
+ return { gradient: parsed };
38
+ }
39
+ if (!input || typeof input !== "object") {
40
+ return { error: "Gradient must be an object." };
41
+ }
42
+ const obj = input;
43
+ const type = obj.type === "radial" || obj.type === "conic" ? obj.type : "linear";
44
+ if (!Array.isArray(obj.stops)) {
45
+ return { error: "Gradient stops must be an array." };
46
+ }
47
+ if (obj.stops.length < 2) {
48
+ return { error: `A gradient needs at least 2 stops; it has ${obj.stops.length}.` };
49
+ }
50
+ const usedIds = /* @__PURE__ */ new Set();
51
+ for (const s of obj.stops) {
52
+ if (s && typeof s === "object" && typeof s.id === "string" && s.id.trim()) {
53
+ usedIds.add(s.id.trim());
54
+ }
55
+ }
56
+ let nextIdIndex = 1;
57
+ const getNextId = () => {
58
+ while (usedIds.has(`s${nextIdIndex}`)) {
59
+ nextIdIndex++;
60
+ }
61
+ const id = `s${nextIdIndex}`;
62
+ usedIds.add(id);
63
+ nextIdIndex++;
64
+ return id;
65
+ };
66
+ const stops = [];
67
+ for (let i = 0; i < obj.stops.length; i++) {
68
+ const s = obj.stops[i];
69
+ if (!s || typeof s !== "object") {
70
+ return { error: `Stop at index ${i} must be an object.` };
71
+ }
72
+ const rawStop = s;
73
+ let normColor = null;
74
+ if (typeof rawStop.color === "string" || rawStop.color && typeof rawStop.color === "object") {
75
+ const dual = _chunk7PT4RJL5cjs.normalizeDualColor.call(void 0, rawStop.color);
76
+ if (dual) {
77
+ normColor = _chunk7PT4RJL5cjs.formatDualColor.call(void 0, dual);
78
+ }
79
+ }
80
+ if (!normColor) {
81
+ return { error: `Stop at index ${i} has an invalid colour.` };
82
+ }
83
+ let offset = Number(rawStop.offset);
84
+ if (!Number.isFinite(offset)) {
85
+ offset = obj.stops.length === 1 ? 0 : i / (obj.stops.length - 1);
86
+ }
87
+ offset = clamp(offset, 0, 1);
88
+ const id = typeof rawStop.id === "string" && rawStop.id.trim() ? rawStop.id.trim() : getNextId();
89
+ const stop = {
90
+ id,
91
+ offset,
92
+ color: normColor
93
+ };
94
+ if (rawStop.midpoint !== void 0 && rawStop.midpoint !== null) {
95
+ const m = Number(rawStop.midpoint);
96
+ if (Number.isFinite(m)) {
97
+ stop.midpoint = clamp(m, 0.05, 0.95);
98
+ }
99
+ }
100
+ stops.push(stop);
101
+ }
102
+ stops.sort((a, b) => a.offset - b.offset);
103
+ const gradient = {
104
+ type,
105
+ stops
106
+ };
107
+ if (type === "linear" || type === "conic") {
108
+ if (obj.angle !== void 0 && Number.isFinite(Number(obj.angle))) {
109
+ gradient.angle = (Number(obj.angle) % 360 + 360) % 360;
110
+ } else {
111
+ gradient.angle = type === "linear" ? 180 : 0;
112
+ }
113
+ }
114
+ if (type === "radial" || type === "conic") {
115
+ if (obj.center && typeof obj.center === "object") {
116
+ const cx = Number(obj.center.x);
117
+ const cy = Number(obj.center.y);
118
+ gradient.center = {
119
+ x: Number.isFinite(cx) ? clamp(cx, 0, 1) : 0.5,
120
+ y: Number.isFinite(cy) ? clamp(cy, 0, 1) : 0.5
121
+ };
122
+ } else {
123
+ gradient.center = { x: 0.5, y: 0.5 };
124
+ }
125
+ }
126
+ if (type === "radial") {
127
+ gradient.shape = obj.shape === "circle" ? "circle" : "ellipse";
128
+ }
129
+ const allowedInterp = ["srgb", "srgb-linear", "oklab", "oklch"];
130
+ if (obj.interpolation && allowedInterp.includes(obj.interpolation)) {
131
+ gradient.interpolation = obj.interpolation;
132
+ } else {
133
+ gradient.interpolation = "oklab";
134
+ }
135
+ if (obj.repeating) {
136
+ gradient.repeating = true;
137
+ }
138
+ return { gradient };
139
+ }
140
+ function createGradient(colorsOrOptions, options) {
141
+ if (Array.isArray(colorsOrOptions)) {
142
+ const colors = colorsOrOptions;
143
+ const resolvedColors = colors.length >= 2 ? colors : colors.length === 1 ? [colors[0], colors[0]] : ["#000000", "#ffffff"];
144
+ const stops = resolvedColors.map((color, i) => ({
145
+ id: `s${i + 1}`,
146
+ offset: i / (resolvedColors.length - 1),
147
+ color: _chunk7PT4RJL5cjs.formatDualColor.call(void 0, _nullishCoalesce(_chunk7PT4RJL5cjs.normalizeDualColor.call(void 0, color), () => ( { light: "#000000", dark: "#000000" })))
148
+ }));
149
+ const res2 = normalizeGradient({
150
+ ...options,
151
+ stops
152
+ });
153
+ if ("error" in res2) {
154
+ throw new Error(res2.error);
155
+ }
156
+ return res2.gradient;
157
+ }
158
+ if (colorsOrOptions && typeof colorsOrOptions === "object" && !Array.isArray(colorsOrOptions)) {
159
+ const raw = colorsOrOptions;
160
+ let stops = raw.stops;
161
+ if (!stops || stops.length === 0) {
162
+ stops = [
163
+ { id: "s1", offset: 0, color: "#000000" },
164
+ { id: "s2", offset: 1, color: "#ffffff" }
165
+ ];
166
+ } else if (typeof stops[0] === "string") {
167
+ const strStops = stops;
168
+ stops = strStops.map((color, i) => ({
169
+ id: `s${i + 1}`,
170
+ offset: i / (strStops.length - 1),
171
+ color
172
+ }));
173
+ }
174
+ const res2 = normalizeGradient({
175
+ ...raw,
176
+ stops
177
+ });
178
+ if ("error" in res2) {
179
+ throw new Error(res2.error);
180
+ }
181
+ return res2.gradient;
182
+ }
183
+ const res = normalizeGradient({
184
+ ...options,
185
+ stops: [
186
+ { id: "s1", offset: 0, color: "#000000" },
187
+ { id: "s2", offset: 1, color: "#ffffff" }
188
+ ]
189
+ });
190
+ if ("error" in res) {
191
+ throw new Error(res.error);
192
+ }
193
+ return res.gradient;
194
+ }
195
+ function isDualColorValue(val) {
196
+ if (typeof val === "string") {
197
+ return val.trim().toLowerCase().startsWith("light-dark(");
198
+ }
199
+ if (_chunk7PT4RJL5cjs.isDualColor.call(void 0, val)) {
200
+ return val.light !== val.dark;
201
+ }
202
+ return false;
203
+ }
204
+ function isDualGradient(g) {
205
+ return g.stops.some((s) => isDualColorValue(s.color));
206
+ }
207
+ function resolveGradient(g, scheme) {
208
+ return {
209
+ ...g,
210
+ center: g.center ? { ...g.center } : void 0,
211
+ stops: g.stops.map((s) => ({
212
+ ...s,
213
+ color: _nullishCoalesce(_chunk7PT4RJL5cjs.resolveColor.call(void 0, s.color, scheme), () => ( s.color))
214
+ }))
215
+ };
216
+ }
217
+ function sampleGradient(g, t, scheme = "light") {
218
+ const resolved = resolveGradient(g, scheme);
219
+ const clampedT = clamp(t, 0, 1);
220
+ const stops = resolved.stops;
221
+ if (clampedT <= stops[0].offset) {
222
+ return stops[0].color;
223
+ }
224
+ if (clampedT >= stops[stops.length - 1].offset) {
225
+ return stops[stops.length - 1].color;
226
+ }
227
+ let i = 0;
228
+ while (i < stops.length - 1 && stops[i + 1].offset < clampedT) {
229
+ i++;
230
+ }
231
+ const s0 = stops[i];
232
+ const s1 = stops[i + 1];
233
+ const span = s1.offset - s0.offset;
234
+ if (span <= 1e-6) {
235
+ return s1.color;
236
+ }
237
+ let u = (clampedT - s0.offset) / span;
238
+ if (s0.midpoint !== void 0) {
239
+ u = gradientMidpointRemap(u, s0.midpoint);
240
+ }
241
+ const mixed = _chunk7PT4RJL5cjs.mixColors.call(void 0, s0.color, s1.color, u, { space: _nullishCoalesce(g.interpolation, () => ( "oklab")) });
242
+ return _nullishCoalesce(mixed, () => ( s0.color));
243
+ }
244
+ function insertGradientStop(g, offset, color) {
245
+ const clampedOffset = clamp(offset, 0, 1);
246
+ const stopColor = color !== void 0 ? color : sampleGradient(g, clampedOffset, "light");
247
+ const used = new Set(g.stops.map((s) => s.id));
248
+ let idx = 1;
249
+ while (used.has(`s${idx}`)) idx++;
250
+ const id = `s${idx}`;
251
+ const newStop = {
252
+ id,
253
+ offset: clampedOffset,
254
+ color: stopColor
255
+ };
256
+ const stops = [...g.stops, newStop];
257
+ const res = normalizeGradient({ ...g, stops });
258
+ if ("error" in res) throw new Error(res.error);
259
+ const added = res.gradient.stops.find((s) => s.id === id);
260
+ return { gradient: res.gradient, stop: added };
261
+ }
262
+ function listValidIds(g) {
263
+ return g.stops.map((s) => `"${s.id}"`).join(", ");
264
+ }
265
+ function updateGradientStop(g, id, patch) {
266
+ const index = g.stops.findIndex((s) => s.id === id);
267
+ if (index === -1) {
268
+ throw new Error(`Stop "${id}" not found. Valid stop IDs are: ${listValidIds(g)}.`);
269
+ }
270
+ const stops = g.stops.map((s, i) => i === index ? { ...s, ...patch } : s);
271
+ const res = normalizeGradient({ ...g, stops });
272
+ if ("error" in res) throw new Error(res.error);
273
+ return res.gradient;
274
+ }
275
+ function removeGradientStop(g, id) {
276
+ if (g.stops.length <= 2) {
277
+ throw new Error("A gradient needs at least 2 stops; add one with add_gradient_stop before removing this one.");
278
+ }
279
+ const index = g.stops.findIndex((s) => s.id === id);
280
+ if (index === -1) {
281
+ throw new Error(`Stop "${id}" not found. Valid stop IDs are: ${listValidIds(g)}.`);
282
+ }
283
+ const stops = g.stops.filter((s) => s.id !== id);
284
+ const res = normalizeGradient({ ...g, stops });
285
+ if ("error" in res) throw new Error(res.error);
286
+ return res.gradient;
287
+ }
288
+ function reverseGradient(g) {
289
+ const reversedStops = g.stops.map((s) => ({
290
+ ...s,
291
+ offset: Math.round((1 - s.offset) * 1e4) / 1e4,
292
+ midpoint: s.midpoint !== void 0 ? Math.round((1 - s.midpoint) * 1e4) / 1e4 : void 0
293
+ }));
294
+ reversedStops.sort((a, b) => a.offset - b.offset);
295
+ return {
296
+ ...g,
297
+ center: g.center ? { ...g.center } : void 0,
298
+ stops: reversedStops
299
+ };
300
+ }
301
+ function distributeGradientStops(g) {
302
+ const n = g.stops.length;
303
+ const distributed = g.stops.map((s, i) => ({
304
+ ...s,
305
+ offset: Math.round(i / (n - 1) * 1e4) / 1e4
306
+ }));
307
+ return {
308
+ ...g,
309
+ center: g.center ? { ...g.center } : void 0,
310
+ stops: distributed
311
+ };
312
+ }
313
+ function formatPercent(fraction) {
314
+ const pct = Math.round(fraction * 1e3) / 10;
315
+ return `${pct}%`;
316
+ }
317
+ function gradientToCss(g) {
318
+ const prefix = g.repeating ? `repeating-${g.type}-gradient` : `${g.type}-gradient`;
319
+ const parts = [];
320
+ const interpPrefix = g.interpolation && g.interpolation !== "srgb" ? `in ${g.interpolation} ` : "";
321
+ if (g.type === "linear") {
322
+ const angle = g.angle !== void 0 ? `${g.angle}deg` : "180deg";
323
+ parts.push(`${interpPrefix}${angle}`.trim());
324
+ } else if (g.type === "radial") {
325
+ const shape = _nullishCoalesce(g.shape, () => ( "ellipse"));
326
+ const cx = g.center ? formatPercent(g.center.x) : "50%";
327
+ const cy = g.center ? formatPercent(g.center.y) : "50%";
328
+ parts.push(`${interpPrefix}${shape} at ${cx} ${cy}`.trim());
329
+ } else if (g.type === "conic") {
330
+ const angle = g.angle !== void 0 ? `from ${g.angle}deg` : "from 0deg";
331
+ const cx = g.center ? formatPercent(g.center.x) : "50%";
332
+ const cy = g.center ? formatPercent(g.center.y) : "50%";
333
+ parts.push(`${interpPrefix}${angle} at ${cx} ${cy}`.trim());
334
+ }
335
+ for (let i = 0; i < g.stops.length; i++) {
336
+ const s = g.stops[i];
337
+ parts.push(`${s.color} ${formatPercent(s.offset)}`);
338
+ if (s.midpoint !== void 0 && i < g.stops.length - 1) {
339
+ const next = g.stops[i + 1];
340
+ const midOffset = s.offset + (next.offset - s.offset) * s.midpoint;
341
+ parts.push(formatPercent(midOffset));
342
+ }
343
+ }
344
+ return `${prefix}(${parts.join(", ")})`;
345
+ }
346
+ function gradientToCssFallback(g, steps = 16, scheme = "light") {
347
+ const prefix = g.repeating ? `repeating-${g.type}-gradient` : `${g.type}-gradient`;
348
+ const parts = [];
349
+ if (g.type === "linear") {
350
+ const angle = g.angle !== void 0 ? `${g.angle}deg` : "180deg";
351
+ parts.push(angle);
352
+ } else if (g.type === "radial") {
353
+ const shape = _nullishCoalesce(g.shape, () => ( "ellipse"));
354
+ const cx = g.center ? formatPercent(g.center.x) : "50%";
355
+ const cy = g.center ? formatPercent(g.center.y) : "50%";
356
+ parts.push(`${shape} at ${cx} ${cy}`);
357
+ } else if (g.type === "conic") {
358
+ const angle = g.angle !== void 0 ? `from ${g.angle}deg` : "from 0deg";
359
+ const cx = g.center ? formatPercent(g.center.x) : "50%";
360
+ const cy = g.center ? formatPercent(g.center.y) : "50%";
361
+ parts.push(`${angle} at ${cx} ${cy}`);
362
+ }
363
+ for (let i = 0; i <= steps; i++) {
364
+ const t = i / steps;
365
+ const color = sampleGradient(g, t, scheme);
366
+ parts.push(`${color} ${formatPercent(t)}`);
367
+ }
368
+ return `${prefix}(${parts.join(", ")})`;
369
+ }
370
+ function splitCssArgs(str) {
371
+ const parts = [];
372
+ let depth = 0;
373
+ let current = "";
374
+ for (let i = 0; i < str.length; i++) {
375
+ const char = str[i];
376
+ if (char === "(") depth++;
377
+ else if (char === ")") depth = Math.max(0, depth - 1);
378
+ if (char === "," && depth === 0) {
379
+ if (current.trim()) parts.push(current.trim());
380
+ current = "";
381
+ } else {
382
+ current += char;
383
+ }
384
+ }
385
+ if (current.trim()) parts.push(current.trim());
386
+ return parts;
387
+ }
388
+ function parseCssAngle(val) {
389
+ const trimmed = val.trim().toLowerCase();
390
+ if (trimmed.endsWith("deg")) return parseFloat(trimmed);
391
+ if (trimmed.endsWith("turn")) return parseFloat(trimmed) * 360;
392
+ if (trimmed.endsWith("rad")) return parseFloat(trimmed) * 180 / Math.PI;
393
+ if (trimmed.startsWith("to ")) {
394
+ const dir = trimmed.slice(3).trim();
395
+ if (dir === "bottom") return 180;
396
+ if (dir === "top") return 0;
397
+ if (dir === "right") return 90;
398
+ if (dir === "left") return 270;
399
+ if (dir === "bottom right" || dir === "right bottom") return 135;
400
+ if (dir === "bottom left" || dir === "left bottom") return 225;
401
+ if (dir === "top right" || dir === "right top") return 45;
402
+ if (dir === "top left" || dir === "left top") return 315;
403
+ }
404
+ const n = parseFloat(trimmed);
405
+ return Number.isFinite(n) ? n : 180;
406
+ }
407
+ function parseCssGradient(css) {
408
+ if (typeof css !== "string") return null;
409
+ const trimmed = css.trim();
410
+ const match = trimmed.match(/^(repeating-)?(linear|radial|conic)-gradient\s*\(([\s\S]*)\)$/i);
411
+ if (!match) return null;
412
+ const repeating = Boolean(match[1]);
413
+ const type = match[2].toLowerCase();
414
+ const body = match[3];
415
+ const rawArgs = splitCssArgs(body);
416
+ if (rawArgs.length === 0) return null;
417
+ let angle = type === "linear" ? 180 : 0;
418
+ let center = type !== "linear" ? { x: 0.5, y: 0.5 } : void 0;
419
+ let shape = type === "radial" ? "ellipse" : void 0;
420
+ let interpolation = "srgb";
421
+ let stopStartIndex = 0;
422
+ let first = rawArgs[0].trim();
423
+ const inMatch = first.match(/^in\s+([a-z0-9-]+)\s*(.*)$/i);
424
+ if (inMatch) {
425
+ const space = inMatch[1].toLowerCase();
426
+ if (space === "oklab" || space === "oklch" || space === "srgb" || space === "srgb-linear") {
427
+ interpolation = space;
428
+ }
429
+ first = inMatch[2].trim();
430
+ if (!first) {
431
+ stopStartIndex = 1;
432
+ first = _nullishCoalesce(_optionalChain([rawArgs, 'access', _ => _[1], 'optionalAccess', _2 => _2.trim, 'call', _3 => _3()]), () => ( ""));
433
+ }
434
+ }
435
+ if (first) {
436
+ if (type === "linear") {
437
+ if (first.endsWith("deg") || first.endsWith("turn") || first.endsWith("rad") || first.startsWith("to ")) {
438
+ angle = parseCssAngle(first);
439
+ stopStartIndex = stopStartIndex === 0 ? 1 : 2;
440
+ }
441
+ } else if (type === "radial") {
442
+ if (first.includes("circle") || first.includes("ellipse") || first.includes("at ")) {
443
+ if (first.includes("circle")) shape = "circle";
444
+ if (first.includes("ellipse")) shape = "ellipse";
445
+ const atIdx = first.indexOf("at ");
446
+ if (atIdx !== -1) {
447
+ const posParts = first.slice(atIdx + 3).trim().split(/\s+/);
448
+ if (posParts[0]) center = { x: parsePos(posParts[0]), y: posParts[1] ? parsePos(posParts[1]) : parsePos(posParts[0]) };
449
+ }
450
+ stopStartIndex = stopStartIndex === 0 ? 1 : 2;
451
+ }
452
+ } else if (type === "conic") {
453
+ if (first.includes("from ") || first.includes("at ")) {
454
+ const fromMatch = first.match(/from\s+([0-9.]+(?:deg|turn|rad)?)/i);
455
+ if (fromMatch) angle = parseCssAngle(fromMatch[1]);
456
+ const atIdx = first.indexOf("at ");
457
+ if (atIdx !== -1) {
458
+ const posParts = first.slice(atIdx + 3).trim().split(/\s+/);
459
+ if (posParts[0]) center = { x: parsePos(posParts[0]), y: posParts[1] ? parsePos(posParts[1]) : parsePos(posParts[0]) };
460
+ }
461
+ stopStartIndex = stopStartIndex === 0 ? 1 : 2;
462
+ }
463
+ }
464
+ }
465
+ const stopArgs = rawArgs.slice(stopStartIndex);
466
+ if (stopArgs.length === 0) return null;
467
+ const items = [];
468
+ for (const str of stopArgs) {
469
+ const s = str.trim();
470
+ if (/^([0-9.]+)%$/.test(s)) {
471
+ items.push({ kind: "hint", offset: parseFloat(s) / 100 });
472
+ continue;
473
+ }
474
+ const posMatch = s.match(/\s+([0-9.]+)%$/);
475
+ if (posMatch) {
476
+ const colorStr = s.slice(0, posMatch.index).trim();
477
+ const offset = parseFloat(posMatch[1]) / 100;
478
+ items.push({ kind: "stop", color: colorStr, offset });
479
+ } else {
480
+ items.push({ kind: "stop", color: s });
481
+ }
482
+ }
483
+ const rawStops = items.filter((it) => it.kind === "stop");
484
+ if (rawStops.length < 2) return null;
485
+ for (let i = 0; i < rawStops.length; i++) {
486
+ if (rawStops[i].offset === void 0) {
487
+ if (i === 0) rawStops[i].offset = 0;
488
+ else if (i === rawStops.length - 1) rawStops[i].offset = 1;
489
+ else {
490
+ let nextIdx = i + 1;
491
+ while (nextIdx < rawStops.length && rawStops[nextIdx].offset === void 0) {
492
+ nextIdx++;
493
+ }
494
+ const nextOffset = nextIdx < rawStops.length ? rawStops[nextIdx].offset : 1;
495
+ const prevOffset = _nullishCoalesce(rawStops[i - 1].offset, () => ( 0));
496
+ const count = nextIdx - (i - 1);
497
+ for (let k = i; k < nextIdx; k++) {
498
+ rawStops[k].offset = prevOffset + (k - (i - 1)) / count * (nextOffset - prevOffset);
499
+ }
500
+ i = nextIdx - 1;
501
+ }
502
+ }
503
+ }
504
+ const finalStops = [];
505
+ let stopIdx = 0;
506
+ for (let i = 0; i < items.length; i++) {
507
+ const it = items[i];
508
+ if (it.kind === "stop") {
509
+ const stop = {
510
+ id: `s${stopIdx + 1}`,
511
+ offset: clamp(_nullishCoalesce(it.offset, () => ( 0)), 0, 1),
512
+ color: it.color
513
+ };
514
+ if (i + 1 < items.length && items[i + 1].kind === "hint") {
515
+ const hintOffset = items[i + 1].offset;
516
+ const nextStopItem = items.slice(i + 2).find((x) => x.kind === "stop");
517
+ if (nextStopItem && nextStopItem.offset !== void 0) {
518
+ const span = nextStopItem.offset - stop.offset;
519
+ if (span > 0) {
520
+ stop.midpoint = clamp((hintOffset - stop.offset) / span, 0.05, 0.95);
521
+ }
522
+ }
523
+ }
524
+ finalStops.push(stop);
525
+ stopIdx++;
526
+ }
527
+ }
528
+ const res = normalizeGradient({
529
+ type,
530
+ angle,
531
+ center,
532
+ shape,
533
+ interpolation,
534
+ repeating,
535
+ stops: finalStops
536
+ });
537
+ return "gradient" in res ? res.gradient : null;
538
+ }
539
+ function parsePos(str) {
540
+ const s = str.trim();
541
+ if (s.endsWith("%")) return clamp(parseFloat(s) / 100, 0, 1);
542
+ const val = parseFloat(s);
543
+ return Number.isFinite(val) ? clamp(val, 0, 1) : 0.5;
544
+ }
545
+ function gradientToSvgDef(g, id, scheme = "light") {
546
+ const steps = 32;
547
+ const stopsXml = [];
548
+ for (let i = 0; i <= steps; i++) {
549
+ const t = i / steps;
550
+ const color = sampleGradient(g, t, scheme);
551
+ stopsXml.push(` <stop offset="${formatPercent(t)}" stop-color="${color}" />`);
552
+ }
553
+ if (g.type === "radial") {
554
+ const cx = g.center ? formatPercent(g.center.x) : "50%";
555
+ const cy = g.center ? formatPercent(g.center.y) : "50%";
556
+ const r = g.shape === "circle" ? "50%" : "70.7%";
557
+ return `<radialGradient id="${id}" cx="${cx}" cy="${cy}" r="${r}">
558
+ ${stopsXml.join("\n")}
559
+ </radialGradient>`;
560
+ }
561
+ if (g.type === "conic") {
562
+ return `<!-- Note: SVG does not natively support conic gradients; rendering as linear fallback -->
563
+ <linearGradient id="${id}" x1="0%" y1="0%" x2="100%" y2="0%">
564
+ ${stopsXml.join("\n")}
565
+ </linearGradient>`;
566
+ }
567
+ const angle = _nullishCoalesce(g.angle, () => ( 180));
568
+ const rad = (90 - angle) * Math.PI / 180;
569
+ const dx = Math.cos(rad);
570
+ const dy = -Math.sin(rad);
571
+ const x1 = formatPercent(0.5 - dx * 0.5);
572
+ const y1 = formatPercent(0.5 - dy * 0.5);
573
+ const x2 = formatPercent(0.5 + dx * 0.5);
574
+ const y2 = formatPercent(0.5 + dy * 0.5);
575
+ return `<linearGradient id="${id}" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">
576
+ ${stopsXml.join("\n")}
577
+ </linearGradient>`;
578
+ }
579
+ function checkGradient(g, options = {}) {
580
+ const dual = isDualGradient(g) || _chunk7PT4RJL5cjs.isDualColor.call(void 0, options.surface) || _chunk7PT4RJL5cjs.isDualColor.call(void 0, options.text);
581
+ if (dual) {
582
+ const lightG = resolveGradient(g, "light");
583
+ const darkG = resolveGradient(g, "dark");
584
+ const lightText = options.text ? _nullishCoalesce(_chunk7PT4RJL5cjs.resolveColor.call(void 0, options.text, "light"), () => ( void 0)) : void 0;
585
+ const darkText = options.text ? _nullishCoalesce(_chunk7PT4RJL5cjs.resolveColor.call(void 0, options.text, "dark"), () => ( void 0)) : void 0;
586
+ const checkScheme = (grad, txt, schemeName) => {
587
+ const issues2 = [];
588
+ for (let i = 0; i < grad.stops.length - 1; i++) {
589
+ const gap = grad.stops[i + 1].offset - grad.stops[i].offset;
590
+ if (gap < 5e-3) {
591
+ issues2.push(`[${schemeName}] Stops "${grad.stops[i].id}" and "${grad.stops[i + 1].id}" are too close (gap < 0.005).`);
592
+ }
593
+ const c1 = _chunkRD7RX52Gcjs.parseColor.call(void 0, grad.stops[i].color);
594
+ const c2 = _chunkRD7RX52Gcjs.parseColor.call(void 0, grad.stops[i + 1].color);
595
+ if (c1 && c2 && gap < 0.05) {
596
+ const dE = _chunkPPFJHTW5cjs.deltaEOK.call(void 0, c1, c2);
597
+ if (dE > 0.5) {
598
+ issues2.push(
599
+ `[${schemeName}] Banding risk between stops "${grad.stops[i].id}" and "${grad.stops[i + 1].id}" (\u0394EOK = ${dE.toFixed(2)} across gap ${gap.toFixed(3)}).`
600
+ );
601
+ }
602
+ }
603
+ }
604
+ for (const s of grad.stops) {
605
+ const c = _chunkRD7RX52Gcjs.parseColor.call(void 0, s.color);
606
+ if (c && !_chunkPPFJHTW5cjs.inSrgbGamut.call(void 0, c)) {
607
+ issues2.push(`[${schemeName}] Stop "${s.id}" colour ${s.color} is outside standard sRGB gamut.`);
608
+ }
609
+ }
610
+ let lowest;
611
+ if (txt) {
612
+ const parsedTxt = _chunkRD7RX52Gcjs.parseColor.call(void 0, txt);
613
+ if (parsedTxt) {
614
+ let minRatio = Infinity;
615
+ for (let step = 0; step <= 32; step++) {
616
+ const sample = _chunkRD7RX52Gcjs.parseColor.call(void 0, sampleGradient(grad, step / 32, schemeName));
617
+ if (sample) {
618
+ const r = _chunkRD7RX52Gcjs.contrastRatio.call(void 0, parsedTxt, sample);
619
+ if (r < minRatio) minRatio = r;
620
+ }
621
+ }
622
+ lowest = Math.round(minRatio * 100) / 100;
623
+ }
624
+ }
625
+ return { issues: issues2, lowest };
626
+ };
627
+ const lRes = checkScheme(lightG, lightText, "light");
628
+ const dRes = checkScheme(darkG, darkText, "dark");
629
+ return {
630
+ issues: [...lRes.issues, ...dRes.issues],
631
+ lowestTextContrast: lRes.lowest !== void 0 && dRes.lowest !== void 0 ? { light: lRes.lowest, dark: dRes.lowest } : void 0
632
+ };
633
+ }
634
+ const issues = [];
635
+ for (let i = 0; i < g.stops.length - 1; i++) {
636
+ const gap = g.stops[i + 1].offset - g.stops[i].offset;
637
+ if (gap < 5e-3) {
638
+ issues.push(`Stops "${g.stops[i].id}" and "${g.stops[i + 1].id}" are too close (gap < 0.005).`);
639
+ }
640
+ const c1 = _chunkRD7RX52Gcjs.parseColor.call(void 0, g.stops[i].color);
641
+ const c2 = _chunkRD7RX52Gcjs.parseColor.call(void 0, g.stops[i + 1].color);
642
+ if (c1 && c2 && gap < 0.05) {
643
+ const dE = _chunkPPFJHTW5cjs.deltaEOK.call(void 0, c1, c2);
644
+ if (dE > 0.5) {
645
+ issues.push(
646
+ `Banding risk between stops "${g.stops[i].id}" and "${g.stops[i + 1].id}" (\u0394EOK = ${dE.toFixed(2)} across gap ${gap.toFixed(3)}).`
647
+ );
648
+ }
649
+ }
650
+ }
651
+ for (const s of g.stops) {
652
+ const c = _chunkRD7RX52Gcjs.parseColor.call(void 0, s.color);
653
+ if (c && !_chunkPPFJHTW5cjs.inSrgbGamut.call(void 0, c)) {
654
+ issues.push(`Stop "${s.id}" colour ${s.color} is outside standard sRGB gamut.`);
655
+ }
656
+ }
657
+ let lowestTextContrast;
658
+ if (options.text) {
659
+ const parsedTxt = _chunkRD7RX52Gcjs.parseColor.call(void 0, options.text);
660
+ if (parsedTxt) {
661
+ let minRatio = Infinity;
662
+ for (let step = 0; step <= 32; step++) {
663
+ const sample = _chunkRD7RX52Gcjs.parseColor.call(void 0, sampleGradient(g, step / 32, "light"));
664
+ if (sample) {
665
+ const r = _chunkRD7RX52Gcjs.contrastRatio.call(void 0, parsedTxt, sample);
666
+ if (r < minRatio) minRatio = r;
667
+ }
668
+ }
669
+ lowestTextContrast = Math.round(minRatio * 100) / 100;
670
+ }
671
+ }
672
+ return { issues, lowestTextContrast };
673
+ }
674
+ var GRADIENT_PRESETS = Object.freeze({
675
+ sunset: {
676
+ type: "linear",
677
+ angle: 90,
678
+ interpolation: "oklab",
679
+ stops: [
680
+ { id: "s1", offset: 0, color: "#f97316" },
681
+ { id: "s2", offset: 1, color: "#ec4899" }
682
+ ]
683
+ },
684
+ ocean: {
685
+ type: "linear",
686
+ angle: 180,
687
+ interpolation: "oklab",
688
+ stops: [
689
+ { id: "s1", offset: 0, color: "#06b6d4" },
690
+ { id: "s2", offset: 1, color: "#3b82f6" }
691
+ ]
692
+ },
693
+ "mono-fade": {
694
+ type: "linear",
695
+ angle: 180,
696
+ interpolation: "oklab",
697
+ stops: [
698
+ { id: "s1", offset: 0, color: "#ffffff" },
699
+ { id: "s2", offset: 1, color: "#000000" }
700
+ ]
701
+ },
702
+ "transparent-fade": {
703
+ type: "linear",
704
+ angle: 180,
705
+ interpolation: "oklab",
706
+ stops: [
707
+ { id: "s1", offset: 0, color: "#000000ff" },
708
+ { id: "s2", offset: 1, color: "#00000000" }
709
+ ]
710
+ },
711
+ aurora: {
712
+ type: "linear",
713
+ angle: 135,
714
+ interpolation: "oklab",
715
+ stops: [
716
+ { id: "s1", offset: 0, color: "#10b981" },
717
+ { id: "s2", offset: 0.5, color: "#06b6d4" },
718
+ { id: "s3", offset: 1, color: "#8b5cf6" }
719
+ ]
720
+ },
721
+ neon: {
722
+ type: "linear",
723
+ angle: 90,
724
+ interpolation: "oklab",
725
+ stops: [
726
+ { id: "s1", offset: 0, color: "#f43f5e" },
727
+ { id: "s2", offset: 1, color: "#8b5cf6" }
728
+ ]
729
+ },
730
+ radial: {
731
+ type: "radial",
732
+ shape: "ellipse",
733
+ center: { x: 0.5, y: 0.5 },
734
+ interpolation: "oklab",
735
+ stops: [
736
+ { id: "s1", offset: 0, color: "#ffffff" },
737
+ { id: "s2", offset: 1, color: "#000000" }
738
+ ]
739
+ },
740
+ conic: {
741
+ type: "conic",
742
+ angle: 0,
743
+ center: { x: 0.5, y: 0.5 },
744
+ interpolation: "oklab",
745
+ stops: [
746
+ { id: "s1", offset: 0, color: "#f43f5e" },
747
+ { id: "s2", offset: 0.33, color: "#eab308" },
748
+ { id: "s3", offset: 0.66, color: "#06b6d4" },
749
+ { id: "s4", offset: 1, color: "#f43f5e" }
750
+ ]
751
+ }
752
+ });
753
+
754
+ // src/gradient/editor.ts
755
+ function stopFromTrackPosition(g, x) {
756
+ return Math.max(0, Math.min(1, Math.round(x * 1e3) / 1e3));
757
+ }
758
+ function trackPositionFromStop(stop) {
759
+ return Math.max(0, Math.min(1, stop.offset));
760
+ }
761
+ function gradientKeyStep(g, id, key, shift = false) {
762
+ const stop = g.stops.find((s) => s.id === id);
763
+ if (!stop) return null;
764
+ const step = shift ? 0.1 : 0.01;
765
+ switch (key) {
766
+ case "ArrowLeft":
767
+ case "ArrowDown": {
768
+ const offset = Math.max(0, Math.round((stop.offset - step) * 1e3) / 1e3);
769
+ return { stopId: id, offset };
770
+ }
771
+ case "ArrowRight":
772
+ case "ArrowUp": {
773
+ const offset = Math.min(1, Math.round((stop.offset + step) * 1e3) / 1e3);
774
+ return { stopId: id, offset };
775
+ }
776
+ case "Delete":
777
+ case "Backspace": {
778
+ return { stopId: id, remove: true };
779
+ }
780
+ default:
781
+ return null;
782
+ }
783
+ }
784
+ function angleFromPointer(center, point, snapStep) {
785
+ const dx = point.x - center.x;
786
+ const dy = point.y - center.y;
787
+ let deg = Math.atan2(dx, -dy) * 180 / Math.PI;
788
+ if (deg < 0) deg += 360;
789
+ if (snapStep && snapStep > 0) {
790
+ deg = Math.round(deg / snapStep) * snapStep;
791
+ if (deg >= 360) deg -= 360;
792
+ }
793
+ return Math.round(deg);
794
+ }
795
+ function gradientTrackBackground(g) {
796
+ const linearCopy = {
797
+ ...g,
798
+ type: "linear",
799
+ angle: 90
800
+ };
801
+ return gradientToCss(linearCopy);
802
+ }
803
+
804
+ // src/gradient/sample.ts
805
+ var DEFAULT_TOLERANCE = 0.06;
806
+ function toRgba(color) {
807
+ if (typeof color === "string") return _chunkRD7RX52Gcjs.parseColor.call(void 0, color);
808
+ if (Array.isArray(color) && color.length >= 4 && color.slice(0, 4).every((v) => Number.isFinite(v))) {
809
+ return { r: color[0] / 255, g: color[1] / 255, b: color[2] / 255, a: color[3] / 255 };
810
+ }
811
+ return null;
812
+ }
813
+ var dist = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2], (a[3] - b[3]) * 0.5);
814
+ function lerpAt(a, b, t) {
815
+ const u = b.t - a.t > 1e-9 ? (t - a.t) / (b.t - a.t) : 0;
816
+ return [0, 1, 2, 3].map((k) => a.v[k] + (b.v[k] - a.v[k]) * u);
817
+ }
818
+ function worst(samples, i0, i1) {
819
+ let index = -1;
820
+ let error = 0;
821
+ for (let i = i0 + 1; i < i1; i++) {
822
+ const e = dist(samples[i].v, lerpAt(samples[i0], samples[i1], samples[i].t));
823
+ if (e > error) {
824
+ error = e;
825
+ index = i;
826
+ }
827
+ }
828
+ return { index, error };
829
+ }
830
+ function maxErrorOf(samples, at) {
831
+ let e = 0;
832
+ for (let k = 0; k + 1 < at.length; k++) e = Math.max(e, worst(samples, at[k], at[k + 1]).error);
833
+ return e;
834
+ }
835
+ function fitGradientStops(samples, options = {}) {
836
+ const tolerance = _nullishCoalesce(options.tolerance, () => ( DEFAULT_TOLERANCE));
837
+ if (!(tolerance > 0 && tolerance <= 1)) throw new Error(`tolerance ${tolerance} is out of range; use a \u0394E above 0 and at most 1 (0.02 is a just-noticeable step).`);
838
+ const minStops = Math.max(2, Math.floor(_nullishCoalesce(options.minStops, () => ( 2))));
839
+ const maxStops = Math.floor(_nullishCoalesce(options.maxStops, () => ( Math.max(5, minStops))));
840
+ if (!(maxStops >= minStops)) throw new Error(`maxStops ${options.maxStops} is below minStops ${minStops}.`);
841
+ const smoothing = Math.max(0, Math.floor(_nullishCoalesce(options.smoothing, () => ( 2))));
842
+ const raw = [];
843
+ for (const s of samples) {
844
+ if (!s || !Number.isFinite(s.offset)) continue;
845
+ const c = toRgba(s.color);
846
+ if (!c) continue;
847
+ const lab2 = _chunk7AHBW2CQcjs.rgbToOklab.call(void 0, { ...c, a: _nullishCoalesce(c.a, () => ( 1)) });
848
+ raw.push({ t: Math.min(1, Math.max(0, s.offset)), v: [lab2.l, lab2.a, lab2.b, _nullishCoalesce(c.a, () => ( 1))] });
849
+ }
850
+ if (raw.length < 2) throw new Error(`Need at least two readable samples to make a gradient; got ${raw.length}.`);
851
+ raw.sort((a, b) => a.t - b.t);
852
+ const lab = raw.map((s, i) => {
853
+ const r = Math.min(smoothing, i, raw.length - 1 - i);
854
+ const v = [0, 0, 0, 0];
855
+ for (let j = i - r; j <= i + r; j++) for (let k = 0; k < 4; k++) v[k] += raw[j].v[k] / (2 * r + 1);
856
+ return { t: s.t, v };
857
+ });
858
+ const at = [0, lab.length - 1];
859
+ while (at.length < maxStops) {
860
+ let best = { index: -1, error: 0 };
861
+ for (let k = 0; k + 1 < at.length; k++) {
862
+ const w = worst(lab, at[k], at[k + 1]);
863
+ if (w.error > best.error) best = w;
864
+ }
865
+ if (best.index < 0 && at.length < minStops) {
866
+ let k = 0;
867
+ for (let j = 1; j + 1 < at.length; j++) if (at[j + 1] - at[j] > at[k + 1] - at[k]) k = j;
868
+ if (at[k + 1] - at[k] > 1) best = { index: at[k] + at[k + 1] >> 1, error: 0 };
869
+ }
870
+ if (best.index < 0 || best.error <= tolerance && at.length >= minStops) break;
871
+ at.push(best.index);
872
+ at.sort((a, b) => a - b);
873
+ }
874
+ for (let k = 1; k + 1 < at.length && at.length > minStops; ) {
875
+ if (worst(lab, at[k - 1], at[k + 1]).error <= tolerance) at.splice(k, 1);
876
+ else k++;
877
+ }
878
+ const stops = at.map((i, k) => {
879
+ const [l, a, b, alpha] = lab[i].v;
880
+ const color = _chunkPPFJHTW5cjs.formatHex.call(void 0, _chunk7AHBW2CQcjs.oklabToRgb.call(void 0, { l, a, b, alpha: Math.min(1, Math.max(0, alpha)) }));
881
+ return { id: `s${k + 1}`, offset: k === 0 ? 0 : k === at.length - 1 ? 1 : Math.round(lab[i].t * 1e3) / 1e3, color };
882
+ });
883
+ return { stops, maxError: Math.round(maxErrorOf(lab, at) * 1e4) / 1e4 };
884
+ }
885
+ function simplifyGradientStops(samples, options) {
886
+ return fitGradientStops(samples, options).stops;
887
+ }
888
+ function sampleGradientFromLine(readPixel, start, end, options = {}) {
889
+ const count = Math.floor(_nullishCoalesce(options.sampleCount, () => ( 32)));
890
+ if (!(count >= 2 && count <= 1024)) throw new Error(`sampleCount ${options.sampleCount} is out of range; use 2..1024.`);
891
+ if (![start.x, start.y, end.x, end.y].every(Number.isFinite)) throw new Error("start and end need finite x and y.");
892
+ const length = Math.hypot(end.x - start.x, end.y - start.y);
893
+ if (length < 1e-9) throw new Error("start and end are the same point; drag a line to sample along.");
894
+ const angle = angleFromPointer(start, end, options.snapStep);
895
+ const to = options.snapStep ? { x: start.x + Math.sin(angle * Math.PI / 180) * length, y: start.y - Math.cos(angle * Math.PI / 180) * length } : end;
896
+ const samples = [];
897
+ for (let i = 0; i < count; i++) {
898
+ const t = i / (count - 1);
899
+ const color = readPixel({ x: start.x + (to.x - start.x) * t, y: start.y + (to.y - start.y) * t });
900
+ if (color !== null && color !== void 0) samples.push({ offset: t, color });
901
+ }
902
+ if (samples.length < 2) return null;
903
+ const { stops } = fitGradientStops(samples, options);
904
+ const res = normalizeGradient({ type: "linear", angle, stops, interpolation: "oklab" });
905
+ if ("error" in res) throw new Error(res.error);
906
+ return res.gradient;
907
+ }
908
+
909
+
910
+
911
+
912
+
913
+
914
+
915
+
916
+
917
+
918
+
919
+
920
+
921
+
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+ exports.gradientMidpointRemap = gradientMidpointRemap; exports.normalizeGradient = normalizeGradient; exports.createGradient = createGradient; exports.isDualColorValue = isDualColorValue; exports.isDualGradient = isDualGradient; exports.resolveGradient = resolveGradient; exports.sampleGradient = sampleGradient; exports.insertGradientStop = insertGradientStop; exports.updateGradientStop = updateGradientStop; exports.removeGradientStop = removeGradientStop; exports.reverseGradient = reverseGradient; exports.distributeGradientStops = distributeGradientStops; exports.gradientToCss = gradientToCss; exports.gradientToCssFallback = gradientToCssFallback; exports.parseCssGradient = parseCssGradient; exports.gradientToSvgDef = gradientToSvgDef; exports.checkGradient = checkGradient; exports.GRADIENT_PRESETS = GRADIENT_PRESETS; exports.stopFromTrackPosition = stopFromTrackPosition; exports.trackPositionFromStop = trackPositionFromStop; exports.gradientKeyStep = gradientKeyStep; exports.angleFromPointer = angleFromPointer; exports.gradientTrackBackground = gradientTrackBackground; exports.fitGradientStops = fitGradientStops; exports.simplifyGradientStops = simplifyGradientStops; exports.sampleGradientFromLine = sampleGradientFromLine;