Skip to content

Instantly share code, notes, and snippets.

@vhdm
Created September 6, 2015 21:29
Show Gist options
  • Save vhdm/8aa59904505d911ebb20 to your computer and use it in GitHub Desktop.
Save vhdm/8aa59904505d911ebb20 to your computer and use it in GitHub Desktop.
Hex To RGB and RGB To Hex
<?php
error_reporting(0);
function toHex($N) {
if ($N==NULL) return "00";
if ($N==0) return "00";
$N=max(0,$N);
$N=min($N,255);
$N=round($N);
$string = "0123456789ABCDEF";
$val = (($N-$N%16)/16);
$s1 = $string{$val};
$val = ($N%16);
$s2 = $string{$val};
return $s1.$s2;
}
function rgb2hex($r,$g,$b){
return toHex($r).toHex($g).toHex($b);
}
function hex2rgb($N){
$dou = str_split($N,2);
return array(
"R" => hexdec($dou[0]),
"G" => hexdec($dou[1]),
"B" => hexdec($dou[2])
);
}
echo rgb2hex(106,48,48);
echo '<p>&nbsp;</p>';
print_r(hex2rgb("6A3030"));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment