colors.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. require_once "lib/floIcon.php";
  3. function _resolve_htmlcolor($color) {
  4. $htmlcolors = array ("aliceblue" => "#f0f8ff",
  5. "antiquewhite" => "#faebd7",
  6. "aqua" => "#00ffff",
  7. "aquamarine" => "#7fffd4",
  8. "azure" => "#f0ffff",
  9. "beige" => "#f5f5dc",
  10. "bisque" => "#ffe4c4",
  11. "black" => "#000000",
  12. "blanchedalmond" => "#ffebcd",
  13. "blue" => "#0000ff",
  14. "blueviolet" => "#8a2be2",
  15. "brown" => "#a52a2a",
  16. "burlywood" => "#deb887",
  17. "cadetblue" => "#5f9ea0",
  18. "chartreuse" => "#7fff00",
  19. "chocolate" => "#d2691e",
  20. "coral" => "#ff7f50",
  21. "cornflowerblue" => "#6495ed",
  22. "cornsilk" => "#fff8dc",
  23. "crimson" => "#dc143c",
  24. "cyan" => "#00ffff",
  25. "darkblue" => "#00008b",
  26. "darkcyan" => "#008b8b",
  27. "darkgoldenrod" => "#b8860b",
  28. "darkgray" => "#a9a9a9",
  29. "darkgrey" => "#a9a9a9",
  30. "darkgreen" => "#006400",
  31. "darkkhaki" => "#bdb76b",
  32. "darkmagenta" => "#8b008b",
  33. "darkolivegreen" => "#556b2f",
  34. "darkorange" => "#ff8c00",
  35. "darkorchid" => "#9932cc",
  36. "darkred" => "#8b0000",
  37. "darksalmon" => "#e9967a",
  38. "darkseagreen" => "#8fbc8f",
  39. "darkslateblue" => "#483d8b",
  40. "darkslategray" => "#2f4f4f",
  41. "darkslategrey" => "#2f4f4f",
  42. "darkturquoise" => "#00ced1",
  43. "darkviolet" => "#9400d3",
  44. "deeppink" => "#ff1493",
  45. "deepskyblue" => "#00bfff",
  46. "dimgray" => "#696969",
  47. "dimgrey" => "#696969",
  48. "dodgerblue" => "#1e90ff",
  49. "firebrick" => "#b22222",
  50. "floralwhite" => "#fffaf0",
  51. "forestgreen" => "#228b22",
  52. "fuchsia" => "#ff00ff",
  53. "gainsboro" => "#dcdcdc",
  54. "ghostwhite" => "#f8f8ff",
  55. "gold" => "#ffd700",
  56. "goldenrod" => "#daa520",
  57. "gray" => "#808080",
  58. "grey" => "#808080",
  59. "green" => "#008000",
  60. "greenyellow" => "#adff2f",
  61. "honeydew" => "#f0fff0",
  62. "hotpink" => "#ff69b4",
  63. "indianred " => "#cd5c5c",
  64. "indigo " => "#4b0082",
  65. "ivory" => "#fffff0",
  66. "khaki" => "#f0e68c",
  67. "lavender" => "#e6e6fa",
  68. "lavenderblush" => "#fff0f5",
  69. "lawngreen" => "#7cfc00",
  70. "lemonchiffon" => "#fffacd",
  71. "lightblue" => "#add8e6",
  72. "lightcoral" => "#f08080",
  73. "lightcyan" => "#e0ffff",
  74. "lightgoldenrodyellow" => "#fafad2",
  75. "lightgray" => "#d3d3d3",
  76. "lightgrey" => "#d3d3d3",
  77. "lightgreen" => "#90ee90",
  78. "lightpink" => "#ffb6c1",
  79. "lightsalmon" => "#ffa07a",
  80. "lightseagreen" => "#20b2aa",
  81. "lightskyblue" => "#87cefa",
  82. "lightslategray" => "#778899",
  83. "lightslategrey" => "#778899",
  84. "lightsteelblue" => "#b0c4de",
  85. "lightyellow" => "#ffffe0",
  86. "lime" => "#00ff00",
  87. "limegreen" => "#32cd32",
  88. "linen" => "#faf0e6",
  89. "magenta" => "#ff00ff",
  90. "maroon" => "#800000",
  91. "mediumaquamarine" => "#66cdaa",
  92. "mediumblue" => "#0000cd",
  93. "mediumorchid" => "#ba55d3",
  94. "mediumpurple" => "#9370db",
  95. "mediumseagreen" => "#3cb371",
  96. "mediumslateblue" => "#7b68ee",
  97. "mediumspringgreen" => "#00fa9a",
  98. "mediumturquoise" => "#48d1cc",
  99. "mediumvioletred" => "#c71585",
  100. "midnightblue" => "#191970",
  101. "mintcream" => "#f5fffa",
  102. "mistyrose" => "#ffe4e1",
  103. "moccasin" => "#ffe4b5",
  104. "navajowhite" => "#ffdead",
  105. "navy" => "#000080",
  106. "oldlace" => "#fdf5e6",
  107. "olive" => "#808000",
  108. "olivedrab" => "#6b8e23",
  109. "orange" => "#ffa500",
  110. "orangered" => "#ff4500",
  111. "orchid" => "#da70d6",
  112. "palegoldenrod" => "#eee8aa",
  113. "palegreen" => "#98fb98",
  114. "paleturquoise" => "#afeeee",
  115. "palevioletred" => "#db7093",
  116. "papayawhip" => "#ffefd5",
  117. "peachpuff" => "#ffdab9",
  118. "peru" => "#cd853f",
  119. "pink" => "#ffc0cb",
  120. "plum" => "#dda0dd",
  121. "powderblue" => "#b0e0e6",
  122. "purple" => "#800080",
  123. "red" => "#ff0000",
  124. "rosybrown" => "#bc8f8f",
  125. "royalblue" => "#4169e1",
  126. "saddlebrown" => "#8b4513",
  127. "salmon" => "#fa8072",
  128. "sandybrown" => "#f4a460",
  129. "seagreen" => "#2e8b57",
  130. "seashell" => "#fff5ee",
  131. "sienna" => "#a0522d",
  132. "silver" => "#c0c0c0",
  133. "skyblue" => "#87ceeb",
  134. "slateblue" => "#6a5acd",
  135. "slategray" => "#708090",
  136. "slategrey" => "#708090",
  137. "snow" => "#fffafa",
  138. "springgreen" => "#00ff7f",
  139. "steelblue" => "#4682b4",
  140. "tan" => "#d2b48c",
  141. "teal" => "#008080",
  142. "thistle" => "#d8bfd8",
  143. "tomato" => "#ff6347",
  144. "turquoise" => "#40e0d0",
  145. "violet" => "#ee82ee",
  146. "wheat" => "#f5deb3",
  147. "white" => "#ffffff",
  148. "whitesmoke" => "#f5f5f5",
  149. "yellow" => "#ffff00",
  150. "yellowgreen" => "#9acd32");
  151. $color = strtolower($color);
  152. if (isset($htmlcolors[$color]))
  153. return $htmlcolors[$color];
  154. else
  155. return $color;
  156. }
  157. ### RGB >> HSL
  158. function _color_rgb2hsl($rgb) {
  159. $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
  160. $min = min($r, min($g, $b)); $max = max($r, max($g, $b));
  161. $delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
  162. if ($l > 0 && $l < 1) {
  163. $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
  164. }
  165. $h = 0;
  166. if ($delta > 0) {
  167. if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
  168. if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
  169. if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
  170. $h /= 6;
  171. } return array($h, $s, $l);
  172. }
  173. ### HSL >> RGB
  174. function _color_hsl2rgb($hsl) {
  175. $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
  176. $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
  177. $m1 = $l * 2 - $m2;
  178. return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
  179. _color_hue2rgb($m1, $m2, $h),
  180. _color_hue2rgb($m1, $m2, $h - 0.33333));
  181. }
  182. ### Helper function for _color_hsl2rgb().
  183. function _color_hue2rgb($m1, $m2, $h) {
  184. $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
  185. if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
  186. if ($h * 2 < 1) return $m2;
  187. if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
  188. return $m1;
  189. }
  190. ### Convert a hex color into an RGB triplet.
  191. function _color_unpack($hex, $normalize = false) {
  192. if (strpos($hex, '#') !== 0)
  193. $hex = _resolve_htmlcolor($hex);
  194. if (strlen($hex) == 4) {
  195. $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
  196. } $c = hexdec($hex);
  197. for ($i = 16; $i >= 0; $i -= 8) {
  198. $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
  199. } return $out;
  200. }
  201. ### Convert an RGB triplet to a hex color.
  202. function _color_pack($rgb, $normalize = false) {
  203. foreach ($rgb as $k => $v) {
  204. $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
  205. }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
  206. }
  207. function rgb2hsl($arr) {
  208. $r = $arr[0];
  209. $g = $arr[1];
  210. $b = $arr[2];
  211. $var_R = ($r / 255);
  212. $var_G = ($g / 255);
  213. $var_B = ($b / 255);
  214. $var_Min = min($var_R, $var_G, $var_B);
  215. $var_Max = max($var_R, $var_G, $var_B);
  216. $del_Max = $var_Max - $var_Min;
  217. $v = $var_Max;
  218. if ($del_Max == 0) {
  219. $h = 0;
  220. $s = 0;
  221. } else {
  222. $s = $del_Max / $var_Max;
  223. $del_R = ( ( ( $max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
  224. $del_G = ( ( ( $max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
  225. $del_B = ( ( ( $max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
  226. if ($var_R == $var_Max) $h = $del_B - $del_G;
  227. else if ($var_G == $var_Max) $h = ( 1 / 3 ) + $del_R - $del_B;
  228. else if ($var_B == $var_Max) $h = ( 2 / 3 ) + $del_G - $del_R;
  229. if ($H < 0) $h++;
  230. if ($H > 1) $h--;
  231. }
  232. return array($h, $s, $v);
  233. }
  234. function hsl2rgb($arr) {
  235. $h = $arr[0];
  236. $s = $arr[1];
  237. $v = $arr[2];
  238. if($s == 0) {
  239. $r = $g = $B = $v * 255;
  240. } else {
  241. $var_H = $h * 6;
  242. $var_i = floor( $var_H );
  243. $var_1 = $v * ( 1 - $s );
  244. $var_2 = $v * ( 1 - $s * ( $var_H - $var_i ) );
  245. $var_3 = $v * ( 1 - $s * (1 - ( $var_H - $var_i ) ) );
  246. if ($var_i == 0) { $var_R = $v ; $var_G = $var_3 ; $var_B = $var_1 ; }
  247. else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $v ; $var_B = $var_1 ; }
  248. else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $v ; $var_B = $var_3 ; }
  249. else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $v ; }
  250. else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $v ; }
  251. else { $var_R = $v ; $var_G = $var_1 ; $var_B = $var_2 ; }
  252. $r = $var_R * 255;
  253. $g = $var_G * 255;
  254. $B = $var_B * 255;
  255. }
  256. return array($r, $g, $B);
  257. }
  258. function colorPalette($imageFile, $numColors, $granularity = 5) {
  259. $granularity = max(1, abs((int)$granularity));
  260. $colors = array();
  261. $size = @getimagesize($imageFile);
  262. if (strtolower($size['mime']) == 'image/vnd.microsoft.icon') {
  263. $ico = new floIcon();
  264. @$ico->readICO($imageFile);
  265. if(count($ico->images)==0)
  266. return null;
  267. else
  268. $img = @$ico->images[count($ico->images)-1]->getImageResource();
  269. } else {
  270. $img = @imagecreatefromstring(file_get_contents($imageFile));
  271. }
  272. if (!$img) return false;
  273. for($x = 0; $x < $size[0]; $x += $granularity) {
  274. for($y = 0; $y < $size[1]; $y += $granularity) {
  275. $thisColor = imagecolorat($img, $x, $y);
  276. $rgb = imagecolorsforindex($img, $thisColor);
  277. $red = round(round(($rgb['red'] / 0x33)) * 0x33);
  278. $green = round(round(($rgb['green'] / 0x33)) * 0x33);
  279. $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
  280. $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
  281. if(array_key_exists($thisRGB, $colors)) {
  282. $colors[$thisRGB]++;
  283. } else{
  284. $colors[$thisRGB] = 1;
  285. }
  286. }
  287. }
  288. arsort($colors);
  289. return array_slice(array_keys($colors), 0, $numColors);
  290. }
  291. function calculate_avg_color($iconFile) {
  292. $palette = colorPalette($iconFile, 4, 4);
  293. if (is_array($palette)) {
  294. foreach ($palette as $p) {
  295. $hsl = rgb2hsl(_color_unpack("#$p"));
  296. if ($hsl[1] > 0.25 && $hsl[2] > 0.25 &&
  297. !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[1] < 0.01) &&
  298. !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[2] > 0.99)) {
  299. return _color_pack(hsl2rgb($hsl));
  300. }
  301. }
  302. }
  303. return '';
  304. }
  305. ?>