color-value-tools 1.0.1 → 1.1.1
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/README.md +343 -70
- package/dist/cjs/index.d.ts +397 -0
- package/dist/cjs/index.js +1410 -0
- package/dist/cli/bin/cli.js +179 -0
- package/dist/cli/src/index.js +1410 -0
- package/dist/esm/index.js +1310 -0
- package/dist/esm/package.json +1 -0
- package/package.json +57 -47
- package/dist/index.d.ts +0 -203
- package/dist/index.js +0 -590
package/dist/index.js
DELETED
|
@@ -1,590 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isCssVariable = isCssVariable;
|
|
4
|
-
exports.isHexColor = isHexColor;
|
|
5
|
-
exports.isRgbColor = isRgbColor;
|
|
6
|
-
exports.isHslColor = isHslColor;
|
|
7
|
-
exports.getColorType = getColorType;
|
|
8
|
-
exports.extractCssVariableName = extractCssVariableName;
|
|
9
|
-
exports.normalizeHex = normalizeHex;
|
|
10
|
-
exports.hexToRgb = hexToRgb;
|
|
11
|
-
exports.hexToRgba = hexToRgba;
|
|
12
|
-
exports.hexToHsl = hexToHsl;
|
|
13
|
-
exports.hslToHex = hslToHex;
|
|
14
|
-
exports.adjustHexBrightness = adjustHexBrightness;
|
|
15
|
-
exports.rotateHue = rotateHue;
|
|
16
|
-
exports.rgbToHex = rgbToHex;
|
|
17
|
-
exports.rgbaToHex = rgbaToHex;
|
|
18
|
-
exports.rgbToRgbaString = rgbToRgbaString;
|
|
19
|
-
exports.rgbaStringToRgba = rgbaStringToRgba;
|
|
20
|
-
exports.rgbToHsl = rgbToHsl;
|
|
21
|
-
exports.hslToRgb = hslToRgb;
|
|
22
|
-
exports.rgbToHsv = rgbToHsv;
|
|
23
|
-
exports.hsvToRgb = hsvToRgb;
|
|
24
|
-
exports.hexToHsv = hexToHsv;
|
|
25
|
-
exports.hsvToHex = hsvToHex;
|
|
26
|
-
exports.hex8ToRgba = hex8ToRgba;
|
|
27
|
-
exports.rgbaToHex8 = rgbaToHex8;
|
|
28
|
-
exports.normalizeColor = normalizeColor;
|
|
29
|
-
exports.mixColors = mixColors;
|
|
30
|
-
exports.relativeLuminance = relativeLuminance;
|
|
31
|
-
exports.contrastRatio = contrastRatio;
|
|
32
|
-
exports.isDark = isDark;
|
|
33
|
-
exports.isLight = isLight;
|
|
34
|
-
exports.rgbToCmyk = rgbToCmyk;
|
|
35
|
-
exports.cmykToRgb = cmykToRgb;
|
|
36
|
-
exports.rgbToLab = rgbToLab;
|
|
37
|
-
exports.labToRgb = labToRgb;
|
|
38
|
-
exports.rgbToLch = rgbToLch;
|
|
39
|
-
exports.lchToRgb = lchToRgb;
|
|
40
|
-
function isCssVariable(value) {
|
|
41
|
-
return value.trim().startsWith('var(--');
|
|
42
|
-
}
|
|
43
|
-
function isHexColor(value) {
|
|
44
|
-
const trimmed = value.trim();
|
|
45
|
-
return /^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(trimmed);
|
|
46
|
-
}
|
|
47
|
-
function isRgbColor(value) {
|
|
48
|
-
const trimmed = value.trim().toLowerCase();
|
|
49
|
-
return trimmed.startsWith('rgb(') || trimmed.startsWith('rgba(');
|
|
50
|
-
}
|
|
51
|
-
function isHslColor(value) {
|
|
52
|
-
const trimmed = value.trim().toLowerCase();
|
|
53
|
-
return trimmed.startsWith('hsl(') || trimmed.startsWith('hsla(');
|
|
54
|
-
}
|
|
55
|
-
function getColorType(value) {
|
|
56
|
-
const trimmed = value.trim().toLowerCase();
|
|
57
|
-
if (isCssVariable(trimmed))
|
|
58
|
-
return 'css-var';
|
|
59
|
-
if (isHexColor(trimmed))
|
|
60
|
-
return 'hex';
|
|
61
|
-
if (isRgbColor(trimmed))
|
|
62
|
-
return 'rgb';
|
|
63
|
-
if (isHslColor(trimmed))
|
|
64
|
-
return 'hsl';
|
|
65
|
-
const namedColors = [
|
|
66
|
-
'transparent', 'currentcolor', 'inherit', 'initial', 'unset', 'black', 'white', 'red', 'green', 'blue', 'yellow', 'purple', 'orange', 'gray', 'grey', 'pink', 'brown', 'cyan', 'magenta', 'violet', 'aqua', 'beige', 'coral', 'gold', 'indigo', 'ivory', 'khaki', 'lavender', 'lime', 'maroon', 'navy', 'olive', 'orchid', 'plum', 'salmon', 'silver', 'tan', 'teal', 'tomato', 'wheat',
|
|
67
|
-
];
|
|
68
|
-
if (namedColors.includes(trimmed))
|
|
69
|
-
return 'named';
|
|
70
|
-
return 'unknown';
|
|
71
|
-
}
|
|
72
|
-
function extractCssVariableName(value) {
|
|
73
|
-
const match = value.match(/var\((--[^)]+)\)/);
|
|
74
|
-
return (match === null || match === void 0 ? void 0 : match[1]) || value;
|
|
75
|
-
}
|
|
76
|
-
function normalizeHex(hex) {
|
|
77
|
-
let cleanHex = hex.trim();
|
|
78
|
-
if (!cleanHex.startsWith('#'))
|
|
79
|
-
cleanHex = `#${cleanHex}`;
|
|
80
|
-
if (cleanHex.length === 4) {
|
|
81
|
-
cleanHex = `#${cleanHex.slice(1).split('').map(c => c + c).join('')}`;
|
|
82
|
-
}
|
|
83
|
-
if (!/^#[0-9A-Fa-f]{6}$/.test(cleanHex)) {
|
|
84
|
-
return '#f5e477';
|
|
85
|
-
}
|
|
86
|
-
return cleanHex.toLowerCase();
|
|
87
|
-
}
|
|
88
|
-
function hexToRgb(hex) {
|
|
89
|
-
const normalizedHex = normalizeHex(hex);
|
|
90
|
-
const r = parseInt(normalizedHex.slice(1, 3), 16);
|
|
91
|
-
const g = parseInt(normalizedHex.slice(3, 5), 16);
|
|
92
|
-
const b = parseInt(normalizedHex.slice(5, 7), 16);
|
|
93
|
-
return [r, g, b];
|
|
94
|
-
}
|
|
95
|
-
function hexToRgba(hex, opacity = 1) {
|
|
96
|
-
const [r, g, b] = hexToRgb(hex);
|
|
97
|
-
return `rgba(${r}, ${g}, ${b}, ${opacity})`;
|
|
98
|
-
}
|
|
99
|
-
function hexToHsl(hex) {
|
|
100
|
-
const [r, g, b] = hexToRgb(hex);
|
|
101
|
-
const red = r / 255;
|
|
102
|
-
const green = g / 255;
|
|
103
|
-
const blue = b / 255;
|
|
104
|
-
const max = Math.max(red, green, blue);
|
|
105
|
-
const min = Math.min(red, green, blue);
|
|
106
|
-
let h = 0, s = 0;
|
|
107
|
-
const l = (max + min) / 2;
|
|
108
|
-
if (max !== min) {
|
|
109
|
-
const d = max - min;
|
|
110
|
-
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
111
|
-
switch (max) {
|
|
112
|
-
case red:
|
|
113
|
-
h = (green - blue) / d + (green < blue ? 6 : 0);
|
|
114
|
-
break;
|
|
115
|
-
case green:
|
|
116
|
-
h = (blue - red) / d + 2;
|
|
117
|
-
break;
|
|
118
|
-
case blue:
|
|
119
|
-
h = (red - green) / d + 4;
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
h /= 6;
|
|
123
|
-
}
|
|
124
|
-
return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)];
|
|
125
|
-
}
|
|
126
|
-
function hslToHex(h, s, l) {
|
|
127
|
-
h = ((h % 360) + 360) % 360;
|
|
128
|
-
s = Math.max(0, Math.min(100, s)) / 100;
|
|
129
|
-
l = Math.max(0, Math.min(100, l)) / 100;
|
|
130
|
-
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
131
|
-
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
132
|
-
const m = l - c / 2;
|
|
133
|
-
let r = 0, g = 0, b = 0;
|
|
134
|
-
if (h >= 0 && h < 60) {
|
|
135
|
-
r = c;
|
|
136
|
-
g = x;
|
|
137
|
-
b = 0;
|
|
138
|
-
}
|
|
139
|
-
else if (h >= 60 && h < 120) {
|
|
140
|
-
r = x;
|
|
141
|
-
g = c;
|
|
142
|
-
b = 0;
|
|
143
|
-
}
|
|
144
|
-
else if (h >= 120 && h < 180) {
|
|
145
|
-
r = 0;
|
|
146
|
-
g = c;
|
|
147
|
-
b = x;
|
|
148
|
-
}
|
|
149
|
-
else if (h >= 180 && h < 240) {
|
|
150
|
-
r = 0;
|
|
151
|
-
g = x;
|
|
152
|
-
b = c;
|
|
153
|
-
}
|
|
154
|
-
else if (h >= 240 && h < 300) {
|
|
155
|
-
r = x;
|
|
156
|
-
g = 0;
|
|
157
|
-
b = c;
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
r = c;
|
|
161
|
-
g = 0;
|
|
162
|
-
b = x;
|
|
163
|
-
}
|
|
164
|
-
r = Math.round((r + m) * 255);
|
|
165
|
-
g = Math.round((g + m) * 255);
|
|
166
|
-
b = Math.round((b + m) * 255);
|
|
167
|
-
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
|
|
168
|
-
}
|
|
169
|
-
function adjustHexBrightness(hex, offsetPercent) {
|
|
170
|
-
const normalizedHex = normalizeHex(hex);
|
|
171
|
-
const p = Math.max(-100, Math.min(100, offsetPercent)) / 100;
|
|
172
|
-
const r = parseInt(normalizedHex.slice(1, 3), 16);
|
|
173
|
-
const g = parseInt(normalizedHex.slice(3, 5), 16);
|
|
174
|
-
const b = parseInt(normalizedHex.slice(5, 7), 16);
|
|
175
|
-
const adjustChannel = (channel) => {
|
|
176
|
-
if (p > 0)
|
|
177
|
-
return Math.min(255, Math.floor(channel + (255 - channel) * p));
|
|
178
|
-
else if (p < 0)
|
|
179
|
-
return Math.max(0, Math.floor(channel * (1 + p)));
|
|
180
|
-
return channel;
|
|
181
|
-
};
|
|
182
|
-
const newR = adjustChannel(r);
|
|
183
|
-
const newG = adjustChannel(g);
|
|
184
|
-
const newB = adjustChannel(b);
|
|
185
|
-
return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`;
|
|
186
|
-
}
|
|
187
|
-
function rotateHue(hex, degrees) {
|
|
188
|
-
const [h, s, l] = hexToHsl(hex);
|
|
189
|
-
const newH = ((h + degrees) % 360 + 360) % 360;
|
|
190
|
-
return hslToHex(newH, s, l);
|
|
191
|
-
}
|
|
192
|
-
function clamp01(v) { return Math.max(0, Math.min(1, v)); }
|
|
193
|
-
function toHex2(n) { return Math.round(n).toString(16).padStart(2, '0'); }
|
|
194
|
-
function rgbToHex({ r, g, b }) {
|
|
195
|
-
return `#${toHex2(r)}${toHex2(g)}${toHex2(b)}`.toLowerCase();
|
|
196
|
-
}
|
|
197
|
-
function rgbaToHex({ r, g, b, a }) {
|
|
198
|
-
const aa = Math.round(clamp01(a) * 255);
|
|
199
|
-
return `#${toHex2(r)}${toHex2(g)}${toHex2(b)}${toHex2(aa)}`.toLowerCase();
|
|
200
|
-
}
|
|
201
|
-
function rgbToRgbaString({ r, g, b }, a) {
|
|
202
|
-
return `rgba(${Math.round(r)}, ${Math.round(g)}, ${Math.round(b)}, ${+a.toFixed(3)})`;
|
|
203
|
-
}
|
|
204
|
-
function rgbaStringToRgba(str) {
|
|
205
|
-
const s = str.trim().toLowerCase();
|
|
206
|
-
const m = s.match(/rgba?\(([^)]+)\)/);
|
|
207
|
-
if (!m)
|
|
208
|
-
return null;
|
|
209
|
-
const parts = m[1].split(/,\s*/).map(p => p.trim());
|
|
210
|
-
if (parts.length < 3)
|
|
211
|
-
return null;
|
|
212
|
-
const parseChannel = (v) => v.endsWith('%') ? Math.round(parseFloat(v) * 2.55) : Math.round(parseFloat(v));
|
|
213
|
-
const r = parseChannel(parts[0]);
|
|
214
|
-
const g = parseChannel(parts[1]);
|
|
215
|
-
const b = parseChannel(parts[2]);
|
|
216
|
-
const a = parts[3] !== undefined ? parseFloat(parts[3]) : 1;
|
|
217
|
-
return { r, g, b, a };
|
|
218
|
-
}
|
|
219
|
-
function rgbToHsl({ r, g, b }) {
|
|
220
|
-
const rd = r / 255, gd = g / 255, bd = b / 255;
|
|
221
|
-
const max = Math.max(rd, gd, bd), min = Math.min(rd, gd, bd);
|
|
222
|
-
let h = 0, s = 0;
|
|
223
|
-
const l = (max + min) / 2;
|
|
224
|
-
if (max !== min) {
|
|
225
|
-
const d = max - min;
|
|
226
|
-
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
227
|
-
switch (max) {
|
|
228
|
-
case rd:
|
|
229
|
-
h = (gd - bd) / d + (gd < bd ? 6 : 0);
|
|
230
|
-
break;
|
|
231
|
-
case gd:
|
|
232
|
-
h = (bd - rd) / d + 2;
|
|
233
|
-
break;
|
|
234
|
-
case bd:
|
|
235
|
-
h = (rd - gd) / d + 4;
|
|
236
|
-
break;
|
|
237
|
-
}
|
|
238
|
-
h /= 6;
|
|
239
|
-
}
|
|
240
|
-
return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)];
|
|
241
|
-
}
|
|
242
|
-
function hslToRgb(h, s, l) {
|
|
243
|
-
h = ((h % 360) + 360) % 360;
|
|
244
|
-
s = Math.max(0, Math.min(100, s)) / 100;
|
|
245
|
-
l = Math.max(0, Math.min(100, l)) / 100;
|
|
246
|
-
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
247
|
-
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
248
|
-
const m = l - c / 2;
|
|
249
|
-
let rd = 0, gd = 0, bd = 0;
|
|
250
|
-
if (h >= 0 && h < 60) {
|
|
251
|
-
rd = c;
|
|
252
|
-
gd = x;
|
|
253
|
-
bd = 0;
|
|
254
|
-
}
|
|
255
|
-
else if (h >= 60 && h < 120) {
|
|
256
|
-
rd = x;
|
|
257
|
-
gd = c;
|
|
258
|
-
bd = 0;
|
|
259
|
-
}
|
|
260
|
-
else if (h >= 120 && h < 180) {
|
|
261
|
-
rd = 0;
|
|
262
|
-
gd = c;
|
|
263
|
-
bd = x;
|
|
264
|
-
}
|
|
265
|
-
else if (h >= 180 && h < 240) {
|
|
266
|
-
rd = 0;
|
|
267
|
-
gd = x;
|
|
268
|
-
bd = c;
|
|
269
|
-
}
|
|
270
|
-
else if (h >= 240 && h < 300) {
|
|
271
|
-
rd = x;
|
|
272
|
-
gd = 0;
|
|
273
|
-
bd = c;
|
|
274
|
-
}
|
|
275
|
-
else {
|
|
276
|
-
rd = c;
|
|
277
|
-
gd = 0;
|
|
278
|
-
bd = x;
|
|
279
|
-
}
|
|
280
|
-
return { r: Math.round((rd + m) * 255), g: Math.round((gd + m) * 255), b: Math.round((bd + m) * 255) };
|
|
281
|
-
}
|
|
282
|
-
function rgbToHsv({ r, g, b }) {
|
|
283
|
-
const rd = r / 255, gd = g / 255, bd = b / 255;
|
|
284
|
-
const max = Math.max(rd, gd, bd), min = Math.min(rd, gd, bd);
|
|
285
|
-
const v = max;
|
|
286
|
-
const d = max - min;
|
|
287
|
-
const s = max === 0 ? 0 : d / max;
|
|
288
|
-
let h = 0;
|
|
289
|
-
if (d !== 0) {
|
|
290
|
-
switch (max) {
|
|
291
|
-
case rd:
|
|
292
|
-
h = (gd - bd) / d + (gd < bd ? 6 : 0);
|
|
293
|
-
break;
|
|
294
|
-
case gd:
|
|
295
|
-
h = (bd - rd) / d + 2;
|
|
296
|
-
break;
|
|
297
|
-
case bd:
|
|
298
|
-
h = (rd - gd) / d + 4;
|
|
299
|
-
break;
|
|
300
|
-
}
|
|
301
|
-
h /= 6;
|
|
302
|
-
}
|
|
303
|
-
return [Math.round(h * 360), Math.round(s * 100), Math.round(v * 100)];
|
|
304
|
-
}
|
|
305
|
-
function hsvToRgb(h, s, v) {
|
|
306
|
-
h = ((h % 360) + 360) % 360;
|
|
307
|
-
s = Math.max(0, Math.min(100, s)) / 100;
|
|
308
|
-
v = Math.max(0, Math.min(100, v)) / 100;
|
|
309
|
-
const i = Math.floor(h / 60);
|
|
310
|
-
const f = h / 60 - i;
|
|
311
|
-
const p = v * (1 - s);
|
|
312
|
-
const q = v * (1 - f * s);
|
|
313
|
-
const t = v * (1 - (1 - f) * s);
|
|
314
|
-
let rd = 0, gd = 0, bd = 0;
|
|
315
|
-
switch (i) {
|
|
316
|
-
case 0:
|
|
317
|
-
rd = v;
|
|
318
|
-
gd = t;
|
|
319
|
-
bd = p;
|
|
320
|
-
break;
|
|
321
|
-
case 1:
|
|
322
|
-
rd = q;
|
|
323
|
-
gd = v;
|
|
324
|
-
bd = p;
|
|
325
|
-
break;
|
|
326
|
-
case 2:
|
|
327
|
-
rd = p;
|
|
328
|
-
gd = v;
|
|
329
|
-
bd = t;
|
|
330
|
-
break;
|
|
331
|
-
case 3:
|
|
332
|
-
rd = p;
|
|
333
|
-
gd = q;
|
|
334
|
-
bd = v;
|
|
335
|
-
break;
|
|
336
|
-
case 4:
|
|
337
|
-
rd = t;
|
|
338
|
-
gd = p;
|
|
339
|
-
bd = v;
|
|
340
|
-
break;
|
|
341
|
-
default:
|
|
342
|
-
rd = v;
|
|
343
|
-
gd = p;
|
|
344
|
-
bd = q;
|
|
345
|
-
break;
|
|
346
|
-
}
|
|
347
|
-
return { r: Math.round(rd * 255), g: Math.round(gd * 255), b: Math.round(bd * 255) };
|
|
348
|
-
}
|
|
349
|
-
function hexToHsv(hex) {
|
|
350
|
-
const [r, g, b] = hexToRgb(hex);
|
|
351
|
-
return rgbToHsv({ r, g, b });
|
|
352
|
-
}
|
|
353
|
-
function hsvToHex(h, s, v) {
|
|
354
|
-
const { r, g, b } = hsvToRgb(h, s, v);
|
|
355
|
-
return rgbToHex({ r, g, b });
|
|
356
|
-
}
|
|
357
|
-
function hex8ToRgba(hex) {
|
|
358
|
-
let h = hex.trim();
|
|
359
|
-
if (!h.startsWith('#'))
|
|
360
|
-
h = `#${h}`;
|
|
361
|
-
if (h.length === 5) { // #rgba shorthand
|
|
362
|
-
h = `#${h[1]}${h[1]}${h[2]}${h[2]}${h[3]}${h[3]}${h[4]}${h[4]}`;
|
|
363
|
-
}
|
|
364
|
-
if (h.length !== 9)
|
|
365
|
-
return null;
|
|
366
|
-
const r = parseInt(h.slice(1, 3), 16);
|
|
367
|
-
const g = parseInt(h.slice(3, 5), 16);
|
|
368
|
-
const b = parseInt(h.slice(5, 7), 16);
|
|
369
|
-
const a = parseInt(h.slice(7, 9), 16) / 255;
|
|
370
|
-
return { r, g, b, a };
|
|
371
|
-
}
|
|
372
|
-
function rgbaToHex8({ r, g, b, a }) {
|
|
373
|
-
return rgbaToHex({ r, g, b, a });
|
|
374
|
-
}
|
|
375
|
-
function normalizeColor(input) {
|
|
376
|
-
if (typeof input !== 'string') {
|
|
377
|
-
if ('r' in input && 'g' in input && 'b' in input) {
|
|
378
|
-
const { r, g, b } = input;
|
|
379
|
-
const hex = rgbToHex({ r, g, b });
|
|
380
|
-
const [h, s, l] = rgbToHsl({ r, g, b });
|
|
381
|
-
const [hh, ss, vv] = rgbToHsv({ r, g, b });
|
|
382
|
-
return { type: 'rgb', hex, r, g, b, a: 1, h, s, l, v: vv };
|
|
383
|
-
}
|
|
384
|
-
if ('h' in input && 's' in input && 'l' in input) {
|
|
385
|
-
const { h, s, l } = input;
|
|
386
|
-
const { r, g, b } = hslToRgb(h, s, l);
|
|
387
|
-
const hex = rgbToHex({ r, g, b });
|
|
388
|
-
const [hh, ss, vv] = rgbToHsv({ r, g, b });
|
|
389
|
-
return { type: 'hsl', hex, r, g, b, a: 1, h, s, l, v: vv };
|
|
390
|
-
}
|
|
391
|
-
return { type: 'unknown' };
|
|
392
|
-
}
|
|
393
|
-
const str = input.trim();
|
|
394
|
-
if (isCssVariable(str))
|
|
395
|
-
return { type: 'css-var', raw: str };
|
|
396
|
-
if (isHexColor(str)) {
|
|
397
|
-
const hex = normalizeHex(str);
|
|
398
|
-
const [r, g, b] = hexToRgb(hex);
|
|
399
|
-
const [h, s, l] = hexToHsl(hex);
|
|
400
|
-
const [hh, ss, vv] = rgbToHsv({ r, g, b });
|
|
401
|
-
return { type: 'hex', hex, r, g, b, a: 1, h, s, l, v: vv };
|
|
402
|
-
}
|
|
403
|
-
const hex8 = str.match(/^#([0-9a-f]{8}|[0-9a-f]{4})$/i);
|
|
404
|
-
if (hex8) {
|
|
405
|
-
const rgba = hex8ToRgba(str);
|
|
406
|
-
if (rgba) {
|
|
407
|
-
const { r, g, b, a } = rgba;
|
|
408
|
-
const hex = rgbToHex({ r, g, b });
|
|
409
|
-
const [h, s, l] = rgbToHsl({ r, g, b });
|
|
410
|
-
const [hh, ss, vv] = rgbToHsv({ r, g, b });
|
|
411
|
-
return { type: 'hex', hex, r, g, b, a, h, s, l, v: vv };
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
if (isRgbColor(str)) {
|
|
415
|
-
const rgba = rgbaStringToRgba(str);
|
|
416
|
-
if (rgba) {
|
|
417
|
-
const { r, g, b, a } = rgba;
|
|
418
|
-
const hex = rgbToHex({ r, g, b });
|
|
419
|
-
const [h, s, l] = rgbToHsl({ r, g, b });
|
|
420
|
-
const [hh, ss, vv] = rgbToHsv({ r, g, b });
|
|
421
|
-
return { type: 'rgb', hex, r, g, b, a, h, s, l, v: vv };
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
if (isHslColor(str)) {
|
|
425
|
-
const m = str.match(/hsla?\(([^)]+)\)/i);
|
|
426
|
-
if (m) {
|
|
427
|
-
const parts = m[1].split(/,\s*/);
|
|
428
|
-
const h = parseFloat(parts[0]);
|
|
429
|
-
const s = parseFloat(parts[1]);
|
|
430
|
-
const l = parseFloat(parts[2]);
|
|
431
|
-
const a = parts[3] ? parseFloat(parts[3]) : 1;
|
|
432
|
-
const { r, g, b } = hslToRgb(h, s, l);
|
|
433
|
-
const hex = rgbToHex({ r, g, b });
|
|
434
|
-
const [hh, ss, vv] = rgbToHsv({ r, g, b });
|
|
435
|
-
return { type: 'hsl', hex, r, g, b, a, h, s, l, v: vv };
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
if (str === 'transparent')
|
|
439
|
-
return { type: 'named', hex: '#000000', r: 0, g: 0, b: 0, a: 0 };
|
|
440
|
-
return { type: 'unknown' };
|
|
441
|
-
}
|
|
442
|
-
function mixColors(c1, c2, t, opts) {
|
|
443
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
|
|
444
|
-
const o1 = normalizeColor(c1);
|
|
445
|
-
const o2 = normalizeColor(c2);
|
|
446
|
-
t = Math.max(0, Math.min(1, t));
|
|
447
|
-
const mode = (opts === null || opts === void 0 ? void 0 : opts.mode) || 'rgb';
|
|
448
|
-
const out = (opts === null || opts === void 0 ? void 0 : opts.format) || 'hex';
|
|
449
|
-
let r = 0, g = 0, b = 0, a = 1;
|
|
450
|
-
if (mode === 'hsl') {
|
|
451
|
-
const h1 = (_a = o1.h) !== null && _a !== void 0 ? _a : rgbToHsl({ r: (_b = o1.r) !== null && _b !== void 0 ? _b : 0, g: (_c = o1.g) !== null && _c !== void 0 ? _c : 0, b: (_d = o1.b) !== null && _d !== void 0 ? _d : 0 })[0];
|
|
452
|
-
const s1 = (_e = o1.s) !== null && _e !== void 0 ? _e : 0;
|
|
453
|
-
const l1 = (_f = o1.l) !== null && _f !== void 0 ? _f : 0;
|
|
454
|
-
const h2 = (_g = o2.h) !== null && _g !== void 0 ? _g : rgbToHsl({ r: (_h = o2.r) !== null && _h !== void 0 ? _h : 0, g: (_j = o2.g) !== null && _j !== void 0 ? _j : 0, b: (_k = o2.b) !== null && _k !== void 0 ? _k : 0 })[0];
|
|
455
|
-
const s2 = (_l = o2.s) !== null && _l !== void 0 ? _l : 0;
|
|
456
|
-
const l2 = (_m = o2.l) !== null && _m !== void 0 ? _m : 0;
|
|
457
|
-
const ih = h1 + (((h2 - h1 + 540) % 360) - 180) * t;
|
|
458
|
-
const is = s1 + (s2 - s1) * t;
|
|
459
|
-
const il = l1 + (l2 - l1) * t;
|
|
460
|
-
const rgb = hslToRgb(ih, is, il);
|
|
461
|
-
r = rgb.r;
|
|
462
|
-
g = rgb.g;
|
|
463
|
-
b = rgb.b;
|
|
464
|
-
a = ((_o = o1.a) !== null && _o !== void 0 ? _o : 1) + (((_p = o2.a) !== null && _p !== void 0 ? _p : 1) - ((_q = o1.a) !== null && _q !== void 0 ? _q : 1)) * t;
|
|
465
|
-
}
|
|
466
|
-
else {
|
|
467
|
-
r = (((_r = o1.r) !== null && _r !== void 0 ? _r : 0) * (1 - t)) + (((_s = o2.r) !== null && _s !== void 0 ? _s : 0) * t);
|
|
468
|
-
g = (((_t = o1.g) !== null && _t !== void 0 ? _t : 0) * (1 - t)) + (((_u = o2.g) !== null && _u !== void 0 ? _u : 0) * t);
|
|
469
|
-
b = (((_v = o1.b) !== null && _v !== void 0 ? _v : 0) * (1 - t)) + (((_w = o2.b) !== null && _w !== void 0 ? _w : 0) * t);
|
|
470
|
-
a = ((_x = o1.a) !== null && _x !== void 0 ? _x : 1) * (1 - t) + ((_y = o2.a) !== null && _y !== void 0 ? _y : 1) * t;
|
|
471
|
-
}
|
|
472
|
-
if (out === 'hex')
|
|
473
|
-
return rgbToHex({ r: Math.round(r), g: Math.round(g), b: Math.round(b) });
|
|
474
|
-
if (out === 'rgba')
|
|
475
|
-
return rgbToRgbaString({ r: Math.round(r), g: Math.round(g), b: Math.round(b) }, a);
|
|
476
|
-
if (out === 'rgb')
|
|
477
|
-
return `rgb(${Math.round(r)}, ${Math.round(g)}, ${Math.round(b)})`;
|
|
478
|
-
if (out === 'hsl') {
|
|
479
|
-
const [hh, ss, ll] = rgbToHsl({ r: Math.round(r), g: Math.round(g), b: Math.round(b) });
|
|
480
|
-
return `hsl(${hh}, ${ss}%, ${ll}%)`;
|
|
481
|
-
}
|
|
482
|
-
return rgbToHex({ r: Math.round(r), g: Math.round(g), b: Math.round(b) });
|
|
483
|
-
}
|
|
484
|
-
function relativeLuminance(color) {
|
|
485
|
-
var _a, _b, _c;
|
|
486
|
-
const n = normalizeColor(color);
|
|
487
|
-
const r = ((_a = n.r) !== null && _a !== void 0 ? _a : 0) / 255;
|
|
488
|
-
const g = ((_b = n.g) !== null && _b !== void 0 ? _b : 0) / 255;
|
|
489
|
-
const b = ((_c = n.b) !== null && _c !== void 0 ? _c : 0) / 255;
|
|
490
|
-
const srgbToLin = (c) => c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
491
|
-
const R = srgbToLin(r), G = srgbToLin(g), B = srgbToLin(b);
|
|
492
|
-
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
|
493
|
-
}
|
|
494
|
-
function contrastRatio(a, b) {
|
|
495
|
-
const L1 = relativeLuminance(a);
|
|
496
|
-
const L2 = relativeLuminance(b);
|
|
497
|
-
const light = Math.max(L1, L2);
|
|
498
|
-
const dark = Math.min(L1, L2);
|
|
499
|
-
return +((light + 0.05) / (dark + 0.05)).toFixed(2);
|
|
500
|
-
}
|
|
501
|
-
function isDark(color, threshold = 0.5) {
|
|
502
|
-
return relativeLuminance(color) < threshold;
|
|
503
|
-
}
|
|
504
|
-
function isLight(color, threshold = 0.5) {
|
|
505
|
-
return !isDark(color, threshold);
|
|
506
|
-
}
|
|
507
|
-
function rgbToCmyk({ r, g, b }) {
|
|
508
|
-
const rd = r / 255, gd = g / 255, bd = b / 255;
|
|
509
|
-
const k = 1 - Math.max(rd, gd, bd);
|
|
510
|
-
if (k === 1)
|
|
511
|
-
return { c: 0, m: 0, y: 0, k: 1 };
|
|
512
|
-
const c = (1 - rd - k) / (1 - k);
|
|
513
|
-
const m = (1 - gd - k) / (1 - k);
|
|
514
|
-
const y = (1 - bd - k) / (1 - k);
|
|
515
|
-
return { c, m, y, k };
|
|
516
|
-
}
|
|
517
|
-
function cmykToRgb({ c, m, y, k }) {
|
|
518
|
-
const r = 255 * (1 - c) * (1 - k);
|
|
519
|
-
const g = 255 * (1 - m) * (1 - k);
|
|
520
|
-
const b = 255 * (1 - y) * (1 - k);
|
|
521
|
-
return { r: Math.round(r), g: Math.round(g), b: Math.round(b) };
|
|
522
|
-
}
|
|
523
|
-
function srgbToLinear(v) {
|
|
524
|
-
v = v / 255;
|
|
525
|
-
return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
526
|
-
}
|
|
527
|
-
function linearToSrgb(v) {
|
|
528
|
-
const t = v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
|
|
529
|
-
return Math.round(Math.max(0, Math.min(1, t)) * 255);
|
|
530
|
-
}
|
|
531
|
-
function rgbToXyz({ r, g, b }) {
|
|
532
|
-
const R = srgbToLinear(r);
|
|
533
|
-
const G = srgbToLinear(g);
|
|
534
|
-
const B = srgbToLinear(b);
|
|
535
|
-
const X = R * 0.4124564 + G * 0.3575761 + B * 0.1804375;
|
|
536
|
-
const Y = R * 0.2126729 + G * 0.7151522 + B * 0.0721750;
|
|
537
|
-
const Z = R * 0.0193339 + G * 0.1191920 + B * 0.9503041;
|
|
538
|
-
return { X: X * 100, Y: Y * 100, Z: Z * 100 };
|
|
539
|
-
}
|
|
540
|
-
function xyzToRgb({ X, Y, Z }) {
|
|
541
|
-
X = X / 100;
|
|
542
|
-
Y = Y / 100;
|
|
543
|
-
Z = Z / 100;
|
|
544
|
-
let R = X * 3.2404542 + Y * -1.5371385 + Z * -0.4985314;
|
|
545
|
-
let G = X * -0.9692660 + Y * 1.8760108 + Z * 0.0415560;
|
|
546
|
-
let B = X * 0.0556434 + Y * -0.2040259 + Z * 1.0572252;
|
|
547
|
-
R = linearToSrgb(R);
|
|
548
|
-
G = linearToSrgb(G);
|
|
549
|
-
B = linearToSrgb(B);
|
|
550
|
-
return { r: R, g: G, b: B };
|
|
551
|
-
}
|
|
552
|
-
function rgbToLab({ r, g, b }) {
|
|
553
|
-
const { X, Y, Z } = rgbToXyz({ r, g, b });
|
|
554
|
-
const refX = 95.047, refY = 100.0, refZ = 108.883;
|
|
555
|
-
const x = X / refX, y = Y / refY, z = Z / refZ;
|
|
556
|
-
const fx = x > 0.008856 ? Math.cbrt(x) : (7.787 * x) + 16 / 116;
|
|
557
|
-
const fy = y > 0.008856 ? Math.cbrt(y) : (7.787 * y) + 16 / 116;
|
|
558
|
-
const fz = z > 0.008856 ? Math.cbrt(z) : (7.787 * z) + 16 / 116;
|
|
559
|
-
const L = (116 * fy) - 16;
|
|
560
|
-
const a = 500 * (fx - fy);
|
|
561
|
-
const b2 = 200 * (fy - fz);
|
|
562
|
-
return { L, a, b: b2 };
|
|
563
|
-
}
|
|
564
|
-
function labToRgb({ L, a, b }) {
|
|
565
|
-
const refX = 95.047, refY = 100.0, refZ = 108.883;
|
|
566
|
-
let fy = (L + 16) / 116;
|
|
567
|
-
let fx = a / 500 + fy;
|
|
568
|
-
let fz = fy - b / 200;
|
|
569
|
-
const fx3 = Math.pow(fx, 3);
|
|
570
|
-
const fz3 = Math.pow(fz, 3);
|
|
571
|
-
const fy3 = Math.pow(fy, 3);
|
|
572
|
-
const xr = fx3 > 0.008856 ? fx3 : (fx - 16 / 116) / 7.787;
|
|
573
|
-
const yr = L > (903.3 * 0.008856) ? fy3 : L / 903.3;
|
|
574
|
-
const zr = fz3 > 0.008856 ? fz3 : (fz - 16 / 116) / 7.787;
|
|
575
|
-
const X = xr * refX, Y = yr * refY, Z = zr * refZ;
|
|
576
|
-
return xyzToRgb({ X, Y, Z });
|
|
577
|
-
}
|
|
578
|
-
function rgbToLch({ r, g, b }) {
|
|
579
|
-
const { L, a, b: bb } = rgbToLab({ r, g, b });
|
|
580
|
-
const C = Math.sqrt(a * a + bb * bb);
|
|
581
|
-
let H = Math.atan2(bb, a) * (180 / Math.PI);
|
|
582
|
-
if (H < 0)
|
|
583
|
-
H += 360;
|
|
584
|
-
return { L, C, H };
|
|
585
|
-
}
|
|
586
|
-
function lchToRgb({ L, C, H }) {
|
|
587
|
-
const a = Math.cos(H * Math.PI / 180) * C;
|
|
588
|
-
const b = Math.sin(H * Math.PI / 180) * C;
|
|
589
|
-
return labToRgb({ L, a, b });
|
|
590
|
-
}
|