Jump to content

Working with colors in PHP?


random1

Recommended Posts

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){
$N = trim($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> </p>';
print_r(hex2rgb("6A3030"));

In color theory, two colors are called complementary if, when mixed in the proper proportion, they produce a neutral color (grey, white, or black).

 

http://en.wikipedia.org/wiki/Complimentary_colors

 

So, if $c1 and $c2 are complimentary

<?php
$c1 = '00FF00';
list ($r, $g, $b) = sscanf($c1,'%2X%2X%2X');

$c2 = sprintf('%02X%02X%02X', 255-$r, 255-$g, 255-$b);
echo "<div style='background-color: #$c1; height:10px'>$c1 <span style='color:#FFF'>$c1</span></div>";
echo "<div style='background-color: #$c2; height:10px'>$c2 <span style='color:#FFF'>$c2</span></div>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.