I'm parsing a color string returned by getComputedStyle to get R, G, B, and A values from it.
So far (in Chrome and Firefox), color values always seem to come back in rgb or rgba format which is easy to parse:
const [, r, g, b, a] = str.replace(/\s/g, "").match(/rgba?\((\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)(?:,(\d+(?:\.\d+)?))?\)/i);
I cannot, however, find any promise about color format in any of the specs for getComputedStyle listed on its MDN page.
Is there any guarantee of color format from getComputedStyle? Or is it entirely up to browser implementation?
I'd prefer not to have to check for HEX and HSLA values (and really whatever else is possible - I'm not entirely sure).
A quick snippet of code for testing color values in your console:
console.log((str => {
const div = document.createElement("div");
div.style.backgroundColor = str;
document.body.append(div);
return getComputedStyle(div).backgroundColor;
})("magenta"));
CSSOM does not state this directly, but instead references css-color-4:
For the purposes of
getComputedStyle(), both of the above lines mean the same thing. Specifically, section 4.7.2 covers the majority of commonly used formats:Section 4.7.6 covers system colors (computes to lowercase),
transparent(computes torgba(0, 0, 0, 0)) andcurrentColor(computes to lowercasecurrentcolor).As css-color-4 introduces several new ways to specify colors, other sections exist for other formats such as §4.7.3 for LCH values, §4.7.4 for the
color()function, and more.This means that color values from
getComputedStyle()are guaranteed to be in eitherrgb()orrgba()format, depending on the alpha value, only when the specified values are in any of the formats in §4.7.2. But §4.7.2 and §4.7.6 cover the vast majority of use cases in everyday CSS, so they can still be relied on. Considering the other, exotic formats aren't really supported anywhere yet, it's probably not worth testing for them until they enjoy any sort of mainstream use.