Marginal refactoring in AR->_findEvery().
[akelos.git] / lib / AkImage / AkImageColorScheme.php
blob58044b8977a5c6fdbf3ec6a8dc128667ce89aaa3
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActiveSupport
13 * @subpackage ImageManipulation
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
19 require_once(AK_LIB_DIR.DS.'AkImage.php');
21 class AkImageColorScheme extends AkObject
23 var $number_of_colors = 12;
24 var $calculate_negatives = true;
25 var $minimum_hits_for_negative = 50;
26 var $Image;
27 var $_tmp_file;
28 var $_frequentColors = array();
30 function setImage($image_path)
32 $this->Image =& new AkImage($image_path);
33 $this->Image->transform('resize',array('size'=>'24x24'));
34 $this->_tmp_file = AK_TMP_DIR.DS.'__AkImageColorScheme_'.Ak::randomString(32).'.jpg';
35 $this->Image->save($this->_tmp_file);
38 function __destruct()
40 if(file_exists($this->_tmp_file)){
41 @Ak::file_delete($this->_tmp_file);
45 function getColorScheme($number_of_colors = null)
47 $colors = array();
48 if($image = @imagecreatefromjpeg($this->_tmp_file)){
49 $imgage_width = $this->Image->Transform->new_x;
50 $imgage_height = $this->Image->Transform->new_y;
51 $inverted_colors = array();
52 for ($y=0; $y < $imgage_height; $y++){
53 for ($x=0; $x < $imgage_width; $x++){
54 $index = imagecolorat($image, $x, $y);
55 $image_colors = imagecolorsforindex($image, $index);
56 $hex = '';
57 foreach ($image_colors as $color=>$value){
58 $image_colors[$color] = intval((($image_colors[$color])+15)/32)*32;
59 $image_colors[$color] = $image_colors[$color] >= 256 ? 240 : $image_colors[$color];
60 $hex .= substr('0'.dechex($image_colors[$color]), -2);
62 $hex = substr($hex, 0, 6);
63 if(strlen($hex) == 6){
64 $colors[$hex] = empty($colors[$hex]) ? 1 : $colors[$hex]+1;
65 $this->_addToFrequentColors($hex);
66 if($this->calculate_negatives && $colors[$hex] > $this->minimum_hits_for_negative){
67 $negative = $this->_getNegativeAsHex($image_colors['red'], $image_colors['green'], $image_colors['blue']);
68 $colors[$negative] = empty($colors[$negative]) ? 1 : $colors[$negative]+1;
69 $this->_addToFrequentColors($negative);
76 return $this->_getColorsFromCounterColorArray($colors, $number_of_colors);
79 function _getColorsFromCounterColorArray($colors_array, $number_of_colors = null)
81 $number_of_colors = empty($number_of_colors) ? $this->number_of_colors : $number_of_colors;
82 asort($colors_array);
83 $colors_array = array_slice(array_unique(array_keys(array_reverse($colors_array, true))), 0, $number_of_colors);
84 natsort($colors_array);
85 return $colors_array;
88 function _getNegativeAsHex($red, $green, $blue)
90 $rgb = $red*0.15 + $green*0.5 + $blue * 0.35;
91 return $this->_rgbToHex(array(255-$rgb, 255-$rgb, 255-$rgb));
94 function _addToFrequentColors($hex_color)
96 $this->_frequentColors[$hex_color] = empty($this->_frequentColors[$hex_color]) ? 1 : $this->_frequentColors[$hex_color]+1;
99 function resetFrequentColors()
101 $this->_frequentColors = array();
104 function getFrequentColors($number_of_colors = null)
106 return $this->_getColorsFromCounterColorArray($this->_frequentColors, $number_of_colors);
109 function _rgbToHex($rgb)
111 $r = str_pad(dechex($rgb[0]), 2, '0', STR_PAD_LEFT);
112 $g = str_pad(dechex($rgb[1]), 2, '0', STR_PAD_LEFT);
113 $b = str_pad(dechex($rgb[2]), 2, '0', STR_PAD_LEFT);
114 return $r.$g.$b;