fazlurr
10/7/2019 - 8:27 AM

Get Text Color

/**
 * Get Color Contrast
 * @param {string} hex Hexa Color Code
 */
export const getColorContrast = (hex) => {
	const color = hexToRgb(hex);
	const treshold = 186;
	const contrast = color
		&& (color.r * 0.299 + color.g * 0.587 + color.b * 0.114) > treshold ? 'black' : 'white';
	// for (const key in color) {
	// 	if (color.hasOwnProperty(key)) {
	// 		let c = color[key];
	// 		c = c / 255.0;
	// 		if (c <= 0.03928) {
	// 			c = c / 12.92;
	// 		}
	// 		else {
	// 			c = ((c + 0.055) / 1.055) ^ 2.4;
	// 		}
	// 		color[key] = c;
	// 	}
	// }
	// const L = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
	// const luminanceTreshold = 0.179;
	// const contrast = L > luminanceTreshold ? 'black' : 'white';
	return contrast;
};

/**
 * Get Text Color
 * @param {string} hex Hexa Color Code
 */
export const getTextColor = (bgColor, lightColor, darkColor) => {
	const contrast = getColorContrast(bgColor);
	return contrast === 'black' ? darkColor : lightColor;
};