How to extract r, g, b, a values from CSS color?

后端 未结 7 524
猫巷女王i
猫巷女王i 2021-01-08 00:54

What would be the easiest way to transform

$(\'#my_element\').css(\'backgroundColor\')

to object like this:

{ r: red_value,         


        
7条回答
  •  春和景丽
    2021-01-08 01:01

    Simple function to extract the RGB numeric values

    function extractRgb (css) {
      return css.match(/[0-9.]+/gi)
    }
    console.log(extractRgb('rgb(255, 255, 255)'))
    console.log(extractRgb('rgba(255, 255, 255, 0.7)'))

提交回复
热议问题