views:

133

answers:

2

Need to convert RGB or HEX colors to "Long Int" for another program that uses this format. Not sure the specifics of the "Long Int" color format though.

It is possible to generate the "Long Int" values manually using this color-picker http://hide-inoki.com/en/soft/chunter/index.html but a php function would be preferred.

hexdec generates the correct "Long Int" for some HEX values ('FFFFFF', '2F2F2F') but not others ('123456').

A: 

Where ever you would place the int values, place it instead as hex, 0x123456, 0xFFFFFF, and 0x2F2F2F.

Mark Tomlin
Doesn't affect hexdec output, php already knows it's hex.
phpwns
+1  A: 

You should be able to use PHP's hexdec function.

hexdec('FFFFFF'): 16777215
hexdec('123456'): 1193046

etc.

Are you saying these values aren't correct? Or were you using dechex instead by mistake?


Update based on your comment which says that color "#123456" should be "5649426" in "Long Int" format:

5649426 in base 16 is 0x563412, so it's clear your format requires BGR instead of RGB.

So just build a "BGR" string from your "RGB" string first then feed it to hexdec:

$rgb = '123456';
$bgr = substr($rgb,4,2) . substr($rgb,2,2) . substr($rgb,0,2);
print hexdec($bgr);

yields 5649426.

dkamins
That was a typo in the question, my bad. R18, G52, R86/#123456 == 5649426 in "Long Int", and that was where hexdec results were not matching up w/ the color-picker conversion. :/
phpwns
Sweet, I was wondering if the color order was different then standard RGB but didn't mess around with it enough, thanks!
phpwns