Completed
Push — master ( c8eead...62889a )
by Michael
04:13
created

include/color.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
It seems like $col2 defined by parameter $col2 on line 90 can also be of type boolean; however, Image_Color::_setColors() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
        }
95
96
        // after finding the average, it will be a float. add 0.5 and then
97
        // cast to an integer to properly round it to an integer.
98
        $color3[0] = (int)((($this->color1[0] + $this->color2[0]) / 2) + 0.5);
99
        $color3[1] = (int)((($this->color1[1] + $this->color2[1]) / 2) + 0.5);
100
        $color3[2] = (int)((($this->color1[2] + $this->color2[2]) / 2) + 0.5);
101
102
        if ($this->_websafeb) {
103
            array_walk($color3, '_makeWebSafe');
104
        }
105
106
        return Image_Color::rgb2hex($color3);
107
    }
108
109
    /**
110
     * Determines whether colors the returned by this class will be rounded to
111
     * the nearest web safe value.
112
     *
113
     * @param boolean $bool Indicates if colors should be limited to the
114
     *                      websafe pallet.
115
     *
116
     * @return void
117
     * @access  public
118
     * @author  Jason Lotito <[email protected]>
119
     */
120
    function setWebSafe($bool = true)
121
    {
122
        $this->_websafeb = (boolean)$bool;
123
    }
124
125
    /**
126
     * Set the two colors this class uses for mixing and ranges.
127
     *
128
     * @param string $col1 The first color in hex format
129
     * @param string $col2 The second color in hex format
130
     *
131
     * @return void
132
     * @access  public
133
     * @author  Jason Lotito <[email protected]>
134
     */
135
    function setColors($col1, $col2)
136
    {
137
        $this->_setColors($col1, $col2);
138
    }
139
140
    /**
141
     * Get the range of colors between the class's two colors, given a degree.
142
     *
143
     * @param integer $degrees How large a 'step' we should take between the
144
     *                         colors.
145
     *
146
     * @return array Returns an array of hex strings, one element for each
147
     *               color.
148
     * @access  public
149
     * @author  Jason Lotito <[email protected]>
150
     * @todo    Allow for degrees for individual parts of the colors.
151
     */
152
    function getRange($degrees = 2)
153
    {
154
        if ($degrees == 0) {
155
            $degrees = 1;
156
        }
157
158
        // The degrees give us how much we should advance each color at each
159
        // phase of the loop.  This way, the advance is equal throughout all
160
        // the colors.
161
162
        $red_steps   = ($this->color2[0] - $this->color1[0]) / $degrees;
163
        $green_steps = ($this->color2[1] - $this->color1[1]) / $degrees;
164
        $blue_steps  = ($this->color2[2] - $this->color1[2]) / $degrees;
165
166
        $allcolors = array();
167
168
        /**
169
         * The loop stops once any color has gone beyond the end color.
170
         */
171
172
        // Loop through all the degrees between the colors
173
        for ($x = 0; $x < $degrees; ++$x) {
174
            $col[0] = $red_steps * $x;
175
            $col[1] = $green_steps * $x;
176
            $col[2] = $blue_steps * $x;
177
178
            // Loop through each R, G, and B
179
            for ($i = 0; $i < 3; ++$i) {
180
                $partcolor = $this->color1[$i] + $col[$i];
181
                // If the color is less than 256
182
                if ($partcolor < 256) {
183
                    // Makes sure the colors is not less than 0
184
                    if ($partcolor > -1) {
185
                        $newcolor[$i] = $partcolor;
186
                    } else {
187
                        $newcolor[$i] = 0;
188
                    }
189
                    // Color was greater than 255
190
                } else {
191
                    $newcolor[$i] = 255;
192
                }
193
            }
194
195
            if ($this->_websafeb) {
196
                array_walk($newcolor, '_makeWebSafe');
197
            }
198
199
            $allcolors[] = Image_Color::rgb2hex($newcolor);
200
        }
201
202
        return $allcolors;
203
    }
204
205
    /**
206
     * Change the lightness of the class's two colors.
207
     *
208
     * @param integer $degree The degree of the change. Positive values
209
     *                        lighten the color while negative values will darken it.
210
     *
211
     * @return void
212
     * @access  public
213
     * @author  Jason Lotito <[email protected]>
214
     * @uses    Image_Color::$color1 as an input and return value.
215
     * @uses    Image_Color::$color2 as an input and return value.
216
     */
217
    function changeLightness($degree = 10)
218
    {
219
        $color1 =& $this->color1;
220
        $color2 =& $this->color2;
221
222
        for ($x = 0; $x < 3; ++$x) {
223 View Code Duplication
            if (($color1[$x] + $degree) < 256) {
224
                if (($color1[$x] + $degree) > -1) {
225
                    $color1[$x] += $degree;
226
                } else {
227
                    $color1[$x] = 0;
228
                }
229
            } else {
230
                $color1[$x] = 255;
231
            }
232
233 View Code Duplication
            if (($color2[$x] + $degree) < 256) {
234
                if (($color2[$x] + $degree) > -1) {
235
                    $color2[$x] += $degree;
236
                } else {
237
                    $color2[$x] = 0;
238
                }
239
            } else {
240
                $color2[$x] = 255;
241
            }
242
        }
243
    }
244
245
    /**
246
     * Determine if a light or dark text color would be more readable on a
247
     * background of a given color. This is determined by the G(reen) value of
248
     * RGB. You can change the dark and the light colors from their default
249
     * black and white.
250
     *
251
     * @param string $color The hex color to analyze
252
     * @param string $light The light color value to return if we should
253
     *                      have light text.
254
     * @param string $dark  The dark color value to return if we should have
255
     *                      dark text.
256
     *
257
     * @return string The light or dark value which would make the text most
258
     *                readable.
259
     * @access  public
260
     * @static
261
     * @author  Jason Lotito <[email protected]>
262
     */
263
    function getTextColor($color, $light = '#FFFFFF', $dark = '#000000')
264
    {
265
        $color = Image_Color::_splitColor($color);
266
        if ($color[1] > hexdec('66')) {
267
            return $dark;
268
        } else {
269
            return $light;
270
        }
271
    }
272
273
    /**
274
     * Internal method to set the colors.
275
     *
276
     * @param string $col1 First color, either a name or hex value
277
     * @param string $col2 Second color, either a name or hex value
278
     *
279
     * @return void
280
     * @access  private
281
     * @author  Jason Lotito <[email protected]>
282
     */
283
    function _setColors($col1, $col2)
284
    {
285
        if ($col1) {
286
            $this->color1 = Image_Color::_splitColor($col1);
287
        }
288
        if ($col2) {
289
            $this->color2 = Image_Color::_splitColor($col2);
290
        }
291
    }
292
293
    /**
294
     * Given a color, properly split it up into a 3 element RGB array.
295
     *
296
     * @param string $color The color.
297
     *
298
     * @return array A three element RGB array.
299
     * @access  private
300
     * @static
301
     * @author  Jason Lotito <[email protected]>
302
     */
303
    function _splitColor($color)
304
    {
305
        $color = str_replace('#', '', $color);
306
        $c[]   = hexdec(substr($color, 0, 2));
307
        $c[]   = hexdec(substr($color, 2, 2));
308
        $c[]   = hexdec(substr($color, 4, 2));
309
310
        return $c;
311
    }
312
313
    /**
314
     * This is deprecated. Use rgb2hex() instead.
315
     *
316
     * @access     private
317
     * @deprecated Function deprecated after 1.0.1
318
     * @see        rgb2hex().
319
     *
320
     * @param $color
321
     *
322
     * @return string
323
     */
324
    function _returnColor($color)
325
    {
326
        return Image_Color::rgb2hex($color);
327
    }
328
329
    /**
330
     * Convert an RGB array to a hex string.
331
     *
332
     * @param array $color 3 element RGB array.
333
     *
334
     * @return string Hex color string.
335
     * @access  public
336
     * @static
337
     * @author  Jason Lotito <[email protected]>
338
     * @see     hex2rgb()
339
     */
340
    function rgb2hex($color)
341
    {
342
        return sprintf('%02X%02X%02X', $color[0], $color[1], $color[2]);
343
    }
344
345
    /**
346
     * Convert a hex color string into an RGB array. An extra fourth element
347
     * will be returned with the original hex value.
348
     *
349
     * @param string $hex Hex color string.
350
     *
351
     * @return array RGB color array with an extra 'hex' element containing
352
     *               the original hex string.
353
     * @access  public
354
     * @static
355
     * @author  Jason Lotito <[email protected]>
356
     * @see     rgb2hex()
357
     */
358
    function hex2rgb($hex)
359
    {
360
        $return        = Image_Color::_splitColor($hex);
361
        $return['hex'] = $hex;
362
363
        return $return;
364
    }
365
366
    /**
367
     * Convert an HSV (Hue, Saturation, Brightness) value to RGB.
368
     *
369
     * @param integer $h Hue
370
     * @param integer $s Saturation
371
     * @param integer $v Brightness
372
     *
373
     * @return array RGB array.
374
     * @access  public
375
     * @static
376
     * @author  Jason Lotito <[email protected]>
377
     * @uses    hsv2hex() to convert the HSV value to Hex.
378
     * @uses    hex2rgb() to convert the Hex value to RGB.
379
     */
380
    function hsv2rgb($h, $s, $v)
381
    {
382
        return Image_Color::hex2rgb(Image_Color::hsv2hex($h, $s, $v));
383
    }
384
385
    /**
386
     * Convert HSV (Hue, Saturation, Brightness) to a hex color string.
387
     *
388
     * Originally written by Jurgen Schwietering. Integrated into the class by
389
     * Jason Lotito.
390
     *
391
     * @param integer $h Hue
392
     * @param integer $s Saturation
393
     * @param integer $v Brightness
394
     *
395
     * @return string The hex string.
396
     * @access  public
397
     * @static
398
     * @author  Jurgen Schwietering <[email protected]>
399
     * @uses    rgb2hex() to convert the return value to a hex string.
400
     */
401
    function hsv2hex($h, $s, $v)
402
    {
403
        $s /= 256.0;
404
        $v /= 256.0;
405
        if ($s == 0.0) {
406
            $r = $g = $b = $v;
407
408
            return '';
409
        } else {
410
            $h = $h / 256.0 * 6.0;
411
            $i = floor($h);
412
            $f = $h - $i;
413
414
            $v *= 256.0;
415
            $p = (integer)($v * (1.0 - $s));
416
            $q = (integer)($v * (1.0 - $s * $f));
417
            $t = (integer)($v * (1.0 - $s * (1.0 - $f)));
418
            switch ($i) {
419
                case 0:
420
                    $r = $v;
421
                    $g = $t;
422
                    $b = $p;
423
                    break;
424
425
                case 1:
426
                    $r = $q;
427
                    $g = $v;
428
                    $b = $p;
429
                    break;
430
431
                case 2:
432
                    $r = $p;
433
                    $g = $v;
434
                    $b = $t;
435
                    break;
436
437
                case 3:
438
                    $r = $p;
439
                    $g = $q;
440
                    $b = $v;
441
                    break;
442
443
                case 4:
444
                    $r = $t;
445
                    $g = $p;
446
                    $b = $v;
447
                    break;
448
449
                default:
450
                    $r = $v;
451
                    $g = $p;
452
                    $b = $q;
453
                    break;
454
            }
455
        }
456
457
        return $this->rgb2hex(array($r, $g, $b));
458
    }
459
460
    /**
461
     * Allocates a color in the given image.
462
     *
463
     * User defined color specifications get translated into an array of RGB
464
     * values.
465
     *
466
     * @param resource     $img   Image handle
467
     * @param string|array $color Name or hex string or an RGB array.
468
     *
469
     * @return resource Image color handle.
470
     * @access  public
471
     * @static
472
     * @uses    ImageColorAllocate() to allocate the color.
473
     * @uses    color2RGB() to parse the color into RGB values.
474
     */
475
    function allocateColor(&$img, $color)
476
    {
477
        $color = Image_Color::color2RGB($color);
0 ignored issues
show
It seems like $color defined by \Image_Color::color2RGB($color) on line 477 can also be of type array; however, Image_Color::color2RGB() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

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

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
716
// {{{
717
/**
718
 * Function for array_walk() to round colors to the closest web safe value.
719
 *
720
 * @param   integer $color One channel of an RGB color.
721
 *
722
 * @return  integer The websafe equivalent of the color channel.
723
 * @author  Jason Lotito <[email protected]>
724
 * @author  Andrew Morton <[email protected]>
725
 * @access  private
726
 * @static
727
 */
728
function _makeWebSafe(&$color)
729
{
730
    if ($color < 0x1a) {
731
        $color = 0x00;
732
    } elseif ($color < 0x4d) {
733
        $color = 0x33;
734
    } elseif ($color < 0x80) {
735
        $color = 0x66;
736
    } elseif ($color < 0xB3) {
737
        $color = 0x99;
738
    } elseif ($color < 0xE6) {
739
        $color = 0xCC;
740
    } else {
741
        $color = 0xFF;
742
    }
743
744
    return $color;
745
}
746
// }}}
747