I'm attempting to retrieve the background color of an element:
var bgcolor = $('.myclass').first().css('background-color')
and then convert it to hex
function rgbhex(color) {
return "#" + $.map(color.match(/\b(\d+)\b/g), function (digit) {
return ('0' + parseInt(digit).toString(16)).slice(-2);
}).join('');
}
However, I'm encountering issues - in FireFox, "transparent"
is returned for bgcolor
, resulting in a failure with the rgbhex()
function and throwing the error:
TypeError: elems is null
On the other hand, Chrome returns rgba(0, 0, 0, 0)
for bgcolor
where rgbhex()
functions correctly.
Is there a way to get the CSS color in a cross-browser compatible format and successfully convert it to hex?