[PHP]将 RGB 或 HEX 转换为“Long Int”颜色

发布于 2024-09-17 07:13:11 字数 357 浏览 3 评论 0原文

需要将 RGB 或 HEX 颜色转换为“Long Int”,以便另一个使用此格式的程序。但不确定“Long Int”颜色格式的具体细节。

可以使用此颜色选择器http 手动生成“Long Int”值://hide-inoki.com/en/soft/chunter/index.html 但最好使用 php 函数。

hexdec 为某些十六进制值('FFFFFF'、'2F2F2F')生成正确的“Long Int”,但不为其他十六进制值('123456')生成正确的“Long Int”。

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').

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2024-09-24 07:13:11

您应该能够使用 PHP 的 hexdec 函数。

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

等等。

你是说这些值不正确吗?或者您错误地使用了 dechex?


根据您的评论进行更新,其中颜色“#123456”应该是“Long Int”格式的“5649426”:

基数16中的5649426是0x563412,所以很明显您的格式需要BGR而不是RGB。

因此,只需首先从“RGB”字符串构建一个“BGR”字符串,然后将其输入十六进制:

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

产生5649426

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文