HSL to RGB color conversion

后端 未结 21 2999
忘了有多久
忘了有多久 2020-11-22 01:59

I am looking for a JavaScript / PHP algorithm to convert between HSL color to RGB.

It seems to me that HSL is not very widely used so I am not having much luck search

21条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 02:21

    An hsl|a color value, set in javascript, will be instantly converted to rgb|a All you need to do then is access the computed style value

    document.body.style.color = 'hsla(44, 100%, 50%, 0.8)';
    
    console.log(window.getComputedStyle(document.body).color);
    
    // displays: rgba(255, 187, 0, 0.8)
    

    Technically, I guess, this isn't even any lines of code - it's just done automatically. So, depending on your environment, you might be able to get away with just this. Not that there aren't a lot of very thoughtful responses here. I don't know what your goal is.

    Now, what if you want to convert from rbg|a to hsl|a?

提交回复
热议问题