GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 486f11...b55f68 )
by t
07:38 queued 01:58
created

Color::__construct()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 20
nc 12
nop 2
dl 0
loc 29
ccs 19
cts 19
cp 1
crap 12
rs 6.9666
c 2
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Class Color
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use Exception;
13
use icy2003\php\I;
14
15
/**
16
 * 颜色处理
17
 */
18
class Color
19
{
20
    /**
21
     * 颜色名
22
     *
23
     * @var array
24
     */
25
    protected static $_names = [
26
        'black' => 0x000000,
27
        'darkgreen' => 0x006400,
28
        'green' => 0x008000,
29
        'maroon' => 0x800000,
30
        'navy' => 0x000080,
31
        'myrtle' => 0x21421E,
32
        'bistre' => 0x3D2B1F,
33
        'darkblue' => 0x00008B,
34
        'darkred' => 0x8B0000,
35
        'sapphire' => 0x082567,
36
        'sangria' => 0x92000A,
37
        'burgundy' => 0x800020,
38
        'midnightblue' => 0x191970,
39
        'ultramarine' => 0x120A8F,
40
        'carmine' => 0x960018,
41
        'taupe' => 0x483C32,
42
        'chocolate' => 0x7B3F00,
43
        'auburn' => 0x6D351A,
44
        'sepia' => 0x704214,
45
        'arsenic' => 0x3B444B,
46
        'darkslategray' => 0x2F4F4F,
47
        'indigo' => 0x4B0082,
48
        'mediumblue' => 0x0000CD,
49
        'forestgreen' => 0x228B22,
50
        'charcoal' => 0x464646,
51
        'russet' => 0x80461B,
52
        'saddlebrown' => 0x8B4513,
53
        'carnelian' => 0xB31B1B,
54
        'liver' => 0x534B4F,
55
        'darkolivegreen' => 0x556B2F,
56
        'cobalt' => 0x0047AB,
57
        'eggplant' => 0x614051,
58
        'firebrick' => 0xB22222,
59
        'bole' => 0x79443B,
60
        'brown' => 0xA52A2A,
61
        'byzantium' => 0x702963,
62
        'feldgrau' => 0x4D5D53,
63
        'blue' => 0x0000FF,
64
        'lava' => 0xCF1020,
65
        'lime' => 0x00FF00,
66
        'red' => 0xFF0000,
67
        'mahogany' => 0xC04000,
68
        'olive' => 0x808000,
69
        'purple' => 0x800080,
70
        'teal' => 0x008080,
71
        'rust' => 0xB7410E,
72
        'cordovan' => 0x893F45,
73
        'darkslateblue' => 0x483D8B,
74
        'seagreen' => 0x2E8B57,
75
        'jade' => 0x00A86B,
76
        'darkcyan' => 0x008B8B,
77
        'darkmagenta' => 0x8B008B,
78
        'cardinal' => 0xC41E3A,
79
        'olivedrab' => 0x6B8E23,
80
        'sienna' => 0xA0522D,
81
        'cerulean' => 0x007BA7,
82
        'scarlet' => 0xFF2400,
83
        'crimson' => 0xDC143C,
84
        'viridian' => 0x40826D,
85
        'limegreen' => 0x32CD32,
86
        'denim' => 0x1560BD,
87
        'malachite' => 0x0BDA51,
88
        'dimgray' => 0x696969,
89
        'harlequin' => 0x3FFF00,
90
        'alizarin' => 0xE32636,
91
        'orangered' => 0xFF4500,
92
        'persimmon' => 0xEC5800,
93
        'darkgoldenrod' => 0xB8860B,
94
        'raspberry' => 0xE30B5C,
95
        'ruby' => 0xE0115F,
96
        'cinnabar' => 0xE34234,
97
        'cinnamon' => 0xD2691E,
98
        'vermilion' => 0xE34234,
99
        'copper' => 0xB87333,
100
        'amaranth' => 0xE52B50,
101
        'mediumseagreen' => 0x3CB371,
102
        'mediumvioletred' => 0xC71585,
103
        'red-violet' => 0xC71585,
104
        'ochre' => 0xCC7722,
105
        'darkviolet' => 0x9400D3,
106
        'xanadu' => 0x738678,
107
        'cerise' => 0xDE3163,
108
        'razzmatazz' => 0xE3256B,
109
        'asparagus' => 0x7BA05B,
110
        'tangerine' => 0xF28500,
111
        'lawngreen' => 0x7CFC00,
112
        'lightseagreen' => 0x20B2AA,
113
        'steelblue' => 0x4682B4,
114
        'bronze' => 0xCD7F32,
115
        'chartreuse' => 0x7FFF00,
116
        'rose' => 0xFF007F,
117
        'springgreen' => 0x00FF7F,
118
        'gray' => 0x808080,
119
        'slategray' => 0x708090,
120
        'chestnut' => 0xCD5C5C,
121
        'indianred' => 0xCD5C5C,
122
        'darkorange' => 0xFF8C00,
123
        'royalblue' => 0x4169E1,
124
        'pumpkin' => 0xFF7518,
125
        'gamboge' => 0xE49B0F,
126
        'emerald' => 0x50C878,
127
        'peru' => 0xCD853F,
128
        'slateblue' => 0x6A5ACD,
129
        'mediumspringgreen' => 0x00FA9A,
130
        'blueviolet' => 0x8A2BE2,
131
        'darkorchid' => 0x9932CC,
132
        'lightslategray' => 0x778899,
133
        'yellowgreen' => 0x9ACD32,
134
        'brass' => 0xB5A642,
135
        'cadetblue' => 0x5F9EA0,
136
        'darkturquoise' => 0x00CED1,
137
        'goldenrod' => 0xDAA520,
138
        'orange' => 0xFFA500,
139
        'deeppink' => 0xFF1493,
140
        'tomato' => 0xFF6347,
141
        'dodgerblue' => 0x1E90FF,
142
        'bluegreen' => 0x00DDDD,
143
        'amber' => 0xFFBF00,
144
        'deepskyblue' => 0x00BFFF,
145
        'fallow' => 0xC19A6B,
146
        'olivine' => 0x9AB973,
147
        'amethyst' => 0x9966CC,
148
        'turquoise' => 0x30D5C8,
149
        'coral' => 0xFF7F50,
150
        'mediumslateblue' => 0x7B68EE,
151
        'gold' => 0xFFD700,
152
        'darkseagreen' => 0x8FBC8F,
153
        'rosybrown' => 0xBC8F8F,
154
        'greenyellow' => 0xADFF2F,
155
        'mediumpurple' => 0x9370D8,
156
        'palevioletred' => 0xD87093,
157
        'mediumaquamarine' => 0x66CDAA,
158
        'darkkhaki' => 0xBDB76B,
159
        'mediumorchid' => 0xBA55D3,
160
        'pear' => 0xD1E231,
161
        'mediumturquoise' => 0x48D1CC,
162
        'cornflowerblue' => 0x6495ED,
163
        'saffron' => 0xF4C430,
164
        'salmon' => 0xFA8072,
165
        'puce' => 0xCC8899,
166
        'lightcoral' => 0xF08080,
167
        'ecru' => 0xC2B280,
168
        'lemon' => 0xFDE910,
169
        'sandybrown' => 0xF4A460,
170
        'darksalmon' => 0xE9967A,
171
        'darkgray' => 0xA9A9A9,
172
        'aqua' => 0x00FFFF,
173
        'cyan' => 0x00FFFF,
174
        'fuchsia' => 0xFF00FF,
175
        'magenta' => 0xFF00FF,
176
        'pink-orange' => 0xFF9966,
177
        'yellow' => 0xFFFF00,
178
        'lightgreen' => 0x90EE90,
179
        'tan' => 0xD2B48C,
180
        'lightsalmon' => 0xFFA07A,
181
        'hotpink' => 0xFF69B4,
182
        'burlywood' => 0xDEB887,
183
        'orchid' => 0xDA70D6,
184
        'palegreen' => 0x98FB98,
185
        'lilac' => 0xC8A2C8,
186
        'mustard' => 0xFFDB58,
187
        'celadon' => 0xACE1AF,
188
        'silver' => 0xC0C0C0,
189
        'skyblue' => 0x87CEEB,
190
        'corn' => 0xFBEC5D,
191
        'maize' => 0xFBEC5D,
192
        'wisteria' => 0xC9A0DC,
193
        'flax' => 0xEEDC82,
194
        'buff' => 0xF0DC82,
195
        'lightskyblue' => 0x87CEFA,
196
        'heliotrope' => 0xDF73FF,
197
        'aquamarine' => 0x7FFFD4,
198
        'lightsteelblue' => 0xB0C4DE,
199
        'plum' => 0xDDA0DD,
200
        'violet' => 0xEE82EE,
201
        'khaki' => 0xF0E68C,
202
        'peach-orange' => 0xFFCC99,
203
        'lightblue' => 0xADD8E6,
204
        'thistle' => 0xD8BFD8,
205
        'lightpink' => 0xFFB6C1,
206
        'powderblue' => 0xB0E0E6,
207
        'lightgrey' => 0xD3D3D3,
208
        'apricot' => 0xFBCEB1,
209
        'palegoldenrod' => 0xEEE8AA,
210
        'peach-yellow' => 0xFADFAD,
211
        'wheat' => 0xF5DEB3,
212
        'navajowhite' => 0xFFDEAD,
213
        'pink' => 0xFFC0CB,
214
        'paleturquoise' => 0xAFEEEE,
215
        'mauve' => 0xE0B0FF,
216
        'peachpuff' => 0xFFDAB9,
217
        'gainsboro' => 0xDCDCDC,
218
        'periwinkle' => 0xCCCCFF,
219
        'moccasin' => 0xFFE4B5,
220
        'peach' => 0xFFE5B4,
221
        'bisque' => 0xFFE4C4,
222
        'platinum' => 0xE5E4E2,
223
        'champaigne' => 0xF7E7CE,
224
        'blanchedalmond' => 0xFFEBCD,
225
        'antiquewhite' => 0xFAEBD7,
226
        'papayawhip' => 0xFFEFD5,
227
        'mistyrose' => 0xFFE4E1,
228
        'beige' => 0xF5F5DC,
229
        'lavender' => 0xE6E6FA,
230
        'lemonchiffon' => 0xFFFACD,
231
        'lightgoldenrodyellow' => 0xFAFAD2,
232
        'cream' => 0xFFFDD0,
233
        'linen' => 0xFAF0E6,
234
        'cornsilk' => 0xFFF8DC,
235
        'oldlace' => 0xFDF5E6,
236
        'lightcyan' => 0xE0FFFF,
237
        'lightyellow' => 0xFFFFE0,
238
        'honeydew' => 0xF0FFF0,
239
        'whitesmoke' => 0xF5F5F5,
240
        'seashell' => 0xFFF5EE,
241
        'lavenderblush' => 0xFFF0F5,
242
        'aliceblue' => 0xF0F8FF,
243
        'floralwhite' => 0xFFFAF0,
244
        'magnolia' => 0xF8F4FF,
245
        'azure' => 0xF0FFFF,
246
        'ivory' => 0xFFFFF0,
247
        'mintcream' => 0xF5FFFA,
248
        'ghostwhite' => 0xF8F8FF,
249
        'snow' => 0xFFFAFA,
250
        'white' => 0xFFFFFF,
251
    ];
252
253
    /**
254
     * 颜色模式
255
     *
256
     * @var string
257
     */
258
    protected $_type = 'unknow';
259
260
    /**
261
     * 颜色值
262
     *
263
     * @var mixed
264
     */
265
    protected $_color;
266
267
    /**
268
     * 自动类型:只支持 RGB、HEX、CMYK 的鉴别
269
     */
270
    const TYPE_AUTO = null;
271
    /**
272
     * RGB
273
     */
274
    const TYPE_RGB = 'rgb';
275
    /**
276
     * HEX
277
     */
278
    const TYPE_HEX = 'hex';
279
    /**
280
     * CMYK
281
     */
282
    const TYPE_CMYK = 'cmyk';
283
284
    /**
285
     * 创建颜色
286
     *
287
     * @param mixed $color 颜色值。
288
     * @param string $type 颜色类型
289
     *
290
     * - 如果给定 $type,则当前颜色被认作是 $type
291
     * - 支持三种颜色模式互转:十六进制、RGB、CMYK,也支持颜色名('red')转成这三种:
292
     *      1. 十六进制。传入十六进制数字(0xFF0000)或字符串('FF0000')
293
     *      2. RGB。传入三个元素的数组([255, 0, 0])
294
     *      3. CMYK。传入四个元素的数组([0.0, 90.2, 82.9, 0.0])
295
     */
296 4
    public function __construct($color, $type = self::TYPE_AUTO)
297
    {
298 4
        if (self::TYPE_AUTO === $type) {
299 4
            if (is_array($color)) {
300 1
                $this->_color = $color;
301 1
                if (3 === count($color)) {
302 1
                    $this->_type = self::TYPE_RGB;
303 1
                } elseif (4 === count($color)) {
304 1
                    $this->_type = self::TYPE_CMYK;
305
                }
306
            } else {
307
                // 接收十六进制(如:0xFF0000和'FF0000')和颜色名字
308 3
                $hex = I::get(static::$_names, $color, $color);
309 3
                if ($hex > 0xFFFFFF || $hex < 0 || hexdec($hex) === 0 && $hex !== '000000' && $hex !== '#000000') {
0 ignored issues
show
Bug introduced by
It seems like $hex can also be of type false; however, parameter $hex_string of hexdec() 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

309
                if ($hex > 0xFFFFFF || $hex < 0 || hexdec(/** @scrutinizer ignore-type */ $hex) === 0 && $hex !== '000000' && $hex !== '#000000') {
Loading history...
310 1
                    throw new Exception('错误的颜色值:' . $color);
311
                }
312
                // 如果是字符形式的十六进制数,则先转成十进制再作后续运算
313 3
                if (is_string($hex)) {
314 1
                    $hex = hexdec($hex);
315
                }
316 3
                $this->_type = self::TYPE_HEX;
317 3
                $this->_color = $hex;
318
            }
319 4
            if ('unknow' === $this->_type) {
320 4
                throw new Exception('错误的颜色类型');
321
            }
322
        } else {
323 1
            $this->_color = $color;
324 1
            $this->_type = $type;
325
        }
326 4
    }
327
328
    /**
329
     * 转成 RGB
330
     *
331
     * @return static
332
     */
333 4
    public function toRGB()
334
    {
335 4
        $type = $this->_type;
336 4
        if (self::TYPE_RGB === $type) {
337
            $this->_color = array_map(function ($i) {
338 4
                return (0.5 + $i) | 0;
339 4
            }, $this->_color);
340 4
        } elseif (self::TYPE_HEX === $type) {
341 3
            $red = ($this->_color & 0xFF0000) >> 16;
342 3
            $green = ($this->_color & 0x00FF00) >> 8;
343 3
            $blue = ($this->_color & 0x0000FF);
344 3
            $this->_color = [$red, $green, $blue];
345 3
            $this->_type = self::TYPE_RGB;
346 3
            $this->toRGB();
347 1
        } elseif (self::TYPE_CMYK === $type) {
348 1
            $cyan = $this->_color[0] * (1 - $this->_color[3]) + $this->_color[3];
349 1
            $magenta = $this->_color[1] * (1 - $this->_color[3]) + $this->_color[3];
350 1
            $yellow = $this->_color[2] * (1 - $this->_color[3]) + $this->_color[3];
351 1
            $this->_color = [(1 - $cyan) * 255, (1 - $magenta) * 255, (1 - $yellow) * 255];
352 1
            $this->_type = self::TYPE_RGB;
353 1
            $this->toRGB();
354
        }
355
356 4
        return $this;
357
    }
358
359
    /**
360
     * 转成十六进制字符串
361
     *
362
     * @return static
363
     */
364 1
    public function toHex()
365
    {
366 1
        $type = $this->_type;
367 1
        if (self::TYPE_HEX === $type) {
368
            // 什么也不做
369 1
            $this->_type = self::TYPE_HEX;
370 1
        } elseif (self::TYPE_RGB === $type) {
371 1
            $this->_color = strtoupper(dechex($this->_color[0] << 16 | $this->_color[1] << 8 | $this->_color[2]));
372 1
            $this->_type = self::TYPE_HEX;
373 1
            $this->toHex();
374 1
        } elseif (self::TYPE_CMYK === $type) {
375 1
            $this->toRGB()->toHex();
376
        }
377
378 1
        return $this;
379
    }
380
381
    /**
382
     * 转成 CMYK
383
     *
384
     * @return static
385
     */
386 1
    public function toCMYK()
387
    {
388 1
        $type = $this->_type;
389 1
        if (self::TYPE_CMYK === $type) {
390
            $this->_color = array_map(function ($i) {
391 1
                return sprintf('%01.4f', $i);
392 1
            }, $this->_color);
393 1
            $this->_type = self::TYPE_CMYK;
394 1
        } elseif (self::TYPE_RGB === $type) {
395 1
            $cyan = 1 - ($this->_color[0] / 255);
396 1
            $magenta = 1 - ($this->_color[1] / 255);
397 1
            $yellow = 1 - ($this->_color[2] / 255);
398 1
            $var_K = 1;
399 1
            if ($cyan < $var_K) {
400 1
                $var_K = $cyan;
401
            }
402 1
            if ($magenta < $var_K) {
403 1
                $var_K = $magenta;
404
            }
405 1
            if ($yellow < $var_K) {
406 1
                $var_K = $yellow;
407
            }
408 1
            if ($var_K == 1) {
409 1
                $cyan = 0;
410 1
                $magenta = 0;
411 1
                $yellow = 0;
412
            } else {
413 1
                $cyan = ($cyan - $var_K) / (1 - $var_K);
414 1
                $magenta = ($magenta - $var_K) / (1 - $var_K);
415 1
                $yellow = ($yellow - $var_K) / (1 - $var_K);
416
            }
417
418 1
            $key = $var_K;
419 1
            $this->_color = [$cyan, $magenta, $yellow, $key];
420 1
            $this->_type = self::TYPE_CMYK;
421 1
            $this->toCMYK();
422 1
        } elseif (self::TYPE_HEX === $type) {
423 1
            $this->toRGB()->toCMYK();
424
        }
425
426 1
        return $this;
427
    }
428
429
    /**
430
     * 返回颜色值
431
     *
432
     * @return mixed
433
     */
434 3
    public function get()
435
    {
436 3
        return $this->_color;
437
    }
438
439
    /**
440
     * __toString
441
     *
442
     * @return string
443
     */
444 1
    public function __toString()
445
    {
446 1
        return is_string($this->_color) ? $this->_color : implode(',', $this->_color);
447
    }
448
}
449