hsl-rgb
Version:
Convert hsl colors to rgb colors
33 lines (27 loc) • 676 B
JavaScript
const hue = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
const hslRgb = (h, s, l) => {
let r, g, b;
h = h / 360;
if (s == 0) {
r = g = b = l;
} else {
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue(p, q, h + 1/3);
g = hue(p, q, h);
b = hue(p, q, h - 1/3);
}
return [
Math.max(0, Math.min(Math.round(r * 255), 255)),
Math.max(0, Math.min(Math.round(g * 255), 255)),
Math.max(0, Math.min(Math.round(b * 255), 255))
];
};
module.exports = hslRgb;