Skip to content

Instantly share code, notes, and snippets.

@uintdev
Created January 30, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uintdev/3692a9b63bc1d65476390d1caa9b97d1 to your computer and use it in GitHub Desktop.
Save uintdev/3692a9b63bc1d65476390d1caa9b97d1 to your computer and use it in GitHub Desktop.
Hex to RGB conversion.
<?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