Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coverts RGB values to HSL
const RGBToHSL = (r,g,b) => {
// Algo http://www.rapidtables.com/convert/color/rgb-to-hsl.htm
const [r1,g1,b1] = [r/255, g/255, b/255];
const [cmax, cmin] = [Math.max(r1,g1,b1), Math.min(r1,g1,b1)];
const d = (cmax - cmin);
const L = (cmax + cmin) / 2;
const S = d == 0 ? 0 : (d / ( 1 - Math.abs(2*L-1)));
const H = d == 0 ? 0 :
cmax == r1 ? (60 * (((g1 - b1) / d) % 6)) :
cmax == g1 ? (60 * (((b1 - r1) / d) + 2)) :
(60 * (((r1 - g1) / d) + 4));
return [H, S, L];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.