Created
December 8, 2010 20:05
-
-
Save brianherbert/733818 to your computer and use it in GitHub Desktop.
This script takes two hex values and produces the average color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$hex1 = 'FF0000'; | |
$hex2 = '0000FF'; | |
$hex3 = ''; | |
$r1 = base_convert($hex1{0}.$hex1{1},16,10); | |
$g1 = base_convert($hex1{2}.$hex1{3},16,10); | |
$b1 = base_convert($hex1{4}.$hex1{5},16,10); | |
$r2 = base_convert($hex2{0}.$hex2{1},16,10); | |
$g2 = base_convert($hex2{2}.$hex2{3},16,10); | |
$b2 = base_convert($hex2{4}.$hex2{5},16,10); | |
$r3 = round(($r1 + $r2) / 2); | |
$g3 = round(($g1 + $g2) / 2); | |
$b3 = round(($b1 + $b2) / 2); | |
$r3 = (string)base_convert($r3,10,16); | |
$g3 = (string)base_convert($g3,10,16); | |
$b3 = (string)base_convert($b3,10,16); | |
if($r3 == 0) $r3 = '00'; | |
if($b3 == 0) $b3 = '00'; | |
if($g3 == 0) $g3 = '00'; | |
$hex3 = $r3.$g3.$b3; | |
?> | |
<div style="width:100px;height:100px;float:left;background-color:#<?php echo $hex1; ?>"><?php echo $hex1; ?></div> | |
<div style="float:left;font-size:72px;"> + </div> | |
<div style="width:100px;height:100px;float:left;background-color:#<?php echo $hex2; ?>"><?php echo $hex2; ?></div> | |
<div style="float:left;font-size:72px;"> = </div> | |
<div style="width:100px;height:100px;float:left;background-color:#<?php echo $hex3; ?>"><?php echo $hex3; ?></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment