Автор: abler98
<?php
function hex2rgb($color = '#fff')
{
$color = ltrim($color, '#');
$length = strlen($color);
if (preg_match('/[^0-9a-f]/i', $color) or $length <> 3 && $length <> 6) {
throw new Exception('Invalid color format');
}
if ($length == 3) {
$color = sprintf('%1$s%1$s%2$s%2$s%3$s%3$s', $color[0], $color[1], $color[2]);
}
$color = hexdec($color);
$r = 0xff & ($color >> 16);
$g = 0xff & ($color >> 8);
$b = 0xff & $color;
return sprintf('rgb(%d, %d, %d)', $r, $g, $b);
}
echo hex2rgb('#000') . PHP_EOL; // rgb(0, 0, 0)
echo hex2rgb('#ffffff') . PHP_EOL; // rgb(255, 255, 255)