Is it possible to determine the hexadecimal value of the named color?

Possible duplicate:
Javascript function to convert color names to hexadecimal codes

Is it possible to get or calculate the value of the hexadecimal value that the browser currently uses for the named color? For example, I would like to do something like the following:

(HTML):

<div id="container" style="background-color: lightgreen"></div> 

(JavaScript):

 var container = document.getElementById("container"); var colorAsHex = getHexColor(container, "background-color"); 

At best, I hope for a jQuery solution that I just miss. In the worst case, I am very good at browsers, while I can reach 4 major browsers.

0
source share
2 answers

$('div').css('background-color') seems to work ... see this example link

+3
source
 var namedColor = "lightgreen"; var rgbColor = $("<div></div>").css("background-color", namedColor).css("background-color"); var match = rgbColor.match(/(\d+),\s*(\d+),\s*(\d+)/); var value = (+match[1] << 16) + (+match[2] << 8) + (+match[3] << 0); var hexColor = value.toString(16); while (hexColor.length < 6) { hexColor = "0" + hexColor; } console.log("#" + hexColor) 

Demo here and here .

+1
source

All Articles