Created
January 30, 2018 15:57
-
-
Save uintdev/3692a9b63bc1d65476390d1caa9b97d1 to your computer and use it in GitHub Desktop.
Hex to RGB conversion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$str = 'ffffff'; | |
$chartype = ctype_alnum($str); | |
$charcount = strlen($str); | |
if ($charcount === 3 && $chartype) { | |
$splitby = 1; | |
} else if ($charcount === 6 && $chartype) { | |
$splitby = 2; | |
} else { | |
exit('invalid hex code format'); | |
} | |
$arr = str_split($str, $splitby); | |
foreach ($arr as $rgbval) { | |
if ($splitby === 1) { | |
$rgbval = $rgbval . $rgbval; | |
} | |
$rgbval = hexdec($rgbval); | |
$rgb[] = $rgbval; | |
} | |
echo 'rgb(' . $rgb[0] . ',' . $rgb[1] . ',' . $rgb[2] . ')'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment