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