-
Notifications
You must be signed in to change notification settings - Fork 742
/
color.js
369 lines (318 loc) · 8.86 KB
/
color.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
import { Colorjs, ArrayFrom } from '../../core/imports';
const hexRegex = /^#[0-9a-f]{3,8}$/i;
const hslRegex = /hsl\(\s*([-\d.]+)(rad|turn)/;
/**
* @class Color
* @memberof axe.commons.color
* @param {number} red
* @param {number} green
* @param {number} blue
* @param {number} alpha
*/
export default class Color {
// color channel values typically in the range of 0-1 (can go below or above)
#r;
#g;
#b;
// color component values resolved to the sRGB color space (0-255)
#red;
#green;
#blue;
constructor(red, green, blue, alpha = 1) {
if (red instanceof Color) {
// preserve out of gamut values
const { r, g, b } = red;
this.r = r;
this.g = g;
this.b = b;
this.alpha = red.alpha;
return;
}
/** @type {number} */
this.red = red;
/** @type {number} */
this.green = green;
/** @type {number} */
this.blue = blue;
/** @type {number} */
this.alpha = alpha;
}
get r() {
return this.#r;
}
set r(value) {
this.#r = value;
this.#red = Math.round(clamp(value, 0, 1) * 255);
}
get g() {
return this.#g;
}
set g(value) {
this.#g = value;
this.#green = Math.round(clamp(value, 0, 1) * 255);
}
get b() {
return this.#b;
}
set b(value) {
this.#b = value;
this.#blue = Math.round(clamp(value, 0, 1) * 255);
}
get red() {
return this.#red;
}
set red(value) {
this.#r = value / 255;
this.#red = clamp(value, 0, 255);
}
get green() {
return this.#green;
}
set green(value) {
this.#g = value / 255;
this.#green = clamp(value, 0, 255);
}
get blue() {
return this.#blue;
}
set blue(value) {
this.#b = value / 255;
this.#blue = clamp(value, 0, 255);
}
/**
* Provide the hex string value for the color
* @method toHexString
* @memberof axe.commons.color.Color
* @instance
* @return {string}
*/
toHexString() {
const redString = Math.round(this.red).toString(16);
const greenString = Math.round(this.green).toString(16);
const blueString = Math.round(this.blue).toString(16);
return (
'#' +
(this.red > 15.5 ? redString : '0' + redString) +
(this.green > 15.5 ? greenString : '0' + greenString) +
(this.blue > 15.5 ? blueString : '0' + blueString)
);
}
toJSON() {
const { red, green, blue, alpha } = this;
return { red, green, blue, alpha };
}
/**
* Parse any valid color string and assign its values to "this"
* @method parseString
* @memberof axe.commons.color.Color
* @instance
*/
parseString(colorString) {
// Colorjs <v0.5.0 does not support rad or turn angle values
// @see https://github.com/LeaVerou/color.js/issues/311
colorString = colorString.replace(hslRegex, (match, angle, unit) => {
const value = angle + unit;
switch (unit) {
case 'rad':
return match.replace(value, radToDeg(angle));
case 'turn':
return match.replace(value, turnToDeg(angle));
}
});
try {
// revert prototype.js override of Array.from
// in order to get color-contrast working
// @see https://github.com/dequelabs/axe-core/issues/4428
let prototypeArrayFrom;
if ('Prototype' in window && 'Version' in window.Prototype) {
prototypeArrayFrom = Array.from;
Array.from = ArrayFrom;
}
// srgb values are between 0 and 1
const color = new Colorjs(colorString).to('srgb');
if (prototypeArrayFrom) {
Array.from = prototypeArrayFrom;
prototypeArrayFrom = null;
}
this.r = color.r;
this.g = color.g;
this.b = color.b;
// color.alpha is a Number object so convert it to a number
this.alpha = +color.alpha;
} catch (err) {
throw new Error(`Unable to parse color "${colorString}"`);
}
return this;
}
/**
* Set the color value based on a CSS RGB/RGBA string
* @method parseRgbString
* @deprecated
* @memberof axe.commons.color.Color
* @instance
* @param {string} rgb The string value
*/
parseRgbString(colorString) {
this.parseString(colorString);
}
/**
* Set the color value based on a CSS RGB/RGBA string
* @method parseHexString
* @deprecated
* @memberof axe.commons.color.Color
* @instance
* @param {string} rgb The string value
*/
parseHexString(colorString) {
if (!colorString.match(hexRegex) || [6, 8].includes(colorString.length)) {
return;
}
this.parseString(colorString);
}
/**
* Set the color value based on a CSS RGB/RGBA string
* @method parseColorFnString
* @deprecated
* @memberof axe.commons.color.Color
* @instance
* @param {string} rgb The string value
*/
parseColorFnString(colorString) {
this.parseString(colorString);
}
/**
* Get the relative luminance value
* using algorithm from http://www.w3.org/WAI/GL/wiki/Relative_luminance
* @method getRelativeLuminance
* @memberof axe.commons.color.Color
* @instance
* @return {number} The luminance value, ranges from 0 to 1
*/
getRelativeLuminance() {
const { r: rSRGB, g: gSRGB, b: bSRGB } = this;
const r =
rSRGB <= 0.03928 ? rSRGB / 12.92 : Math.pow((rSRGB + 0.055) / 1.055, 2.4);
const g =
gSRGB <= 0.03928 ? gSRGB / 12.92 : Math.pow((gSRGB + 0.055) / 1.055, 2.4);
const b =
bSRGB <= 0.03928 ? bSRGB / 12.92 : Math.pow((bSRGB + 0.055) / 1.055, 2.4);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
/**
* Add a value to the color channels
* @private
* @param {number} value The value to add
* @return {Color} A new color instance
*/
#add(value) {
const C = new Color(this);
C.r += value;
C.g += value;
C.b += value;
return C;
}
/**
* Get the luminosity of a color
* using algorithm from https://www.w3.org/TR/compositing-1/#blendingnonseparable
* @method getLuminosity
* @memberof axe.commons.color.Color
* @instance
* @return {number} The luminosity of the color
*/
getLuminosity() {
return 0.3 * this.r + 0.59 * this.g + 0.11 * this.b;
}
/**
* Set the luminosity of a color
* using algorithm from https://www.w3.org/TR/compositing-1/#blendingnonseparable
* @method setLuminosity
* @memberof axe.commons.color.Color
* @instance
* @param {number} L The luminosity
* @return {Color} A new color instance
*/
setLuminosity(L) {
const d = L - this.getLuminosity();
return this.#add(d).clip();
}
/**
* Get the saturation of a color
* using algorithm from https://www.w3.org/TR/compositing-1/#blendingnonseparable
* @method getSaturation
* @memberof axe.commons.color.Color
* @instance
* @return {number} The saturation of the color
*/
getSaturation() {
return Math.max(this.r, this.g, this.b) - Math.min(this.r, this.g, this.b);
}
/**
* Set the saturation of a color
* using algorithm from https://www.w3.org/TR/compositing-1/#blendingnonseparable
* @method setSaturation
* @memberof axe.commons.color.Color
* @instance
* @param {number} s The saturation
* @return {Color} A new color instance
*/
setSaturation(s) {
const C = new Color(this);
const colorEntires = [
{ name: 'r', value: C.r },
{ name: 'g', value: C.g },
{ name: 'b', value: C.b }
];
// find the min, mid, and max values of the color components
const [Cmin, Cmid, Cmax] = colorEntires.sort((a, b) => {
return a.value - b.value;
});
if (Cmax.value > Cmin.value) {
Cmid.value = ((Cmid.value - Cmin.value) * s) / (Cmax.value - Cmin.value);
Cmax.value = s;
} else {
Cmid.value = Cmax.value = 0;
}
Cmin.value = 0;
C[Cmax.name] = Cmax.value;
C[Cmin.name] = Cmin.value;
C[Cmid.name] = Cmid.value;
return C;
}
/**
* Clip the color between RGB 0-1 accounting for the luminosity of the color. Color must be normalized before calling.
* using algorithm from https://www.w3.org/TR/compositing-1/#blendingnonseparable
* @method clip
* @memberof axe.commons.color.Color
* @instance
* @return {Color} A new color instance clipped between 0-1
*/
clip() {
const C = new Color(this);
const L = C.getLuminosity();
const n = Math.min(C.r, C.g, C.b);
const x = Math.max(C.r, C.g, C.b);
if (n < 0) {
C.r = L + ((C.r - L) * L) / (L - n);
C.g = L + ((C.g - L) * L) / (L - n);
C.b = L + ((C.b - L) * L) / (L - n);
}
if (x > 1) {
C.r = L + ((C.r - L) * (1 - L)) / (x - L);
C.g = L + ((C.g - L) * (1 - L)) / (x - L);
C.b = L + ((C.b - L) * (1 - L)) / (x - L);
}
return C;
}
}
// clamp a value between two numbers (inclusive)
function clamp(value, min, max) {
return Math.min(Math.max(min, value), max);
}
// convert radians to degrees
function radToDeg(rad) {
return (rad * 180) / Math.PI;
}
// convert turn to degrees
function turnToDeg(turn) {
return turn * 360;
}