function isDark(color) { // Variables for red, green, blue values let r; let g; let b; let hsp; // If hex --> Convert it to RGB: http://gist.github.com/983661 color = +`0x${color.slice(1).replace(color.length < 5 && /./g, "$&$&")}`; r = color >> 16; g = (color >> 8) & 255; b = color & 255; // HSP equation from http://alienryderflex.com/hsp.html hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)); // Using the HSP value, determine whether the color is light or dark if (hsp > 127.5) { return false; // light } return true; // dark } function isLight(color) { return !isDark(color); } function isValidHex(color) { if (!color || typeof color !== "string") return false; if (color.substring(0, 1) === "#") color = color.substring(1); switch (color.length) { case 3: return /^[0-9A-F]{3}$/i.test(color); case 6: return /^[0-9A-F]{6}$/i.test(color); case 8: return /^[0-9A-F]{8}$/i.test(color); default: return false; } } function formatHex(str) { return `#${str.toUpperCase().replace("#", "")}`; } export { isLight, isDark, isValidHex, formatHex };