colorupjs
Version:
A modern, lightweight, and comprehensive color utility library for JavaScript/TypeScript with advanced palette generation and WCAG accessibility tools.
121 lines (105 loc) • 2.59 kB
text/typescript
import { NAME_WITH_HEXCODE } from '../core/constants';
export function hexToRgb(hex: string): { r: number; g: number; b: number } {
hex = hex.replace(/^#/, '');
if (hex.length === 3)
hex = hex
.split('')
.map((c) => c + c)
.join('');
const num = parseInt(hex, 16);
return {
r: (num >> 16) & 255,
g: (num >> 8) & 255,
b: num & 255,
};
}
export function rgbToHex(r: number, g: number, b: number): string {
return (
'#' +
[r, g, b]
.map((x) => {
const hex = x.toString(16);
return hex.length === 1 ? '0' + hex : hex;
})
.join('')
);
}
export function hslToRgb(
h: number,
s: number,
l: number
): { r: number; g: number; b: number } {
s /= 100;
l /= 100;
const c = (1 - Math.abs(2 * l - 1)) * s;
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
const m = l - c / 2;
let r = 0,
g = 0,
b = 0;
if (0 <= h && h < 60) [r, g, b] = [c, x, 0];
else if (h < 120) [r, g, b] = [x, c, 0];
else if (h < 180) [r, g, b] = [0, c, x];
else if (h < 240) [r, g, b] = [0, x, c];
else if (h < 300) [r, g, b] = [x, 0, c];
else [r, g, b] = [c, 0, x];
return {
r: Math.round((r + m) * 255),
g: Math.round((g + m) * 255),
b: Math.round((b + m) * 255),
};
}
export function rgbToHsl(
r: number,
g: number,
b: number
): { h: number; s: number; l: number } {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b),
min = Math.min(r, g, b);
const l = (max + min) / 2;
let h = 0,
s = 0;
if (max !== min) {
const d = max - min;
s = d / (1 - Math.abs(2 * l - 1));
switch (max) {
case r:
h = ((g - b) / d + (g < b ? 6 : 0)) * 60;
break;
case g:
h = ((b - r) / d + 2) * 60;
break;
case b:
h = ((r - g) / d + 4) * 60;
break;
}
}
return {
h: Math.round(h),
s: Math.round(s * 100),
l: Math.round(l * 100),
};
}
export function hexToHsl(hex: string): { h: number; s: number; l: number } {
const { r, g, b } = hexToRgb(hex);
return rgbToHsl(r, g, b);
}
export function nameToHex(name: string): string {
const color = NAME_WITH_HEXCODE[name.toLowerCase()];
if (!color) {
throw new Error(`Color name "${name}" is not recognized.`);
}
return color;
}
export function HexToName(hex: string): string {
const color = Object.entries(NAME_WITH_HEXCODE).find(
([, value]) => value.toLowerCase() === hex.toLowerCase()
);
if (!color) {
throw new Error(`Hex code "${hex}" is not recognized.`);
}
return color[0];
}