/** * Calculate light or darker color based on the luminance arg. * * @param {String} hex Color hex string to calculate on. * @param {Number} lum Luminance. * @return {String} The new color hex string. */ export const colorLuminance = (hex, lum) => { // validate hex string hex = String(hex).replace(/[^0-9a-f]/gi, ''); if (hex.length < 6) { hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; } lum = lum || 0; // convert to decimal and change luminosity let rgb = '#'; let c; let i; for (i = 0; i < 3; i++) { c = parseInt(hex.substr(i * 2, 2), 16); c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16); rgb += ('00' + c).substr(c.length); } return rgb; } /** * Expand a hex color to six digits, ignoring an alpha byte. * Accepts #rgb, #rgba, #rrggbb and #rrggbbaa — the '#' is optional. * * @param {String} hex Color hex string. * @return {String} Six-digit lower-case hex without the '#', or '' when unparsable. */ const expandHex = hex => { let value = String(hex == null ? '' : hex).trim().replace(/^#/, ''); if (!/^[0-9a-f]+$/i.test(value)) { return ''; } if (value.length === 3 || value.length === 4) { // #rgb / #rgba: every digit is doubled (#369 → #336699). value = value.slice(0, 3).split('').map(c => c + c).join(''); } else if (value.length === 6 || value.length === 8) { value = value.slice(0, 6); } else { return ''; } return value.toLowerCase(); } /** * Calculate an appropriate contrast color. * * @param {String} hex Hex string of the color we're searching for the right contrast. * @param {String} dark Dark color to return. Defaults to #000000. * @param {String} light Light color to return. Defaults to #ffffff. * @return {String} Hex color string with the appropriate contrast color. */ export const contrastTextColor = (hex, dark, light) => { dark = dark || '#000000'; light = light || '#ffffff'; // Hex may be #rgb, #rgba, #rrggbb or #rrggbbaa; the alpha byte is ignored. // Anything unparsable keeps the historical dark fallback. const value = expandHex(hex); if (!value) { return dark; } const red = parseInt(value.substr(0, 2), 16); const green = parseInt(value.substr(2, 2), 16); const blue = parseInt(value.substr(4, 2), 16); let brightness; brightness = (red * 299) + (green * 587) + (blue * 114); brightness /= 255000; // values range from 0 to 1 // anything greater than 0.6 should be bright enough for dark text if (brightness >= 0.6) { return dark; } return light; } /** * Convert rgb(r, g, b) to hex. * * @param {String} rgb Rgb string to convert. * @return {String} The hex string. */ export const rgbToHex = rgb => { const matches = /rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*[\d.]+\s*)?\)/.exec(String(rgb == null ? '' : rgb)); if (!matches) { return ''; } // Channels are clamped, so out-of-range input cannot overflow the hex. // An alpha channel is ignored — this returns an opaque hex. const value = [matches[1], matches[2], matches[3]] .map(c => Math.min(255, Number(c)).toString(16).padStart(2, '0')) .join(''); return `#${value}`; }