function GetImagePalette($imagepath, $post_id, $range = 10) { $command = 'convert ' . $imagepath . ' -colors ' . $range . ' +dither -format %c histogram:info: &'; exec($command, $colors, $exit); $palette = array(); foreach ($colors as $color) { preg_match('/( {2,})(\\d+:) (\\(.+\\)) (#.{6}) (.+)/i', $color, $matches); if (isset($matches[2])) { @($PIXELS = (int) substr($matches[2], 0, strlen($matches[2]) - 1)); @($PERCENT = $PIXELS / 120000 * 100); @($HEX = $matches[4]); @($CLEAN_RGBA = str_replace(' ', '', substr($matches[3], 1, strlen($matches[3]) - 2))); // var_dump($CLEAN_RGBA); $P_RGBA = explode(',', $CLEAN_RGBA); $R = $P_RGBA[0]; $G = $P_RGBA[1]; $B = $P_RGBA[2]; if (isset($P_RGBA[3])) { $A = $P_RGBA[3]; } else { $A = null; } $RGBA = array('R' => $R, 'G' => $G, 'B' => $B, 'A' => $A); $RGB = array('R' => $R, 'G' => $G, 'B' => $B); $HSL = RGBToHSL($RGB); $XYZ = RGBToXYZ(array('R' => $RGBA['R'], 'G' => $RGBA['G'], 'B' => $RGBA['A'])); $LAB = XYZToLAB($XYZ); $COLOR_DATA = array('palette_post_id' => $post_id, 'PERCENT' => $PERCENT, 'HEX' => $HEX, 'RGBA_R' => $RGBA['R'], 'RGBA_G' => $RGBA['G'], 'RGBA_B' => $RGBA['B'], 'RGBA_A' => $RGBA['A'], 'HSL_H' => $HSL['H'], 'HSL_S' => $HSL['S'], 'HSL_L' => $HSL['L'], 'LAB_L' => $LAB['L'], 'LAB_A' => $LAB['A'], 'LAB_B' => $LAB['B']); if ($PERCENT > 0.2) { array_push($palette, $COLOR_DATA); } } } return $palette; }
public function im_get() { $dir = "/home/pedro/work/tribble/data/b5e0eaebec229148d61d1881b27d1865e1bb5003/"; $url = "data/b5e0eaebec229148d61d1881b27d1865e1bb5003/"; $imagename = $this->get('f'); $command = 'convert ' . $dir . $imagename . ' -colors 10 -format %c histogram:info: &'; // var_dump($command); exec($command, $colors, $exit); // var_dump($out); $histogram = array(); $hue_matrix = array(); $interval = 0.05; if ($exit == 0) { foreach ($colors as $color) { preg_match('/( {2,})(\\d+:) (\\(.+\\)) (#.{6}) (.+)/i', $color, $matches); if (isset($matches[2])) { @($PIXELS = (int) substr($matches[2], 0, strlen($matches[2]) - 1)); @($PERCENT = $PIXELS / 120000 * 100); @($HEX = $matches[4]); @($CLEAN_RGBA = str_replace(' ', '', substr($matches[3], 1, strlen($matches[3]) - 2))); // var_dump($matches[5]); // var_dump($CLEAN_RGBA); @(list($R, $G, $B, $A) = explode(',', $CLEAN_RGBA)); @($RGBA = array('R' => $R, 'G' => $G, 'B' => $B, 'A' => $A)); $HSV = RGBToHSV($RGBA['R'], $RGBA['G'], $RGBA['B']); $XYZ = RGBToXYZ(array('R' => $RGBA['R'], 'G' => $RGBA['G'], 'B' => $RGBA['B'])); $LAB = XYZToLAB($XYZ); $COLOR_DATA = array('PERCENTAGE' => $PERCENT, 'PIXEL_COUNT' => $PIXELS, 'HEX' => $HEX, 'RGBA' => $RGBA, 'HSV' => $HSV, 'LAB' => $LAB); if ($PERCENT > 0.68) { @array_push($histogram, $COLOR_DATA); } } } foreach ($histogram as $HC) { $hue = 0; while ($hue < 1) { if ($HC['HSV']['H'] >= $hue && $HC['HSV']['H'] < $hue + $interval) { // echo 'Hue: '.$hue.' - color: '.$COLOR_DATA['HSV']['H'].'<br>'; $hue_matrix[(string) $hue][] = $HC; } $hue += $interval; } } // var_dump($hue_matrix); ksort($hue_matrix); foreach ($hue_matrix as $hue_interval => $colors) { echo "<h3>Hue: " . $hue_interval . " to " . ($hue_interval + $interval) . " - " . count($colors) . " colors</h3>"; krsort($colors); foreach ($colors as $color) { if ($color['HSV']['V'] < 0.5 && $color['HSV']['S'] <= 1) { $text = "#fff"; } else { $text = "#000"; } echo '<div style="font-family: sans-serif; padding: 10px; color: ' . $text . '; background-color: ' . $color['HEX'] . '">'; echo 'PERGENTAGE: ' . $color['PERCENTAGE'] . '% - H: ' . $color['HSV']['H'] . ' S: ' . $color['HSV']['S'] . ' V: ' . $color['HSV']['V']; echo '</div>'; // var_dump($color['HEX']); } } // arsort($histogram); echo "<style>pre, h3, hr {clear: both;} p {font-size: 12px; text-align: center; line-height: 36px; margin: 0; }</style>"; echo '<img src="http://10.134.132.97:8082/' . $url . $imagename . '">'; echo "<hr>"; echo "<h3>Palette colors: " . count($histogram) . "</h3>"; ksort($histogram); foreach ($histogram as $color) { if ($color['HSV']['V'] < 0.8 && $color['HSV']['S'] <= 1) { $text = "#fff"; } else { $text = "#000"; } $display = '<div style="float: left; color: ' . $text . '; margin: 1px;">'; $display .= '<div style="width: 150px; height: 150px; background-color: ' . $color['HEX'] . '">'; $display .= '<p>' . round($color['PERCENTAGE'], 5) . '%</p>'; $display .= '<p>H: ' . $color['HSV']['H'] . '</p>'; $display .= '<p>S: ' . $color['HSV']['S'] . '</p>'; $display .= '<p>V: ' . $color['HSV']['V'] . '</p>'; $display .= '</div></div>'; echo $display; } } }
function colorSearch($HEX, $VARIATION = 40, $COVERAGE = 10) { if (strlen($HEX) != 6) { // exit('VAI MEXER NO CARALHO!'); } $RGB = HEXToRGB($HEX); $HSL = RGBToHSL($RGB); $XYZ = RGBToXYZ($RGB); $LAB1 = XYZToLAB($XYZ); // CHECK IF HUE IS NEAR 0 - RED $HSL['H'] >= 350 || $HSL['H'] <= 20 ? $RED = true : ($RED = false); // CHECK IF HUE IS NEAR 0 - RED $HSL['H'] >= 21 && $HSL['H'] <= 70 ? $YELLOW = true : ($YELLOW = false); // CHECK IF HUE IS NEAR 120 - GREEN $HSL['H'] >= 71 && $HSL['H'] < 165 ? $GREEN = true : ($GREEN = false); // CHECK IF HUE IS NEAR 240 - BLUE $HSL['H'] >= 1166 && $HSL['H'] < 350 ? $BLUE = true : ($BLUE = false); $H_VARIATION = $VARIATION * 360 / 100 / 20; $L_VARIATION = $VARIATION / 1.2; $S_VARIATION = $VARIATION * 3.5; if ($RED) { $H_VARIATION = $VARIATION * 360 / 100 / 30; $L_VARIATION = $VARIATION * 1.2; $S_VARIATION = $VARIATION / 1.1; } elseif ($YELLOW) { $H_VARIATION = $VARIATION * 360 / 100 / 80; $L_VARIATION = $VARIATION / 1; $S_VARIATION = $VARIATION * 1; } elseif ($GREEN) { $H_VARIATION = $VARIATION * 360 / 100 / 20; $L_VARIATION = $VARIATION / 1; $S_VARIATION = $VARIATION * 1; } elseif ($BLUE) { $H_VARIATION = $VARIATION * 360 / 100 / 20; $L_VARIATION = $VARIATION / 1.2; $S_VARIATION = $VARIATION * 3.5; } $DELTA_VARIATION = 80 / 100 * $VARIATION; $HB = $HSL['H'] - $H_VARIATION * 3.5; $HT = $HSL['H'] + $H_VARIATION * 3; $HB < 0 ? $H_BOTTOM = 360 + $HB : ($H_BOTTOM = $HB); $HT > 360 ? @($H_TOP += $HT - 360) : ($H_TOP = $HT); $DELTA = 0; if ($H_BOTTOM > $H_TOP) { $DELTA = 360 - $H_BOTTOM; $H_BOTTOM = 0; $H_TOP = $H_TOP + $DELTA; } $SB = $HSL['S'] - $S_VARIATION; $ST = $HSL['S'] + $S_VARIATION; $SB < 0 ? $S_BOTTOM = 0 : ($S_BOTTOM = $SB); $ST > 100 ? $S_TOP = 100 : ($S_TOP = $ST); $LB = $HSL['L'] - $L_VARIATION; $LT = $HSL['L'] + $L_VARIATION; $LB < 0 ? $L_BOTTOM = 0 : ($L_BOTTOM = $LB); $LT > 100 ? $L_TOP = 100 : ($L_TOP = $LT); // check if the search color is grey(ish) $HSL['S'] < 3 ? $is_grey = true : ($is_grey = false); $this->db->select('tr_palette.palette_post_id, HEX, PERCENT, HSL_H, HSL_S, HSL_L, LAB_L as L, LAB_A as A, LAB_B as B'); $this->db->distinct(); $this->db->from('palette'); $this->db->join('post', 'tr_post.post_id = tr_palette.palette_post_id', 'inner'); $this->db->where('tr_palette.HSL_H + ' . $DELTA . ' BETWEEN ' . $H_BOTTOM . ' AND ' . $H_TOP); $this->db->where('tr_palette.HSL_S BETWEEN ' . $S_BOTTOM . ' AND ' . $S_TOP); $this->db->where('tr_palette.HSL_L BETWEEN ' . $L_BOTTOM . ' AND ' . $L_TOP); $this->db->where('tr_palette.PERCENT > ' . $COVERAGE); $this->db->where(array('tr_post.post_is_deleted' => 0)); $this->db->group_by('post_id'); $query = $this->db->get(); $COLORS = $query->result(); if (count($COLORS) == 0) { return 0; } $GREY_FILTERED_COLORS = array(); if (!$is_grey) { foreach ($COLORS as $COLOR) { if ($COLOR->HSL_H != 0 && $COLOR->HSL_S != 0 && $COLOR->HSL_S > 15 && $COLOR->HSL_L > 15) { // var_dump($COLOR); array_push($GREY_FILTERED_COLORS, $COLOR); } } } else { foreach ($COLORS as $COLOR) { if ($COLOR->HSL_S == 0 && $COLOR->HSL_S < 3) { // var_dump($COLOR); array_push($GREY_FILTERED_COLORS, $COLOR); } } } return array('post_count' => count($GREY_FILTERED_COLORS), 'posts' => $GREY_FILTERED_COLORS, 'HSL' => $HSL); }