colors.php 9.7 KB

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