If you know your hexadecimal to decimal conversions, and decimal to hexadecimal, it's easy:

To begin we need to undertsand that #00FF00 separates to:

Hex    color    Dec
===    =====    ===
00     red      0
FF     green    255
00     blue     0

we get 255 because the characters 0 - 9 and A - F are used in hexidecimal notation. The numbers 0 - 9 in hexadecimal represent their correspnding values in decimal.  The letters A - F in hexadecimal represent the numbers 10 - 15 in decimal respectively.  The units in hexadecimal are base 16 so starting at the right and working toward the left you have 1's (16 raised to the power of 0; i.e. 16^0), 16's (16 raised to the power of 1; 16^1), 256's (16 raised to the power of 2; 16^2), 4096 (16 raised to the power of 4; 16^4), etc. for hexidecimal just as in decimal it is (starting from the right) 1's (10^0), 10's (10^1), 100's (10^2), 1000's (10^3), etc.  Therefore FF in hexidecimal is (15 * 1) + (15*16) = 255 in decimal.

Using this idea you do the same thing for converting the decimal number 65280 into the hexadecimal representation but work backwards.  Also instead of working in base 16 you are working in base 256 with each pair of hexadecimal numbers.  Also you are working from left to right.  The "red column" is 1's (256^0), the "green column" is 256's (256^1), and the "blue column" is 25536's (256^2).  In your example 00 (red) in hex is 0 in decimal so we have (0 * 1 = 0), next you have FF (green) in hex is 255 in decimal so we have (256 * 255 = 65280), and finally 00 (blue) in hex is 0 in decimal so we have (0 * 65536 = 0).  Putting this altogether you have 0 + 65280 + 0 = 65280.

Of course you wanted to know how to convert the other way.  This is just as easy.  You just work with the opposite idea.  You had 65280 so you divide by the highest possible power which is 65536, the result is 0.99609375, the integer portion is 0 so we know we have 0 for the blue column, then you subtract the inter value you receive (in this case 0) and we are still left with 0.99609375, which we multiply by the next highest power (256) and we get 255 for our green column, then you subtract the integer value from the result (255 - 255 = 0), and the result is the red columns value.  Of course all the values you get are in decimal so you will need to convert these smaller values into their hexadecimal equivalents: 0 -> 0, 255 -> FF, 0 -> 0   and join these values together: 00FF00.

It's pretty simple if you understand powers and bases.

If you want I could write a little script that would do the conversion for you but I thought I would explain how the result is achieved so that you can understand it better.

This whole description might be a little confusing so if anyone can explain it better or has an easier way to do the conversion please go ahead and post it.

Jonathan Southwick
Technical & Network Services
Allegheny College
Meadville, PA 16335
(814) 332-2755

At 3/10/2003 03:56 AM, you wrote:

From the win32 gui example colors.gpl

$color = GUI::ChooseColor(-owner => $w);

Any idea how I can convert the decimal RGB value, $color to the HTML HEX format ?

ie: selecting the colour green returns 65280 How do I convert this to the HTML HEX value: #00FF00 ?



Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, and more