]> git.wh0rd.org - tt-rss.git/blob - include/colors.php
Revert "_DISABLE_FLOICON -> _ENABLE_FLOICON"
[tt-rss.git] / include / colors.php
1 <?php
2
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
154 $color = strtolower($color);
155
156 if (isset($htmlcolors[$color]))
157 return $htmlcolors[$color];
158 else
159 return $color;
160 }
161
162 ### RGB >> HSL
163 function _color_rgb2hsl($rgb) {
164 $r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
165 $min = min($r, min($g, $b)); $max = max($r, max($g, $b));
166 $delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
167 if ($l > 0 && $l < 1) {
168 $s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
169 }
170 $h = 0;
171 if ($delta > 0) {
172 if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
173 if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
174 if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
175 $h /= 6;
176 } return array($h, $s, $l);
177 }
178
179 ### HSL >> RGB
180 function _color_hsl2rgb($hsl) {
181 $h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
182 $m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
183 $m1 = $l * 2 - $m2;
184 return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
185 _color_hue2rgb($m1, $m2, $h),
186 _color_hue2rgb($m1, $m2, $h - 0.33333));
187 }
188
189 ### Helper function for _color_hsl2rgb().
190 function _color_hue2rgb($m1, $m2, $h) {
191 $h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
192 if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
193 if ($h * 2 < 1) return $m2;
194 if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
195 return $m1;
196 }
197
198 ### Convert a hex color into an RGB triplet.
199 function _color_unpack($hex, $normalize = false) {
200
201 if (strpos($hex, '#') !== 0)
202 $hex = _resolve_htmlcolor($hex);
203
204 if (strlen($hex) == 4) {
205 $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
206 } $c = hexdec($hex);
207 for ($i = 16; $i >= 0; $i -= 8) {
208 $out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
209 } return $out;
210 }
211
212 ### Convert an RGB triplet to a hex color.
213 function _color_pack($rgb, $normalize = false) {
214 foreach ($rgb as $k => $v) {
215 $out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
216 }return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
217 }
218
219 function rgb2hsl($arr) {
220 $r = $arr[0];
221 $g = $arr[1];
222 $b = $arr[2];
223
224 $var_R = ($r / 255);
225 $var_G = ($g / 255);
226 $var_B = ($b / 255);
227
228 $var_Min = min($var_R, $var_G, $var_B);
229 $var_Max = max($var_R, $var_G, $var_B);
230 $del_Max = $var_Max - $var_Min;
231
232 $v = $var_Max;
233
234 if ($del_Max == 0) {
235 $h = 0;
236 $s = 0;
237 } else {
238 $s = $del_Max / $var_Max;
239
240 $del_R = ((($max - $var_R ) / 6 ) + ($del_Max / 2 ) ) / $del_Max;
241 $del_G = ((($max - $var_G ) / 6 ) + ($del_Max / 2 ) ) / $del_Max;
242 $del_B = ((($max - $var_B ) / 6 ) + ($del_Max / 2 ) ) / $del_Max;
243
244 if ($var_R == $var_Max) $h = $del_B - $del_G;
245 else if ($var_G == $var_Max) $h = (1 / 3 ) + $del_R - $del_B;
246 else if ($var_B == $var_Max) $h = (2 / 3 ) + $del_G - $del_R;
247
248 if ($H < 0) $h++;
249 if ($H > 1) $h--;
250 }
251
252 return array($h, $s, $v);
253 }
254
255 function hsl2rgb($arr) {
256 $h = $arr[0];
257 $s = $arr[1];
258 $v = $arr[2];
259
260 if($s == 0) {
261 $r = $g = $B = $v * 255;
262 } else {
263 $var_H = $h * 6;
264 $var_i = floor($var_H );
265 $var_1 = $v * (1 - $s );
266 $var_2 = $v * (1 - $s * ($var_H - $var_i ) );
267 $var_3 = $v * (1 - $s * (1 - ($var_H - $var_i ) ) );
268
269 if ($var_i == 0) { $var_R = $v ; $var_G = $var_3 ; $var_B = $var_1 ; }
270 else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $v ; $var_B = $var_1 ; }
271 else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $v ; $var_B = $var_3 ; }
272 else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $v ; }
273 else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $v ; }
274 else { $var_R = $v ; $var_G = $var_1 ; $var_B = $var_2 ; }
275
276 $r = $var_R * 255;
277 $g = $var_G * 255;
278 $B = $var_B * 255;
279 }
280 return array($r, $g, $B);
281 }
282
283 function colorPalette($imageFile, $numColors, $granularity = 5) {
284 $granularity = max(1, abs((int)$granularity));
285 $colors = array();
286
287 $size = @getimagesize($imageFile);
288
289 if (!defined('_DISABLE_FLOICON') && strtolower($size['mime']) == 'image/vnd.microsoft.icon') {
290 $ico = new floIcon();
291 @$ico->readICO($imageFile);
292
293 if(count($ico->images)==0)
294 return null;
295 else
296 $img = @$ico->images[count($ico->images)-1]->getImageResource();
297
298 } else {
299 $img = @imagecreatefromstring(file_get_contents($imageFile));
300 }
301
302 if (!$img) return false;
303
304 for($x = 0; $x < $size[0]; $x += $granularity) {
305 for($y = 0; $y < $size[1]; $y += $granularity) {
306 $thisColor = imagecolorat($img, $x, $y);
307 $rgb = imagecolorsforindex($img, $thisColor);
308 $red = round(round(($rgb['red'] / 0x33)) * 0x33);
309 $green = round(round(($rgb['green'] / 0x33)) * 0x33);
310 $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
311 $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
312 if(array_key_exists($thisRGB, $colors)) {
313 $colors[$thisRGB]++;
314 } else{
315 $colors[$thisRGB] = 1;
316 }
317 }
318 }
319
320 arsort($colors);
321 return array_slice(array_keys($colors), 0, $numColors);
322 }
323
324 function calculate_avg_color($iconFile) {
325 $palette = colorPalette($iconFile, 4, 4);
326
327 if (is_array($palette)) {
328 foreach ($palette as $p) {
329 $hsl = rgb2hsl(_color_unpack("#$p"));
330
331 if ($hsl[1] > 0.25 && $hsl[2] > 0.25 &&
332 !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[1] < 0.01) &&
333 !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[2] > 0.99)) {
334
335 return _color_pack(hsl2rgb($hsl));
336 }
337 }
338 }
339 return '';
340 }
341 ?>