Skip to content

Instantly share code, notes, and snippets.

@PaulKinlan
Last active June 1, 2016 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PaulKinlan/d4053acfffd49abfa197a8d370a18337 to your computer and use it in GitHub Desktop.
Save PaulKinlan/d4053acfffd49abfa197a8d370a18337 to your computer and use it in GitHub Desktop.
Converts an HSL value to RGB
const HSLToRBG = (H, S, L) => {
// algo http://www.rapidtables.com/convert/color/hsl-to-rgb.htm
const C = (1 - Math.abs(2*L - 1)) * S;
const X = C * (1 - Math.abs(((H / 60) % 2)-1));
const m = L - C/2;
const [r1, g1, b1] = H >= 0 && H < 60 ? [C, X, 0] :
H >= 60 && H < 120 ? [X, C, 0] :
H >= 120 && H < 180 ? [0, C, X] :
H >= 180 && H < 240 ? [0, X, C] :
H >= 240 && H < 300 ? [X, 0, C] :
[C, 0, X];
return [(r1 + m) * 255, (g1 + m) * 255, (b1 + m) * 255];
};
@morkro
Copy link

morkro commented Apr 18, 2016

Your method is still named HSLToRBG. Should be HSLToRGB 🙋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment