RGB to HSL

Convert RGB color values to HSL (Hue, Saturation, Lightness) color space.

Code

Utilities
const r1 = r / 255, g1 = g / 255, b1 = b / 255;
const max = Math.max(r1, g1, b1), min = Math.min(r1, g1, b1);
let h, s, l = (max + min) / 2;
if (max === min) { h = s = 0; }
else {
  const d = max - min;
  s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  h = max === r1 ? ((g1 - b1) / d + (g1 < b1 ? 6 : 0)) / 6 : max === g1 ? ((b1 - r1) / d + 2) / 6 : ((r1 - g1) / d + 4) / 6;
}
return { h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100) };

Parameters

Red value (0-255).

Green value (0-255).

Blue value (0-255).

Browser·fetch() may be limited by CORS

More JavaScript Snippets