Last active
May 29, 2024 22:09
-
-
Save moriarty99779/7d4250fb0c1027759155a7f826f9a137 to your computer and use it in GitHub Desktop.
Javascript - Convert RGB to HSL
This file contains hidden or 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 convertRGBToHSL = (r, g, b) => { | |
[r, g, b] = [r, g, b].map(val => val / 255); | |
const [max, min] = [Math.max(r, g, b), Math.min(r, g, b)]; | |
let h = (max !== min) ? ((max === r ? g - b : (max === g ? b - r : r - g)) / (max - min) + (max === g ? 2 : (max === b ? 4 : 0))) / 6 : 0; | |
let s = (max !== min) ? (l => l > 0.5 ? (max - min) / (2 - max - min) : (max - min) / (max + min))(l) : 0; | |
let l = (max + min) / 2; | |
return { h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100) }; | |
}; | |
document.write(convertRGBToHSL(77, 123, 44)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment