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 ( 24ae15...d40ff0 )
by t
05:40 queued 03:33
created

Color::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
use icy2003\php\C;
15
16
/**
17
 * 颜色处理
18
 */
19
class Color
20
{
21
    /**
22
     * 颜色名
23
     *
24
     * @var array
25
     */
26
    protected static $_names = [
27
        'black' => 0x000000,
28
        'darkgreen' => 0x006400,
29
        'green' => 0x008000,
30
        'maroon' => 0x800000,
31
        'navy' => 0x000080,
32
        'myrtle' => 0x21421E,
33
        'bistre' => 0x3D2B1F,
34
        'darkblue' => 0x00008B,
35
        'darkred' => 0x8B0000,
36
        'sapphire' => 0x082567,
37
        'sangria' => 0x92000A,
38
        'burgundy' => 0x800020,
39
        'midnightblue' => 0x191970,
40
        'ultramarine' => 0x120A8F,
41
        'carmine' => 0x960018,
42
        'taupe' => 0x483C32,
43
        'chocolate' => 0x7B3F00,
44
        'auburn' => 0x6D351A,
45
        'sepia' => 0x704214,
46
        'arsenic' => 0x3B444B,
47
        'darkslategray' => 0x2F4F4F,
48
        'indigo' => 0x4B0082,
49
        'mediumblue' => 0x0000CD,
50
        'forestgreen' => 0x228B22,
51
        'charcoal' => 0x464646,
52
        'russet' => 0x80461B,
53
        'saddlebrown' => 0x8B4513,
54
        'carnelian' => 0xB31B1B,
55
        'liver' => 0x534B4F,
56
        'darkolivegreen' => 0x556B2F,
57
        'cobalt' => 0x0047AB,
58
        'eggplant' => 0x614051,
59
        'firebrick' => 0xB22222,
60
        'bole' => 0x79443B,
61
        'brown' => 0xA52A2A,
62
        'byzantium' => 0x702963,
63
        'feldgrau' => 0x4D5D53,
64
        'blue' => 0x0000FF,
65
        'lava' => 0xCF1020,
66
        'lime' => 0x00FF00,
67
        'red' => 0xFF0000,
68
        'mahogany' => 0xC04000,
69
        'olive' => 0x808000,
70
        'purple' => 0x800080,
71
        'teal' => 0x008080,
72
        'rust' => 0xB7410E,
73
        'cordovan' => 0x893F45,
74
        'darkslateblue' => 0x483D8B,
75
        'seagreen' => 0x2E8B57,
76
        'jade' => 0x00A86B,
77
        'darkcyan' => 0x008B8B,
78
        'darkmagenta' => 0x8B008B,
79
        'cardinal' => 0xC41E3A,
80
        'olivedrab' => 0x6B8E23,
81
        'sienna' => 0xA0522D,
82
        'cerulean' => 0x007BA7,
83
        'scarlet' => 0xFF2400,
84
        'crimson' => 0xDC143C,
85
        'viridian' => 0x40826D,
86
        'limegreen' => 0x32CD32,
87
        'denim' => 0x1560BD,
88
        'malachite' => 0x0BDA51,
89
        'dimgray' => 0x696969,
90
        'harlequin' => 0x3FFF00,
91
        'alizarin' => 0xE32636,
92
        'orangered' => 0xFF4500,
93
        'persimmon' => 0xEC5800,
94
        'darkgoldenrod' => 0xB8860B,
95
        'raspberry' => 0xE30B5C,
96
        'ruby' => 0xE0115F,
97
        'cinnabar' => 0xE34234,
98
        'cinnamon' => 0xD2691E,
99
        'vermilion' => 0xE34234,
100
        'copper' => 0xB87333,
101
        'amaranth' => 0xE52B50,
102
        'mediumseagreen' => 0x3CB371,
103
        'mediumvioletred' => 0xC71585,
104
        'red-violet' => 0xC71585,
105
        'ochre' => 0xCC7722,
106
        'darkviolet' => 0x9400D3,
107
        'xanadu' => 0x738678,
108
        'cerise' => 0xDE3163,
109
        'razzmatazz' => 0xE3256B,
110
        'asparagus' => 0x7BA05B,
111
        'tangerine' => 0xF28500,
112
        'lawngreen' => 0x7CFC00,
113
        'lightseagreen' => 0x20B2AA,
114
        'steelblue' => 0x4682B4,
115
        'bronze' => 0xCD7F32,
116
        'chartreuse' => 0x7FFF00,
117
        'rose' => 0xFF007F,
118
        'springgreen' => 0x00FF7F,
119
        'gray' => 0x808080,
120
        'slategray' => 0x708090,
121
        'chestnut' => 0xCD5C5C,
122
        'indianred' => 0xCD5C5C,
123
        'darkorange' => 0xFF8C00,
124
        'royalblue' => 0x4169E1,
125
        'pumpkin' => 0xFF7518,
126
        'gamboge' => 0xE49B0F,
127
        'emerald' => 0x50C878,
128
        'peru' => 0xCD853F,
129
        'slateblue' => 0x6A5ACD,
130
        'mediumspringgreen' => 0x00FA9A,
131
        'blueviolet' => 0x8A2BE2,
132
        'darkorchid' => 0x9932CC,
133
        'lightslategray' => 0x778899,
134
        'yellowgreen' => 0x9ACD32,
135
        'brass' => 0xB5A642,
136
        'cadetblue' => 0x5F9EA0,
137
        'darkturquoise' => 0x00CED1,
138
        'goldenrod' => 0xDAA520,
139
        'orange' => 0xFFA500,
140
        'deeppink' => 0xFF1493,
141
        'tomato' => 0xFF6347,
142
        'dodgerblue' => 0x1E90FF,
143
        'bluegreen' => 0x00DDDD,
144
        'amber' => 0xFFBF00,
145
        'deepskyblue' => 0x00BFFF,
146
        'fallow' => 0xC19A6B,
147
        'olivine' => 0x9AB973,
148
        'amethyst' => 0x9966CC,
149
        'turquoise' => 0x30D5C8,
150
        'coral' => 0xFF7F50,
151
        'mediumslateblue' => 0x7B68EE,
152
        'gold' => 0xFFD700,
153
        'darkseagreen' => 0x8FBC8F,
154
        'rosybrown' => 0xBC8F8F,
155
        'greenyellow' => 0xADFF2F,
156
        'mediumpurple' => 0x9370D8,
157
        'palevioletred' => 0xD87093,
158
        'mediumaquamarine' => 0x66CDAA,
159
        'darkkhaki' => 0xBDB76B,
160
        'mediumorchid' => 0xBA55D3,
161
        'pear' => 0xD1E231,
162
        'mediumturquoise' => 0x48D1CC,
163
        'cornflowerblue' => 0x6495ED,
164
        'saffron' => 0xF4C430,
165
        'salmon' => 0xFA8072,
166
        'puce' => 0xCC8899,
167
        'lightcoral' => 0xF08080,
168
        'ecru' => 0xC2B280,
169
        'lemon' => 0xFDE910,
170
        'sandybrown' => 0xF4A460,
171
        'darksalmon' => 0xE9967A,
172
        'darkgray' => 0xA9A9A9,
173
        'aqua' => 0x00FFFF,
174
        'cyan' => 0x00FFFF,
175
        'fuchsia' => 0xFF00FF,
176
        'magenta' => 0xFF00FF,
177
        'pink-orange' => 0xFF9966,
178
        'yellow' => 0xFFFF00,
179
        'lightgreen' => 0x90EE90,
180
        'tan' => 0xD2B48C,
181
        'lightsalmon' => 0xFFA07A,
182
        'hotpink' => 0xFF69B4,
183
        'burlywood' => 0xDEB887,
184
        'orchid' => 0xDA70D6,
185
        'palegreen' => 0x98FB98,
186
        'lilac' => 0xC8A2C8,
187
        'mustard' => 0xFFDB58,
188
        'celadon' => 0xACE1AF,
189
        'silver' => 0xC0C0C0,
190
        'skyblue' => 0x87CEEB,
191
        'corn' => 0xFBEC5D,
192
        'maize' => 0xFBEC5D,
193
        'wisteria' => 0xC9A0DC,
194
        'flax' => 0xEEDC82,
195
        'buff' => 0xF0DC82,
196
        'lightskyblue' => 0x87CEFA,
197
        'heliotrope' => 0xDF73FF,
198
        'aquamarine' => 0x7FFFD4,
199
        'lightsteelblue' => 0xB0C4DE,
200
        'plum' => 0xDDA0DD,
201
        'violet' => 0xEE82EE,
202
        'khaki' => 0xF0E68C,
203
        'peach-orange' => 0xFFCC99,
204
        'lightblue' => 0xADD8E6,
205
        'thistle' => 0xD8BFD8,
206
        'lightpink' => 0xFFB6C1,
207
        'powderblue' => 0xB0E0E6,
208
        'lightgrey' => 0xD3D3D3,
209
        'apricot' => 0xFBCEB1,
210
        'palegoldenrod' => 0xEEE8AA,
211
        'peach-yellow' => 0xFADFAD,
212
        'wheat' => 0xF5DEB3,
213
        'navajowhite' => 0xFFDEAD,
214
        'pink' => 0xFFC0CB,
215
        'paleturquoise' => 0xAFEEEE,
216
        'mauve' => 0xE0B0FF,
217
        'peachpuff' => 0xFFDAB9,
218
        'gainsboro' => 0xDCDCDC,
219
        'periwinkle' => 0xCCCCFF,
220
        'moccasin' => 0xFFE4B5,
221
        'peach' => 0xFFE5B4,
222
        'bisque' => 0xFFE4C4,
223
        'platinum' => 0xE5E4E2,
224
        'champaigne' => 0xF7E7CE,
225
        'blanchedalmond' => 0xFFEBCD,
226
        'antiquewhite' => 0xFAEBD7,
227
        'papayawhip' => 0xFFEFD5,
228
        'mistyrose' => 0xFFE4E1,
229
        'beige' => 0xF5F5DC,
230
        'lavender' => 0xE6E6FA,
231
        'lemonchiffon' => 0xFFFACD,
232
        'lightgoldenrodyellow' => 0xFAFAD2,
233
        'cream' => 0xFFFDD0,
234
        'linen' => 0xFAF0E6,
235
        'cornsilk' => 0xFFF8DC,
236
        'oldlace' => 0xFDF5E6,
237
        'lightcyan' => 0xE0FFFF,
238
        'lightyellow' => 0xFFFFE0,
239
        'honeydew' => 0xF0FFF0,
240
        'whitesmoke' => 0xF5F5F5,
241
        'seashell' => 0xFFF5EE,
242
        'lavenderblush' => 0xFFF0F5,
243
        'aliceblue' => 0xF0F8FF,
244
        'floralwhite' => 0xFFFAF0,
245
        'magnolia' => 0xF8F4FF,
246
        'azure' => 0xF0FFFF,
247
        'ivory' => 0xFFFFF0,
248
        'mintcream' => 0xF5FFFA,
249
        'ghostwhite' => 0xF8F8FF,
250
        'snow' => 0xFFFAFA,
251
        'white' => 0xFFFFFF,
252
    ];
253
254
    /**
255
     * 颜色模式
256
     *
257
     * @var string
258
     */
259
    protected $_type = 'unknow';
260
261
    /**
262
     * 颜色值
263
     *
264
     * @var mixed
265
     */
266
    protected $_color;
267
268
    /**
269
     * 自动类型:只支持 RGB、HEX、CMYK 的鉴别
270
     */
271
    const TYPE_AUTO = null;
272
    /**
273
     * RGB
274
     */
275
    const TYPE_RGB = 'rgb';
276
    /**
277
     * HEX
278
     */
279
    const TYPE_HEX = 'hex';
280
    /**
281
     * CMYK
282
     */
283
    const TYPE_CMYK = 'cmyk';
284
285
    /**
286
     * 创建颜色
287
     *
288
     * @param mixed $color 颜色值。
289
     * @param string $type 颜色类型
290
     *
291
     * - 如果给定 $type,则当前颜色被认作是 $type
292
     * - 支持三种颜色模式互转:十六进制、RGB、CMYK,也支持颜色名('red')转成这三种:
293
     *      1. 十六进制。传入十六进制数字(0xFF0000)或字符串('FF0000')
294
     *      2. RGB。传入三个元素的数组([255, 0, 0])
295
     *      3. CMYK。传入四个元素的数组([0.0, 90.2, 82.9, 0.0])
296
     */
297 4
    public function __construct($color, $type = self::TYPE_AUTO)
298
    {
299 4
        if (self::TYPE_AUTO === $type) {
300 4
            if (is_array($color)) {
301 1
                $this->_color = $color;
302 1
                if (3 === count($color)) {
303 1
                    $this->_type = self::TYPE_RGB;
304 1
                } elseif (4 === count($color)) {
305 1
                    $this->_type = self::TYPE_CMYK;
306
                }
307
            } else {
308
                // 接收十六进制(如:0xFF0000和'FF0000')和颜色名字
309 3
                $hex = I::get(self::$_names, $color, $color);
310 3
                if ($hex) {
311 3
                    C::assertNotTrue($hex > 0xFFFFFF || $hex < 0 || hexdec($hex) === 0 && $hex !== '000000' && $hex !== '#000000' , '错误的颜色值:' . $color);
312
                }
313
                // 如果是字符形式的十六进制数,则先转成十进制再作后续运算
314 3
                if (is_string($hex)) {
315 1
                    $hex = hexdec($hex);
316
                }
317 3
                $this->_type = self::TYPE_HEX;
318 3
                $this->_color = $hex;
319
            }
320 4
            C::assertNotTrue('unknow' === $this->_type, '错误的颜色类型');
321
        } else {
322 1
            $this->_color = $color;
323 1
            $this->_type = $type;
324
        }
325 4
    }
326
327
    /**
328
     * 转成 RGB
329
     *
330
     * @return static
331
     */
332 4
    public function toRGB()
333
    {
334 4
        $type = $this->_type;
335 4
        if (self::TYPE_RGB === $type) {
336
            $this->_color = array_map(function ($i) {
337 4
                return (0.5 + $i) | 0;
338 4
            }, $this->_color);
339 4
        } elseif (self::TYPE_HEX === $type) {
340 3
            $red = ($this->_color & 0xFF0000) >> 16;
341 3
            $green = ($this->_color & 0x00FF00) >> 8;
342 3
            $blue = ($this->_color & 0x0000FF);
343 3
            $this->_color = [$red, $green, $blue];
344 3
            $this->_type = self::TYPE_RGB;
345 3
            $this->toRGB();
346 1
        } elseif (self::TYPE_CMYK === $type) {
347 1
            $cyan = $this->_color[0] * (1 - $this->_color[3]) + $this->_color[3];
348 1
            $magenta = $this->_color[1] * (1 - $this->_color[3]) + $this->_color[3];
349 1
            $yellow = $this->_color[2] * (1 - $this->_color[3]) + $this->_color[3];
350 1
            $this->_color = [(1 - $cyan) * 255, (1 - $magenta) * 255, (1 - $yellow) * 255];
351 1
            $this->_type = self::TYPE_RGB;
352 1
            $this->toRGB();
353
        }
354
355 4
        return $this;
356
    }
357
358
    /**
359
     * 转成十六进制字符串
360
     *
361
     * @return static
362
     */
363 1
    public function toHex()
364
    {
365 1
        $type = $this->_type;
366 1
        if (self::TYPE_HEX === $type) {
367
            // 什么也不做
368 1
            $this->_type = self::TYPE_HEX;
369 1
        } elseif (self::TYPE_RGB === $type) {
370 1
            $this->_color = strtoupper(dechex($this->_color[0] << 16 | $this->_color[1] << 8 | $this->_color[2]));
371 1
            $this->_type = self::TYPE_HEX;
372 1
            $this->toHex();
373 1
        } elseif (self::TYPE_CMYK === $type) {
374 1
            $this->toRGB()->toHex();
375
        }
376
377 1
        return $this;
378
    }
379
380
    /**
381
     * 转成 CMYK
382
     *
383
     * @return static
384
     */
385 1
    public function toCMYK()
386
    {
387 1
        $type = $this->_type;
388 1
        if (self::TYPE_CMYK === $type) {
389
            $this->_color = array_map(function ($i) {
390 1
                return sprintf('%01.4f', $i);
391 1
            }, $this->_color);
392 1
            $this->_type = self::TYPE_CMYK;
393 1
        } elseif (self::TYPE_RGB === $type) {
394 1
            $cyan = 1 - ($this->_color[0] / 255);
395 1
            $magenta = 1 - ($this->_color[1] / 255);
396 1
            $yellow = 1 - ($this->_color[2] / 255);
397 1
            $var_K = 1;
398 1
            if ($cyan < $var_K) {
399 1
                $var_K = $cyan;
400
            }
401 1
            if ($magenta < $var_K) {
402 1
                $var_K = $magenta;
403
            }
404 1
            if ($yellow < $var_K) {
405 1
                $var_K = $yellow;
406
            }
407 1
            if ($var_K == 1) {
408 1
                $cyan = 0;
409 1
                $magenta = 0;
410 1
                $yellow = 0;
411
            } else {
412 1
                $cyan = ($cyan - $var_K) / (1 - $var_K);
413 1
                $magenta = ($magenta - $var_K) / (1 - $var_K);
414 1
                $yellow = ($yellow - $var_K) / (1 - $var_K);
415
            }
416
417 1
            $key = $var_K;
418 1
            $this->_color = [$cyan, $magenta, $yellow, $key];
419 1
            $this->_type = self::TYPE_CMYK;
420 1
            $this->toCMYK();
421 1
        } elseif (self::TYPE_HEX === $type) {
422 1
            $this->toRGB()->toCMYK();
423
        }
424
425 1
        return $this;
426
    }
427
428
    /**
429
     * 返回颜色值
430
     *
431
     * @return mixed
432
     */
433 3
    public function get()
434
    {
435 3
        return $this->_color;
436
    }
437
438
    /**
439
     * __toString
440
     *
441
     * @return string
442
     */
443 1
    public function __toString()
444
    {
445 1
        return is_string($this->_color) ? $this->_color : implode(',', $this->_color);
446
    }
447
}
448