@kurkle/color 0.1.7 → 0.2.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.
- package/LICENSE.md +1 -1
- package/README.md +0 -1
- package/dist/color.esm.js +344 -323
- package/dist/color.js +345 -323
- package/dist/color.min.js +3 -3
- package/package.json +21 -26
- package/dist/color.esm.min.js +0 -14
package/dist/color.esm.js
CHANGED
|
@@ -1,213 +1,184 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @kurkle/color v0.
|
|
2
|
+
* @kurkle/color v0.2.0
|
|
3
3
|
* https://github.com/kurkle/color#readme
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) 2022 Jukka Kurkela
|
|
5
5
|
* Released under the MIT License
|
|
6
6
|
*/
|
|
7
|
-
const map = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15};
|
|
8
|
-
const hex = '0123456789ABCDEF';
|
|
9
|
-
const h1 = (b) => hex[b & 0xF];
|
|
10
|
-
const h2 = (b) => hex[(b & 0xF0) >> 4] + hex[b & 0xF];
|
|
11
|
-
const eq = (b) => (((b & 0xF0) >> 4) === (b & 0xF));
|
|
12
|
-
function isShort(v) {
|
|
13
|
-
return eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a);
|
|
14
|
-
}
|
|
15
|
-
function hexParse(str) {
|
|
16
|
-
var len = str.length;
|
|
17
|
-
var ret;
|
|
18
|
-
if (str[0] === '#') {
|
|
19
|
-
if (len === 4 || len === 5) {
|
|
20
|
-
ret = {
|
|
21
|
-
r: 255 & map[str[1]] * 17,
|
|
22
|
-
g: 255 & map[str[2]] * 17,
|
|
23
|
-
b: 255 & map[str[3]] * 17,
|
|
24
|
-
a: len === 5 ? map[str[4]] * 17 : 255
|
|
25
|
-
};
|
|
26
|
-
} else if (len === 7 || len === 9) {
|
|
27
|
-
ret = {
|
|
28
|
-
r: map[str[1]] << 4 | map[str[2]],
|
|
29
|
-
g: map[str[3]] << 4 | map[str[4]],
|
|
30
|
-
b: map[str[5]] << 4 | map[str[6]],
|
|
31
|
-
a: len === 9 ? (map[str[7]] << 4 | map[str[8]]) : 255
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return ret;
|
|
36
|
-
}
|
|
37
|
-
function hexString(v) {
|
|
38
|
-
var f = isShort(v) ? h1 : h2;
|
|
39
|
-
return v
|
|
40
|
-
? '#' + f(v.r) + f(v.g) + f(v.b) + (v.a < 255 ? f(v.a) : '')
|
|
41
|
-
: v;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
7
|
function round(v) {
|
|
45
|
-
|
|
8
|
+
return v + 0.5 | 0;
|
|
46
9
|
}
|
|
47
10
|
const lim = (v, l, h) => Math.max(Math.min(v, h), l);
|
|
48
11
|
function p2b(v) {
|
|
49
|
-
|
|
12
|
+
return lim(round(v * 2.55), 0, 255);
|
|
50
13
|
}
|
|
51
14
|
function b2p(v) {
|
|
52
|
-
|
|
15
|
+
return lim(round(v / 2.55), 0, 100);
|
|
53
16
|
}
|
|
54
17
|
function n2b(v) {
|
|
55
|
-
|
|
18
|
+
return lim(round(v * 255), 0, 255);
|
|
56
19
|
}
|
|
57
20
|
function b2n(v) {
|
|
58
|
-
|
|
21
|
+
return lim(round(v / 2.55) / 100, 0, 1);
|
|
59
22
|
}
|
|
60
23
|
function n2p(v) {
|
|
61
|
-
|
|
24
|
+
return lim(round(v * 100), 0, 100);
|
|
62
25
|
}
|
|
63
26
|
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
27
|
+
const map$1 = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15};
|
|
28
|
+
const hex = [...'0123456789ABCDEF'];
|
|
29
|
+
const h1 = b => hex[b & 0xF];
|
|
30
|
+
const h2 = b => hex[(b & 0xF0) >> 4] + hex[b & 0xF];
|
|
31
|
+
const eq = b => ((b & 0xF0) >> 4) === (b & 0xF);
|
|
32
|
+
const isShort = v => eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a);
|
|
33
|
+
function hexParse(str) {
|
|
34
|
+
var len = str.length;
|
|
35
|
+
var ret;
|
|
36
|
+
if (str[0] === '#') {
|
|
37
|
+
if (len === 4 || len === 5) {
|
|
38
|
+
ret = {
|
|
39
|
+
r: 255 & map$1[str[1]] * 17,
|
|
40
|
+
g: 255 & map$1[str[2]] * 17,
|
|
41
|
+
b: 255 & map$1[str[3]] * 17,
|
|
42
|
+
a: len === 5 ? map$1[str[4]] * 17 : 255
|
|
43
|
+
};
|
|
44
|
+
} else if (len === 7 || len === 9) {
|
|
45
|
+
ret = {
|
|
46
|
+
r: map$1[str[1]] << 4 | map$1[str[2]],
|
|
47
|
+
g: map$1[str[3]] << 4 | map$1[str[4]],
|
|
48
|
+
b: map$1[str[5]] << 4 | map$1[str[6]],
|
|
49
|
+
a: len === 9 ? (map$1[str[7]] << 4 | map$1[str[8]]) : 255
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return ret;
|
|
88
54
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
55
|
+
const alpha = (a, f) => a < 255 ? f(a) : '';
|
|
56
|
+
function hexString(v) {
|
|
57
|
+
var f = isShort(v) ? h1 : h2;
|
|
58
|
+
return v
|
|
59
|
+
? '#' + f(v.r) + f(v.g) + f(v.b) + alpha(v.a, f)
|
|
60
|
+
: undefined;
|
|
95
61
|
}
|
|
96
62
|
|
|
97
63
|
const HUE_RE = /^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/;
|
|
98
64
|
function hsl2rgbn(h, s, l) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
65
|
+
const a = s * Math.min(l, 1 - l);
|
|
66
|
+
const f = (n, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
67
|
+
return [f(0), f(8), f(4)];
|
|
102
68
|
}
|
|
103
69
|
function hsv2rgbn(h, s, v) {
|
|
104
|
-
|
|
105
|
-
|
|
70
|
+
const f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
71
|
+
return [f(5), f(3), f(1)];
|
|
106
72
|
}
|
|
107
73
|
function hwb2rgbn(h, w, b) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
74
|
+
const rgb = hsl2rgbn(h, 1, 0.5);
|
|
75
|
+
let i;
|
|
76
|
+
if (w + b > 1) {
|
|
77
|
+
i = 1 / (w + b);
|
|
78
|
+
w *= i;
|
|
79
|
+
b *= i;
|
|
80
|
+
}
|
|
81
|
+
for (i = 0; i < 3; i++) {
|
|
82
|
+
rgb[i] *= 1 - w - b;
|
|
83
|
+
rgb[i] += w;
|
|
84
|
+
}
|
|
85
|
+
return rgb;
|
|
86
|
+
}
|
|
87
|
+
function hueValue(r, g, b, d, max) {
|
|
88
|
+
if (r === max) {
|
|
89
|
+
return ((g - b) / d) + (g < b ? 6 : 0);
|
|
90
|
+
}
|
|
91
|
+
if (g === max) {
|
|
92
|
+
return (b - r) / d + 2;
|
|
93
|
+
}
|
|
94
|
+
return (r - g) / d + 4;
|
|
120
95
|
}
|
|
121
96
|
function rgb2hsl(v) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
: (r - g) / d + 4;
|
|
138
|
-
h = h * 60 + 0.5;
|
|
139
|
-
}
|
|
140
|
-
return [h | 0, s || 0, l];
|
|
97
|
+
const range = 255;
|
|
98
|
+
const r = v.r / range;
|
|
99
|
+
const g = v.g / range;
|
|
100
|
+
const b = v.b / range;
|
|
101
|
+
const max = Math.max(r, g, b);
|
|
102
|
+
const min = Math.min(r, g, b);
|
|
103
|
+
const l = (max + min) / 2;
|
|
104
|
+
let h, s, d;
|
|
105
|
+
if (max !== min) {
|
|
106
|
+
d = max - min;
|
|
107
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
108
|
+
h = hueValue(r, g, b, d, max);
|
|
109
|
+
h = h * 60 + 0.5;
|
|
110
|
+
}
|
|
111
|
+
return [h | 0, s || 0, l];
|
|
141
112
|
}
|
|
142
113
|
function calln(f, a, b, c) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
114
|
+
return (
|
|
115
|
+
Array.isArray(a)
|
|
116
|
+
? f(a[0], a[1], a[2])
|
|
117
|
+
: f(a, b, c)
|
|
118
|
+
).map(n2b);
|
|
148
119
|
}
|
|
149
120
|
function hsl2rgb(h, s, l) {
|
|
150
|
-
|
|
121
|
+
return calln(hsl2rgbn, h, s, l);
|
|
151
122
|
}
|
|
152
123
|
function hwb2rgb(h, w, b) {
|
|
153
|
-
|
|
124
|
+
return calln(hwb2rgbn, h, w, b);
|
|
154
125
|
}
|
|
155
126
|
function hsv2rgb(h, s, v) {
|
|
156
|
-
|
|
127
|
+
return calln(hsv2rgbn, h, s, v);
|
|
157
128
|
}
|
|
158
129
|
function hue(h) {
|
|
159
|
-
|
|
130
|
+
return (h % 360 + 360) % 360;
|
|
160
131
|
}
|
|
161
132
|
function hueParse(str) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
133
|
+
const m = HUE_RE.exec(str);
|
|
134
|
+
let a = 255;
|
|
135
|
+
let v;
|
|
136
|
+
if (!m) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (m[5] !== v) {
|
|
140
|
+
a = m[6] ? p2b(+m[5]) : n2b(+m[5]);
|
|
141
|
+
}
|
|
142
|
+
const h = hue(+m[2]);
|
|
143
|
+
const p1 = +m[3] / 100;
|
|
144
|
+
const p2 = +m[4] / 100;
|
|
145
|
+
if (m[1] === 'hwb') {
|
|
146
|
+
v = hwb2rgb(h, p1, p2);
|
|
147
|
+
} else if (m[1] === 'hsv') {
|
|
148
|
+
v = hsv2rgb(h, p1, p2);
|
|
149
|
+
} else {
|
|
150
|
+
v = hsl2rgb(h, p1, p2);
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
r: v[0],
|
|
154
|
+
g: v[1],
|
|
155
|
+
b: v[2],
|
|
156
|
+
a: a
|
|
157
|
+
};
|
|
187
158
|
}
|
|
188
159
|
function rotate(v, deg) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
160
|
+
var h = rgb2hsl(v);
|
|
161
|
+
h[0] = hue(h[0] + deg);
|
|
162
|
+
h = hsl2rgb(h);
|
|
163
|
+
v.r = h[0];
|
|
164
|
+
v.g = h[1];
|
|
165
|
+
v.b = h[2];
|
|
195
166
|
}
|
|
196
167
|
function hslString(v) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
168
|
+
if (!v) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const a = rgb2hsl(v);
|
|
172
|
+
const h = a[0];
|
|
173
|
+
const s = n2p(a[1]);
|
|
174
|
+
const l = n2p(a[2]);
|
|
175
|
+
return v.a < 255
|
|
176
|
+
? `hsla(${h}, ${s}%, ${l}%, ${b2n(v.a)})`
|
|
177
|
+
: `hsl(${h}, ${s}%, ${l}%)`;
|
|
207
178
|
}
|
|
208
179
|
|
|
209
|
-
|
|
210
|
-
|
|
180
|
+
const map = {
|
|
181
|
+
x: 'dark',
|
|
211
182
|
Z: 'light',
|
|
212
183
|
Y: 're',
|
|
213
184
|
X: 'blu',
|
|
@@ -235,23 +206,7 @@ var map$1 = {
|
|
|
235
206
|
I: 'ightg',
|
|
236
207
|
J: 'wh'
|
|
237
208
|
};
|
|
238
|
-
|
|
239
|
-
var unpacked = {};
|
|
240
|
-
var keys = Object.keys(obj);
|
|
241
|
-
var tkeys = Object.keys(map$1);
|
|
242
|
-
var i, j, k, ok, nk;
|
|
243
|
-
for (i = 0; i < keys.length; i++) {
|
|
244
|
-
ok = nk = keys[i];
|
|
245
|
-
for (j = 0; j < tkeys.length; j++) {
|
|
246
|
-
k = tkeys[j];
|
|
247
|
-
nk = nk.replace(k, map$1[k]);
|
|
248
|
-
}
|
|
249
|
-
k = parseInt(obj[ok], 16);
|
|
250
|
-
unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF];
|
|
251
|
-
}
|
|
252
|
-
return unpacked;
|
|
253
|
-
}
|
|
254
|
-
const names = unpack({
|
|
209
|
+
const names$1 = {
|
|
255
210
|
OiceXe: 'f0f8ff',
|
|
256
211
|
antiquewEte: 'faebd7',
|
|
257
212
|
aqua: 'ffff',
|
|
@@ -400,164 +355,230 @@ const names = unpack({
|
|
|
400
355
|
wEtesmoke: 'f5f5f5',
|
|
401
356
|
Lw: 'ffff00',
|
|
402
357
|
LwgYF: '9acd32'
|
|
403
|
-
}
|
|
358
|
+
};
|
|
359
|
+
function unpack() {
|
|
360
|
+
const unpacked = {};
|
|
361
|
+
const keys = Object.keys(names$1);
|
|
362
|
+
const tkeys = Object.keys(map);
|
|
363
|
+
let i, j, k, ok, nk;
|
|
364
|
+
for (i = 0; i < keys.length; i++) {
|
|
365
|
+
ok = nk = keys[i];
|
|
366
|
+
for (j = 0; j < tkeys.length; j++) {
|
|
367
|
+
k = tkeys[j];
|
|
368
|
+
nk = nk.replace(k, map[k]);
|
|
369
|
+
}
|
|
370
|
+
k = parseInt(names$1[ok], 16);
|
|
371
|
+
unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF];
|
|
372
|
+
}
|
|
373
|
+
return unpacked;
|
|
374
|
+
}
|
|
404
375
|
|
|
405
|
-
names
|
|
376
|
+
let names;
|
|
406
377
|
function nameParse(str) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
378
|
+
if (!names) {
|
|
379
|
+
names = unpack();
|
|
380
|
+
names.transparent = [0, 0, 0, 0];
|
|
381
|
+
}
|
|
382
|
+
const a = names[str.toLowerCase()];
|
|
383
|
+
return a && {
|
|
384
|
+
r: a[0],
|
|
385
|
+
g: a[1],
|
|
386
|
+
b: a[2],
|
|
387
|
+
a: a.length === 4 ? a[3] : 255
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const RGB_RE = /^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/;
|
|
392
|
+
function rgbParse(str) {
|
|
393
|
+
const m = RGB_RE.exec(str);
|
|
394
|
+
let a = 255;
|
|
395
|
+
let r, g, b;
|
|
396
|
+
if (!m) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (m[7] !== r) {
|
|
400
|
+
const v = +m[7];
|
|
401
|
+
a = m[8] ? p2b(v) : lim(v * 255, 0, 255);
|
|
402
|
+
}
|
|
403
|
+
r = +m[1];
|
|
404
|
+
g = +m[3];
|
|
405
|
+
b = +m[5];
|
|
406
|
+
r = 255 & (m[2] ? p2b(r) : lim(r, 0, 255));
|
|
407
|
+
g = 255 & (m[4] ? p2b(g) : lim(g, 0, 255));
|
|
408
|
+
b = 255 & (m[6] ? p2b(b) : lim(b, 0, 255));
|
|
409
|
+
return {
|
|
410
|
+
r: r,
|
|
411
|
+
g: g,
|
|
412
|
+
b: b,
|
|
413
|
+
a: a
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
function rgbString(v) {
|
|
417
|
+
return v && (
|
|
418
|
+
v.a < 255
|
|
419
|
+
? `rgba(${v.r}, ${v.g}, ${v.b}, ${b2n(v.a)})`
|
|
420
|
+
: `rgb(${v.r}, ${v.g}, ${v.b})`
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
function rgbMix(rgb1, rgb2, t = 0.5) {
|
|
424
|
+
t = 1 - t;
|
|
425
|
+
rgb1.r = 0xFF & rgb1.r + t * (rgb2.r - rgb1.r) + 0.5;
|
|
426
|
+
rgb1.g = 0xFF & rgb1.g + t * (rgb2.g - rgb1.g) + 0.5;
|
|
427
|
+
rgb1.b = 0xFF & rgb1.b + t * (rgb2.b - rgb1.b) + 0.5;
|
|
428
|
+
rgb1.a = rgb1.a + t * (rgb2.a - rgb1.a);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const to = v => v <= 0.0031308 ? v * 12.92 : Math.pow(v, 1.0 / 2.4) * 1.055 - 0.055;
|
|
432
|
+
const from = v => v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
433
|
+
function interpolate(rgb1, rgb2, t) {
|
|
434
|
+
const r = from(b2n(rgb1.r));
|
|
435
|
+
const g = from(b2n(rgb1.g));
|
|
436
|
+
const b = from(b2n(rgb1.b));
|
|
437
|
+
return {
|
|
438
|
+
r: n2b(to(r + t * (from(b2n(rgb2.r)) - r))),
|
|
439
|
+
g: n2b(to(g + t * (from(b2n(rgb2.g)) - g))),
|
|
440
|
+
b: n2b(to(b + t * (from(b2n(rgb2.b)) - b))),
|
|
441
|
+
a: rgb1.a + t * (rgb2.a - rgb1.a)
|
|
442
|
+
};
|
|
414
443
|
}
|
|
415
444
|
|
|
416
445
|
function modHSL(v, i, ratio) {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
446
|
+
if (v) {
|
|
447
|
+
let tmp = rgb2hsl(v);
|
|
448
|
+
tmp[i] = Math.max(0, Math.min(tmp[i] + tmp[i] * ratio, i === 0 ? 360 : 1));
|
|
449
|
+
tmp = hsl2rgb(tmp);
|
|
450
|
+
v.r = tmp[0];
|
|
451
|
+
v.g = tmp[1];
|
|
452
|
+
v.b = tmp[2];
|
|
453
|
+
}
|
|
425
454
|
}
|
|
426
455
|
function clone(v, proto) {
|
|
427
|
-
|
|
456
|
+
return v ? Object.assign(proto || {}, v) : v;
|
|
428
457
|
}
|
|
429
458
|
function fromObject(input) {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
459
|
+
var v = {r: 0, g: 0, b: 0, a: 255};
|
|
460
|
+
if (Array.isArray(input)) {
|
|
461
|
+
if (input.length >= 3) {
|
|
462
|
+
v = {r: input[0], g: input[1], b: input[2], a: 255};
|
|
463
|
+
if (input.length > 3) {
|
|
464
|
+
v.a = n2b(input[3]);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
} else {
|
|
468
|
+
v = clone(input, {r: 0, g: 0, b: 0, a: 1});
|
|
469
|
+
v.a = n2b(v.a);
|
|
470
|
+
}
|
|
471
|
+
return v;
|
|
443
472
|
}
|
|
444
473
|
function functionParse(str) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
474
|
+
if (str.charAt(0) === 'r') {
|
|
475
|
+
return rgbParse(str);
|
|
476
|
+
}
|
|
477
|
+
return hueParse(str);
|
|
449
478
|
}
|
|
450
479
|
class Color {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
modHSL(this._rgb, 1, -ratio);
|
|
550
|
-
return this;
|
|
551
|
-
}
|
|
552
|
-
rotate(deg) {
|
|
553
|
-
rotate(this._rgb, deg);
|
|
554
|
-
return this;
|
|
555
|
-
}
|
|
480
|
+
constructor(input) {
|
|
481
|
+
if (input instanceof Color) {
|
|
482
|
+
return input;
|
|
483
|
+
}
|
|
484
|
+
const type = typeof input;
|
|
485
|
+
let v;
|
|
486
|
+
if (type === 'object') {
|
|
487
|
+
v = fromObject(input);
|
|
488
|
+
} else if (type === 'string') {
|
|
489
|
+
v = hexParse(input) || nameParse(input) || functionParse(input);
|
|
490
|
+
}
|
|
491
|
+
this._rgb = v;
|
|
492
|
+
this._valid = !!v;
|
|
493
|
+
}
|
|
494
|
+
get valid() {
|
|
495
|
+
return this._valid;
|
|
496
|
+
}
|
|
497
|
+
get rgb() {
|
|
498
|
+
var v = clone(this._rgb);
|
|
499
|
+
if (v) {
|
|
500
|
+
v.a = b2n(v.a);
|
|
501
|
+
}
|
|
502
|
+
return v;
|
|
503
|
+
}
|
|
504
|
+
set rgb(obj) {
|
|
505
|
+
this._rgb = fromObject(obj);
|
|
506
|
+
}
|
|
507
|
+
rgbString() {
|
|
508
|
+
return this._valid ? rgbString(this._rgb) : undefined;
|
|
509
|
+
}
|
|
510
|
+
hexString() {
|
|
511
|
+
return this._valid ? hexString(this._rgb) : undefined;
|
|
512
|
+
}
|
|
513
|
+
hslString() {
|
|
514
|
+
return this._valid ? hslString(this._rgb) : undefined;
|
|
515
|
+
}
|
|
516
|
+
mix(color, weight) {
|
|
517
|
+
if (color) {
|
|
518
|
+
rgbMix(this._rgb, color._rgb, weight);
|
|
519
|
+
}
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
522
|
+
interpolate(color, t) {
|
|
523
|
+
if (color) {
|
|
524
|
+
this._rgb = interpolate(this._rgb, color._rgb, t);
|
|
525
|
+
}
|
|
526
|
+
return this;
|
|
527
|
+
}
|
|
528
|
+
clone() {
|
|
529
|
+
return new Color(this.rgb);
|
|
530
|
+
}
|
|
531
|
+
alpha(a) {
|
|
532
|
+
this._rgb.a = n2b(a);
|
|
533
|
+
return this;
|
|
534
|
+
}
|
|
535
|
+
clearer(ratio) {
|
|
536
|
+
const rgb = this._rgb;
|
|
537
|
+
rgb.a *= 1 - ratio;
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
greyscale() {
|
|
541
|
+
const rgb = this._rgb;
|
|
542
|
+
const val = round(rgb.r * 0.3 + rgb.g * 0.59 + rgb.b * 0.11);
|
|
543
|
+
rgb.r = rgb.g = rgb.b = val;
|
|
544
|
+
return this;
|
|
545
|
+
}
|
|
546
|
+
opaquer(ratio) {
|
|
547
|
+
const rgb = this._rgb;
|
|
548
|
+
rgb.a *= 1 + ratio;
|
|
549
|
+
return this;
|
|
550
|
+
}
|
|
551
|
+
negate() {
|
|
552
|
+
const v = this._rgb;
|
|
553
|
+
v.r = 255 - v.r;
|
|
554
|
+
v.g = 255 - v.g;
|
|
555
|
+
v.b = 255 - v.b;
|
|
556
|
+
return this;
|
|
557
|
+
}
|
|
558
|
+
lighten(ratio) {
|
|
559
|
+
modHSL(this._rgb, 2, ratio);
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
darken(ratio) {
|
|
563
|
+
modHSL(this._rgb, 2, -ratio);
|
|
564
|
+
return this;
|
|
565
|
+
}
|
|
566
|
+
saturate(ratio) {
|
|
567
|
+
modHSL(this._rgb, 1, ratio);
|
|
568
|
+
return this;
|
|
569
|
+
}
|
|
570
|
+
desaturate(ratio) {
|
|
571
|
+
modHSL(this._rgb, 1, -ratio);
|
|
572
|
+
return this;
|
|
573
|
+
}
|
|
574
|
+
rotate(deg) {
|
|
575
|
+
rotate(this._rgb, deg);
|
|
576
|
+
return this;
|
|
577
|
+
}
|
|
556
578
|
}
|
|
557
579
|
|
|
558
580
|
function index_esm(input) {
|
|
559
|
-
|
|
581
|
+
return new Color(input);
|
|
560
582
|
}
|
|
561
583
|
|
|
562
|
-
export default
|
|
563
|
-
export { Color, b2n, b2p, hexParse, hexString, hsl2rgb, hslString, hsv2rgb, hueParse, hwb2rgb, n2b, n2p, nameParse, p2b, rgb2hsl, rgbParse, rgbString, rotate, round };
|
|
584
|
+
export { Color, b2n, b2p, index_esm as default, hexParse, hexString, hsl2rgb, hslString, hsv2rgb, hueParse, hwb2rgb, lim, n2b, n2p, nameParse, p2b, rgb2hsl, rgbMix, rgbParse, rgbString, rotate, round };
|