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