Last active
June 1, 2016 10:24
-
-
Save PaulKinlan/4843350cbccd75a4dbeb351b21054b58 to your computer and use it in GitHub Desktop.
Coverts RGB values to HSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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