HSL to RGB Converter
Deatils
EXAMPLE
the quick brown fox
HTML
<p>the quick brown fox</p>
JavaScript
const convertHSLToRGB = (h, s, l) => {
s /= 100
l /= 100
const k = (n) => (n + h / 30) % 12
const a = s * Math.min(l, 1 - l)
const f = (n) =>
l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))
return [
Math.round(255 * f(0)),
Math.round(255 * f(8)),
Math.round(255 * f(4)),
]
}
const kickoff = () => {
console.log(convertHSLToRGB(140, 20, 80))
}
document.addEventListener('DOMContentLoaded', kickoff)
Notes
- Got the code from this page. Had to add Math.round to it to get it to return integers
References
- HSL to RGB - 30 Seconds Of Code - This is where I got the initail formula. I had to add Math.round() to produce integers though
- HSL to RGB color conversion - Stack Overflow - This is the top ranked stack overflow answer and the first code search result. The formula in there expects HSL values to be inbetween 0 and 1. I can do with with the 's' and 'l' values which are percentages, but I don't want to have to add more math to deal with the hue which is in degrees