Convert RGB color values to HSL (Hue, Saturation, Lightness) color space.
Code
Utilitiesconst 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
Blend Colors
Blend two hex colors together with a customizable ratio.
Darken Color
Darken a hex color by a percentage.
Hex to RGB
Convert a hex color code to RGB values.
Invert Color
Invert a hex color (get complementary color).
Is Light Color
Check if a hex color is light (good for determining text color).
Lighten Color
Lighten a hex color by a percentage.