UNPKG

counter-color

Version:

Helps to determine what color of text goes best for a given background, light or dark (and vice versa).

40 lines 1.36 kB
export const sRGB = (rgb) => { const normalized = rgb.map(channel => channel / 255); const srgb = normalized.map(channel => channel <= 0.039_28 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4); return srgb; }; export function hexToRGB(hex) { if (hex.startsWith("#")) hex = hex.substring(1); const toDec = (hex) => parseInt(hex, 16); return hex.length == 3 ? [toDec(hex[0] + hex[0]), toDec(hex[1] + hex[1]), toDec(hex[2] + hex[2])] : [toDec(hex[0] + hex[1]), toDec(hex[2] + hex[3]), toDec(hex[4] + hex[5])]; } export function decToRGB(dec) { return [(dec >> 16) & 0xff, (dec >> 8) & 0xff, dec & 0xff]; } export function toRGB(color) { if (typeof color === "string") return hexToRGB(color); else if (typeof color === "number") return decToRGB(color); else if (Array.isArray(color)) return color; throw new Error(`Bad color value: ${typeof color} = ${color}`); } export function rgbToHex(rgb) { return `#` + rgb.map(_ => _.toString(16).padStart(2, "0")).join(""); } export function decToHex(dec) { return rgbToHex(decToRGB(dec)); } export function hexToDec(hex) { if (hex.startsWith("#")) hex = hex.substring(1); return parseInt(hex, 16); } export function rbgToDec(rgb) { return hexToDec(rgbToHex(rgb)); } //# sourceMappingURL=rgb.js.map