Skip to content

Instantly share code, notes, and snippets.

@PaulKinlan
Last active June 1, 2016 10:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PaulKinlan/4843350cbccd75a4dbeb351b21054b58 to your computer and use it in GitHub Desktop.
Save PaulKinlan/4843350cbccd75a4dbeb351b21054b58 to your computer and use it in GitHub Desktop.
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