function hex2rgb($color)
{
$color = str_replace('#', '', $color);
$hex = strlen($color) == 3
? [$color[0].$color[0], $color[1].$color[1], $color[2].$color[2]]
: [$color[0].$color[1], $color[2].$color[3], $color[4].$color[5]];
list($r, $g, $b) = $hex;
return sprintf(
'rgb(%s, %s, %s)',
hexdec($r), hexdec($g), hexdec($b)
);
}
function rgb2hex($r, $g = null, $b = null)
{
if(strpos($r, 'rgb') !== false || strpos($r, 'rgba') !== false)
{
if(preg_match_all('/\(([^\)]*)\)/', $r, $matches) && isset($matches[1][0]))
{
list($r, $g, $b) = explode(',', $matches[1][0]);
}
else
{
return false;
}
}
$result = '';
foreach([$r, $g, $b] as $c)
{
$hex = base_convert($c, 10, 16);
$result .= ($c < 16) ? ('0'.$hex) : $hex;
}
return '#'.$result;
}PHP 함수 모음 라이브러리를 만드려고 검색하는 도중 쓸만한 코드가 보여 업로드 합니다.
https://github.com/ngfw/Recipe 에 포함된 코드이며, MIT 라이선스를 따릅니다.
