在 PHP 中,是否有可能将 RGBA 颜色代码转换为十六进制或 RGB 等效颜色代码?我搜索了很多但只找到了一些 JS 函数,没有 PHP 的。请帮忙。
当JavaScript中存在源代码时,将其迁移到PHP不应该是一个问题...
RGB转HEX(假设我们有R、G、B颜色分别存储在$r
、$g
、$b
变量中):
function toHex($n) {
$n = intval($n);
if (!$n)
return '00';
$n = max(0, min($n, 255)); // make sure the $n is not bigger than 255 and not less than 0
$index1 = (int) ($n - ($n % 16)) / 16;
$index2 = (int) $n % 16;
return substr("0123456789ABCDEF", $index1, 1)
. substr("0123456789ABCDEF", $index2, 1);
}
echo $hex = '#' . toHex($r) . toHex($g) . toHex($b);
尚未测试,但应该可以正常工作。如果您需要 RGBa -> RGB 转换,请告诉我...
a
透明通道信息。你想要实现什么? - Pekka