@visulima/colorize 1.4.25 → 1.4.27

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.
Files changed (32) hide show
  1. package/CHANGELOG.md +41 -1
  2. package/README.md +208 -12
  3. package/dist/gradient.cjs +60 -3
  4. package/dist/gradient.mjs +54 -3
  5. package/dist/index.browser.mjs +63 -1
  6. package/dist/index.server.cjs +249 -2
  7. package/dist/index.server.mjs +63 -1
  8. package/dist/packem_shared/Colorize-BenP3bFn.mjs +241 -0
  9. package/dist/packem_shared/Colorize-Ca9CXTcj.mjs +413 -0
  10. package/dist/packem_shared/GradientBuilder-Ba4Teq6P.mjs +487 -0
  11. package/dist/packem_shared/GradientBuilder-DjJK22kP.cjs +491 -0
  12. package/dist/packem_shared/colorize.server-BA3gZZXz.cjs +193 -0
  13. package/dist/packem_shared/colorize.server-ry9FZNfG.mjs +191 -0
  14. package/dist/packem_shared/convertHexToRgb-CWdAm2kE.mjs +13 -0
  15. package/dist/packem_shared/convertHexToRgb-DvkHBM3-.cjs +17 -0
  16. package/dist/packem_shared/rgbToAnsi256-BdS0fomP.mjs +41 -0
  17. package/dist/packem_shared/rgbToAnsi256-DL8eajTz.cjs +47 -0
  18. package/dist/template.cjs +144 -2
  19. package/dist/template.mjs +138 -2
  20. package/dist/utils.cjs +13 -1
  21. package/dist/utils.mjs +2 -1
  22. package/package.json +2 -2
  23. package/dist/packem_shared/Colorize-Bn8gI3ME.mjs +0 -1
  24. package/dist/packem_shared/Colorize-CfIXnDUD.mjs +0 -2
  25. package/dist/packem_shared/GradientBuilder-C2IS34v_.cjs +0 -1
  26. package/dist/packem_shared/GradientBuilder-I_j6wpvU.mjs +0 -1
  27. package/dist/packem_shared/ansi256To16--f09kvlF.cjs +0 -1
  28. package/dist/packem_shared/ansi256To16-YETEPW8R.mjs +0 -1
  29. package/dist/packem_shared/colorize.server-9hV-s1hY.mjs +0 -2
  30. package/dist/packem_shared/colorize.server-B0BWdm8_.cjs +0 -2
  31. package/dist/packem_shared/convertHexToRgb-BEnfEytq.cjs +0 -1
  32. package/dist/packem_shared/convertHexToRgb-DGcGUKnj.mjs +0 -1
@@ -0,0 +1,487 @@
1
+ import { convertHexToRgb } from './convertHexToRgb-CWdAm2kE.mjs';
2
+
3
+ const colorNames = {
4
+ aliceblue: [240, 248, 255],
5
+ antiquewhite: [250, 235, 215],
6
+ aqua: [0, 255, 255],
7
+ aquamarine: [127, 255, 212],
8
+ azure: [240, 255, 255],
9
+ beige: [245, 245, 220],
10
+ bisque: [255, 228, 196],
11
+ black: [0, 0, 0],
12
+ blanchedalmond: [255, 235, 205],
13
+ blue: [0, 0, 255],
14
+ blueviolet: [138, 43, 226],
15
+ brown: [165, 42, 42],
16
+ burlywood: [222, 184, 135],
17
+ cadetblue: [95, 158, 160],
18
+ chartreuse: [127, 255, 0],
19
+ chocolate: [210, 105, 30],
20
+ coral: [255, 127, 80],
21
+ cornflowerblue: [100, 149, 237],
22
+ cornsilk: [255, 248, 220],
23
+ crimson: [220, 20, 60],
24
+ cyan: [0, 255, 255],
25
+ darkblue: [0, 0, 139],
26
+ darkcyan: [0, 139, 139],
27
+ darkgoldenrod: [184, 134, 11],
28
+ darkgray: [169, 169, 169],
29
+ darkgreen: [0, 100, 0],
30
+ darkgrey: [169, 169, 169],
31
+ darkkhaki: [189, 183, 107],
32
+ darkmagenta: [139, 0, 139],
33
+ darkolivegreen: [85, 107, 47],
34
+ darkorange: [255, 140, 0],
35
+ darkorchid: [153, 50, 204],
36
+ darkred: [139, 0, 0],
37
+ darksalmon: [233, 150, 122],
38
+ darkseagreen: [143, 188, 143],
39
+ darkslateblue: [72, 61, 139],
40
+ darkslategray: [47, 79, 79],
41
+ darkslategrey: [47, 79, 79],
42
+ darkturquoise: [0, 206, 209],
43
+ darkviolet: [148, 0, 211],
44
+ deeppink: [255, 20, 147],
45
+ deepskyblue: [0, 191, 255],
46
+ dimgray: [105, 105, 105],
47
+ dimgrey: [105, 105, 105],
48
+ dodgerblue: [30, 144, 255],
49
+ firebrick: [178, 34, 34],
50
+ floralwhite: [255, 250, 240],
51
+ forestgreen: [34, 139, 34],
52
+ fuchsia: [255, 0, 255],
53
+ gainsboro: [220, 220, 220],
54
+ ghostwhite: [248, 248, 255],
55
+ gold: [255, 215, 0],
56
+ goldenrod: [218, 165, 32],
57
+ gray: [128, 128, 128],
58
+ green: [0, 128, 0],
59
+ greenyellow: [173, 255, 47],
60
+ grey: [128, 128, 128],
61
+ honeydew: [240, 255, 240],
62
+ hotpink: [255, 105, 180],
63
+ indianred: [205, 92, 92],
64
+ indigo: [75, 0, 130],
65
+ ivory: [255, 255, 240],
66
+ khaki: [240, 230, 140],
67
+ lavender: [230, 230, 250],
68
+ lavenderblush: [255, 240, 245],
69
+ lawngreen: [124, 252, 0],
70
+ lemonchiffon: [255, 250, 205],
71
+ lightblue: [173, 216, 230],
72
+ lightcoral: [240, 128, 128],
73
+ lightcyan: [224, 255, 255],
74
+ lightgoldenrodyellow: [250, 250, 210],
75
+ lightgray: [211, 211, 211],
76
+ lightgreen: [144, 238, 144],
77
+ lightgrey: [211, 211, 211],
78
+ lightpink: [255, 182, 193],
79
+ lightsalmon: [255, 160, 122],
80
+ lightseagreen: [32, 178, 170],
81
+ lightskyblue: [135, 206, 250],
82
+ lightslategray: [119, 136, 153],
83
+ lightslategrey: [119, 136, 153],
84
+ lightsteelblue: [176, 196, 222],
85
+ lightyellow: [255, 255, 224],
86
+ lime: [0, 255, 0],
87
+ limegreen: [50, 205, 50],
88
+ linen: [250, 240, 230],
89
+ magenta: [255, 0, 255],
90
+ maroon: [128, 0, 0],
91
+ mediumaquamarine: [102, 205, 170],
92
+ mediumblue: [0, 0, 205],
93
+ mediumorchid: [186, 85, 211],
94
+ mediumpurple: [147, 112, 219],
95
+ mediumseagreen: [60, 179, 113],
96
+ mediumslateblue: [123, 104, 238],
97
+ mediumspringgreen: [0, 250, 154],
98
+ mediumturquoise: [72, 209, 204],
99
+ mediumvioletred: [199, 21, 133],
100
+ midnightblue: [25, 25, 112],
101
+ mintcream: [245, 255, 250],
102
+ mistyrose: [255, 228, 225],
103
+ moccasin: [255, 228, 181],
104
+ navajowhite: [255, 222, 173],
105
+ navy: [0, 0, 128],
106
+ oldlace: [253, 245, 230],
107
+ olive: [128, 128, 0],
108
+ olivedrab: [107, 142, 35],
109
+ orange: [255, 165, 0],
110
+ orangered: [255, 69, 0],
111
+ orchid: [218, 112, 214],
112
+ palegoldenrod: [238, 232, 170],
113
+ palegreen: [152, 251, 152],
114
+ paleturquoise: [175, 238, 238],
115
+ palevioletred: [219, 112, 147],
116
+ papayawhip: [255, 239, 213],
117
+ peachpuff: [255, 218, 185],
118
+ peru: [205, 133, 63],
119
+ pink: [255, 192, 203],
120
+ plum: [221, 160, 221],
121
+ powderblue: [176, 224, 230],
122
+ purple: [128, 0, 128],
123
+ rebeccapurple: [102, 51, 153],
124
+ red: [255, 0, 0],
125
+ rosybrown: [188, 143, 143],
126
+ royalblue: [65, 105, 225],
127
+ saddlebrown: [139, 69, 19],
128
+ salmon: [250, 128, 114],
129
+ sandybrown: [244, 164, 96],
130
+ seagreen: [46, 139, 87],
131
+ seashell: [255, 245, 238],
132
+ sienna: [160, 82, 45],
133
+ silver: [192, 192, 192],
134
+ skyblue: [135, 206, 235],
135
+ slateblue: [106, 90, 205],
136
+ slategray: [112, 128, 144],
137
+ slategrey: [112, 128, 144],
138
+ snow: [255, 250, 250],
139
+ springgreen: [0, 255, 127],
140
+ steelblue: [70, 130, 180],
141
+ tan: [210, 180, 140],
142
+ teal: [0, 128, 128],
143
+ thistle: [216, 191, 216],
144
+ tomato: [255, 99, 71],
145
+ turquoise: [64, 224, 208],
146
+ violet: [238, 130, 238],
147
+ wheat: [245, 222, 179],
148
+ white: [255, 255, 255],
149
+ whitesmoke: [245, 245, 245],
150
+ yellow: [255, 255, 0],
151
+ yellowgreen: [154, 205, 50]
152
+ };
153
+
154
+ const computeSubSteps = (stops, steps) => {
155
+ const l = stops.length;
156
+ steps = Number.parseInt(steps.toString(), 10);
157
+ if (Number.isNaN(steps) || steps < 2) {
158
+ throw new Error("Invalid number of steps (< 2)");
159
+ }
160
+ if (steps < l) {
161
+ throw new Error("Number of steps cannot be inferior to number of stops");
162
+ }
163
+ const substeps = [];
164
+ for (let index = 1; index < l; index++) {
165
+ const step = (steps - 1) * (stops[index].position - stops[index - 1].position);
166
+ substeps.push(Math.max(1, Math.round(step)));
167
+ }
168
+ let totalSubsteps = 1;
169
+ for (let n = l - 1; n--; ) {
170
+ totalSubsteps += substeps[n];
171
+ }
172
+ while (totalSubsteps !== steps) {
173
+ if (totalSubsteps < steps) {
174
+ const min = Math.min(...substeps);
175
+ substeps[substeps.indexOf(min)]++;
176
+ totalSubsteps++;
177
+ } else {
178
+ const max = Math.max(...substeps);
179
+ substeps[substeps.indexOf(max)]--;
180
+ totalSubsteps--;
181
+ }
182
+ }
183
+ return substeps;
184
+ };
185
+
186
+ const hsvToRgb = (h, s, v) => {
187
+ let r = 0;
188
+ let g = 0;
189
+ let b = 0;
190
+ const index = Math.floor(h / 60);
191
+ const f = h / 60 - index;
192
+ const p = v * (1 - s);
193
+ const q = v * (1 - f * s);
194
+ const t = v * (1 - (1 - f) * s);
195
+ if (index % 6 === 0) {
196
+ r = v;
197
+ g = t;
198
+ b = p;
199
+ } else if (index % 6 === 1) {
200
+ r = q;
201
+ g = v;
202
+ b = p;
203
+ } else if (index % 6 === 2) {
204
+ r = p;
205
+ g = v;
206
+ b = t;
207
+ } else if (index % 6 === 3) {
208
+ r = p;
209
+ g = q;
210
+ b = v;
211
+ } else if (index % 6 === 4) {
212
+ r = t;
213
+ g = p;
214
+ b = v;
215
+ } else if (index % 6 === 5) {
216
+ r = v;
217
+ g = p;
218
+ b = q;
219
+ }
220
+ return { b: Math.round(b * 255), g: Math.round(g * 255), r: Math.round(r * 255) };
221
+ };
222
+
223
+ const rgbToHsv = ({ b, g, r }) => {
224
+ let rdif;
225
+ let gdif;
226
+ let bdif;
227
+ let h = 0;
228
+ let s = 0;
229
+ r /= 255;
230
+ g /= 255;
231
+ b /= 255;
232
+ const v = Math.max(r, g, b);
233
+ const diff = v - Math.min(r, g, b);
234
+ const diffc = (c) => (v - c) / 6 / diff + 1 / 2;
235
+ if (diff !== 0) {
236
+ s = diff / v;
237
+ rdif = diffc(r);
238
+ gdif = diffc(g);
239
+ bdif = diffc(b);
240
+ if (v === r) {
241
+ h = bdif - gdif;
242
+ } else if (v === g) {
243
+ h = 1 / 3 + rdif - bdif;
244
+ } else if (v === b) {
245
+ h = 2 / 3 + gdif - rdif;
246
+ }
247
+ if (h < 0) {
248
+ h += 1;
249
+ } else if (h > 1) {
250
+ h -= 1;
251
+ }
252
+ }
253
+ return {
254
+ h: h * 360,
255
+ s,
256
+ v
257
+ };
258
+ };
259
+
260
+ const RGBA_MAX = { b: 256, g: 256, r: 256 };
261
+ const HSV_MAX = { h: 360, s: 1, v: 1 };
262
+ const calculateStepSize = (start, end, steps) => {
263
+ const step = {};
264
+ for (const k in start) {
265
+ if (Object.prototype.hasOwnProperty.call(start, k)) {
266
+ step[k] = steps === 0 ? 0 : (end[k] - start[k]) / steps;
267
+ }
268
+ }
269
+ return step;
270
+ };
271
+ const interpolate = (step, start, index, max) => {
272
+ const color = {};
273
+ for (const k in start) {
274
+ if (Object.prototype.hasOwnProperty.call(start, k)) {
275
+ color[k] = step[k] * index + start[k];
276
+ color[k] = color[k] < 0 ? (
277
+ // eslint-disable-next-line security/detect-object-injection
278
+ color[k] + max[k]
279
+ ) : (
280
+ // eslint-disable-next-line security/detect-object-injection
281
+ max[k] === 1 ? (
282
+ // eslint-disable-next-line security/detect-object-injection
283
+ color[k]
284
+ ) : (
285
+ // eslint-disable-next-line security/detect-object-injection
286
+ color[k] % max[k]
287
+ )
288
+ );
289
+ }
290
+ }
291
+ return color;
292
+ };
293
+ const interpolateRgb = (stop1, stop2, steps) => {
294
+ const start = { b: stop1.color[2], g: stop1.color[1], r: stop1.color[0] };
295
+ const end = { b: stop2.color[2], g: stop2.color[1], r: stop2.color[0] };
296
+ const step = calculateStepSize(start, end, steps);
297
+ const gradient = [{ ...start }];
298
+ for (let index = 1; index < steps; index++) {
299
+ const color = interpolate(step, start, index, RGBA_MAX);
300
+ gradient.push({
301
+ b: Math.floor(color.b),
302
+ g: Math.floor(color.g),
303
+ r: Math.floor(color.r)
304
+ });
305
+ }
306
+ return gradient;
307
+ };
308
+ const interpolateHsv = (stop1, stop2, steps, mode) => {
309
+ const start = rgbToHsv({ b: stop1.color[2], g: stop1.color[1], r: stop1.color[0] });
310
+ const end = rgbToHsv({ b: stop2.color[2], g: stop2.color[1], r: stop2.color[0] });
311
+ if (start.s === 0 || end.s === 0) {
312
+ return interpolateRgb(stop1, stop2, steps);
313
+ }
314
+ let trigonometric;
315
+ if (typeof mode === "boolean") {
316
+ trigonometric = mode;
317
+ } else {
318
+ const trigShortest = start.h < end.h && end.h - start.h < 180 || start.h > end.h && start.h - end.h > 180;
319
+ trigonometric = mode === "long" && trigShortest || mode === "short" && !trigShortest;
320
+ }
321
+ const step = calculateStepSize(start, end, steps);
322
+ const gradient = [
323
+ {
324
+ b: stop1.color[2],
325
+ g: stop1.color[1],
326
+ r: stop1.color[0]
327
+ }
328
+ ];
329
+ let diff;
330
+ if (start.h <= end.h && !trigonometric || start.h >= end.h && trigonometric) {
331
+ diff = end.h - start.h;
332
+ } else if (trigonometric) {
333
+ diff = 360 - end.h + start.h;
334
+ } else {
335
+ diff = 360 - start.h + end.h;
336
+ }
337
+ step.h = (-1) ** (trigonometric ? 1 : 0) * Math.abs(diff) / steps;
338
+ for (let index = 1; index < steps; index++) {
339
+ const color = interpolate(step, start, index, HSV_MAX);
340
+ gradient.push(hsvToRgb(color.h, color.s, color.v));
341
+ }
342
+ return gradient;
343
+ };
344
+
345
+ class GradientBuilder {
346
+ #colorize;
347
+ stops;
348
+ // eslint-disable-next-line sonarjs/cognitive-complexity
349
+ constructor(colorize, stops) {
350
+ this.#colorize = colorize;
351
+ this.stops = [];
352
+ if (stops.length < 2) {
353
+ throw new Error("Invalid number of stops (< 2)");
354
+ }
355
+ const havingPositions = stops[0].position !== void 0;
356
+ let l = stops.length;
357
+ let p = -1;
358
+ let lastColorLess = false;
359
+ for (const [index, stop_] of stops.entries()) {
360
+ let stop = {};
361
+ const hasPosition = stop_.position !== void 0;
362
+ if (havingPositions !== hasPosition) {
363
+ throw new Error("Cannot mix positioned and non-positioned color stops");
364
+ }
365
+ if (hasPosition) {
366
+ const stopInput = stop_;
367
+ const hasColor = stopInput.color !== void 0;
368
+ if (!hasColor && (lastColorLess || index === 0 || index === l - 1)) {
369
+ throw new Error("Cannot define two consecutive position-only stops");
370
+ }
371
+ lastColorLess = !hasColor;
372
+ let color;
373
+ if (hasColor) {
374
+ if (Array.isArray(stopInput.color)) {
375
+ color = stopInput.color;
376
+ } else if (typeof stopInput.color === "string") {
377
+ color = stopInput.color.includes("#") ? convertHexToRgb(stopInput.color) : colorNames[stopInput.color];
378
+ } else if (stopInput.color.r !== void 0 && stopInput.color.g !== void 0 && stopInput.color.b) {
379
+ color = [stopInput.color.r, stopInput.color.g, stopInput.color.b];
380
+ }
381
+ }
382
+ stop = {
383
+ color,
384
+ colorLess: !hasColor,
385
+ position: stopInput.position
386
+ };
387
+ if (stop.position < 0 || stop.position > 1) {
388
+ throw new Error("Color stops positions must be between 0 and 1");
389
+ } else if (stop.position < p) {
390
+ throw new Error("Color stops positions are not ordered");
391
+ }
392
+ p = stop.position;
393
+ } else if (Array.isArray(stop_)) {
394
+ stop = {
395
+ color: stop_,
396
+ position: index / (l - 1)
397
+ };
398
+ } else if (typeof stop_ === "string") {
399
+ stop = {
400
+ color: stop_.includes("#") ? convertHexToRgb(stop_) : colorNames[stop_],
401
+ position: index / (l - 1)
402
+ };
403
+ } else if (stop_.r !== void 0 && stop_.g !== void 0 && stop_.b !== void 0) {
404
+ stop = {
405
+ color: [stop_.r, stop_.g, stop_.b],
406
+ position: index / (l - 1)
407
+ };
408
+ } else {
409
+ throw new Error("Invalid color stop");
410
+ }
411
+ this.stops.push(stop);
412
+ }
413
+ if (this.stops[0].position !== 0) {
414
+ this.stops.unshift({
415
+ color: this.stops[0].color,
416
+ position: 0
417
+ });
418
+ l++;
419
+ }
420
+ if (this.stops[l - 1].position !== 1) {
421
+ this.stops.push({
422
+ color: this.stops[l - 1].color,
423
+ position: 1
424
+ });
425
+ }
426
+ }
427
+ reverse() {
428
+ const stops = [];
429
+ for (const stop of this.stops) {
430
+ const stop_ = { ...stop };
431
+ stop_.position = 1 - stop.position;
432
+ stops.push(stop_);
433
+ }
434
+ return new GradientBuilder(this.#colorize, stops.reverse());
435
+ }
436
+ loop() {
437
+ const stops1 = [];
438
+ const stops2 = [];
439
+ for (const stop of this.stops) {
440
+ stops1.push({
441
+ color: stop.color,
442
+ position: (stop.position || 0) / 2
443
+ });
444
+ }
445
+ for (const stop of this.stops.slice(0, -1)) {
446
+ stops2.push({
447
+ color: stop.color,
448
+ position: 1 - (stop.position || 0) / 2
449
+ });
450
+ }
451
+ return new GradientBuilder(this.#colorize, [...stops1, ...stops2.reverse()]);
452
+ }
453
+ rgb(steps) {
454
+ const subSteps = computeSubSteps(this.stops, steps);
455
+ const gradient = [];
456
+ this.stops.forEach((stop, index) => {
457
+ if (stop.colorLess) {
458
+ const rgbs = interpolateRgb(this.stops[index - 1], this.stops[index + 1], 2);
459
+ stop.color = [rgbs[1].r, rgbs[1].g, rgbs[1].b];
460
+ }
461
+ });
462
+ for (let index = 0, l = this.stops.length; index < l - 1; index++) {
463
+ const rgbs = interpolateRgb(this.stops[index], this.stops[index + 1], subSteps[index]);
464
+ gradient.splice(gradient.length, 0, ...rgbs.map((rgb) => this.#colorize.rgb(rgb.r, rgb.g, rgb.b)));
465
+ }
466
+ gradient.push(this.#colorize.rgb(...this.stops.at(-1).color));
467
+ return gradient;
468
+ }
469
+ hsv(steps, mode = false) {
470
+ const subSteps = computeSubSteps(this.stops, steps);
471
+ const gradient = [];
472
+ this.stops.forEach((stop, index) => {
473
+ if (stop.colorLess) {
474
+ const rgbs = interpolateHsv(this.stops[index - 1], this.stops[index + 1], 2, mode);
475
+ stop.color = [rgbs[1].r, rgbs[1].g, rgbs[1].b];
476
+ }
477
+ });
478
+ for (let index = 0, l = this.stops.length; index < l - 1; index++) {
479
+ const rgbs = interpolateHsv(this.stops[index], this.stops[index + 1], subSteps[index], mode);
480
+ gradient.splice(gradient.length, 0, ...rgbs.map((rgb) => this.#colorize.rgb(rgb.r, rgb.g, rgb.b)));
481
+ }
482
+ gradient.push(this.#colorize.rgb(...this.stops.at(-1).color));
483
+ return gradient;
484
+ }
485
+ }
486
+
487
+ export { GradientBuilder };