Issues (1270)

include/colors.php (6 issues)

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

319
            /** @scrutinizer ignore-unhandled */ @$ico->readICO($imageFile);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Are you sure the usage of $ico->readICO($imageFile) targeting floIcon::readICO() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
320
321
            if (count($ico->images) == 0) {
322
                            return false;
323
            } else {
324
                            $img = @$ico->images[count($ico->images) - 1]->getImageResource();
325
            }
326
327
        } else {
328
            return false;
329
        }
330
331
    } else if ($size[0] > 0 && $size[1] > 0) {
332
        $img = @imagecreatefromstring(file_get_contents($imageFile));
333
    }
334
335
    if (!$img) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $img does not seem to be defined for all execution paths leading up to this point.
Loading history...
336
        return false;
337
    }
338
339
    for ($x = 0; $x < $size[0]; $x += $granularity) {
340
        for ($y = 0; $y < $size[1]; $y += $granularity) {
341
            $thisColor = imagecolorat($img, $x, $y);
342
            $rgb = imagecolorsforindex($img, $thisColor);
343
            $red = round(round(($rgb['red'] / 0x33)) * 0x33);
344
            $green = round(round(($rgb['green'] / 0x33)) * 0x33);
345
            $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
346
            $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
347
            if (array_key_exists($thisRGB, $colors)) {
348
                $colors[$thisRGB]++;
349
            } else {
350
                $colors[$thisRGB] = 1;
351
            }
352
        }
353
    }
354
355
    arsort($colors);
356
    return array_slice(array_keys($colors), 0, $numColors);
357
}
358
359
function calculate_avg_color($iconFile) {
360
    $palette = colorPalette($iconFile, 4, 4);
361
362
    if (is_array($palette)) {
363
        foreach ($palette as $p) {
364
            $hsl = rgb2hsl(_color_unpack("#$p"));
365
366
            if ($hsl[1] > 0.25 && $hsl[2] > 0.25 &&
367
                !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[1] < 0.01) &&
368
                !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[2] > 0.99)) {
369
370
                return _color_pack(hsl2rgb($hsl));
371
            }
372
        }
373
    }
374
    return '';
375
}
376