Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

netscape 6 color 1

Status
Not open for further replies.

UCENwebster

Programmer
Jun 7, 2002
18
US
does anybody have a good way to switch NS6-7 colors from the format "rgb(R,G,B)" to a hex value like "#rrggbb"?
 
just ran into that problem myself. this is what i came up with (the first function is not my own)...

Code:
function toHex(dec)
{
 hexChars = "0123456789ABCDEF";
 if (dec > 255)
 {
  return null;
 }
 var i = dec % 16;
 var j = (dec - i) / 16;
 result = hexChars.charAt(j);
 result += hexChars.charAt(i);
 return result;
}

function convertRGB2Hex(rgb)
{
 var hexColor = "#";
 rgb = rgb.substring(rgb.indexOf("(")+1,rgb.indexOf(")"));
 rgb = rgb.split(",");
 for (i=0;i<rgb.length;i++)
 {
  hexColor += toHex(rgb[i]);
 }
 return hexColor;
}

then, as an example of how to use it...

Code:
var ns = (navigator.appName == &quot;Netscape&quot;) ? true : false;
var rules = (ns==true) ? &quot;cssRules&quot; : &quot;rules&quot;;


var colorCode = eval(&quot;document.styleSheets[0].&quot; + rules + &quot;[0].style.color&quot;);
if  (colorCode.indexOf(&quot;(&quot;) > -1)
{
 colorCode = convertRGB2Hex(colorCode);
}

hope that helps,

glenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top