views:

120

answers:

3

I'm trying to write a JavaScript tool to work on items of a certain colour. On a test page, I set the colour using an inline style, to mimic the target pages, but when the page is rendered, the colour is specified using the CSS rgb() function.

The HTML tries to emulate the GMail container I want to change the background colour on. When I inspect the element in Firebug, the colour is displayed as rgb().

<div style="width: 100%; height: 30px; border: 1px solid black; 
    background: #6694E3 none repeat scroll 0 0;">    
</div>

How can I stop the colour being converted from #6694E3 to rgb(102, 148, 227)?

+1  A: 

This is not a conversion, it's just a different way of expressing the same thing. If you want to check for a particular colour, you will have to first express it in rgb format, then do the check. As far as I am aware, the css value returned for a DOM element is whatever it is - you cannot change the format.

Richard
Why then when I view GMail elements in Firebug, there colours are displayed as hex, but when I view my own page, they are displayed as rgb?
ProfK
+3  A: 

Check out this script to convert rgb() notation to hex.

Pekka
+1 for inspiration. What I actually needed was a script to convert hex to RGB (in a string form), so I can use a jQuery filter on the inline style text.
ProfK
A: 

You are probably looking at the value through firebug, which translates all colors to RGB form in the console. This does not mean it changed in the DOM.

Corey Hart
I think it is changed in the DOM, at least in in 3.5 - I recently fixed a JS bug caused by this problem, code was looking for a hex value for computer style and it was getting an rgb one and failing the match.
robertc
Yes, lots of values are converted to an equivalent unit when the computedStyle is calculated.
bobince
@Corey Hart: firebug as got nothing to to with this, both Safari and Chrome renderds style.color as "rgb(r,g,b)" and not like IE that always renderd them as an hex string "#rrggbb"
Marco Demajo