Skip to content

Instantly share code, notes, and snippets.

@lemmon
Created June 5, 2012 15:12
Show Gist options
  • Save lemmon/2875587 to your computer and use it in GitHub Desktop.
Save lemmon/2875587 to your computer and use it in GitHub Desktop.
<?php
class Some_Class_To_Parse_Colors_From_Images
{
function logo()
{
$src = 'public/dummy/logo2.png';
$im = imagecreatefrompng(ROOT . $src);
$w0 = imagesx($im);
$h0 = imagesy($im);
for ($y=0; $y < $h0; $y++)
{
for ($x=0; $x < $w0; $x++)
{
$rgb_index = imagecolorat($im, $x, $y);
$rgb = imagecolorsforindex($im, $rgb_index);
$hsv = $this->rgb2hsv($rgb);
$hue_index = $hsv[0];
#$hue_index = round( ( $rgb['red']*256*256 + $rgb['green']*256 + $rgb['blue'] ) / 65536 );
$palette[ $hue_index ][ $rgb_index ] += $hsv[1] * $hsv[2];
$hues[ $hue_index ] += $hsv[1] * $hsv[2];
}
}
foreach ($palette as $hue => $palette1)
{
$sort[$hue] = count($palette1) * array_sum($palette1);
arsort($palette[$hue]);
}
arsort($sort);
#ksort($sort);
foreach ($sort as $hue => $total_density)
{
$palette1 = $palette[$hue];
/*
foreach ($palette1 as $rgb_index => $density)
{
$rgb = imagecolorsforindex($im, $rgb_index);
echo '<div class="box" style="background:rgb(' . $rgb['red'] . ',' . $rgb['green'] . ',' . $rgb['blue'] . ')">'
. number_format($total_density) . '<br>'
. sprintf('#%02s%02s%02s', base_convert($rgb['red'], 10, 16), base_convert($rgb['green'], 10, 16), base_convert($rgb['blue'], 10, 16)) . '<br>'
. '</div>';
break;
}
*/
}
#echo '<div class="clear"></div>';
$hues_max = max($hues);
$hues_avg = round( array_sum($hues) / count($hues) );
$hues_median = $this->median($hues);
dump(number_format($hues_median));
dump(number_format($hues_avg));
$hues1 = $hues;
foreach ($hues1 as $i => $hue) if ($hues[$i]<$hues_median) unset($hues1[$i]);
arsort($hues1);
$hues1 = array_slice($hues1, 0, 90, true);
ksort($hues1);
#$hues = $hues1;
$hues1_keys = array_keys($hues1);
$hues1 = array_values($hues1);
$hues1_count = count($hues1);
for ($i=0; $i < $hues1_count; $i++) /*if ($hues[$i]>$hues_median)*/
{
$j = $hues1_keys[$i];
if ($palette[$j])
{
$rgb_index = key($palette[$j]);
$rgb = imagecolorsforindex($im, $rgb_index);
$hsv = $this->rgb2hsv($rgb);
}
else
{
$rgb = array();
}
if ($hsv[2]>10
and $hues1[$i]>$hues1[$i-1] and $hues1[$i]>$hues1[$i+1]
and $hues1[$i]>$hues1[$i-2] and $hues1[$i]>$hues1[$i+2]
and $hues1[$i]>$hues1[$i-3] and $hues1[$i]>$hues1[$i+3]
and $hues1[$i]>$hues1[$i-4] and $hues1[$i]>$hues1[$i+4]
and $hues1[$i]>$hues1[$i-5] and $hues1[$i]>$hues1[$i+5]
and $hues1[$i]>$hues1[$i-6] and $hues1[$i]>$hues1[$i+6]
and $hues1[$i]>$hues1[$i-7] and $hues1[$i]>$hues1[$i+7]
/*and $hues1[$i]>$hues1[$i-8] and $hues1[$i]>$hues1[$i+8]
and $hues1[$i]>$hues1[$i-9] and $hues1[$i]>$hues1[$i+9]*/)
{
echo '<div class="box" style="' . ($hues1[$i]<$hues_avg ? 'margin-top:15px;' : '') . 'background:rgb(' . (int)$rgb['red'] . ',' . (int)$rgb['green'] . ',' . (int)$rgb['blue'] . ')">'
. number_format($i) . '/' . $hsv[1] . '/' . $hsv[2] . '<br>'
. sprintf('#%02s%02s%02s', base_convert($rgb['red'], 10, 16), base_convert($rgb['green'], 10, 16), base_convert($rgb['blue'], 10, 16))
. '</div>';
}
}
echo '<div class="clear"></div>';
echo '<img src="/' . $src . '">';
echo '<div class="clear"></div>';
echo '<div class="margin" style="margin-top:' . round($hues_avg/$hues_max*100*3) . 'px"></div>';
for ($i=0; $i < 360; $i++)
{
if ($palette[$i])
{
$rgb_index = key($palette[$i]);
$rgb = imagecolorsforindex($im, $rgb_index);
}
else
{
$rgb = array();
}
echo '<div class="line" style="height:' . round($hues[$i]/$hues_max*100*3+1) . 'px;background:rgb(' . (int)$rgb['red'] . ',' . (int)$rgb['green'] . ',' . (int)$rgb['blue'] . ')"></div>';
}
echo '<div class="clear"></div>';
echo <<<EOF
<style type="text/css" media="screen">
.margin {
position:absolute;
height:1px;
width:1440px;
background:rgba(255, 0, 0, .35);
}
.line {
width:3px;
background:#000;
margin-right:1px;
float:left
}
.box {
width:59px;
height:43px;
padding-top:16px;
margin:0 1px 1px 0;
text-align:center;
font:10px/13px tahoma;
color:#000;
float:left;
}
.clear {
clear:both;
height:9px;
}
</style>
EOF;
return false;
}
function rgb2hsv($rgb)
{
$r = $rgb['red'];
$g = $rgb['green'];
$b = $rgb['blue'];
$minVal = min($r, $g, $b);
$maxVal = max($r, $g, $b);
$delta = $maxVal - $minVal;
$v = $maxVal / 255;
if ($delta == 0)
{
$h = 0;
$s = 0;
} else {
$s = $delta / $maxVal;
$del_R = ((($maxVal - $r) / 6) + ($delta / 2)) / $delta;
$del_G = ((($maxVal - $g) / 6) + ($delta / 2)) / $delta;
$del_B = ((($maxVal - $b) / 6) + ($delta / 2)) / $delta;
if ($r == $maxVal)
{
$h = $del_B - $del_G;
}
else if ($g == $maxVal)
{
$h = (1 / 3) + $del_R - $del_B;
}
else if ($b == $maxVal)
{
$h = (2 / 3) + $del_G - $del_R;
}
if ($h < 0)
{
$h++;
}
if ($h > 1)
{
$h--;
}
}
$h = round($h * 360);
$s = round($s * 100);
$v = round($v * 100);
return array($h, $s, $v);
}
function median()
{
$args = func_get_args();
switch(func_num_args())
{
case 0:
trigger_error('median() requires at least one parameter',E_USER_WARNING);
return false;
break;
case 1:
$args = array_pop($args);
// fallthrough
default:
if(!is_array($args))
{
trigger_error('median() requires a list of numbers to operate on or an array of numbers',E_USER_NOTICE);
return false;
}
sort($args);
$n = count($args);
$h = intval($n / 2);
if($n % 2 == 0)
{
$median = ($args[$h] + $args[$h-1]) / 2;
}
else
{
$median = $args[$h];
}
break;
}
return $median;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment