Is it possible to convert rgba color code to hex or rgb equivalent color code in php. I have seared a lot but I found some js functions but not in php.
Please help
When there are sources in JavaScript it should not be a problem to migrate the code into PHP...
RGB to HEX (considering we have our R, G, B colors in $r
, $g
, $b
variables):
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);
Didn't tested but should be working. Should You need RGBa -> RGB conversion desirabily, let me know...