javascript-excel 2.0.0-alpha.4 → 2.0.0-alpha.6

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/dom.cjs CHANGED
@@ -24,7 +24,16 @@ function autoWidths(rows, options = {}) {
24
24
  return Array.from(widest, (w) => Math.min(max, Math.max(min, (w ?? 0) + require_sheet_names.AUTO_WIDTH.padding)));
25
25
  }
26
26
  // src/internal/css-color.ts
27
- /** CSS colour values to RRGGBB, for computed-style mapping in javascript-excel/dom. */
27
+ /**
28
+ * CSS colour values to RRGGBB, for computed-style mapping in javascript-excel/dom.
29
+ *
30
+ * Browsers serialise computed colours in the space they were authored in, so a computed style
31
+ * can be `rgb()`/`rgba()`, `lab()`, `lch()`, `oklab()`, `oklch()` or `color(<space> …)` (CSS
32
+ * Color 4). Every form is converted to sRGB: through linear sRGB (OKLab), or through CIE XYZ
33
+ * (CIE Lab/LCH in D50 with Bradford adaptation, and the predefined RGB and XYZ spaces). Colours
34
+ * outside sRGB are clipped per channel. `none` components count as 0. The matrices are the ones
35
+ * in the CSS Color 4 sample code.
36
+ */
28
37
  const NAMED = {
29
38
  black: "000000",
30
39
  white: "FFFFFF",
@@ -47,6 +56,390 @@ const NAMED = {
47
56
  navy: "000080",
48
57
  orange: "FFA500"
49
58
  };
59
+ const mul = (m, v) => [
60
+ m[0][0] * v[0] + m[0][1] * v[1] + m[0][2] * v[2],
61
+ m[1][0] * v[0] + m[1][1] * v[1] + m[1][2] * v[2],
62
+ m[2][0] * v[0] + m[2][1] * v[1] + m[2][2] * v[2]
63
+ ];
64
+ const XYZ65_TO_LSRGB = [
65
+ [
66
+ 3.2409699419045226,
67
+ -1.537383177570094,
68
+ -.4986107602930034
69
+ ],
70
+ [
71
+ -.9692436362808796,
72
+ 1.8759675015077202,
73
+ .04155505740717559
74
+ ],
75
+ [
76
+ .05563007969699366,
77
+ -.20397695888897652,
78
+ 1.0569715142428786
79
+ ]
80
+ ];
81
+ /** Bradford chromatic adaptation, D50 to D65. */
82
+ const D50_TO_D65 = [
83
+ [
84
+ .955473421488075,
85
+ -.02309845494876471,
86
+ .06325924320057072
87
+ ],
88
+ [
89
+ -.0283697093338637,
90
+ 1.0099953980813041,
91
+ .021041441191917323
92
+ ],
93
+ [
94
+ .012314014864481998,
95
+ -.020507649298898964,
96
+ 1.330365926242124
97
+ ]
98
+ ];
99
+ const LP3_TO_XYZ65 = [
100
+ [
101
+ .4865709486482162,
102
+ .26566769316909306,
103
+ .1982172852343625
104
+ ],
105
+ [
106
+ .2289745640697488,
107
+ .6917385218365064,
108
+ .079286914093745
109
+ ],
110
+ [
111
+ 0,
112
+ .04511338185890264,
113
+ 1.043944368900976
114
+ ]
115
+ ];
116
+ const LA98_TO_XYZ65 = [
117
+ [
118
+ .5766690429101305,
119
+ .1855582379065463,
120
+ .1882286462349947
121
+ ],
122
+ [
123
+ .29734497525053605,
124
+ .6273635662554661,
125
+ .07529145849399788
126
+ ],
127
+ [
128
+ .02703136138641234,
129
+ .07068885253582723,
130
+ .9913375368376388
131
+ ]
132
+ ];
133
+ const LPROPHOTO_TO_XYZ50 = [
134
+ [
135
+ .7977666449006423,
136
+ .13518129740053308,
137
+ .0313477341283922
138
+ ],
139
+ [
140
+ .2880748288194013,
141
+ .711835234241873,
142
+ 8993693872564e-17
143
+ ],
144
+ [
145
+ 0,
146
+ 0,
147
+ .8251046025104602
148
+ ]
149
+ ];
150
+ const LREC2020_TO_XYZ65 = [
151
+ [
152
+ .6369580483012914,
153
+ .14461690358620832,
154
+ .1688809751641721
155
+ ],
156
+ [
157
+ .2627002120112671,
158
+ .6779980715188708,
159
+ .05930171646986196
160
+ ],
161
+ [
162
+ 0,
163
+ .028072693049087428,
164
+ 1.060985057710791
165
+ ]
166
+ ];
167
+ const OKLAB_TO_LMS = [
168
+ [
169
+ 1,
170
+ .3963377773761749,
171
+ .2158037573099136
172
+ ],
173
+ [
174
+ 1,
175
+ -.1055613458156586,
176
+ -.0638541728258133
177
+ ],
178
+ [
179
+ 1,
180
+ -.0894841775298119,
181
+ -1.2914855480194092
182
+ ]
183
+ ];
184
+ const LMS_TO_LSRGB = [
185
+ [
186
+ 4.076741636075958,
187
+ -3.307711539258063,
188
+ .2309699031821043
189
+ ],
190
+ [
191
+ -1.2684379732850315,
192
+ 2.609757349287688,
193
+ -.3413193760026573
194
+ ],
195
+ [
196
+ -.0041960761386756,
197
+ -.7034186179359363,
198
+ 1.7076146940746117
199
+ ]
200
+ ];
201
+ const signed = (c, f) => Math.sign(c) * f(Math.abs(c));
202
+ /** The sRGB (and display-p3) transfer function, inverse: encoded to linear. */
203
+ const srgbLinear = (c) => signed(c, (a) => a <= .04045 ? a / 12.92 : ((a + .055) / 1.055) ** 2.4);
204
+ const srgbEncode = (c) => signed(c, (a) => a > .0031308 ? 1.055 * a ** (1 / 2.4) - .055 : 12.92 * a);
205
+ const a98Linear = (c) => signed(c, (a) => a ** (563 / 256));
206
+ const prophotoLinear = (c) => signed(c, (a) => a <= 16 / 512 ? a / 16 : a ** 1.8);
207
+ const REC_A = 1.09929682680944;
208
+ const REC_B = .018053968510807;
209
+ const rec2020Linear = (c) => signed(c, (a) => a < REC_B * 4.5 ? a / 4.5 : ((a + REC_A - 1) / REC_A) ** (1 / .45));
210
+ const xyz65ToLinearSrgb = (v) => mul(XYZ65_TO_LSRGB, v);
211
+ const xyz50ToLinearSrgb = (v) => mul(XYZ65_TO_LSRGB, mul(D50_TO_D65, v));
212
+ const D50 = [
213
+ .3457 / .3585,
214
+ 1,
215
+ .2958 / .3585
216
+ ];
217
+ function labToLinearSrgb(l, a, b) {
218
+ const k = 24389 / 27;
219
+ const e = 216 / 24389;
220
+ const fy = (l + 16) / 116;
221
+ const fx = a / 500 + fy;
222
+ const fz = fy - b / 200;
223
+ const xyz = [
224
+ fx ** 3 > e ? fx ** 3 : (116 * fx - 16) / k,
225
+ l > k * e ? fy ** 3 : l / k,
226
+ fz ** 3 > e ? fz ** 3 : (116 * fz - 16) / k
227
+ ];
228
+ return xyz50ToLinearSrgb([
229
+ xyz[0] * D50[0],
230
+ xyz[1] * D50[1],
231
+ xyz[2] * D50[2]
232
+ ]);
233
+ }
234
+ function oklabToLinearSrgb(l, a, b) {
235
+ const lms = mul(OKLAB_TO_LMS, [
236
+ l,
237
+ a,
238
+ b
239
+ ]);
240
+ return mul(LMS_TO_LSRGB, [
241
+ lms[0] ** 3,
242
+ lms[1] ** 3,
243
+ lms[2] ** 3
244
+ ]);
245
+ }
246
+ const toHex = (encoded) => encoded.map((c) => Math.round(Math.min(1, Math.max(0, Number.isFinite(c) ? c : 0)) * 255).toString(16).padStart(2, "0")).join("").toUpperCase();
247
+ const linearToHex = (v) => toHex([
248
+ srgbEncode(v[0]),
249
+ srgbEncode(v[1]),
250
+ srgbEncode(v[2])
251
+ ]);
252
+ /** One component: a number, a percentage (scaled so 100% = `pct`), an angle in degrees, or none (0). */
253
+ function num(token, pct) {
254
+ if (token === "none") return 0;
255
+ const m = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|grad|rad|turn)?$/.exec(token);
256
+ if (!m) return void 0;
257
+ const n = parseFloat(m[1]);
258
+ switch (m[2]) {
259
+ case "%": return n * pct / 100;
260
+ case "grad": return n * 360 / 400;
261
+ case "rad": return n * 180 / Math.PI;
262
+ case "turn": return n * 360;
263
+ default: return n;
264
+ }
265
+ }
266
+ /** The components and optional alpha of a functional colour, or undefined if it is malformed. */
267
+ function parts(body) {
268
+ const [main, alpha, extra] = body.split("/");
269
+ if (extra !== void 0 || main === void 0) return void 0;
270
+ const c = main.trim().split(/\s*,\s*|\s+/).filter(Boolean);
271
+ const a = alpha?.trim();
272
+ if (a === "") return void 0;
273
+ if (a === void 0 && c.length === 4 && main.includes(",")) return {
274
+ c: c.slice(0, 3),
275
+ alpha: c[3]
276
+ };
277
+ return {
278
+ c,
279
+ alpha: a
280
+ };
281
+ }
282
+ function hueRgb(h, s, l) {
283
+ const f = (n) => {
284
+ const k = (n + h / 30) % 12;
285
+ return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1));
286
+ };
287
+ return [
288
+ f(0),
289
+ f(8),
290
+ f(4)
291
+ ];
292
+ }
293
+ /** The predefined colour spaces of color(): encoded components to linear sRGB. */
294
+ const SPACES = {
295
+ srgb: (v) => [
296
+ srgbLinear(v[0]),
297
+ srgbLinear(v[1]),
298
+ srgbLinear(v[2])
299
+ ],
300
+ "srgb-linear": (v) => v,
301
+ "display-p3": (v) => xyz65ToLinearSrgb(mul(LP3_TO_XYZ65, [
302
+ srgbLinear(v[0]),
303
+ srgbLinear(v[1]),
304
+ srgbLinear(v[2])
305
+ ])),
306
+ "a98-rgb": (v) => xyz65ToLinearSrgb(mul(LA98_TO_XYZ65, [
307
+ a98Linear(v[0]),
308
+ a98Linear(v[1]),
309
+ a98Linear(v[2])
310
+ ])),
311
+ "prophoto-rgb": (v) => xyz50ToLinearSrgb(mul(LPROPHOTO_TO_XYZ50, [
312
+ prophotoLinear(v[0]),
313
+ prophotoLinear(v[1]),
314
+ prophotoLinear(v[2])
315
+ ])),
316
+ rec2020: (v) => xyz65ToLinearSrgb(mul(LREC2020_TO_XYZ65, [
317
+ rec2020Linear(v[0]),
318
+ rec2020Linear(v[1]),
319
+ rec2020Linear(v[2])
320
+ ])),
321
+ xyz: xyz65ToLinearSrgb,
322
+ "xyz-d65": xyz65ToLinearSrgb,
323
+ "xyz-d50": xyz50ToLinearSrgb
324
+ };
325
+ function functional(fn, body) {
326
+ const p = parts(body);
327
+ if (!p) return void 0;
328
+ let space;
329
+ let c = p.c;
330
+ if (fn === "color") {
331
+ space = c[0];
332
+ c = c.slice(1);
333
+ }
334
+ if (c.length !== 3) return void 0;
335
+ if (p.alpha !== void 0) {
336
+ const a = num(p.alpha, 1);
337
+ if (a === void 0) return void 0;
338
+ if (!(a > 0)) return void 0;
339
+ }
340
+ const s = {
341
+ rgb: [
342
+ 255,
343
+ 255,
344
+ 255
345
+ ],
346
+ rgba: [
347
+ 255,
348
+ 255,
349
+ 255
350
+ ],
351
+ hsl: [
352
+ 1,
353
+ 100,
354
+ 100
355
+ ],
356
+ hsla: [
357
+ 1,
358
+ 100,
359
+ 100
360
+ ],
361
+ hwb: [
362
+ 1,
363
+ 100,
364
+ 100
365
+ ],
366
+ lab: [
367
+ 100,
368
+ 125,
369
+ 125
370
+ ],
371
+ lch: [
372
+ 100,
373
+ 150,
374
+ 1
375
+ ],
376
+ oklab: [
377
+ 1,
378
+ .4,
379
+ .4
380
+ ],
381
+ oklch: [
382
+ 1,
383
+ .4,
384
+ 1
385
+ ],
386
+ color: [
387
+ 1,
388
+ 1,
389
+ 1
390
+ ]
391
+ }[fn];
392
+ if (!s) return void 0;
393
+ const v = [];
394
+ for (let i = 0; i < 3; i++) {
395
+ const n = num(c[i], s[i]);
396
+ if (n === void 0) return void 0;
397
+ v.push(n);
398
+ }
399
+ const [x, y, z] = v;
400
+ const rad = (deg) => deg * Math.PI / 180;
401
+ switch (fn) {
402
+ case "rgb":
403
+ case "rgba": return toHex([
404
+ x / 255,
405
+ y / 255,
406
+ z / 255
407
+ ]);
408
+ case "hsl":
409
+ case "hsla": return toHex(hueRgb((x % 360 + 360) % 360, Math.max(0, y) / 100, z / 100));
410
+ case "hwb": {
411
+ const w = Math.max(0, y) / 100;
412
+ const b = Math.max(0, z) / 100;
413
+ if (w + b >= 1) {
414
+ const g = w / (w + b);
415
+ return toHex([
416
+ g,
417
+ g,
418
+ g
419
+ ]);
420
+ }
421
+ return toHex(hueRgb((x % 360 + 360) % 360, 1, .5).map((ch) => ch * (1 - w - b) + w));
422
+ }
423
+ case "lab": return linearToHex(labToLinearSrgb(Math.min(100, Math.max(0, x)), y, z));
424
+ case "lch": {
425
+ const ch = Math.max(0, y);
426
+ return linearToHex(labToLinearSrgb(Math.min(100, Math.max(0, x)), ch * Math.cos(rad(z)), ch * Math.sin(rad(z))));
427
+ }
428
+ case "oklab": return linearToHex(oklabToLinearSrgb(Math.min(1, Math.max(0, x)), y, z));
429
+ case "oklch": {
430
+ const ch = Math.max(0, y);
431
+ return linearToHex(oklabToLinearSrgb(Math.min(1, Math.max(0, x)), ch * Math.cos(rad(z)), ch * Math.sin(rad(z))));
432
+ }
433
+ default: {
434
+ const conv = space === void 0 ? void 0 : SPACES[space];
435
+ return conv ? linearToHex(conv([
436
+ x,
437
+ y,
438
+ z
439
+ ])) : void 0;
440
+ }
441
+ }
442
+ }
50
443
  /** A CSS colour as RRGGBB, or undefined for transparent or unparseable values. */
51
444
  function cssColor(value) {
52
445
  const v = value.trim().toLowerCase();
@@ -61,18 +454,8 @@ function cssColor(value) {
61
454
  if (hex.length === 8) return hex.slice(6) === "00" ? void 0 : hex.slice(0, 6).toUpperCase();
62
455
  return;
63
456
  }
64
- const m = /^rgba?\(\s*([\d.]+%?)[\s,]+([\d.]+%?)[\s,]+([\d.]+%?)(?:\s*[,/]\s*([\d.]+%?))?\s*\)$/.exec(v);
65
- if (m) {
66
- const ch = (s) => Math.round(Math.min(255, Math.max(0, s.endsWith("%") ? parseFloat(s) * 255 / 100 : parseFloat(s))));
67
- if (m[4] !== void 0) {
68
- if (!((m[4].endsWith("%") ? parseFloat(m[4]) / 100 : parseFloat(m[4])) > 0)) return void 0;
69
- }
70
- return [
71
- m[1],
72
- m[2],
73
- m[3]
74
- ].map((s) => ch(s).toString(16).padStart(2, "0")).join("").toUpperCase();
75
- }
457
+ const fn = /^([a-z]+)\(\s*([^()]*)\)$/.exec(v);
458
+ if (fn) return functional(fn[1], fn[2]);
76
459
  return NAMED[v];
77
460
  }
78
461
  function isElement(n) {