hsl2rgb · JavaScript Function to Convert Colors from HSL to RGB

Home | See also: hsl2rgb PHP Function
function hsl2rgb (h, s, l) {

    function calculate (offset) {
        const x = (h_ratio + 12 - offset) % 12
        const value = Math.max(3 - x, x - 9)
        const clipped = Math.max(-1, Math.min(1, value))
        return Math.round((l_ratio + clipped * range) * 255)
    }

    const h_ratio = (h % 360 + 360) % 360 / 30
    const s_ratio = Math.max(0, Math.min(1, s / 100))
    const l_ratio = Math.max(0, Math.min(1, l / 100))
    const range = s_ratio * Math.min(l_ratio, 1 - l_ratio)

    return {
        r: calculate(0),
        g: calculate(4),
        b: calculate(8),
    }

}
Last updated 25 days ago.
nichabi.com