Skip to content

Commit 02b218b

Browse files
author
Marco Cesarato
committed
feat(Utils): add color contrast function
1 parent bb1f49e commit 02b218b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/Utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,24 @@ export const mergeViewStyle = (style, defaultStyle) => {
8989
}
9090
return style;
9191
};
92+
93+
/**
94+
* Get color contrast
95+
* @param hexcolor
96+
* @returns {string}
97+
*/
98+
export const getColorContrast = (hexcolor) => {
99+
if (hexcolor.slice(0, 1) === '#') {
100+
hexcolor = hexcolor.slice(1);
101+
}
102+
if (hexcolor.length === 3) {
103+
hexcolor = hexcolor.split('').map(function (hex) {
104+
return hex + hex;
105+
}).join('');
106+
}
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';
112+
};

0 commit comments

Comments
 (0)