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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I seem to be spending a lot of time

Status
Not open for further replies.

MatthewP

Programmer
Jan 16, 2001
176
GB
I seem to be spending a lot of time trying to convert Hexadecimal colour values into RGB values for photoshop, so I can match graphics to background colours and stuff. Surely there's some way I can write a converter in Javascript, so I can input an rgb value and get it to spit out a hex value and vice versa.. any ideas?

Thanks
Matt.
 
Photoshop 5.5+ shows you the RGB versions. You can also use (maybe only in IE) the following:

<div style=&quot;background:rgb(120,120,120)&quot;>

I think lucid created some hex convertor at some point. Maybe he'll post it here. jared@aauser.com -
 
alright... it can be kinda slow for very large numbers, but, here it is.

//function that returns hex from numbers formatted as a string.
//this can be very very slow for large numbers.
Number.prototype.toHex = function()
{
var x, y;
x = 0;
y = 0;
for(var i = 1; i <= this; i++)
{
if(!(i%16))
{
x++;
y = 0;
}
else
{
y++;
}
}
if(x > 15)
{
x = x.toHex();
}
else
{
if(x > 9)
{
switch(x)
{
case 10:
x = &quot;a&quot;;
break;
case 11:
x = &quot;b&quot;;
break;
case 12:
x = &quot;c&quot;;
break;
case 13:
x = &quot;d&quot;;
break;
case 14:
x = &quot;e&quot;;
break;
case 15:
x = &quot;f&quot;;
break;
}
}
else
{
x = new String(x);
}
}
if(y > 9)
{
switch(y)
{
case 10:
y = &quot;a&quot;;
break;
case 11:
y = &quot;b&quot;;
break;
case 12:
y = &quot;c&quot;;
break;
case 13:
y = &quot;d&quot;;
break;
case 14:
y = &quot;e&quot;;
break;
case 15:
y = &quot;f&quot;;
break;
}
}
else
{
y = new String(y);
}
return x+y;
} adam@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top