-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathtrueColor.js
50 lines (49 loc) · 1.59 KB
/
trueColor.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
import hexToRGB from './hexToRGB';
/**
* trueColor
*
* Transform any color to rgba()/rgb() and return a nice RGB(a) object.
*
* @param {string} colorString the color input
* @returns {KUTE.colorObject} the {r,g,b,a} color object
*/
const trueColor = (colorString) => {
let result;
if (/rgb|rgba/.test(colorString)) { // first check if it's a rgb string
const vrgb = colorString.replace(/\s|\)/, '').split('(')[1].split(',');
const colorAlpha = vrgb[3] ? vrgb[3] : null;
if (!colorAlpha) {
result = { r: parseInt(vrgb[0], 10), g: parseInt(vrgb[1], 10), b: parseInt(vrgb[2], 10) };
} else {
result = {
r: parseInt(vrgb[0], 10),
g: parseInt(vrgb[1], 10),
b: parseInt(vrgb[2], 10),
a: parseFloat(colorAlpha),
};
}
} if (/^#/.test(colorString)) {
const fromHex = hexToRGB(colorString);
result = { r: fromHex.r, g: fromHex.g, b: fromHex.b };
} if (/transparent|none|initial|inherit/.test(colorString)) {
result = {
r: 0, g: 0, b: 0, a: 0,
};
}
// maybe we can check for web safe colors
// only works in a browser
if (!/^#|^rgb/.test(colorString)) {
const siteHead = document.getElementsByTagName('head')[0];
siteHead.style.color = colorString;
let webColor = getComputedStyle(siteHead, null).color;
webColor = /rgb/.test(webColor) ? webColor.replace(/[^\d,]/g, '').split(',') : [0, 0, 0];
siteHead.style.color = '';
result = {
r: parseInt(webColor[0], 10),
g: parseInt(webColor[1], 10),
b: parseInt(webColor[2], 10),
};
}
return result;
};
export default trueColor;