RGBA格式HEX转换成RGB格式HEX? PHP
我想在 PHP 中在 RGBA 格式的十六进制颜色(如 0xFF0000FF
)和 RGB 格式的十六进制颜色(如 0xFF0000
)之间来回转换。
我该怎么做?
I want to convert back and forth between RGBA-formated HEX colors (like0xFF0000FF
) and RGB-formated HEX colors (like 0xFF0000
) in PHP.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这两个函数将满足您的需要:
第一个函数只是删除最后两个字符,而第二个函数只是附加
FF
。These two functions will do what you need:
The first one simply removes the last two characters, whilst the second one simply appends
FF
.这些怎么样:
第一个中的 $blend 实际上是要混合的背景颜色。
编辑:修复由于整数空间不足而破坏 RGBA 的问题。
RGBA 现在作为四部分数组进行处理。 RGB 仍然是一个数字:
How about these:
$blend in the first is effectively a background color to blend with.
Edit: fix due to not enough space in ints ruining RGBA.
RGBA is now handled as a four part array. RGB remains a number.:
RGBA-> RGB 应该很简单,因为它只是去掉最后两位数字。另一个方向是不可能的,因为 alpha 信息不是以 RGB 编码的。
RGBA -> RGB should be simple, as it's just cutting off the last two digits. The other direction is impossible, as alpha information isn't encoded in RGB.
正如 Frank 提到的,您可以简单地去掉最后 2 位数字。
十六进制格式是 3 对数字,第一对红色值,接下来是绿色,再接下来是蓝色。如果添加另一对,则假定它是 alpha 值。
As Frank mentioned, you can simply strip the last 2 digits.
The hex format is 3 pairs of numbers, the first pair Red value, the next is Green, and the next is blue. If you add another pair, it's assumed to be the alpha value.