[Javascript] Hex values and RGB values?

Steven Chipman sgchipman at gmail.com
Sat Jun 14 19:50:03 CDT 2008


> How can I get the hex value from
>
> document.getElementById('matte').style.background
>



I'd recommend using backgroundColor as your getter/setter to avoid the
extraneous information. This will still give you an RGB value, but you
can always parse the 3 values out of the string and convert them to
hex by calling toString(16) on each value.

Example:

function toHex(dec) {
	hex=dec.toString(16);
	if(hex.length==1)hex="0"+hex;
	return hex.toUpperCase();
}


Calling toHex(255) will return FF. Then, just stick them all back
together to get your 6 character hex value.



-- 
S.G. Chipman
Software Engineer, Portrait Artist
http://slayeroffice.com | http://sgchipman.com



More information about the Javascript mailing list