是否可以在 php.ini 中将 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 转换,请告诉我...