random1 Posted August 1, 2008 Share Posted August 1, 2008 Does anyone know any good color PHP classes? I'm looking for these functionalities: * Conversion of RGB color to Hex color * Finding complimentary colors * Conversion of hex color to Hex color * etc... Quote Link to comment https://forums.phpfreaks.com/topic/117675-working-with-colors-in-php/ Share on other sites More sharing options...
wildteen88 Posted August 1, 2008 Share Posted August 1, 2008 PHP does not have a built in function for your needs, you'll need to create one. Have a look at the following comment over at php.net for such a function. Quote Link to comment https://forums.phpfreaks.com/topic/117675-working-with-colors-in-php/#findComment-605559 Share on other sites More sharing options...
The Little Guy Posted August 1, 2008 Share Posted August 1, 2008 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")); Quote Link to comment https://forums.phpfreaks.com/topic/117675-working-with-colors-in-php/#findComment-605582 Share on other sites More sharing options...
Barand Posted August 1, 2008 Share Posted August 1, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117675-working-with-colors-in-php/#findComment-605660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.