PHP - Hex color to RGB color converter
From Global Programming Syntax
Turning a hex color (#FFFFFF) to a RGB color (255,255,255) is a simple process of letter to number conversion then multiplication and addition at the end. So below is a function that will convert hex to RGB.
<?
function hex_to_rgb($color,$rgb)
{
$color=str_replace("#",'',$color);
$red1=substr_replace($color,'',-5);
$red2=substr_replace($color,'',-4);
$red2=substr_replace($red2,'',0,1);
$green1=substr_replace($color,'',-3);
$green1=substr_replace($green1,'',0,2);
$green2=substr_replace($color,'',-2);
$green2=substr_replace($green2,'',0,3);
$blue1=substr_replace($color,'',-1);
$blue1=substr_replace($blue1,'',0,4);
$blue2=substr_replace($color,'',0,5);
if ($red1=='A') {$red1=10;} else
if ($red1=='B') {$red1=11;} else
if ($red1=='C') {$red1=12;} else
if ($red1=='D') {$red1=13;} else
if ($red1=='E') {$red1=14;} else
if ($red1=='F') {$red1=15;}
if ($red2=='A') {$red2=10;} else
if ($red2=='B') {$red2=11;} else
if ($red2=='C') {$red2=12;} else
if ($red2=='D') {$red2=13;} else
if ($red2=='E') {$red2=14;} else
if ($red2=='F') {$red2=15;}
if ($green1=='A') {$green1=10;} else
if ($green1=='B') {$green1=11;} else
if ($green1=='C') {$green1=12;} else
if ($green1=='D') {$green1=13;} else
if ($green1=='E') {$green1=14;} else
if ($green1=='F') {$green1=15;}
if ($green2=='A') {$green2=10;} else
if ($green2=='B') {$green2=11;} else
if ($green2=='C') {$green2=12;} else
if ($green2=='D') {$green2=13;} else
if ($green2=='E') {$green2=14;} else
if ($green2=='F') {$green2=15;}
if ($blue1==='A') {$blue1=10;} else
if ($blue1==='B') {$blue1=11;} else
if ($blue1==='C') {$blue1=12;} else
if ($blue1==='D') {$blue1=13;} else
if ($blue1==='E') {$blue1=14;} else
if ($blue1==='F') {$blue1=15;}
if ($blue2=='A') {$blue2=10;} else
if ($blue2=='B') {$blue2=11;} else
if ($blue2=='C') {$blue2=12;} else
if ($blue2=='D') {$blue2=13;} else
if ($blue2=='E') {$blue2=14;} else
if ($blue2=='F') {$blue2=15;}
$color_red=$red1*16+$red2;
$color_green=$green1*16+$green2;
$color_blue=$blue1*16+$blue2;
if ($rgb='r' || $rgb='red')
{
return $color_red;
}
if ($rgb='g' || $rgb='green')
{
return $color_green;
}
if ($rgb='b' || $rgb='blue')
{
return $color_blue;
}
}
?>
It is simple to use this hex converter. If you want to display each color (red, green and blue) then you can use the following code. And have fun using it as it is really usefull with the gd library.
<?
//below will display the 3 colors.
echo 'red='.hex_to_rgb('#FFFFFF','red').'<br>';
echo 'green='.hex_to_rgb('#FFFFFF','green').'<br>';
echo 'blue='.hex_to_rgb('#FFFFFF','blue').'<br>';
?>
External Code Recourse
The following function is one made by a user called ShawnCplus from www.daniweb.com. It is by far more better to use as it uses the hexdec function to convert each character.
<?
function torgb($string)
{
$string = str_replace("#","",$string);
$r = substr($string, 0, 2);
$g = substr($string, 2, 2);
$b = substr($string, 4, 2);
return array('r'=>hexdec($r), 'g'=>hexdec($g), 'b'=>hexdec($b));
}
?>
Then to use the above code:
<?
//below will display the 3 colors.
$color=torgb('#FFFFFF');
echo "red=" .$color['r']."<br>";
echo "green=".$color['g']."<br>";
echo "blue=" .$color['b']."<br>";
?>
Also if you were to make the function simular to the first function in this topic, it would look something like the following:
function hex_to_rgb($string,$rgb)
{
$string = str_replace('#','',$string);
$r = substr($string, 0, 2);
$g = substr($string, 2, 2);
$b = substr($string, 4, 2);
if (in_array($rgb,array('r','red'))) { return hexdec($r); }
if (in_array($rgb,array('g','green'))) { return hexdec($g); }
if (in_array($rgb,array('r','red'))) { return hexdec($b); }
}
//now to use the function
echo 'red='.hex_to_rgb('#FFFFFF','red').'<br>';
echo 'green='.hex_to_rgb('#FFFFFF','green').'<br>';
echo 'blue='.hex_to_rgb('#FFFFFF','blue').'<br>';
