-
Notifications
You must be signed in to change notification settings - Fork 14
/
color.js
361 lines (312 loc) · 10.7 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
import {
DEFAULT_ADJUSTERS,
} from 'constants';
import color from 'color';
import colorFn from 'css-color-function';
import tinycolor from 'tinycolor2';
const REBECCAPURPLEHEX = '#663399';
/**
* @param {String} hslStr - A valid hsl(a) color string.
* @return {Object} An object containing the h,s,l values of the given hslStr.
* Object keys include; hue, saturation, lightness.
*/
export const getHslObjFromStr = hslStr => {
// First remove the 'hsl' or 'hsla' and the opening '('
const arr = hslStr.replace(/hsla?\(/gi, '')
// Remove any spaces
.replace(/\s+/g, '')
// Remove any percentage chars
.replace(/%/g, '')
// Create an array of the values
.split(',')
// Discard the forth value–the alpha–if it's there
.slice(0, 3)
// Ensure each value is an Int
.map(v => parseInt(v, 10));
return {
h: arr[0],
s: arr[1],
l: arr[2]
};
};
/**
* @param {String} colorStr - A valid hex, hex4, hex8, rgb(a), hsl(a), or
* keyword color.
* @return {Object} An object containing the properties of the given colorStr.
* properties include; rgb, hsl, hwb, alpha, lightness,
* blackness, whiteness.
*/
export const getColorProperties = colorStr => {
const tinyColorObj = tinycolor(colorStr);
const {
_a: alpha,
_r: red,
_g: green,
_b: blue
} = tinyColorObj;
// NOTE: The `tinycolor` package sometimes changes input values for hsl(a)
// strings. We should always trust the direct input. If this colorStr is an
// hsl(a) color, use our internal method that only breaks the string into
// the components and does not modify the values. If it's not an hsl color
// we can trust tinycolor to do it's thing.
let hue = 0;
let saturation = 0;
let lightness = 0;
if (colorStr.includes('hsl')) {
const {h, s, l} = getHslObjFromStr(colorStr);
hue = h;
saturation = s;
lightness = l;
} else {
const {h, s, l} = tinyColorObj.toHsl();
hue = h;
saturation = s * 100;
lightness = l * 100;
}
// NOTE: the `color` package cannot handle hex4 or hex8 values.
// TODO: Figure out a way of getting the blackness and whiteness values
// here instead of using other package.
const colorObj = color(tinyColorObj.toRgbString());
const {
b: blackness,
w: whiteness
} = colorObj.hwb().object();
const properties = {
alpha: alpha * 100,
hue,
lightness,
saturation,
blackness,
whiteness,
red,
green,
blue
};
// Remove any floats on the way out.
return Object.keys(properties).reduce((prev, curr) => {
prev[curr] = parseInt(properties[curr], 10);
return prev;
}, {});
};
/**
* @param {String} colorStr - A valid hex, rgb, rgba color.
* @return {Array} An array of adjuster objects for the colorStr.
*/
export const getAdjustersForColor = (colorStr, baseAdjusters) => {
const colorProperties = getColorProperties(colorStr);
return baseAdjusters.map(a => {
let adjuster = {...a};
const prop = colorProperties[a.name];
adjuster.enabled = false;
if (prop !== undefined) {
adjuster.value = prop;
}
return adjuster;
});
};
/**
* @param {Array} adjusters - An array of adjuster objects.
* @param {Bool} useShortNames - Optional param to use short names for
* adjusters when available. Default: false
* @return {String} A string of space-seprated color adjusters.
* ex; "alpha(100%) hue(250) saturation(50%)"
*/
export const getAdjustersString = (adjusters, useShortNames = false) => {
return adjusters.reduce((prev, curr) => {
const {
name,
value,
shortName,
unit = ''
} = curr;
const nameToUse = (shortName && useShortNames) ? shortName : name;
if (curr.enabled) {
return `${prev} ${nameToUse}(${value}${unit})`;
}
return prev;
}, '');
};
/**
* @param {String} baseColor - A valid color string
* @param {String} adjustersStr - A valid color adjusters string
* @return {String} CSS color function string or empty string if no adjusters.
* ex; "color(#ff0004 alpha(100%) hue(250) saturation(50%))"
*/
export const getColorFuncString = (baseColor, adjustersStr = '') => {
return `color(${baseColor}${adjustersStr})`;
};
/**
* @param {String} val - A string that looks like a hex, rgb, or rgba color.
* @return {String} A valid color as a string.
*/
export const getColorFromQueryVal = (val) => {
const {
hash,
search
} = window.location;
let baseColor = val.toLowerCase();
try {
baseColor = decodeURIComponent(val.toLowerCase());
} catch (e) {
console.info(`getColorFromQueryVal saw a malformed URL in the
color string provided in URL: ${search}. Using the input as is.`
.replace(/\n/gm, ' '));
}
// Assume the user put an non-encoded "#" in the value for a hex color.
// Browsers will see this as window.location.hash.
if (baseColor.length === 0 && hash) {
baseColor = hash;
}
const c = tinycolor(baseColor);
if (c.isValid()) {
// If the user enters a hex value without a '#' char, add it for them.
if (c.getFormat().includes('hex') && !baseColor.includes('#')) {
baseColor = `#${baseColor}`;
}
return baseColor;
} else {
console.info(`getColorFromQueryVal couldn't figure out how to parse the
color string provided in URL: ${search}. Returning null.`.replace(/\n/gm, ' '));
return null;
}
};
/**
* getContrastColor - For the given baseColor, return a color with sufficient
* contrast per WCAG Guidelines.
*
* @param {String} baseColor - A hex, hex3, hex4, hex8 rgb(a), hsl(a) or
* keyword color.
* @param {String} amt - optional - The contrast amount. Default: 100%.
* @return {String} A color with sufficient contrast to the base color or black
* if the baseColor has an alpha value. less than 50.
*/
export const getContrastColor = (baseColor, amt = '100%') => {
const aThreshold = 50;
const props = getColorProperties(baseColor);
// This ensures we never use a color with an alpha value less than 100.
const colorToUse = props.alpha < aThreshold ? '#000' : baseColor;
const safeColor = tinycolor(colorToUse).setAlpha(1).toRgbString();
// If the alpha drops below 50, always return black, else return the
// sufficiently contrasting color.
if (props.alpha < aThreshold) {
return safeColor;
} else {
return colorFn.convert(getColorFuncString(safeColor, ` contrast(${amt})`));
}
};
/**
* @param {Object} tinyColorObj - A tinycolor object
* @return {Object} - The input format, the original input and the unique
* formats.
*/
export const getColorFormats = tinyColorObj => {
const allFormats = {
hex: tinyColorObj.toString('hex'),
hex3: tinyColorObj.toString('hex3'),
hex4: tinyColorObj.toString('hex4'),
hex8: tinyColorObj.toString('hex8'),
hsl: tinyColorObj.toString('hsl'),
keyword: tinyColorObj.toString('name'),
rgb: tinyColorObj.toString('rgb')
};
const alpha = tinyColorObj.getAlpha();
// Not every color can be represented by all available formats. For each color
// filter out any formats that are not applicable to that color.
let formats = Object.keys(allFormats).filter(f => {
const val = allFormats[f];
// Keyword colors will not include a '#' char.
// Unless this is rebeccapurple. tinycolor doesn't recognize the keyword.
if (f === 'keyword' && val.indexOf('#') === 0 && val !== REBECCAPURPLEHEX) {
return false;
}
// This will catch the rebeccapurple case, it's gets weird because we
// kind of hard-code that use case.
if (f === 'keyword' && alpha < 1) {
return false;
}
// Semi-transparent colors cannot be represented as hex or hex shorthand.
if ((f === 'hex' || f === 'hex3') && alpha < 1) {
return false;
}
// Hex shorthand should not be longer than four. (including '#').
if (f === 'hex3' && val.length > 4) {
return false;
}
// rrggbbaa shorthand should not be longer than five. (including '#').
if (f === 'hex4' && val.length > 5) {
return false;
}
return f;
}).reduce((obj, frmt) => {
let val = allFormats[frmt];
// This is a special case. Tinycolor doesn't recognize 'rebeccapurple' as
// a keyword color yet.
if (frmt === 'keyword' && val === REBECCAPURPLEHEX) {
obj[frmt] = 'rebeccapurple';
} else {
obj[frmt] = val;
}
return obj;
}, {});
// Tinycolor calls them "name", change that to "keyword".
const ogFormat = tinyColorObj.getFormat();
const format = ogFormat === 'name' ? 'keyword' : ogFormat;
return {
format,
formats,
original: tinyColorObj.getOriginalInput()
};
};
/**
* @param {String} colorStr - A valid color string;
* keyword, rgb, rgba, rrggbbaa, hsl
* @return {Object} colorObj - A treasure trove of color information.
*/
export const getColorObj = (colorStr, adjusters = DEFAULT_ADJUSTERS) => {
if (colorStr && typeof colorStr !== 'string') {
throw new Error('In getColorObj, the colorStr param must be a string.');
}
const c = tinycolor(colorStr);
let isValid = c.isValid();
if (isValid) {
const baseColor = getColorFormats(c);
const newAdjusters = (adjusters === DEFAULT_ADJUSTERS) ?
getAdjustersForColor(colorStr, adjusters) : adjusters;
const adjustersStr = getAdjustersString(newAdjusters);
const adjustersStrShortNames = getAdjustersString(newAdjusters, true);
const colorFuncStr = getColorFuncString(colorStr, adjustersStr);
const colorFuncStrShortNames = getColorFuncString(colorStr,
adjustersStrShortNames);
let colorFnResult;
// NOTE: Wrap in try/catch because we sometimes have a color string without
// closing parens, or other things `css-color-function` can't work with.
try {
// NOTE: `css-color-function` package can't work with rrggbbaa format.
// so we use the rgb value.
colorFnResult = (baseColor.format === 'hex8') ?
colorFn.convert(getColorFuncString(baseColor.formats.rgb, adjustersStr)) :
colorFn.convert(colorFuncStr);
} catch (e) {
colorFnResult = baseColor.original;
if (process.env.NODE_ENV === 'development') {
console.warn('In getColorObj:', e);
}
}
const convertedColor = tinycolor(colorFnResult);
const outputColor = getColorFormats(convertedColor);
return {
adjusters: newAdjusters,
adjustersStr,
adjustersStrShortNames,
baseColor,
baseContrastColor: getContrastColor(baseColor.original),
colorFuncStr,
colorFuncStrShortNames,
isValid,
outputColor,
outputContrastColor: getContrastColor(outputColor.original)
};
} else {
return {};
}
};