@visulima/colorize 2.0.0-alpha.1 → 2.0.0-alpha.10
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/CHANGELOG.md +149 -0
- package/LICENSE.md +1 -1
- package/README.md +32 -30
- package/dist/ansi-codes.d.mts +1 -1
- package/dist/colorize.browser.d.mts +1 -1
- package/dist/colorize.server.d.mts +1 -1
- package/dist/css-code.d.mts +1 -1
- package/dist/gradient.cjs +9 -7
- package/dist/gradient.mjs +9 -7
- package/dist/index.browser.d.mts +5 -5
- package/dist/index.browser.mjs +1 -1
- package/dist/index.server.cjs +24 -15
- package/dist/index.server.d.cts +2 -2
- package/dist/index.server.d.mts +5 -5
- package/dist/index.server.d.ts +2 -2
- package/dist/index.server.mjs +1 -1
- package/dist/packem_shared/{Colorize-Ca9CXTcj.mjs → Colorize-BQ-Wlgs0.js} +9 -13
- package/dist/packem_shared/{Colorize-BenP3bFn.mjs → Colorize-rJUN0RFg.js} +24 -15
- package/dist/packem_shared/{GradientBuilder-DTnSGyYW.mjs → GradientBuilder-CkX4Imo9.mjs} +36 -39
- package/dist/packem_shared/{GradientBuilder-BXOgJeHi.cjs → GradientBuilder-DRrwebdU.cjs} +36 -39
- package/dist/packem_shared/{colorize.server-BA3gZZXz.cjs → colorize.server-CwTmhs0e.cjs} +14 -9
- package/dist/packem_shared/{colorize.server-ry9FZNfG.mjs → colorize.server-YxoHIDUp.mjs} +14 -9
- package/dist/packem_shared/convertHexToRgb-51-edHxE.cjs +21 -0
- package/dist/packem_shared/convertHexToRgb-BZwJEiMZ.mjs +17 -0
- package/dist/template.cjs +15 -7
- package/dist/template.mjs +15 -7
- package/dist/types.d.cts +3 -3
- package/dist/types.d.mts +3 -3
- package/dist/types.d.ts +3 -3
- package/dist/util/clamp.d.cts +1 -1
- package/dist/util/clamp.d.mts +1 -1
- package/dist/util/clamp.d.ts +1 -1
- package/dist/util/convert-hex-to-rgb.d.cts +1 -15
- package/dist/util/convert-hex-to-rgb.d.mts +1 -15
- package/dist/util/convert-hex-to-rgb.d.ts +1 -15
- package/dist/utils.cjs +1 -1
- package/dist/utils.mjs +1 -1
- package/package.json +4 -4
- package/dist/packem_shared/convertHexToRgb-CWdAm2kE.mjs +0 -13
- package/dist/packem_shared/convertHexToRgb-DvkHBM3-.cjs +0 -17
|
@@ -8,18 +8,27 @@ function ansiRegex({ onlyFirst = false } = {}) {
|
|
|
8
8
|
return new RegExp(pattern, onlyFirst ? void 0 : "g");
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
const clamp = (number_, min, max) =>
|
|
11
|
+
const clamp = (number_, min, max) => {
|
|
12
|
+
if (min > number_) {
|
|
13
|
+
return min;
|
|
14
|
+
}
|
|
15
|
+
return Math.min(number_, max);
|
|
16
|
+
};
|
|
12
17
|
|
|
18
|
+
const HEX_COLOR_REGEX = /([a-f\d]{3,6})/i;
|
|
13
19
|
const convertHexToRgb = (hex) => {
|
|
14
|
-
let [, color] =
|
|
15
|
-
const
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
let [, color] = HEX_COLOR_REGEX.exec(hex) ?? [];
|
|
21
|
+
const colorLength = color ? color.length : 0;
|
|
22
|
+
if (colorLength === 3) {
|
|
23
|
+
const c0 = color.charAt(0);
|
|
24
|
+
const c1 = color.charAt(1);
|
|
25
|
+
const c2 = color.charAt(2);
|
|
26
|
+
color = c0 + c0 + c1 + c1 + c2 + c2;
|
|
27
|
+
} else if (colorLength !== 6) {
|
|
19
28
|
return [0, 0, 0];
|
|
20
29
|
}
|
|
21
|
-
const
|
|
22
|
-
return [
|
|
30
|
+
const colorNumber = Number.parseInt(color, 16);
|
|
31
|
+
return [colorNumber >> 16 & 255, colorNumber >> 8 & 255, colorNumber & 255];
|
|
23
32
|
};
|
|
24
33
|
|
|
25
34
|
const rgbToAnsi256 = (r, g, b) => {
|
|
@@ -68,17 +77,17 @@ const bgOffset = 10;
|
|
|
68
77
|
const supportedColor = isStdoutColorSupported();
|
|
69
78
|
const mono = { close: "", open: "" };
|
|
70
79
|
const esc = supportedColor > 0 ? (open, close) => {
|
|
71
|
-
return { close: `\x1B[${close}m`, open: `\x1B[${open}m` };
|
|
80
|
+
return { close: `\x1B[${String(close)}m`, open: `\x1B[${String(open)}m` };
|
|
72
81
|
} : () => mono;
|
|
73
82
|
const createRgbFunction = (function_) => (r, g, b) => function_(rgbToAnsi256(Number(r), Number(g), Number(b)));
|
|
74
83
|
const createHexFunction = (function_) => (hex) => {
|
|
75
84
|
const [r, g, b] = convertHexToRgb(hex);
|
|
76
85
|
return function_(r, g, b);
|
|
77
86
|
};
|
|
78
|
-
let createAnsi256 = (code) => esc(`38;5;${code}`, closeCode);
|
|
79
|
-
let createBgAnsi256 = (code) => esc(`48;5;${code}`, bgCloseCode);
|
|
80
|
-
let createRgb = (r, g, b) => esc(`38;2;${r};${g};${b}`, closeCode);
|
|
81
|
-
let createBgRgb = (r, g, b) => esc(`48;2;${r};${g};${b}`, bgCloseCode);
|
|
87
|
+
let createAnsi256 = (code) => esc(`38;5;${String(code)}`, closeCode);
|
|
88
|
+
let createBgAnsi256 = (code) => esc(`48;5;${String(code)}`, bgCloseCode);
|
|
89
|
+
let createRgb = (r, g, b) => esc(`38;2;${String(r)};${String(g)};${String(b)}`, closeCode);
|
|
90
|
+
let createBgRgb = (r, g, b) => esc(`48;2;${String(r)};${String(g)};${String(b)}`, bgCloseCode);
|
|
82
91
|
if (supportedColor === 1) {
|
|
83
92
|
createAnsi256 = (code) => esc(ansi256To16(Number(code)), closeCode);
|
|
84
93
|
createBgAnsi256 = (code) => esc(ansi256To16(Number(code)) + bgOffset, bgCloseCode);
|
|
@@ -179,7 +188,7 @@ const wrapText = (strings, values, properties) => {
|
|
|
179
188
|
if (!strings) {
|
|
180
189
|
return "";
|
|
181
190
|
}
|
|
182
|
-
let string = strings.raw
|
|
191
|
+
let string = strings.raw === void 0 ? String(strings) : String.raw(strings, ...values);
|
|
183
192
|
if (string.includes("\x1B")) {
|
|
184
193
|
for (let currentProperties = properties; currentProperties; currentProperties = currentProperties.props) {
|
|
185
194
|
string = stringReplaceAll(string, currentProperties.close, currentProperties.open);
|
|
@@ -201,7 +210,7 @@ const createStyle = ({ props }, { close, open }) => {
|
|
|
201
210
|
return style;
|
|
202
211
|
};
|
|
203
212
|
const Colorize = function() {
|
|
204
|
-
const self = (string_) =>
|
|
213
|
+
const self = (string_) => String(string_);
|
|
205
214
|
self.strip = (value) => value.replaceAll(ansiRegex(), "");
|
|
206
215
|
for (const name in baseColors) {
|
|
207
216
|
styles[name] = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { convertHexToRgb } from './convertHexToRgb-
|
|
1
|
+
import { convertHexToRgb } from './convertHexToRgb-BZwJEiMZ.mjs';
|
|
2
2
|
|
|
3
3
|
const colorNames = {
|
|
4
4
|
aliceblue: [240, 248, 255],
|
|
@@ -161,23 +161,25 @@ const computeSubSteps = (stops, steps) => {
|
|
|
161
161
|
throw new Error("Number of steps cannot be inferior to number of stops");
|
|
162
162
|
}
|
|
163
163
|
const substeps = [];
|
|
164
|
-
for (let index = 1; index < l; index
|
|
164
|
+
for (let index = 1; index < l; index += 1) {
|
|
165
165
|
const step = (steps - 1) * (stops[index].position - stops[index - 1].position);
|
|
166
166
|
substeps.push(Math.max(1, Math.round(step)));
|
|
167
167
|
}
|
|
168
168
|
let totalSubsteps = 1;
|
|
169
|
-
for (let n = l -
|
|
169
|
+
for (let n = l - 2; n >= 0; n -= 1) {
|
|
170
170
|
totalSubsteps += substeps[n];
|
|
171
171
|
}
|
|
172
172
|
while (totalSubsteps !== steps) {
|
|
173
173
|
if (totalSubsteps < steps) {
|
|
174
174
|
const min = Math.min(...substeps);
|
|
175
|
-
substeps
|
|
176
|
-
|
|
175
|
+
const minIndex = substeps.indexOf(min);
|
|
176
|
+
substeps[minIndex] = substeps[minIndex] + 1;
|
|
177
|
+
totalSubsteps += 1;
|
|
177
178
|
} else {
|
|
178
179
|
const max = Math.max(...substeps);
|
|
179
|
-
substeps
|
|
180
|
-
|
|
180
|
+
const maxIndex = substeps.indexOf(max);
|
|
181
|
+
substeps[maxIndex] = substeps[maxIndex] - 1;
|
|
182
|
+
totalSubsteps -= 1;
|
|
181
183
|
}
|
|
182
184
|
}
|
|
183
185
|
return substeps;
|
|
@@ -262,7 +264,7 @@ const HSV_MAX = { h: 360, s: 1, v: 1 };
|
|
|
262
264
|
const calculateStepSize = (start, end, steps) => {
|
|
263
265
|
const step = {};
|
|
264
266
|
for (const k in start) {
|
|
265
|
-
if (Object.
|
|
267
|
+
if (Object.hasOwn(start, k)) {
|
|
266
268
|
step[k] = steps === 0 ? 0 : (end[k] - start[k]) / steps;
|
|
267
269
|
}
|
|
268
270
|
}
|
|
@@ -271,21 +273,14 @@ const calculateStepSize = (start, end, steps) => {
|
|
|
271
273
|
const interpolate = (step, start, index, max) => {
|
|
272
274
|
const color = {};
|
|
273
275
|
for (const k in start) {
|
|
274
|
-
if (Object.
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
);
|
|
276
|
+
if (Object.hasOwn(start, k)) {
|
|
277
|
+
let value = step[k] * index + start[k];
|
|
278
|
+
if (value < 0) {
|
|
279
|
+
value += max[k];
|
|
280
|
+
} else if (max[k] !== 1) {
|
|
281
|
+
value %= max[k];
|
|
282
|
+
}
|
|
283
|
+
color[k] = value;
|
|
289
284
|
}
|
|
290
285
|
}
|
|
291
286
|
return color;
|
|
@@ -295,7 +290,7 @@ const interpolateRgb = (stop1, stop2, steps) => {
|
|
|
295
290
|
const end = { b: stop2.color[2], g: stop2.color[1], r: stop2.color[0] };
|
|
296
291
|
const step = calculateStepSize(start, end, steps);
|
|
297
292
|
const gradient = [{ ...start }];
|
|
298
|
-
for (let index = 1; index < steps; index
|
|
293
|
+
for (let index = 1; index < steps; index += 1) {
|
|
299
294
|
const color = interpolate(step, start, index, RGBA_MAX);
|
|
300
295
|
gradient.push({
|
|
301
296
|
b: Math.floor(color.b),
|
|
@@ -335,7 +330,7 @@ const interpolateHsv = (stop1, stop2, steps, mode) => {
|
|
|
335
330
|
diff = 360 - start.h + end.h;
|
|
336
331
|
}
|
|
337
332
|
step.h = (-1) ** (trigonometric ? 1 : 0) * Math.abs(diff) / steps;
|
|
338
|
-
for (let index = 1; index < steps; index
|
|
333
|
+
for (let index = 1; index < steps; index += 1) {
|
|
339
334
|
const color = interpolate(step, start, index, HSV_MAX);
|
|
340
335
|
gradient.push(hsvToRgb(color.h, color.s, color.v));
|
|
341
336
|
}
|
|
@@ -357,7 +352,7 @@ class GradientBuilder {
|
|
|
357
352
|
let p = -1;
|
|
358
353
|
let lastColorLess = false;
|
|
359
354
|
for (const [index, stop_] of stops.entries()) {
|
|
360
|
-
let stop
|
|
355
|
+
let stop;
|
|
361
356
|
const hasPosition = stop_.position !== void 0;
|
|
362
357
|
if (havingPositions !== hasPosition) {
|
|
363
358
|
throw new Error("Cannot mix positioned and non-positioned color stops");
|
|
@@ -375,7 +370,7 @@ class GradientBuilder {
|
|
|
375
370
|
color = stopInput.color;
|
|
376
371
|
} else if (typeof stopInput.color === "string") {
|
|
377
372
|
color = stopInput.color.includes("#") ? convertHexToRgb(stopInput.color) : colorNames[stopInput.color];
|
|
378
|
-
} else if (stopInput.color
|
|
373
|
+
} else if (stopInput.color && "r" in stopInput.color && "g" in stopInput.color && "b" in stopInput.color) {
|
|
379
374
|
color = [stopInput.color.r, stopInput.color.g, stopInput.color.b];
|
|
380
375
|
}
|
|
381
376
|
}
|
|
@@ -400,7 +395,7 @@ class GradientBuilder {
|
|
|
400
395
|
color: stop_.includes("#") ? convertHexToRgb(stop_) : colorNames[stop_],
|
|
401
396
|
position: index / (l - 1)
|
|
402
397
|
};
|
|
403
|
-
} else if (stop_
|
|
398
|
+
} else if (stop_?.r !== void 0 && stop_?.g !== void 0 && stop_?.b !== void 0) {
|
|
404
399
|
stop = {
|
|
405
400
|
color: [stop_.r, stop_.g, stop_.b],
|
|
406
401
|
position: index / (l - 1)
|
|
@@ -410,16 +405,18 @@ class GradientBuilder {
|
|
|
410
405
|
}
|
|
411
406
|
this.stops.push(stop);
|
|
412
407
|
}
|
|
413
|
-
|
|
408
|
+
const firstStop = this.stops[0];
|
|
409
|
+
if (firstStop.position !== 0) {
|
|
414
410
|
this.stops.unshift({
|
|
415
|
-
color:
|
|
411
|
+
color: firstStop.color,
|
|
416
412
|
position: 0
|
|
417
413
|
});
|
|
418
|
-
l
|
|
414
|
+
l += 1;
|
|
419
415
|
}
|
|
420
|
-
|
|
416
|
+
const lastStop = this.stops[l - 1];
|
|
417
|
+
if (lastStop.position !== 1) {
|
|
421
418
|
this.stops.push({
|
|
422
|
-
color:
|
|
419
|
+
color: lastStop.color,
|
|
423
420
|
position: 1
|
|
424
421
|
});
|
|
425
422
|
}
|
|
@@ -427,10 +424,10 @@ class GradientBuilder {
|
|
|
427
424
|
reverse() {
|
|
428
425
|
const stops = [];
|
|
429
426
|
for (const stop of this.stops) {
|
|
430
|
-
const
|
|
431
|
-
stops.push(
|
|
427
|
+
const reversedStop = { ...stop, position: 1 - stop.position };
|
|
428
|
+
stops.push(reversedStop);
|
|
432
429
|
}
|
|
433
|
-
return new GradientBuilder(this.#colorize, stops.
|
|
430
|
+
return new GradientBuilder(this.#colorize, stops.toReversed());
|
|
434
431
|
}
|
|
435
432
|
loop() {
|
|
436
433
|
const stops1 = [];
|
|
@@ -447,7 +444,7 @@ class GradientBuilder {
|
|
|
447
444
|
position: 1 - (stop.position || 0) / 2
|
|
448
445
|
});
|
|
449
446
|
}
|
|
450
|
-
return new GradientBuilder(this.#colorize, [...stops1, ...stops2.
|
|
447
|
+
return new GradientBuilder(this.#colorize, [...stops1, ...stops2.toReversed()]);
|
|
451
448
|
}
|
|
452
449
|
rgb(steps) {
|
|
453
450
|
const subSteps = computeSubSteps(this.stops, steps);
|
|
@@ -458,7 +455,7 @@ class GradientBuilder {
|
|
|
458
455
|
stop.color = [rgbs[1].r, rgbs[1].g, rgbs[1].b];
|
|
459
456
|
}
|
|
460
457
|
});
|
|
461
|
-
for (let index = 0, l = this.stops.length; index < l - 1; index
|
|
458
|
+
for (let index = 0, l = this.stops.length; index < l - 1; index += 1) {
|
|
462
459
|
const rgbs = interpolateRgb(this.stops[index], this.stops[index + 1], subSteps[index]);
|
|
463
460
|
gradient.splice(gradient.length, 0, ...rgbs.map((rgb) => this.#colorize.rgb(rgb.r, rgb.g, rgb.b)));
|
|
464
461
|
}
|
|
@@ -474,7 +471,7 @@ class GradientBuilder {
|
|
|
474
471
|
stop.color = [rgbs[1].r, rgbs[1].g, rgbs[1].b];
|
|
475
472
|
}
|
|
476
473
|
});
|
|
477
|
-
for (let index = 0, l = this.stops.length; index < l - 1; index
|
|
474
|
+
for (let index = 0, l = this.stops.length; index < l - 1; index += 1) {
|
|
478
475
|
const rgbs = interpolateHsv(this.stops[index], this.stops[index + 1], subSteps[index], mode);
|
|
479
476
|
gradient.splice(gradient.length, 0, ...rgbs.map((rgb) => this.#colorize.rgb(rgb.r, rgb.g, rgb.b)));
|
|
480
477
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
4
|
|
|
5
|
-
const convertHexToRgb = require('./convertHexToRgb-
|
|
5
|
+
const convertHexToRgb = require('./convertHexToRgb-51-edHxE.cjs');
|
|
6
6
|
|
|
7
7
|
const colorNames = {
|
|
8
8
|
aliceblue: [240, 248, 255],
|
|
@@ -165,23 +165,25 @@ const computeSubSteps = (stops, steps) => {
|
|
|
165
165
|
throw new Error("Number of steps cannot be inferior to number of stops");
|
|
166
166
|
}
|
|
167
167
|
const substeps = [];
|
|
168
|
-
for (let index = 1; index < l; index
|
|
168
|
+
for (let index = 1; index < l; index += 1) {
|
|
169
169
|
const step = (steps - 1) * (stops[index].position - stops[index - 1].position);
|
|
170
170
|
substeps.push(Math.max(1, Math.round(step)));
|
|
171
171
|
}
|
|
172
172
|
let totalSubsteps = 1;
|
|
173
|
-
for (let n = l -
|
|
173
|
+
for (let n = l - 2; n >= 0; n -= 1) {
|
|
174
174
|
totalSubsteps += substeps[n];
|
|
175
175
|
}
|
|
176
176
|
while (totalSubsteps !== steps) {
|
|
177
177
|
if (totalSubsteps < steps) {
|
|
178
178
|
const min = Math.min(...substeps);
|
|
179
|
-
substeps
|
|
180
|
-
|
|
179
|
+
const minIndex = substeps.indexOf(min);
|
|
180
|
+
substeps[minIndex] = substeps[minIndex] + 1;
|
|
181
|
+
totalSubsteps += 1;
|
|
181
182
|
} else {
|
|
182
183
|
const max = Math.max(...substeps);
|
|
183
|
-
substeps
|
|
184
|
-
|
|
184
|
+
const maxIndex = substeps.indexOf(max);
|
|
185
|
+
substeps[maxIndex] = substeps[maxIndex] - 1;
|
|
186
|
+
totalSubsteps -= 1;
|
|
185
187
|
}
|
|
186
188
|
}
|
|
187
189
|
return substeps;
|
|
@@ -266,7 +268,7 @@ const HSV_MAX = { h: 360, s: 1, v: 1 };
|
|
|
266
268
|
const calculateStepSize = (start, end, steps) => {
|
|
267
269
|
const step = {};
|
|
268
270
|
for (const k in start) {
|
|
269
|
-
if (Object.
|
|
271
|
+
if (Object.hasOwn(start, k)) {
|
|
270
272
|
step[k] = steps === 0 ? 0 : (end[k] - start[k]) / steps;
|
|
271
273
|
}
|
|
272
274
|
}
|
|
@@ -275,21 +277,14 @@ const calculateStepSize = (start, end, steps) => {
|
|
|
275
277
|
const interpolate = (step, start, index, max) => {
|
|
276
278
|
const color = {};
|
|
277
279
|
for (const k in start) {
|
|
278
|
-
if (Object.
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
// eslint-disable-next-line security/detect-object-injection
|
|
287
|
-
color[k]
|
|
288
|
-
) : (
|
|
289
|
-
// eslint-disable-next-line security/detect-object-injection
|
|
290
|
-
color[k] % max[k]
|
|
291
|
-
)
|
|
292
|
-
);
|
|
280
|
+
if (Object.hasOwn(start, k)) {
|
|
281
|
+
let value = step[k] * index + start[k];
|
|
282
|
+
if (value < 0) {
|
|
283
|
+
value += max[k];
|
|
284
|
+
} else if (max[k] !== 1) {
|
|
285
|
+
value %= max[k];
|
|
286
|
+
}
|
|
287
|
+
color[k] = value;
|
|
293
288
|
}
|
|
294
289
|
}
|
|
295
290
|
return color;
|
|
@@ -299,7 +294,7 @@ const interpolateRgb = (stop1, stop2, steps) => {
|
|
|
299
294
|
const end = { b: stop2.color[2], g: stop2.color[1], r: stop2.color[0] };
|
|
300
295
|
const step = calculateStepSize(start, end, steps);
|
|
301
296
|
const gradient = [{ ...start }];
|
|
302
|
-
for (let index = 1; index < steps; index
|
|
297
|
+
for (let index = 1; index < steps; index += 1) {
|
|
303
298
|
const color = interpolate(step, start, index, RGBA_MAX);
|
|
304
299
|
gradient.push({
|
|
305
300
|
b: Math.floor(color.b),
|
|
@@ -339,7 +334,7 @@ const interpolateHsv = (stop1, stop2, steps, mode) => {
|
|
|
339
334
|
diff = 360 - start.h + end.h;
|
|
340
335
|
}
|
|
341
336
|
step.h = (-1) ** (trigonometric ? 1 : 0) * Math.abs(diff) / steps;
|
|
342
|
-
for (let index = 1; index < steps; index
|
|
337
|
+
for (let index = 1; index < steps; index += 1) {
|
|
343
338
|
const color = interpolate(step, start, index, HSV_MAX);
|
|
344
339
|
gradient.push(hsvToRgb(color.h, color.s, color.v));
|
|
345
340
|
}
|
|
@@ -361,7 +356,7 @@ class GradientBuilder {
|
|
|
361
356
|
let p = -1;
|
|
362
357
|
let lastColorLess = false;
|
|
363
358
|
for (const [index, stop_] of stops.entries()) {
|
|
364
|
-
let stop
|
|
359
|
+
let stop;
|
|
365
360
|
const hasPosition = stop_.position !== void 0;
|
|
366
361
|
if (havingPositions !== hasPosition) {
|
|
367
362
|
throw new Error("Cannot mix positioned and non-positioned color stops");
|
|
@@ -379,7 +374,7 @@ class GradientBuilder {
|
|
|
379
374
|
color = stopInput.color;
|
|
380
375
|
} else if (typeof stopInput.color === "string") {
|
|
381
376
|
color = stopInput.color.includes("#") ? convertHexToRgb.convertHexToRgb(stopInput.color) : colorNames[stopInput.color];
|
|
382
|
-
} else if (stopInput.color
|
|
377
|
+
} else if (stopInput.color && "r" in stopInput.color && "g" in stopInput.color && "b" in stopInput.color) {
|
|
383
378
|
color = [stopInput.color.r, stopInput.color.g, stopInput.color.b];
|
|
384
379
|
}
|
|
385
380
|
}
|
|
@@ -404,7 +399,7 @@ class GradientBuilder {
|
|
|
404
399
|
color: stop_.includes("#") ? convertHexToRgb.convertHexToRgb(stop_) : colorNames[stop_],
|
|
405
400
|
position: index / (l - 1)
|
|
406
401
|
};
|
|
407
|
-
} else if (stop_
|
|
402
|
+
} else if (stop_?.r !== void 0 && stop_?.g !== void 0 && stop_?.b !== void 0) {
|
|
408
403
|
stop = {
|
|
409
404
|
color: [stop_.r, stop_.g, stop_.b],
|
|
410
405
|
position: index / (l - 1)
|
|
@@ -414,16 +409,18 @@ class GradientBuilder {
|
|
|
414
409
|
}
|
|
415
410
|
this.stops.push(stop);
|
|
416
411
|
}
|
|
417
|
-
|
|
412
|
+
const firstStop = this.stops[0];
|
|
413
|
+
if (firstStop.position !== 0) {
|
|
418
414
|
this.stops.unshift({
|
|
419
|
-
color:
|
|
415
|
+
color: firstStop.color,
|
|
420
416
|
position: 0
|
|
421
417
|
});
|
|
422
|
-
l
|
|
418
|
+
l += 1;
|
|
423
419
|
}
|
|
424
|
-
|
|
420
|
+
const lastStop = this.stops[l - 1];
|
|
421
|
+
if (lastStop.position !== 1) {
|
|
425
422
|
this.stops.push({
|
|
426
|
-
color:
|
|
423
|
+
color: lastStop.color,
|
|
427
424
|
position: 1
|
|
428
425
|
});
|
|
429
426
|
}
|
|
@@ -431,10 +428,10 @@ class GradientBuilder {
|
|
|
431
428
|
reverse() {
|
|
432
429
|
const stops = [];
|
|
433
430
|
for (const stop of this.stops) {
|
|
434
|
-
const
|
|
435
|
-
stops.push(
|
|
431
|
+
const reversedStop = { ...stop, position: 1 - stop.position };
|
|
432
|
+
stops.push(reversedStop);
|
|
436
433
|
}
|
|
437
|
-
return new GradientBuilder(this.#colorize, stops.
|
|
434
|
+
return new GradientBuilder(this.#colorize, stops.toReversed());
|
|
438
435
|
}
|
|
439
436
|
loop() {
|
|
440
437
|
const stops1 = [];
|
|
@@ -451,7 +448,7 @@ class GradientBuilder {
|
|
|
451
448
|
position: 1 - (stop.position || 0) / 2
|
|
452
449
|
});
|
|
453
450
|
}
|
|
454
|
-
return new GradientBuilder(this.#colorize, [...stops1, ...stops2.
|
|
451
|
+
return new GradientBuilder(this.#colorize, [...stops1, ...stops2.toReversed()]);
|
|
455
452
|
}
|
|
456
453
|
rgb(steps) {
|
|
457
454
|
const subSteps = computeSubSteps(this.stops, steps);
|
|
@@ -462,7 +459,7 @@ class GradientBuilder {
|
|
|
462
459
|
stop.color = [rgbs[1].r, rgbs[1].g, rgbs[1].b];
|
|
463
460
|
}
|
|
464
461
|
});
|
|
465
|
-
for (let index = 0, l = this.stops.length; index < l - 1; index
|
|
462
|
+
for (let index = 0, l = this.stops.length; index < l - 1; index += 1) {
|
|
466
463
|
const rgbs = interpolateRgb(this.stops[index], this.stops[index + 1], subSteps[index]);
|
|
467
464
|
gradient.splice(gradient.length, 0, ...rgbs.map((rgb) => this.#colorize.rgb(rgb.r, rgb.g, rgb.b)));
|
|
468
465
|
}
|
|
@@ -478,7 +475,7 @@ class GradientBuilder {
|
|
|
478
475
|
stop.color = [rgbs[1].r, rgbs[1].g, rgbs[1].b];
|
|
479
476
|
}
|
|
480
477
|
});
|
|
481
|
-
for (let index = 0, l = this.stops.length; index < l - 1; index
|
|
478
|
+
for (let index = 0, l = this.stops.length; index < l - 1; index += 1) {
|
|
482
479
|
const rgbs = interpolateHsv(this.stops[index], this.stops[index + 1], subSteps[index], mode);
|
|
483
480
|
gradient.splice(gradient.length, 0, ...rgbs.map((rgb) => this.#colorize.rgb(rgb.r, rgb.g, rgb.b)));
|
|
484
481
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const isAnsiColorSupported = require('@visulima/is-ansi-color-supported');
|
|
4
|
-
const convertHexToRgb = require('./convertHexToRgb-
|
|
4
|
+
const convertHexToRgb = require('./convertHexToRgb-51-edHxE.cjs');
|
|
5
5
|
const rgbToAnsi256 = require('./rgbToAnsi256-DL8eajTz.cjs');
|
|
6
6
|
|
|
7
7
|
function ansiRegex({ onlyFirst = false } = {}) {
|
|
@@ -12,7 +12,12 @@ function ansiRegex({ onlyFirst = false } = {}) {
|
|
|
12
12
|
return new RegExp(pattern, onlyFirst ? void 0 : "g");
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const clamp = (number_, min, max) =>
|
|
15
|
+
const clamp = (number_, min, max) => {
|
|
16
|
+
if (min > number_) {
|
|
17
|
+
return min;
|
|
18
|
+
}
|
|
19
|
+
return Math.min(number_, max);
|
|
20
|
+
};
|
|
16
21
|
|
|
17
22
|
const closeCode = 39;
|
|
18
23
|
const bgCloseCode = 49;
|
|
@@ -20,17 +25,17 @@ const bgOffset = 10;
|
|
|
20
25
|
const supportedColor = isAnsiColorSupported.isStdoutColorSupported();
|
|
21
26
|
const mono = { close: "", open: "" };
|
|
22
27
|
const esc = supportedColor > 0 ? (open, close) => {
|
|
23
|
-
return { close: `\x1B[${close}m`, open: `\x1B[${open}m` };
|
|
28
|
+
return { close: `\x1B[${String(close)}m`, open: `\x1B[${String(open)}m` };
|
|
24
29
|
} : () => mono;
|
|
25
30
|
const createRgbFunction = (function_) => (r, g, b) => function_(rgbToAnsi256.rgbToAnsi256(Number(r), Number(g), Number(b)));
|
|
26
31
|
const createHexFunction = (function_) => (hex) => {
|
|
27
32
|
const [r, g, b] = convertHexToRgb.convertHexToRgb(hex);
|
|
28
33
|
return function_(r, g, b);
|
|
29
34
|
};
|
|
30
|
-
let createAnsi256 = (code) => esc(`38;5;${code}`, closeCode);
|
|
31
|
-
let createBgAnsi256 = (code) => esc(`48;5;${code}`, bgCloseCode);
|
|
32
|
-
let createRgb = (r, g, b) => esc(`38;2;${r};${g};${b}`, closeCode);
|
|
33
|
-
let createBgRgb = (r, g, b) => esc(`48;2;${r};${g};${b}`, bgCloseCode);
|
|
35
|
+
let createAnsi256 = (code) => esc(`38;5;${String(code)}`, closeCode);
|
|
36
|
+
let createBgAnsi256 = (code) => esc(`48;5;${String(code)}`, bgCloseCode);
|
|
37
|
+
let createRgb = (r, g, b) => esc(`38;2;${String(r)};${String(g)};${String(b)}`, closeCode);
|
|
38
|
+
let createBgRgb = (r, g, b) => esc(`48;2;${String(r)};${String(g)};${String(b)}`, bgCloseCode);
|
|
34
39
|
if (supportedColor === 1) {
|
|
35
40
|
createAnsi256 = (code) => esc(rgbToAnsi256.ansi256To16(Number(code)), closeCode);
|
|
36
41
|
createBgAnsi256 = (code) => esc(rgbToAnsi256.ansi256To16(Number(code)) + bgOffset, bgCloseCode);
|
|
@@ -131,7 +136,7 @@ const wrapText = (strings, values, properties) => {
|
|
|
131
136
|
if (!strings) {
|
|
132
137
|
return "";
|
|
133
138
|
}
|
|
134
|
-
let string = strings.raw
|
|
139
|
+
let string = strings.raw === void 0 ? String(strings) : String.raw(strings, ...values);
|
|
135
140
|
if (string.includes("\x1B")) {
|
|
136
141
|
for (let currentProperties = properties; currentProperties; currentProperties = currentProperties.props) {
|
|
137
142
|
string = stringReplaceAll(string, currentProperties.close, currentProperties.open);
|
|
@@ -153,7 +158,7 @@ const createStyle = ({ props }, { close, open }) => {
|
|
|
153
158
|
return style;
|
|
154
159
|
};
|
|
155
160
|
const Colorize = function() {
|
|
156
|
-
const self = (string_) =>
|
|
161
|
+
const self = (string_) => String(string_);
|
|
157
162
|
self.strip = (value) => value.replaceAll(ansiRegex(), "");
|
|
158
163
|
for (const name in baseColors) {
|
|
159
164
|
styles[name] = {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isStdoutColorSupported } from '@visulima/is-ansi-color-supported';
|
|
2
|
-
import { convertHexToRgb } from './convertHexToRgb-
|
|
2
|
+
import { convertHexToRgb } from './convertHexToRgb-BZwJEiMZ.mjs';
|
|
3
3
|
import { ansi256To16, rgbToAnsi16, rgbToAnsi256 } from './rgbToAnsi256-BdS0fomP.mjs';
|
|
4
4
|
|
|
5
5
|
function ansiRegex({ onlyFirst = false } = {}) {
|
|
@@ -10,7 +10,12 @@ function ansiRegex({ onlyFirst = false } = {}) {
|
|
|
10
10
|
return new RegExp(pattern, onlyFirst ? void 0 : "g");
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
const clamp = (number_, min, max) =>
|
|
13
|
+
const clamp = (number_, min, max) => {
|
|
14
|
+
if (min > number_) {
|
|
15
|
+
return min;
|
|
16
|
+
}
|
|
17
|
+
return Math.min(number_, max);
|
|
18
|
+
};
|
|
14
19
|
|
|
15
20
|
const closeCode = 39;
|
|
16
21
|
const bgCloseCode = 49;
|
|
@@ -18,17 +23,17 @@ const bgOffset = 10;
|
|
|
18
23
|
const supportedColor = isStdoutColorSupported();
|
|
19
24
|
const mono = { close: "", open: "" };
|
|
20
25
|
const esc = supportedColor > 0 ? (open, close) => {
|
|
21
|
-
return { close: `\x1B[${close}m`, open: `\x1B[${open}m` };
|
|
26
|
+
return { close: `\x1B[${String(close)}m`, open: `\x1B[${String(open)}m` };
|
|
22
27
|
} : () => mono;
|
|
23
28
|
const createRgbFunction = (function_) => (r, g, b) => function_(rgbToAnsi256(Number(r), Number(g), Number(b)));
|
|
24
29
|
const createHexFunction = (function_) => (hex) => {
|
|
25
30
|
const [r, g, b] = convertHexToRgb(hex);
|
|
26
31
|
return function_(r, g, b);
|
|
27
32
|
};
|
|
28
|
-
let createAnsi256 = (code) => esc(`38;5;${code}`, closeCode);
|
|
29
|
-
let createBgAnsi256 = (code) => esc(`48;5;${code}`, bgCloseCode);
|
|
30
|
-
let createRgb = (r, g, b) => esc(`38;2;${r};${g};${b}`, closeCode);
|
|
31
|
-
let createBgRgb = (r, g, b) => esc(`48;2;${r};${g};${b}`, bgCloseCode);
|
|
33
|
+
let createAnsi256 = (code) => esc(`38;5;${String(code)}`, closeCode);
|
|
34
|
+
let createBgAnsi256 = (code) => esc(`48;5;${String(code)}`, bgCloseCode);
|
|
35
|
+
let createRgb = (r, g, b) => esc(`38;2;${String(r)};${String(g)};${String(b)}`, closeCode);
|
|
36
|
+
let createBgRgb = (r, g, b) => esc(`48;2;${String(r)};${String(g)};${String(b)}`, bgCloseCode);
|
|
32
37
|
if (supportedColor === 1) {
|
|
33
38
|
createAnsi256 = (code) => esc(ansi256To16(Number(code)), closeCode);
|
|
34
39
|
createBgAnsi256 = (code) => esc(ansi256To16(Number(code)) + bgOffset, bgCloseCode);
|
|
@@ -129,7 +134,7 @@ const wrapText = (strings, values, properties) => {
|
|
|
129
134
|
if (!strings) {
|
|
130
135
|
return "";
|
|
131
136
|
}
|
|
132
|
-
let string = strings.raw
|
|
137
|
+
let string = strings.raw === void 0 ? String(strings) : String.raw(strings, ...values);
|
|
133
138
|
if (string.includes("\x1B")) {
|
|
134
139
|
for (let currentProperties = properties; currentProperties; currentProperties = currentProperties.props) {
|
|
135
140
|
string = stringReplaceAll(string, currentProperties.close, currentProperties.open);
|
|
@@ -151,7 +156,7 @@ const createStyle = ({ props }, { close, open }) => {
|
|
|
151
156
|
return style;
|
|
152
157
|
};
|
|
153
158
|
const Colorize = function() {
|
|
154
|
-
const self = (string_) =>
|
|
159
|
+
const self = (string_) => String(string_);
|
|
155
160
|
self.strip = (value) => value.replaceAll(ansiRegex(), "");
|
|
156
161
|
for (const name in baseColors) {
|
|
157
162
|
styles[name] = {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
+
|
|
5
|
+
const HEX_COLOR_REGEX = /([a-f\d]{3,6})/i;
|
|
6
|
+
const convertHexToRgb = (hex) => {
|
|
7
|
+
let [, color] = HEX_COLOR_REGEX.exec(hex) ?? [];
|
|
8
|
+
const colorLength = color ? color.length : 0;
|
|
9
|
+
if (colorLength === 3) {
|
|
10
|
+
const c0 = color.charAt(0);
|
|
11
|
+
const c1 = color.charAt(1);
|
|
12
|
+
const c2 = color.charAt(2);
|
|
13
|
+
color = c0 + c0 + c1 + c1 + c2 + c2;
|
|
14
|
+
} else if (colorLength !== 6) {
|
|
15
|
+
return [0, 0, 0];
|
|
16
|
+
}
|
|
17
|
+
const colorNumber = Number.parseInt(color, 16);
|
|
18
|
+
return [colorNumber >> 16 & 255, colorNumber >> 8 & 255, colorNumber & 255];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.convertHexToRgb = convertHexToRgb;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const HEX_COLOR_REGEX = /([a-f\d]{3,6})/i;
|
|
2
|
+
const convertHexToRgb = (hex) => {
|
|
3
|
+
let [, color] = HEX_COLOR_REGEX.exec(hex) ?? [];
|
|
4
|
+
const colorLength = color ? color.length : 0;
|
|
5
|
+
if (colorLength === 3) {
|
|
6
|
+
const c0 = color.charAt(0);
|
|
7
|
+
const c1 = color.charAt(1);
|
|
8
|
+
const c2 = color.charAt(2);
|
|
9
|
+
color = c0 + c0 + c1 + c1 + c2 + c2;
|
|
10
|
+
} else if (colorLength !== 6) {
|
|
11
|
+
return [0, 0, 0];
|
|
12
|
+
}
|
|
13
|
+
const colorNumber = Number.parseInt(color, 16);
|
|
14
|
+
return [colorNumber >> 16 & 255, colorNumber >> 8 & 255, colorNumber & 255];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { convertHexToRgb };
|