Hexadecimal To RGB

This algorithm converts hexadecimal color code to RGB color model.



									class RGB
{
	public $R;
	public $G;
	public $B;
}

function HexadecimalToDecimal($hex)
{
	$hex = strtoupper($hex);

	$hexLength = strlen($hex);
	$dec = 0;

	for ($i = 0; $i < $hexLength; $i++)
	{
		$b = $hex[$i];

		if ($b >= 48 && $b <= 57)
			$b -= 48;
		else if ($b >= 65 && $b <= 70)
			$b -= 55;

		$dec += $b * pow(16, (($hexLength - $i) - 1));
	}

	return (int)$dec;
}

function HexadecimalToRGB($hex) {
	if ($hex[0] == '#')
		$hex = substr($hex, 1);

	$rgb = new RGB();

	$rgb->R = floor(HexadecimalToDecimal(substr($hex, 0, 2)));
	$rgb->G = floor(HexadecimalToDecimal(substr($hex, 2, 2)));
	$rgb->B = floor(HexadecimalToDecimal(substr($hex, 4, 2)));

	return $rgb;
}
								


Example

									$data = "#520057";
$value = HexadecimalToRGB($data);
								


Output

									R: 82
G: 0
B: 87