-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExColor.js
407 lines (360 loc) · 12.1 KB
/
ExColor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* @typedef {[number, number, number, number?]} ColorArray
*/
/**
* @typedef {object} RgbaObject
* @property {number} r - The red value (0-255).
* @property {number} g - The green value (0-255).
* @property {number} b - The blue value (0-255).
* @property {number=} a - The alpha value (0-255).
*/
/**
* @typedef {object} HslaObject
* @property {number} h - The hue value (0-1).
* @property {number} s - The saturation value (0-1).
* @property {number} l - The luminosity value (0-1).
* @property {number=} a - The alpha value (0-1).
*/
/**
* Provides many utility methods to convert between color representations.
*/
export class ExColor {
/** @type {ColorArray?} */ #rgba;
/** @type {ColorArray?} */ #hsla;
/** @type {String?} */ #hex;
/**
* @hideconstructor
* Use the static constructor functions `ExColor.fromRgb()` and `ExColor.fromHsl()` instead.
*
* Creates a new `ExColor` object with the specified RGB values. At least one object must be provided.
*
* @param {ColorArray | RgbaObject | null} rgba An array of numbers `[r, g, b, a?]` or an object `{ r: number, g: number, b: number, a?: number }`.
* Numbers should be within the range `0-255`.
*
* @param {ColorArray | HslaObject | null} hsla An array of numbers `[h, s, l, a?]` or an object `{ h: number, s: number, l: number, a?: number }`.
* Numbers should be within the range `0-1`.
*
*
* The only validation done is checking for an array or an object for performance reasons.
*/
constructor(rgba, hsla) {
if (!rgba && !hsla)
throw new Error(`No arguments provided to constructor.`);
if (rgba) {
if (Array.isArray(rgba)) {
this.#rgba = rgba;
} else if (typeof rgba === 'object') {
this.#rgba = [rgba.r, rgba.g, rgba.b, rgba.a];
} else {
throw new TypeError(`Unsupported data type '${typeof rgba}'.`);
}
}
if (hsla) {
if (Array.isArray(hsla)) {
this.#hsla = hsla;
} else if (typeof hsla === 'object') {
this.#hsla = [hsla.h, hsla.s, hsla.l, hsla.a];
} else {
throw new TypeError(`Unsupported data type '${typeof hsla}'.`);
}
}
}
/**
*
* @returns {RgbaObject}
*/
toRgb() {
if (!this.#rgba) {
this.#rgba = ExColor.hsl2rgb(this.#hsla);
}
const [r, g, b, a] = this.#rgba;
return {r, g, b, a};
}
/**
*
* @returns {HslaObject}
*/
toHsl() {
if (!this.#hsla) {
this.#hsla = ExColor.rgb2hsl(this.#rgba);
}
const [h, s, l, a] = this.#hsla;
return {h, s, l, a};
}
/**
* Converts this color value to a hex color string. The resulting string does not have a leading hash `#`.
*
* @param {boolean} shortform If `true`, will return a shortform version of the hex color if possible.
* @returns {string}
*/
toHex(shortform = false) {
if (!this.#hex) {
if (!this.#rgba) {
this.#rgba = ExColor.hsl2rgb(this.#hsla);
}
this.#hex = ExColor.rgb2hex(this.#rgba);
}
let hex = this.#hex;
if (shortform) {
hex = ExColor.hexLongToShort(hex) || hex;
}
return hex;
}
/**
* Creates a new `ExColor` object from the specified RGBA values.
*
* @param {ColorArray | RgbaObject} rgba An array of numbers `[r, g, b, a?]` or an object `{ r: number, g: number, b: number, a?: number }`.
* Numbers should be within the range `0-255`.
*
* The only validation done is checking for an array or an object for performance reasons.
*
* @returns {ExColor}
*/
static fromRgb(rgba) {
return new ExColor(rgba, null);
}
/**
* Creates a new `ExColor` object from the specified HSLA values.
*
* @param {ColorArray | HslaObject} hsla An array of numbers `[h, s, l, a?]` or an object `{ h: number, s: number, l: number, a?: number }`.
* Numbers should be within the range `0-1`.
*
* The only validation done is checking for an array or an object for performance reasons.
*
* @returns {ExColor}
*/
static fromHsl(hsla) {
return new ExColor(null, hsla);
}
/**
* Creates a new `ExColor` object from the specified hex color value.
*
* @param {string} hex A string with an optional leading hash `#`, followed by exactly three or six hexadecimal characters.
* @param {number=} alpha An optional alpha value between `0-255`.
*
* The only validation done is checking for an array or an object for performance reasons.
*
* @returns {ExColor}
*/
static fromHex(hex, alpha) {
const rgba = ExColor.hex2rgb(hex, alpha);
return new ExColor(rgba, null);
}
/**
* Converts the specified RGBA values to HSLA. The alpha value is scaled to fit the resulting range.
*
* @param {ColorArray | RgbaObject} rgba An array of numbers `[r, g, b, a?]` or an object `{ r: number, g: number, b: number, a?: number }`.
* Numbers should be within the range `0-255`.
*
* @returns {ColorArray} The resulting HSLA values, in the range `0-1`.
*/
static rgb2hsl(rgba) {
let r, g, b, a;
if (Array.isArray(rgba)) {
[r, g, b, a = 255] = rgba;
} else if (typeof rgba === 'object') {
[r, g, b, a = 255] = [rgba.r, rgba.g, rgba.b, rgba.a];
} else {
throw new TypeError(`Unsupported data type '${typeof rgba}'.`);
}
r /= 255;
g /= 255;
b /= 255;
a /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const maxMin = (max + min);
let h, s, l = maxMin / 2;
if (max !== min) {
let d = max - min;
s = l > 0.5 ? d / (2 - d) : d / maxMin;
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4;
}
h /= 6;
} else { // achromatic
h = s = 0;
}
return [h, s, l, a];
}
/**
* Converts the specified HSLA values to RGBA. The alpha value is scaled to fit the resulting range.
*
* @param {ColorArray | HslaObject} hsla An array of numbers `[h, s, l, a?]` or an object `{ h: number, s: number, l: number, a?: number }`.
* Numbers should be within the range `0-1`.
*
* @returns {ColorArray} The resulting RGBA values, in the range `0-255`.
*/
static hsl2rgb(hsla) {
let h, s, l, a;
if (Array.isArray(hsla)) {
[h, s, l, a = 1] = hsla;
} else if (typeof hsla === 'object') {
[h, s, l, a = 1] = [hsla.h, hsla.s, hsla.l, hsla.a];
} else {
throw new TypeError(`Unsupported data type '${typeof hsla}'.`);
}
let r, g, b;
if (s !== 0) {
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
let q = l < 0.5
? l * (1 + s)
: l + s - l * s;
let p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
} else { // achromatic
r = g = b = l;
}
r = Math.round(r * 255);
g = Math.round(g * 255);
b = Math.round(b * 255);
a = Math.round(a * 255);
return [r, g, b, a];
}
/**
* Converts the specified RGB value into a hex color string. The resulting string does not have a leading hash `#`.
* @param {ColorArray | RgbaObject} rgb
* @returns {string}
*/
static rgb2hex(rgb) {
let r, g, b;
if (Array.isArray(rgb)) {
[r, g, b] = rgb;
} else if (typeof rgb === 'object') {
[r, g, b] = [rgb.r, rgb.g, rgb.b];
} else {
throw new TypeError(`Unsupported data type '${typeof rgb}'.`);
}
function num2hex(num) {
const hex = num.toString(16);
return hex.length == 1 ? '0' + hex : hex;
}
return num2hex(r) + num2hex(g) + num2hex(b);
}
/**
* Converts the specified HSL value into a hex color string. The resulting string does not have a leading hash `#`.
*
* This function works by first converting to RGB.
*
* @param {ColorArray | HslaObject} hsl
* @returns {string}
*/
static hsl2hex(hsl) {
let rgb = ExColor.hsl2rgb(hsl);
return ExColor.rgb2hex(rgb);
}
/**
* Converts the specified hex color value into an RGB value. Does not support alpha.
*
* @param {string} hex A string with an optional leading hash `#` followed by exactly three or six hexadecimal characters.
* @param {number=} alpha An optional alpha value between `0-255`.
* @returns {ColorArray}
*/
static hex2rgb(hex, alpha) {
hex = this.#hexNormalize(hex);
if (hex.length === 3) hex = ExColor.hexShortToLong(hex);
let r = parseInt(hex.slice(0, 2), 16);
let g = parseInt(hex.slice(2, 4), 16);
let b = parseInt(hex.slice(4, 6), 16);
return [r, g, b, alpha];
}
/**
* Gets whether the given object is a valid hex color string. A leading hash `#` is allowed.
*
* @param {any} obj
* @returns {boolean}
*/
static isValidHexString(obj) {
if (typeof obj !== 'string') return false;
return /^#?(?:[0-9A-F]{3}){1,2}$/i.test(obj);
}
/**
* Returns whether the specified long hex string can be converted to shortform. Returns `true` if the hex string is already short.
* @param {string} hex
* @returns {boolean}
*/
static hexCanBeShort(hex) {
hex = this.#hexNormalize(hex);
if (hex.length === 3) return true;
return hex[0] == hex[1] &&
hex[2] == hex[3] &&
hex[4] == hex[5];
}
/**
* @param {string} hex
* @returns {boolean}
*/
static hexIsShort(hex) {
hex = this.#hexNormalize(hex);
return length === 3;
}
/**
* Converts the specified long hex color string into shortform.
* Returns `hex` if it is already short.
* Returns `null` if it cannot be represented in shortform.
*
* @param {string} hex
* @returns {string?}
*/
static hexLongToShort(hex) {
hex = this.#hexNormalize(hex);
if (!this.hexCanBeShort(hex)) return null;
if (hex.length === 3) return hex;
return hex[0] + hex [2] + hex[4];
}
/**
* Converts the specified short hex color string into longform.
*
* @param {string} hex
* @returns {string}
*/
static hexShortToLong(hex) {
hex = this.#hexNormalize(hex);
if (hex.length === 6) return hex;
return hex[0] + hex[0] +
hex[1] + hex[1] +
hex[2] + hex[2];
}
/**
* Ensures the given hex string has a leading hash.
*
* @param {string} hex
* @returns {string}
*/
static getHexWithHash(hex) {
hex = this.#hexNormalize(hex);
return '#' + hex;
}
/**
* Gets the given hex string without any leading hash.
*
* @param {string} hex
* @returns {string}
*/
static getHexWithoutHash(hex) {
return this.#hexNormalize(hex);
}
/**
* Trims any beginning hash `#` from the hex string and throws on an invalid hex color string.
*
* @param {any} hex
* @returns {string}
*/
static #hexNormalize(hex) {
if (!this.isValidHexString(hex))
throw new TypeError("Invalid hex color string.");
hex = hex.substring(hex.lastIndexOf('#') + 1);
return hex;
}
}