RGBA格式HEX转换成RGB格式HEX? PHP

发布于 2024-11-19 21:01:59 字数 121 浏览 2 评论 0原文

我想在 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技术交流群

发布评论

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

评论(4

红衣飘飘貌似仙 2024-11-26 21:01:59

这两个函数将满足您的需要:

function rgbaToRgb ($rgba) {
    return substr($rgba, 0, -2);
}

function rgbToRgba ($rgb) {
    return $rgb . "FF";
}

第一个函数只是删除最后两个字符,而第二个函数只是附加 FF

These two functions will do what you need:

function rgbaToRgb ($rgba) {
    return substr($rgba, 0, -2);
}

function rgbToRgba ($rgb) {
    return $rgb . "FF";
}

The first one simply removes the last two characters, whilst the second one simply appends FF.

看海 2024-11-26 21:01:59

这些怎么样:

function rgbaToRgb ($rgba, $blend = 0xFFFFFF) {
    return (($rgba >> 8)*($rgba & 0xFF)/0xFF) * ($blend / 0xFFFFFF);
}

function rgbToRgba ($rgb) {
    return ($rgb << 8) | 0xFF;
}

第一个中的 $blend 实际上是要混合的背景颜色。

编辑:修复由于整数空间不足而破坏 RGBA 的问题。
RGBA 现在作为四部分数组进行处理。 RGB 仍然是一个数字:

function rgbaToRgb ($rgba, $blend = 0xFFFFFF) {
$rbg = array($rgba[0] * $rgba[3]/0xFF + ($blend>>16) * (0xFF-$rgba[3])/0xFF, $rgba[1] * $rgba[3]/0xFF + (($blend>>8)&0xFF)*(0xFF-$rgba[3])/0xFF, $rgba[2] * $rgba[3]/0xFF + ($blend&0xFF) * (0xFF-$rgba[3])/0xFF);
return ($rbg[0]<<16) + ($rbg[1]<<8) + $rbg[2];
}

function rgbToRgba ($rgb) {
    return array($rgb >> 16, ($rgb >> 8) & 0xFF, $rgb & 0xFF, 0xFF);
}

How about these:

function rgbaToRgb ($rgba, $blend = 0xFFFFFF) {
    return (($rgba >> 8)*($rgba & 0xFF)/0xFF) * ($blend / 0xFFFFFF);
}

function rgbToRgba ($rgb) {
    return ($rgb << 8) | 0xFF;
}

$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.:

function rgbaToRgb ($rgba, $blend = 0xFFFFFF) {
$rbg = array($rgba[0] * $rgba[3]/0xFF + ($blend>>16) * (0xFF-$rgba[3])/0xFF, $rgba[1] * $rgba[3]/0xFF + (($blend>>8)&0xFF)*(0xFF-$rgba[3])/0xFF, $rgba[2] * $rgba[3]/0xFF + ($blend&0xFF) * (0xFF-$rgba[3])/0xFF);
return ($rbg[0]<<16) + ($rbg[1]<<8) + $rbg[2];
}

function rgbToRgba ($rgb) {
    return array($rgb >> 16, ($rgb >> 8) & 0xFF, $rgb & 0xFF, 0xFF);
}
怎言笑 2024-11-26 21:01:59

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.

风流物 2024-11-26 21:01:59

正如 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.

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