colors.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. if (strlen($hex) == 4) {
  197. $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
  198. } $c = hexdec($hex);
  199. for ($i = 16; $i >= 0; $i -= 8) {
  200. $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
  201. } return $out;
  202. }
  203. ### Convert an RGB triplet to a hex color.
  204. function _color_pack($rgb, $normalize = false) {
  205. foreach ($rgb as $k => $v) {
  206. $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
  207. }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
  208. }
  209. function rgb2hsl($arr) {
  210. $r = $arr[0];
  211. $g = $arr[1];
  212. $b = $arr[2];
  213. $var_R = ($r / 255);
  214. $var_G = ($g / 255);
  215. $var_B = ($b / 255);
  216. $var_Min = min($var_R, $var_G, $var_B);
  217. $var_Max = max($var_R, $var_G, $var_B);
  218. $del_Max = $var_Max - $var_Min;
  219. $v = $var_Max;
  220. if ($del_Max == 0) {
  221. $h = 0;
  222. $s = 0;
  223. } else {
  224. $s = $del_Max / $var_Max;
  225. $del_R = ((($var_Max - $var_R ) / 6 ) + ($del_Max / 2 ) ) / $del_Max;
  226. $del_G = ((($var_Max - $var_G ) / 6 ) + ($del_Max / 2 ) ) / $del_Max;
  227. $del_B = ((($var_Max - $var_B ) / 6 ) + ($del_Max / 2 ) ) / $del_Max;
  228. if ($var_R == $var_Max) $h = $del_B - $del_G;
  229. else if ($var_G == $var_Max) $h = (1 / 3 ) + $del_R - $del_B;
  230. else if ($var_B == $var_Max) $h = (2 / 3 ) + $del_G - $del_R;
  231. if ($h < 0) $h++;
  232. if ($h > 1) $h--;
  233. }
  234. return array($h, $s, $v);
  235. }
  236. function hsl2rgb($arr) {
  237. $h = $arr[0];
  238. $s = $arr[1];
  239. $v = $arr[2];
  240. if($s == 0) {
  241. $r = $g = $B = $v * 255;
  242. } else {
  243. $var_H = $h * 6;
  244. $var_i = floor($var_H );
  245. $var_1 = $v * (1 - $s );
  246. $var_2 = $v * (1 - $s * ($var_H - $var_i ) );
  247. $var_3 = $v * (1 - $s * (1 - ($var_H - $var_i ) ) );
  248. if ($var_i == 0) { $var_R = $v ; $var_G = $var_3 ; $var_B = $var_1 ; }
  249. else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $v ; $var_B = $var_1 ; }
  250. else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $v ; $var_B = $var_3 ; }
  251. else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $v ; }
  252. else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $v ; }
  253. else { $var_R = $v ; $var_G = $var_1 ; $var_B = $var_2 ; }
  254. $r = $var_R * 255;
  255. $g = $var_G * 255;
  256. $B = $var_B * 255;
  257. }
  258. return array($r, $g, $B);
  259. }
  260. function colorPalette($imageFile, $numColors, $granularity = 5) {
  261. $granularity = max(1, abs((int)$granularity));
  262. $colors = array();
  263. $size = @getimagesize($imageFile);
  264. // to enable .ico support place floIcon.php into lib/
  265. if (strtolower($size['mime']) == 'image/vnd.microsoft.icon') {
  266. if (class_exists("floIcon")) {
  267. $ico = new floIcon();
  268. @$ico->readICO($imageFile);
  269. if(count($ico->images)==0)
  270. return false;
  271. else
  272. $img = @$ico->images[count($ico->images)-1]->getImageResource();
  273. } else {
  274. return false;
  275. }
  276. } else if ($size[0] > 0 && $size[1] > 0) {
  277. $img = @imagecreatefromstring(file_get_contents($imageFile));
  278. }
  279. if (!$img) return false;
  280. for($x = 0; $x < $size[0]; $x += $granularity) {
  281. for($y = 0; $y < $size[1]; $y += $granularity) {
  282. $thisColor = imagecolorat($img, $x, $y);
  283. $rgb = imagecolorsforindex($img, $thisColor);
  284. $red = round(round(($rgb['red'] / 0x33)) * 0x33);
  285. $green = round(round(($rgb['green'] / 0x33)) * 0x33);
  286. $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
  287. $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
  288. if(array_key_exists($thisRGB, $colors)) {
  289. $colors[$thisRGB]++;
  290. } else{
  291. $colors[$thisRGB] = 1;
  292. }
  293. }
  294. }
  295. arsort($colors);
  296. return array_slice(array_keys($colors), 0, $numColors);
  297. }
  298. function calculate_avg_color($iconFile) {
  299. $palette = colorPalette($iconFile, 4, 4);
  300. if (is_array($palette)) {
  301. foreach ($palette as $p) {
  302. $hsl = rgb2hsl(_color_unpack("#$p"));
  303. if ($hsl[1] > 0.25 && $hsl[2] > 0.25 &&
  304. !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[1] < 0.01) &&
  305. !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[2] > 0.99)) {
  306. return _color_pack(hsl2rgb($hsl));
  307. }
  308. }
  309. }
  310. return '';
  311. }