does object.style.color only return rgb

javascript

Solution

If you set:

document.getElementById('text1').style.color = '#000';

It will return `#000`.

However, if you set:

document.getElementById('text1').style.color = 'rgb(0,0,0)';

It will return `rgb(0,0,0)`, so this returned value depends on the value that was set.

You can use `getComputedStyle` to get the color in RGB format and then convert to HEX. See this code:

var hexChars = '0123456789ABCDEF';
var rgb = getComputedStyle(document.body).color.match(/\d+/g);
var r = parseInt(rgb[0]).toString(16);
var g = parseInt(rgb[1]).toString(16);
var b = parseInt(rgb[2]).toString(16);
var hex = '#' + r + g + b;

Problem

environment: JavaScript object.style.color returns something like `"rgb(255,0,0)"` Is there another return format, like hex? ``` var colorvariable = document.getElementById('text1').style.color ```

Original source