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

Viewed 15924

What would be the easiest way to transform

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

to object like this:

{ r: red_value, g: green_value, b: blue_value, a: alpha_value }

?

7 Answers

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)'))

Related