...

View Full Version : Hex code from backgroundColor in firefox



_Dan
06-26-2006, 07:49 PM
Hi.

I want to get the colour in HEX format using "thing.style.backgroundColor". Like "#FFFFFF" or whatever. IE gives this but FF gives it as "rgb(0,0,0)" format.

Is there a simple function/method of getting it as hex? (or a conversion function)

Thanks. :)

vwphillips
06-26-2006, 08:10 PM
<script language="JavaScript">
<!--
R = HexToR("#FFFFFF");
G = HexToG("#FFFFFF");
B = HexToB("#FFFFFF");

function HexToR(h) { return parseInt((cutHex(h)).substring(0,2),16) }
function HexToG(h) { return parseInt((cutHex(h)).substring(2,4),16) }
function HexToB(h) { return parseInt((cutHex(h)).substring(4,6),16) }
function cutHex(h) { return (h.charAt(0)=="#") ? h.substring(1,7) : h}

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}

function toHex(N) {
if (N==null) return "00";
N=parseInt(N); if (N==0 || isNaN(N)) return "00";
N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
return "0123456789ABCDEF".charAt((N-N%16)/16)
+ "0123456789ABCDEF".charAt(N%16);
}

//-->
</script>

_Dan
06-26-2006, 09:06 PM
I'd still have to parse the string to get the rgb numbers. What it returns should be a standard. :(

Thanks.

_Dan
06-26-2006, 10:13 PM
I'm sure a cleaner "RGBstrToHex" could be written but hey it works!:)

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}

function toHex(N)
{
if (N==null) return "00";
N=parseInt(N);
if (N==0 || isNaN(N)) return "00";
N=Math.max(0,N);
N=Math.min(N,255);
N=Math.round(N);
return ("0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16) );
}

function RGBstrToHex(str)
{
if (str.indexOf('rgb') == -1) return str;
str = str.replace("rgb", '');
str = str.replace("(", '');
str = str.replace(")", '');
str = str.replace(" ", '');
str = str.replace(" ", '');
var temp = new Array();
temp = str.split(',');
var r = temp[0];
var g = temp[1];
var b = temp[2];
return "#" + RGBtoHex(r,g,b);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum