php convert hex color to rgb and rgb to hex Webmaster SearchEngine Forum

Search Forums:
Search All Member Forums as well?

search forums Reported posts Topics since your last visit Webmaster SearchEngine Forum Webmaster Coding & HTML Forum: Webmaster Coding And Programming Php Convert Hex Color To Rgb And Rgb To Hex

Php Convert Hex Color To Rgb And Rgb To Hex

#1 Report Post 21 Feb 2011 1:51:07 PM
Threads: 5
Posts: 6

Websitedays

Message Info Pad Profile

php convert hex color to rgb and rgb to hex

Here are 2 PHP functions to convert html hex colors to RGB, and RGB colors back to html hex color format.

First function rgb2html takes three inputs as RGB values like:
rgb2html(255, 255, 255);
and returns the hex color for this like: FFFFFF

Second function html2rgb takes one input which is the hex color (should be 6 digit) without the # sign like:
html2rgb('ffffff');
and returns the color back in RGB format like: 255.255.255


function rgb2html($r=0,$g=0,$b=0){
if($r<0 || $r>255){ $r=0; }
if($g<0 || $g>255){ $g=0; }
if($b<0 || $b>255){ $b=0; }
$r=dechex($r); if(strlen($r)<2){ $r='0'.$r; }
$g=dechex($g); if(strlen($g)<2){ $g='0'.$g; }
$b=dechex($b); if(strlen($b)<2){ $b='0'.$b; }
$color=$r.$g.$b;
return $color;
}

function html2rgb($h='ffffff'){
if(strlen($h)==6){
$r=substr($h,0,2);
$g=substr($h,2,2);
$b=substr($h,4,2);
$color=hexdec($r).'.'.hexdec($g).'.'.hexdec($b);
}
return $color;
}
?>


Enjoy!

Last Editted: 21 Feb 2011 2:52:36 PM - Reason:

Quote Reply

Post a Message:

Show Your Signature?

Total: (1) Pages: (1) First | 1 | Last |
Domain:
Com Net Org In More
SEO WebDesign