How to convert “background-color” to rgb() format?

前端 未结 3 1194
無奈伤痛
無奈伤痛 2021-02-19 22:47

Here is my problem:

I want to change the opacity of background-color of one of the elements on my page. in order to do this I need to first convert the color to rgb() fo

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 23:06

    Use window.getComputedStyle(elem, null).getPropertyValue("background-color"); to get the background-color string. Then, check if it's in a desired format.

    • rgb(n, n, n)
    • Others, such as hsl, rgba, hsla, ... convert these using an easy-to-find algorithm
    • Color names: Create a map of colornames-to-rgb (such as: var name2rgb = {red: "rgb(255, 0, 0)"};)

    A list of color names can be found on the web. Visit this site for a list of colornames (which is probably not complete).

提交回复
热议问题