We've got some JavaScript code that gets the current background color.
It works, but Netscape returns it in rgb format, while IE returns it as
the color
text name (e.g., 'white'). We need to do some math calculations on the
color,
so I want the rgb values. How can I get IE to return the background
color in rgb
format, or how do I convert the text color to its rgb value? Thanks!
Only works for Internet Explorer!!!!!!!!!!
Create a table with it's visibility style set to hidden, set this tables
bgcolor
to the named color:
<table id="temptable" bgcolor="indigo"><tr><td></td></tr></table>
then request back the color:
document.getElementById("temptable").bgColor
it should return a hex color...
full sample:
<html>
<head>
<style>
#temptable{visibility:hidden}
</style>
<script>
var hexChars = "0123456789ABCDEF";
function Dec2Hex (Dec) {
// this function isn't used in this sample
var a = Dec % 16;
var b = (Dec - a)/16;
hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
return hex;
}
function Hex2Dec(HexVal){
HexVal = HexVal.toUpperCase();
var DecVal = 0;
var temp = HexVal.substring(0,1);
DecVal = (hexChars.indexOf(temp) * 16);
temp = HexVal.substring(1);
DecVal += hexChars.indexOf(temp);
return DecVal;
}
function test(){
var HexString = document.getElementById("temptable").bgColor;
var r = Hex2Dec(HexString.substring(1,3));
var g = Hex2Dec(HexString.substring(3,5));
var b = Hex2Dec(HexString.substring(5,7));
alert("hex: " + HexString + "\nrgb: " + r + " " + g + " " + b);
}
</script>
</head>
<body onclick="test()">
<table id="temptable" bgcolor="indigo"><tr><td></td></tr></table>
</body>
</html>