开发者

[PHP]Converting RGB or HEX To "Long Int" Color

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 t开发者_C百科he "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').


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜