PHPBuilder - RGB to HEX - HEX to RGB Convert Function



RSS Twitter
Snippets Algorithms

RGB to HEX - HEX to RGB Convert Function

by: Halit YESIL
|
March 22, 2007

Version: 1.0

Type: Function

Category: Algorithms

License: GNU General Public License

Description: RGB to Hex Hex to RGB Function echo '#FFFFFF => '.rgb2hex2rgb('#FFFFFF').'<br>'; echo '#FFCCEE => '.rgb2hex2rgb('#FFCCEE').'<br>'; echo 'CC22FF => '.rgb2hex2rgb('CC22FF').'<br>'; echo '0 65 255 => '.rgb2hex2rgb('0 65 255').'<br>'; echo '255.150.3 => '.rgb2hex2rgb('255.150.3').'<br>'; echo '100,100,250 => '.rgb2hex2rgb('100,100,250').'<br>'; #FFFFFF => Array{ red=>255, green=>255, blue=>255, r=>255, g=>255, b=>255, 0=>255, 1=>255, 2=>255 } #FFCCEE => Array{ red=>255, green=>204, blue=>238, r=>255, g=>204, b=>238, 0=>255, 1=>204, 2=>238 } CC22FF => Array{ red=>204, green=>34, blue=>255, r=>204, g=>34, b=>255, 0=>204, 1=>34, 2=>255 } 0 65 255 => #0041FF 255.150.3 => #FF9603 100,100,250 => #6464FA



RGB to Hex
Hex to RGB
Function

<?PHP

function rgb2hex2rgb($c){
			if(!$c) return false;
			$c = trim($c);
			$out = false;
			if(eregi("^[0-9ABCDEFabcdef\#]+$", $c)){
				$c = str_replace('#','', $c);
				$l = strlen($c);
				if($l == 3){
					unset($out);
					$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1));
					$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1,1));
					$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2,1));
				}elseif($l == 6){
					unset($out);
					$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,2));
					$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 2,2));
					$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 4,2));
				}else $out = false;
				
			}elseif (eregi("^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$", $c)){
				if(eregi(",", $c))
					$e = explode(",",$c);
				else if(eregi(" ", $c))
					$e = explode(" ",$c);
				else if(eregi(".", $c))
					$e = explode(".",$c);
				else return false;
				
				if(count($e) != 3) return false;
				
				$out = '#';
				for($i = 0; $i<3; $i++)
					$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
				
				for($i = 0; $i<3; $i++)
					$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
					
				$out = strtoupper($out);
			}else $out = false;
			
			return $out;
		}


		echo '#FFFFFF => '.rgb2hex2rgb('#FFFFFF').'<br>';
		echo '#FFCCEE => '.rgb2hex2rgb('#FFCCEE').'<br>';
		echo 'CC22FF => '.rgb2hex2rgb('CC22FF').'<br>';
		echo '0 65 255 => '.rgb2hex2rgb('0 65 255').'<br>';
		echo '255.150.3 => '.rgb2hex2rgb('255.150.3').'<br>';
		echo '100,100,250 => '.rgb2hex2rgb('100,100,250').'<br>';

?>

Output

#FFFFFF => 
 Array{
   red=>255,
   green=>255,
   blue=>255,
   r=>255,
   g=>255,
   b=>255,
   0=>255,
   1=>255,
   2=>255
 }
 

#FFCCEE =>
 Array{
   red=>255,
   green=>204,
   blue=>238,
   r=>255,
   g=>204,
   b=>238,
   0=>255,
   1=>204,
   2=>238
 }
CC22FF =>
 Array{
   red=>204,
   green=>34,
   blue=>255,
   r=>204,
   g=>34,
   b=>255,
   0=>204,
   1=>34,
   2=>255
 }

0 65 255 => #0041FF
255.150.3 => #FF9603
100,100,250 => #6464FA

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Halit YESIL

Comment:



Comment:

(Maximum characters: 1200). You have characters left.