color-tf
Version:
RGB, HSL, HSV, HWB and more color models convertors
17 lines (12 loc) • 420 B
JavaScript
;
var rgb2hsl = (r, g, b) => {
const max = Math.max(r, g, b),
min = Math.min(r, g, b);
const l = (max + min) / 2,
d = max - min;
if (d <= 0) return [0, 0, l]; // achromatic
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
const h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
return [h / 6, s, l];
};
module.exports = rgb2hsl;