convert Hsl to rgb and hex

后端 未结 6 2025
时光说笑
时光说笑 2020-11-27 07:47

I need a color converter to convert from hsl to rgb and hex value. I am going to do similar like this. I am using jquery and jquery ui range slider for this. Here is my co

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 08:06

    Another way to solve this problem is to leverage the window.getComputedStyle capability of modern browsers:

    1. Create an element on the page (it can be hidden, e.g. with display:none, but that appears to suppress output of the opacity / "A" value)

    2. Set a color-valued property of that element using the representation of your choice, e.g. e.style.color = 'hsla(100, 50%, 75%, 0.8)'; (or even named colors like 'rebeccapurple')

    3. Read the value back using window.getComputedStyle(e).color. It will be a string of the form rgb(r,g,b) or rgba(r,g,b,a).

    Live demo on CodePen

提交回复
热议问题