ImageColor   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 653
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 284
dl 0
loc 653
rs 8.8
c 1
b 0
f 0
wmc 45

17 Methods

Rating   Name   Duplication   Size   Complexity  
A color2RGB() 0 11 2
A mixColors() 0 17 3
A hsv2rgb() 0 3 1
A setWebSafe() 0 3 1
A hex2rgb() 0 6 1
A setColors() 0 3 1
A setInternalColors() 0 7 3
A splitColor() 0 8 1
B namedColor2RGB() 0 156 3
A allocateColor() 0 5 1
A percentageColor2RGB() 0 23 4
B hsv2hex() 0 51 7
A returnColor() 0 3 1
A rgb2hex() 0 3 1
B getRange() 0 51 7
A changeLightness() 0 24 6
A getTextColor() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like ImageColor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ImageColor, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\Pedigree;
4
5
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6
7
/**
8
 * Color.php is the implementation of ImageColor.
9
 *
10
 * PHP versions 4 and 5
11
 *
12
 * LICENSE: This source file is subject to version 3.0 of the PHP license
13
 * that is available through the world-wide-web at the following URI:
14
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
15
 * the PHP License and are unable to obtain it through the web, please
16
 * send a note to [email protected] so we can mail you a copy immediately.
17
 *
18
 * @category    Image
19
 * @package     ImageColor
20
 * @author      Jason Lotito <[email protected]>
21
 * @author      Andrew Morton <[email protected]>
22
 * @copyright   2003-2005 The PHP Group
23
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
24
 * @version     1.1 2006/04/30
25
 * @link        http://pear.php.net/package/ImageColor
26
 */
27
28
/**
29
 * ImageColor handles color conversion and mixing.
30
 *
31
 * The class is quick, simple to use, and does its job fairly well but it's got
32
 * some code smells:
33
 *  - Call setColors() for some functions but not others.
34
 *  - Different functions expect different color formats. setColors() only
35
 *    accepts hex while allocateColor() will accept named or hex (provided the
36
 *    hex ones start with the # character).
37
 *  - Some conversions go in only one direction, ie HSV->RGB but no RGB->HSV.
38
 * I'm going to try to straighten out some of this but I'll be hard to do so
39
 * without breaking backwards compatibility.
40
 *
41
 * @category    Image
42
 * @package     ImageColor
43
 * @author      Jason Lotito <[email protected]>
44
 * @author      Andrew Morton <[email protected]>
45
 * @copyright   2003-2005 The PHP Group
46
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
47
 * @version     Release: 0.1.2
48
 * @link        http://pear.php.net/package/ImageColor
49
 */
50
51
/**
52
 * Class ImageColor
53
 */
54
class ImageColor
55
{
56
    /**
57
     * First color that the class handles for ranges and mixes.
58
     *
59
     * @var array
60
     * @access  public
61
     * @see     setColors()
62
     */
63
    public $color1 = [];
64
    /**
65
     * Second color that the class handles for ranges and mixes.
66
     *
67
     * @var array
68
     * @access  public
69
     * @see     setColors()
70
     */
71
    public $color2 = [];
72
    /**
73
     * Boolean value for determining whether colors outputted should be limited
74
     * to the web safe pallet or not.
75
     *
76
     * @var bool
77
     * @access  private
78
     * @see     setWebSafe()
79
     */
80
    private $websafeb = false;
81
82
    /**
83
     * Mix two colors together by finding their average. If the colors are not
84
     * passed as parameters, the class's colors will be mixed instead.
85
     *
86
     * @param bool|string $col1 The first color you want to mix
87
     * @param bool|string $col2 The second color you want to mix
88
     *
89
     * @return string The mixed color.
90
     * @access  public
91
     * @author  Jason Lotito <[email protected]>
92
     * @uses    setInternalColors() to assign the colors if any are passed to the
93
     *                class.
94
     */
95
    public function mixColors($col1 = false, $col2 = false)
96
    {
97
        if ($col1) {
98
            $this->setInternalColors($col1, $col2);
0 ignored issues
show
Bug introduced by
It seems like $col1 can also be of type true; however, parameter $col1 of XoopsModules\Pedigree\Im...or::setInternalColors() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

98
            $this->setInternalColors(/** @scrutinizer ignore-type */ $col1, $col2);
Loading history...
99
        }
100
101
        // after finding the average, it will be a float. add 0.5 and then
102
        // cast to an integer to properly round it to an integer.
103
        $color3[0] = (int)((($this->color1[0] + $this->color2[0]) / 2) + 0.5);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$color3 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $color3 = array(); before regardless.
Loading history...
104
        $color3[1] = (int)((($this->color1[1] + $this->color2[1]) / 2) + 0.5);
105
        $color3[2] = (int)((($this->color1[2] + $this->color2[2]) / 2) + 0.5);
106
107
        if ($this->websafeb) {
108
            \array_walk($color3, '\_makeWebSafe');
109
        }
110
111
        return $this->rgb2hex($color3);
112
    }
113
114
    /**
115
     * Determines whether colors the returned by this class will be rounded to
116
     * the nearest web safe value.
117
     *
118
     * @param bool $bool    Indicates if colors should be limited to the
119
     *                      websafe pallet.
120
     *
121
     * @access  public
122
     * @author  Jason Lotito <[email protected]>
123
     */
124
    public function setWebSafe($bool = true)
125
    {
126
        $this->websafeb = (bool)$bool;
127
    }
128
129
    /**
130
     * Set the two colors this class uses for mixing and ranges.
131
     *
132
     * @param string $col1 The first color in hex format
133
     * @param string $col2 The second color in hex format
134
     *
135
     * @access  public
136
     * @author  Jason Lotito <[email protected]>
137
     */
138
    public function setColors($col1, $col2)
139
    {
140
        $this->setInternalColors($col1, $col2);
141
    }
142
143
    /**
144
     * Get the range of colors between the class's two colors, given a degree.
145
     *
146
     * @param int $degrees     How large a 'step' we should take between the
147
     *                         colors.
148
     *
149
     * @return array Returns an array of hex strings, one element for each
150
     *               color.
151
     * @access  public
152
     * @author  Jason Lotito <[email protected]>
153
     * @todo    Allow for degrees for individual parts of the colors.
154
     */
155
    public function getRange($degrees = 2)
156
    {
157
        if (0 == $degrees) {
158
            $degrees = 1;
159
        }
160
161
        // The degrees give us how much we should advance each color at each
162
        // phase of the loop.  This way, the advance is equal throughout all
163
        // the colors.
164
165
        $red_steps   = ($this->color2[0] - $this->color1[0]) / $degrees;
166
        $green_steps = ($this->color2[1] - $this->color1[1]) / $degrees;
167
        $blue_steps  = ($this->color2[2] - $this->color1[2]) / $degrees;
168
169
        $allcolors = [];
170
171
        /**
172
         * The loop stops once any color has gone beyond the end color.
173
         */
174
175
        // Loop through all the degrees between the colors
176
        for ($x = 0; $x < $degrees; ++$x) {
177
            $col[0] = $red_steps * $x;
178
            $col[1] = $green_steps * $x;
179
            $col[2] = $blue_steps * $x;
180
181
            // Loop through each R, G, and B
182
            for ($i = 0; $i < 3; ++$i) {
183
                $partcolor = $this->color1[$i] + $col[$i];
184
                // If the color is less than 256
185
                if ($partcolor < 256) {
186
                    // Makes sure the colors is not less than 0
187
                    if ($partcolor > -1) {
188
                        $newcolor[$i] = $partcolor;
189
                    } else {
190
                        $newcolor[$i] = 0;
191
                    }
192
                    // Color was greater than 255
193
                } else {
194
                    $newcolor[$i] = 255;
195
                }
196
            }
197
198
            if ($this->websafeb) {
199
                \array_walk($newcolor, '\_makeWebSafe');
200
            }
201
202
            $allcolors[] = $this->rgb2hex($newcolor);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newcolor does not seem to be defined for all execution paths leading up to this point.
Loading history...
203
        }
204
205
        return $allcolors;
206
    }
207
208
    /**
209
     * Change the lightness of the class's two colors.
210
     *
211
     * @param int $degree     The degree of the change. Positive values
212
     *                        lighten the color while negative values will darken it.
213
     *
214
     * @access  public
215
     * @author  Jason Lotito <[email protected]>
216
     * @uses    ImageColor::$color1 as an input and return value.
217
     * @uses    ImageColor::$color2 as an input and return value.
218
     */
219
    public function changeLightness($degree = 10)
220
    {
221
        $color1 = &$this->color1;
222
        $color2 = &$this->color2;
223
224
        for ($x = 0; $x < 3; ++$x) {
225
            if (($color1[$x] + $degree) < 256) {
226
                if (($color1[$x] + $degree) > -1) {
227
                    $color1[$x] += $degree;
228
                } else {
229
                    $color1[$x] = 0;
230
                }
231
            } else {
232
                $color1[$x] = 255;
233
            }
234
235
            if (($color2[$x] + $degree) < 256) {
236
                if (($color2[$x] + $degree) > -1) {
237
                    $color2[$x] += $degree;
238
                } else {
239
                    $color2[$x] = 0;
240
                }
241
            } else {
242
                $color2[$x] = 255;
243
            }
244
        }
245
    }
246
247
    /**
248
     * Determine if a light or dark text color would be more readable on a
249
     * background of a given color. This is determined by the G(reen) value of
250
     * RGB. You can change the dark and the light colors from their default
251
     * black and white.
252
     *
253
     * @param string|array $color The hex color to analyze
254
     * @param string       $light The light color value to return if we should
255
     *                            have light text.
256
     * @param string       $dark  The dark color value to return if we should have
257
     *                            dark text.
258
     *
259
     * @return string The light or dark value which would make the text most
260
     *                readable.
261
     * @access  public
262
     * @static
263
     * @author  Jason Lotito <[email protected]>
264
     */
265
    public function getTextColor($color, $light = '#FFFFFF', $dark = '#000000')
266
    {
267
        $color = $this->splitColor($color);
0 ignored issues
show
Bug introduced by
It seems like $color can also be of type array; however, parameter $color of XoopsModules\Pedigree\ImageColor::splitColor() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

267
        $color = $this->splitColor(/** @scrutinizer ignore-type */ $color);
Loading history...
268
        if ($color[1] > \hexdec('66')) {
269
            return $dark;
270
        }
271
272
        return $light;
273
    }
274
275
    /**
276
     * Internal method to set the colors.
277
     *
278
     * @param string $col1 First color, either a name or hex value
279
     * @param string $col2 Second color, either a name or hex value
280
     *
281
     * @access  private
282
     * @author  Jason Lotito <[email protected]>
283
     */
284
    private function setInternalColors($col1, $col2)
285
    {
286
        if ($col1) {
287
            $this->color1 = $this->splitColor($col1);
288
        }
289
        if ($col2) {
290
            $this->color2 = $this->splitColor($col2);
291
        }
292
    }
293
294
    /**
295
     * Given a color, properly split it up into a 3 element RGB array.
296
     *
297
     * @param string $color The color.
298
     *
299
     * @return array A three element RGB array.
300
     * @access  private
301
     * @static
302
     * @author  Jason Lotito <[email protected]>
303
     */
304
    private function splitColor($color)
305
    {
306
        $color = \str_replace('#', '', $color);
307
        $c[]   = \hexdec(\mb_substr($color, 0, 2));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$c was never initialized. Although not strictly required by PHP, it is generally a good practice to add $c = array(); before regardless.
Loading history...
308
        $c[]   = \hexdec(\mb_substr($color, 2, 2));
309
        $c[]   = \hexdec(\mb_substr($color, 4, 2));
310
311
        return $c;
312
    }
313
314
    /**
315
     * This is deprecated. Use rgb2hex() instead.
316
     *
317
     * @access     private
318
     * @param $color
319
     *
320
     * @return string
321
     * @deprecated Function deprecated after 1.0.1
322
     * @see        rgb2hex().
323
     *
324
     */
325
    private function returnColor($color)
0 ignored issues
show
Unused Code introduced by
The method returnColor() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
326
    {
327
        return $this->rgb2hex($color);
328
    }
329
330
    /**
331
     * Convert an RGB array to a hex string.
332
     *
333
     * @param array $color 3 element RGB array.
334
     *
335
     * @return string Hex color string.
336
     * @access  public
337
     * @static
338
     * @author  Jason Lotito <[email protected]>
339
     * @see     hex2rgb()
340
     */
341
    public function rgb2hex($color)
342
    {
343
        return \sprintf('%02X%02X%02X', $color[0], $color[1], $color[2]);
344
    }
345
346
    /**
347
     * Convert a hex color string into an RGB array. An extra fourth element
348
     * will be returned with the original hex value.
349
     *
350
     * @param string $hex Hex color string.
351
     *
352
     * @return array RGB color array with an extra 'hex' element containing
353
     *               the original hex string.
354
     * @access  public
355
     * @static
356
     * @author  Jason Lotito <[email protected]>
357
     * @see     rgb2hex()
358
     */
359
    public function hex2rgb($hex)
360
    {
361
        $return        = $this->splitColor($hex);
362
        $return['hex'] = $hex;
363
364
        return $return;
365
    }
366
367
    /**
368
     * Convert an HSV (Hue, Saturation, Brightness) value to RGB.
369
     *
370
     * @param int $h Hue
371
     * @param int $s Saturation
372
     * @param int $v Brightness
373
     *
374
     * @return array RGB array.
375
     * @access  public
376
     * @static
377
     * @author  Jason Lotito <[email protected]>
378
     * @uses    hsv2hex() to convert the HSV value to Hex.
379
     * @uses    hex2rgb() to convert the Hex value to RGB.
380
     */
381
    public function hsv2rgb($h, $s, $v)
382
    {
383
        return $this->hex2rgb($this->hsv2hex($h, $s, $v));
384
    }
385
386
    /**
387
     * Convert HSV (Hue, Saturation, Brightness) to a hex color string.
388
     *
389
     * Originally written by Jurgen Schwietering. Integrated into the class by
390
     * Jason Lotito.
391
     *
392
     * @param int $h Hue
393
     * @param int $s Saturation
394
     * @param int $v Brightness
395
     *
396
     * @return string The hex string.
397
     * @access  public
398
     * @static
399
     * @author  Jurgen Schwietering <[email protected]>
400
     * @uses    rgb2hex() to convert the return value to a hex string.
401
     */
402
    public function hsv2hex($h, $s, $v)
403
    {
404
        $s /= 256.0;
405
        $v /= 256.0;
406
        if (0.0 == $s) {
407
            $r = $g = $b = $v;
0 ignored issues
show
Unused Code introduced by
The assignment to $b is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $r is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $g is dead and can be removed.
Loading history...
408
409
            return '';
410
        }
411
        $h = $h / 256.0 * 6.0;
412
        $i = \floor($h);
413
        $f = $h - $i;
414
415
        $v *= 256.0;
416
        $p = (int)($v * (1.0 - $s));
417
        $q = (int)($v * (1.0 - $s * $f));
418
        $t = (int)($v * (1.0 - $s * (1.0 - $f)));
419
        switch ($i) {
420
            case 0:
421
                $r = $v;
422
                $g = $t;
423
                $b = $p;
424
                break;
425
            case 1:
426
                $r = $q;
427
                $g = $v;
428
                $b = $p;
429
                break;
430
            case 2:
431
                $r = $p;
432
                $g = $v;
433
                $b = $t;
434
                break;
435
            case 3:
436
                $r = $p;
437
                $g = $q;
438
                $b = $v;
439
                break;
440
            case 4:
441
                $r = $t;
442
                $g = $p;
443
                $b = $v;
444
                break;
445
            default:
446
                $r = $v;
447
                $g = $p;
448
                $b = $q;
449
                break;
450
        }
451
452
        return $this->rgb2hex([$r, $g, $b]);
453
    }
454
455
    /**
456
     * Allocates a color in the given image.
457
     *
458
     * User defined color specifications get translated into an array of RGB
459
     * values.
460
     *
461
     * @param resource     $img   Image handle
462
     * @param string|array $color Name or hex string or an RGB array.
463
     *
464
     * @return bool Image color handle.
465
     * @access  public
466
     * @static
467
     * @uses    imagefilledarc() to allocate the color.
468
     * @uses    color2RGB() to parse the color into RGB values.
469
     */
470
    public function allocateColor($img, $color)
471
    {
472
        $color = $this->color2RGB($color);
0 ignored issues
show
Bug introduced by
It seems like $color can also be of type array; however, parameter $color of XoopsModules\Pedigree\ImageColor::color2RGB() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

472
        $color = $this->color2RGB(/** @scrutinizer ignore-type */ $color);
Loading history...
473
474
        return \imagefilledarc($img, $color[0], $color[1], $color[2]);
0 ignored issues
show
Bug introduced by
The call to imagefilledarc() has too few arguments starting with height. ( Ignorable by Annotation )

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

474
        return /** @scrutinizer ignore-call */ \imagefilledarc($img, $color[0], $color[1], $color[2]);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
475
    }
476
477
    /**
478
     * Convert a named or hex color string to an RGB array. If the color begins
479
     * with the # character it will be treated as a hex value. Everything else
480
     * will be treated as a named color. If the named color is not known, black
481
     * will be returned.
482
     *
483
     * @param string $color
484
     *
485
     * @return array RGB color
486
     * @access  public
487
     * @static
488
     * @author  Laurent Laville <[email protected]>
489
     * @uses    hex2rgb() to convert colors begining with the # character.
490
     * @uses    namedColor2RGB() to convert everything not starting with a #.
491
     */
492
    public function color2RGB($color)
493
    {
494
        $c = [];
495
496
        if ('#' === $color[0]) {
497
            $c = $this->hex2rgb($color);
498
        } else {
499
            $c = $this->namedColor2RGB($color);
500
        }
501
502
        return $c;
503
    }
504
505
    /**
506
     * Convert a named color to an RGB array. If the color is unknown black
507
     * is returned.
508
     *
509
     * @param string $color Case insensitive color name.
510
     *
511
     * @return array RGB color array. If the color was unknown, the result
512
     *               will be black.
513
     * @access  public
514
     * @static
515
     * @author  Sebastian Bergmann <[email protected]>
516
     */
517
    public function namedColor2RGB($color)
518
    {
519
        static $colornames;
520
521
        if (!isset($colornames)) {
522
            $colornames = [
523
                'aliceblue'            => [240, 248, 255],
524
                'antiquewhite'         => [250, 235, 215],
525
                'aqua'                 => [0, 255, 255],
526
                'aquamarine'           => [127, 255, 212],
527
                'azure'                => [240, 255, 255],
528
                'beige'                => [245, 245, 220],
529
                'bisque'               => [255, 228, 196],
530
                'black'                => [0, 0, 0],
531
                'blanchedalmond'       => [255, 235, 205],
532
                'blue'                 => [0, 0, 255],
533
                'blueviolet'           => [138, 43, 226],
534
                'brown'                => [165, 42, 42],
535
                'burlywood'            => [222, 184, 135],
536
                'cadetblue'            => [95, 158, 160],
537
                'chartreuse'           => [127, 255, 0],
538
                'chocolate'            => [210, 105, 30],
539
                'coral'                => [255, 127, 80],
540
                'cornflowerblue'       => [100, 149, 237],
541
                'cornsilk'             => [255, 248, 220],
542
                'crimson'              => [220, 20, 60],
543
                'cyan'                 => [0, 255, 255],
544
                'darkblue'             => [0, 0, 13],
545
                'darkcyan'             => [0, 139, 139],
546
                'darkgoldenrod'        => [184, 134, 11],
547
                'darkgray'             => [169, 169, 169],
548
                'darkgreen'            => [0, 100, 0],
549
                'darkkhaki'            => [189, 183, 107],
550
                'darkmagenta'          => [139, 0, 139],
551
                'darkolivegreen'       => [85, 107, 47],
552
                'darkorange'           => [255, 140, 0],
553
                'darkorchid'           => [153, 50, 204],
554
                'darkred'              => [139, 0, 0],
555
                'darksalmon'           => [233, 150, 122],
556
                'darkseagreen'         => [143, 188, 143],
557
                'darkslateblue'        => [72, 61, 139],
558
                'darkslategray'        => [47, 79, 79],
559
                'darkturquoise'        => [0, 206, 209],
560
                'darkviolet'           => [148, 0, 211],
561
                'deeppink'             => [255, 20, 147],
562
                'deepskyblue'          => [0, 191, 255],
563
                'dimgray'              => [105, 105, 105],
564
                'dodgerblue'           => [30, 144, 255],
565
                'firebrick'            => [178, 34, 34],
566
                'floralwhite'          => [255, 250, 240],
567
                'forestgreen'          => [34, 139, 34],
568
                'fuchsia'              => [255, 0, 255],
569
                'gainsboro'            => [220, 220, 220],
570
                'ghostwhite'           => [248, 248, 255],
571
                'gold'                 => [255, 215, 0],
572
                'goldenrod'            => [218, 165, 32],
573
                'gray'                 => [128, 128, 128],
574
                'green'                => [0, 128, 0],
575
                'greenyellow'          => [173, 255, 47],
576
                'honeydew'             => [240, 255, 240],
577
                'hotpink'              => [255, 105, 180],
578
                'indianred'            => [205, 92, 92],
579
                'indigo'               => [75, 0, 130],
580
                'ivory'                => [255, 255, 240],
581
                'khaki'                => [240, 230, 140],
582
                'lavender'             => [230, 230, 250],
583
                'lavenderblush'        => [255, 240, 245],
584
                'lawngreen'            => [124, 252, 0],
585
                'lemonchiffon'         => [255, 250, 205],
586
                'lightblue'            => [173, 216, 230],
587
                'lightcoral'           => [240, 128, 128],
588
                'lightcyan'            => [224, 255, 255],
589
                'lightgoldenrodyellow' => [250, 250, 210],
590
                'lightgreen'           => [144, 238, 144],
591
                'lightgrey'            => [211, 211, 211],
592
                'lightpink'            => [255, 182, 193],
593
                'lightsalmon'          => [255, 160, 122],
594
                'lightseagreen'        => [32, 178, 170],
595
                'lightskyblue'         => [135, 206, 250],
596
                'lightslategray'       => [119, 136, 153],
597
                'lightsteelblue'       => [176, 196, 222],
598
                'lightyellow'          => [255, 255, 224],
599
                'lime'                 => [0, 255, 0],
600
                'limegreen'            => [50, 205, 50],
601
                'linen'                => [250, 240, 230],
602
                'magenta'              => [255, 0, 255],
603
                'maroon'               => [128, 0, 0],
604
                'mediumaquamarine'     => [102, 205, 170],
605
                'mediumblue'           => [0, 0, 205],
606
                'mediumorchid'         => [186, 85, 211],
607
                'mediumpurple'         => [147, 112, 219],
608
                'mediumseagreen'       => [60, 179, 113],
609
                'mediumslateblue'      => [123, 104, 238],
610
                'mediumspringgreen'    => [0, 250, 154],
611
                'mediumturquoise'      => [72, 209, 204],
612
                'mediumvioletred'      => [199, 21, 133],
613
                'midnightblue'         => [25, 25, 112],
614
                'mintcream'            => [245, 255, 250],
615
                'mistyrose'            => [255, 228, 225],
616
                'moccasin'             => [255, 228, 181],
617
                'navajowhite'          => [255, 222, 173],
618
                'navy'                 => [0, 0, 128],
619
                'oldlace'              => [253, 245, 230],
620
                'olive'                => [128, 128, 0],
621
                'olivedrab'            => [107, 142, 35],
622
                'orange'               => [255, 165, 0],
623
                'orangered'            => [255, 69, 0],
624
                'orchid'               => [218, 112, 214],
625
                'palegoldenrod'        => [238, 232, 170],
626
                'palegreen'            => [152, 251, 152],
627
                'paleturquoise'        => [175, 238, 238],
628
                'palevioletred'        => [219, 112, 147],
629
                'papayawhip'           => [255, 239, 213],
630
                'peachpuff'            => [255, 218, 185],
631
                'peru'                 => [205, 133, 63],
632
                'pink'                 => [255, 192, 203],
633
                'plum'                 => [221, 160, 221],
634
                'powderblue'           => [176, 224, 230],
635
                'purple'               => [128, 0, 128],
636
                'red'                  => [255, 0, 0],
637
                'rosybrown'            => [188, 143, 143],
638
                'royalblue'            => [65, 105, 225],
639
                'saddlebrown'          => [139, 69, 19],
640
                'salmon'               => [250, 128, 114],
641
                'sandybrown'           => [244, 164, 96],
642
                'seagreen'             => [46, 139, 87],
643
                'seashell'             => [255, 245, 238],
644
                'sienna'               => [160, 82, 45],
645
                'silver'               => [192, 192, 192],
646
                'skyblue'              => [135, 206, 235],
647
                'slateblue'            => [106, 90, 205],
648
                'slategray'            => [112, 128, 144],
649
                'snow'                 => [255, 250, 250],
650
                'springgreen'          => [0, 255, 127],
651
                'steelblue'            => [70, 130, 180],
652
                'tan'                  => [210, 180, 140],
653
                'teal'                 => [0, 128, 128],
654
                'thistle'              => [216, 191, 216],
655
                'tomato'               => [255, 99, 71],
656
                'turquoise'            => [64, 224, 208],
657
                'violet'               => [238, 130, 238],
658
                'wheat'                => [245, 222, 179],
659
                'white'                => [255, 255, 255],
660
                'whitesmoke'           => [245, 245, 245],
661
                'yellow'               => [255, 255, 0],
662
                'yellowgreen'          => [154, 205, 50],
663
            ];
664
        }
665
666
        $color = \mb_strtolower($color);
667
668
        if (isset($colornames[$color])) {
669
            return $colornames[$color];
670
        }
671
672
        return [0, 0, 0];
673
    }
674
675
    /**
676
     * Convert an RGB percentage string into an RGB array.
677
     *
678
     * @param string|array $color Percentage color string like "50%,20%,100%".
679
     *
680
     * @return array RGB color array.
681
     * @access  public
682
     * @static
683
     */
684
    public function percentageColor2RGB($color)
685
    {
686
        // remove spaces
687
        $color = \str_replace(' ', '', $color);
688
        // remove the percent signs
689
        $color = \str_replace('%', '', $color);
690
        // split the string by commas
691
        $color = \explode(',', $color);
692
693
        $ret = [];
694
        foreach ($color as $k => $v) {
695
            // range checks
696
            if ($v <= 0) {
697
                $ret[$k] = 0;
698
            } elseif ($v <= 100) {
699
                // add 0.5 then cast to an integer to round the value.
700
                $ret[$k] = (int)((2.55 * $v) + 0.5);
701
            } else {
702
                $ret[$k] = 255;
703
            }
704
        }
705
706
        return $ret;
707
    }
708
}
709
710
// For Array Walk
711
// {{{
712
/**
713
 * Function for array_walk() to round colors to the closest web safe value.
714
 *
715
 * @param int $color One channel of an RGB color.
716
 *
717
 * @return int The websafe equivalent of the color channel.
718
 * @author  Jason Lotito <[email protected]>
719
 * @author  Andrew Morton <[email protected]>
720
 * @access  private
721
 * @static
722
 */
723
function _makeWebSafe(&$color)
724
{
725
    if ($color < 0x1a) {
726
        $color = 0x00;
727
    } elseif ($color < 0x4d) {
728
        $color = 0x33;
729
    } elseif ($color < 0x80) {
730
        $color = 0x66;
731
    } elseif ($color < 0xB3) {
732
        $color = 0x99;
733
    } elseif ($color < 0xE6) {
734
        $color = 0xCC;
735
    } else {
736
        $color = 0xFF;
737
    }
738
739
    return $color;
740
}
741
// }}}
742