Skip to content

Commit 4e33d32

Browse files
committed
feat(Utils): add parse color and is transparent util
1 parent 8f2cbcb commit 4e33d32

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

src/Utils.js

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,62 @@ export const isEmpty = (x) => {
2929
return false;
3030
};
3131

32+
/**
33+
* Parse color.
34+
* @param color
35+
* @returns {number[]}
36+
*/
37+
export const parseColor = (color) => {
38+
let cache;
39+
let p = parseInt;
40+
color = color.replace(/\s/g, "");
41+
if (
42+
(cache = /#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/.exec(
43+
color,
44+
))
45+
) {
46+
// #000000FF
47+
cache = [
48+
p(cache[1], 16),
49+
p(cache[2], 16),
50+
p(cache[3], 16),
51+
p(cache[4], 16),
52+
];
53+
} else if (
54+
(cache = /#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/.exec(color))
55+
) {
56+
// #000000
57+
cache = [p(cache[1], 16), p(cache[2], 16), p(cache[3], 16)];
58+
} else if ((cache = /#([\da-fA-F])([\da-fA-F])([\da-fA-F])/.exec(color))) {
59+
cache = [p(cache[1], 16) * 17, p(cache[2], 16) * 17, p(cache[3], 16) * 17];
60+
} else if (
61+
// #000
62+
(cache = /rgba\(([\d]+),([\d]+),([\d]+),([\d]+|[\d]*.[\d]+)\)/.exec(color))
63+
) {
64+
// rgba(255,255,255,255)
65+
cache = [+cache[1], +cache[2], +cache[3], +cache[4]];
66+
} else if ((cache = /rgb\(([\d]+),([\d]+),([\d]+)\)/.exec(color))) {
67+
// rgb(255,255,255)
68+
cache = [+cache[1], +cache[2], +cache[3]];
69+
} else {
70+
cache = [0, 0, 0, 0];
71+
}
72+
isNaN(cache[3]) && (cache[3] = 1);
73+
return cache.slice(0, 4);
74+
};
75+
76+
/**
77+
* Is transparent color
78+
* @private
79+
* @param color
80+
* @returns {boolean}
81+
*/
82+
export const isTransparentColor = (color) => {
83+
color = String(color).toLowerCase().trim();
84+
const parse = parseColor(color);
85+
return String(color).toLowerCase().trim() === "transparent" || parse[4] === 0;
86+
};
87+
3288
/**
3389
* Is variable callable
3490
* @private
@@ -96,17 +152,20 @@ export const mergeViewStyle = (style, defaultStyle) => {
96152
* @returns {string}
97153
*/
98154
export const getColorContrast = (hexcolor) => {
99-
if (hexcolor.slice(0, 1) === '#') {
155+
if (hexcolor.slice(0, 1) === "#") {
100156
hexcolor = hexcolor.slice(1);
101157
}
102158
if (hexcolor.length === 3) {
103-
hexcolor = hexcolor.split('').map(function (hex) {
104-
return hex + hex;
105-
}).join('');
159+
hexcolor = hexcolor
160+
.split("")
161+
.map(function (hex) {
162+
return hex + hex;
163+
})
164+
.join("");
106165
}
107-
var r = parseInt(hexcolor.substr(0,2),16);
108-
var g = parseInt(hexcolor.substr(2,2),16);
109-
var b = parseInt(hexcolor.substr(4,2),16);
110-
var yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
111-
return (yiq >= 128) ? '#000000' : '#FFFFFF';
166+
var r = parseInt(hexcolor.substr(0, 2), 16);
167+
var g = parseInt(hexcolor.substr(2, 2), 16);
168+
var b = parseInt(hexcolor.substr(4, 2), 16);
169+
var yiq = (r * 299 + g * 587 + b * 114) / 1000;
170+
return yiq >= 170 ? "#000000" : "#FFFFFF";
112171
};

0 commit comments

Comments
 (0)