Martin AJ Martin AJ - 3 months ago 22
Javascript Question

Can I rely on the element's color?

I have lots of such a condition in my codes: (conditions which are based on the element's color)

if ( $(elmnt).css("color") == "rgb(134, 134, 134)" ) {
// do stuff

Recently I've heard:

relying on the color's element is wrong. Because different browsers return different kind of color. Like:

  • rgb (0,0,0)

  • #000000

  • black

Is this ^ true? anyway, as I said, I have lots of conditions like above in my JS codes, and now I don't know what should I do. Any suggestion?

Note: Changing all those conditions in my codes seems like a nightmare.

Answer Source

You can use this RGB color parser for JavaScript:

Example use (from site):

var color = new RGBColor('darkblue');
if (color.ok) { // 'ok' is true when the parsing was a success
    // alert channels
    alert(color.r + ', ' + color.g + ', ' + color.b);
    // alert HEX and RGB