When you are to get a value from a CSSStyleDeclaration, the browser has to call the serialize a CSS value algorithm.
Various values will have different parsing rules.
If we take your value, backgroundColor
is a longhand property, so we can directly jump to the component #FF0000
, which is a <color> component. We thus have to use the algorithm described here:
Our <color>'s alpha value is 1
, we will use "the serialization of the rgb() functional equivalent of the opaque color"
"rgb(" + 255 + ", " + 0 + ", " + 0 + ")"
This particular example has a very clear output if we follow the specs.
And I think most browsers do follow this part of the specs since enough of a long time for it to be considered safe assumption modern ones will return this value.
But this is not true for all CSS values.
If you take for instance the shorthand values, then different browsers will output different results, because the specs are more lenient as to what should happen.
const style = document.createElement('a').style;
style.background = "#FF0000";
console.log(style.background);
// in Firefox "rgb(255, 0, 0) none repeat scroll 0% 0%"
// in Chrome and Safari "rgb(255, 0, 0)"
Same thing for other components like where browsers don't agree on the output format of the url()
func.
const style = document.createElement('a').style;
style.backgroundImage = "url('foo.bar'), url('baz.bla')";
console.log(style.backgroundImage);
// in Firefox and Chrome 'url("foo.bar"), url("baz.bla")'
// in Safari 'url("https://stacksnippets.net/foo.bar"), url("https://stacksnippets.net/baz.bla")'
// and IIRC previous versions of one of these browsers did use `'` instead of `"` for quite a long time.
So I'm not sure what you really are after, but if what you want is just to parse a #RrGgBb
notation to an rgb()
one, you might actually prefer to do it manually, at least because it should have less side-effects than using a CSSStyleDeclaration.