Passed
Push — master ( d4a329...711fef )
by Jeroen De
03:36
created

Colors::RGBaToColorName()   B

Complexity

Conditions 11
Paths 6

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 16
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 32
rs 7.3166

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
/**
4
 * SCSSPHP
5
 *
6
 * @copyright 2012-2020 Leaf Corcoran
7
 *
8
 * @license http://opensource.org/licenses/MIT MIT
9
 *
10
 * @link http://scssphp.github.io/scssphp
11
 */
12
13
namespace ScssPhp\ScssPhp;
14
15
/**
16
 * CSS Colors
17
 *
18
 * @author Leaf Corcoran <[email protected]>
19
 */
20
class Colors
21
{
22
    /**
23
     * CSS Colors
24
     *
25
     * @see http://www.w3.org/TR/css3-color
26
     *
27
     * @var array
28
     */
29
    protected static $cssColors = [
30
        'aliceblue' => '240,248,255',
31
        'antiquewhite' => '250,235,215',
32
        'cyan' => '0,255,255',
33
        'aqua' => '0,255,255',
34
        'aquamarine' => '127,255,212',
35
        'azure' => '240,255,255',
36
        'beige' => '245,245,220',
37
        'bisque' => '255,228,196',
38
        'black' => '0,0,0',
39
        'blanchedalmond' => '255,235,205',
40
        'blue' => '0,0,255',
41
        'blueviolet' => '138,43,226',
42
        'brown' => '165,42,42',
43
        'burlywood' => '222,184,135',
44
        'cadetblue' => '95,158,160',
45
        'chartreuse' => '127,255,0',
46
        'chocolate' => '210,105,30',
47
        'coral' => '255,127,80',
48
        'cornflowerblue' => '100,149,237',
49
        'cornsilk' => '255,248,220',
50
        'crimson' => '220,20,60',
51
        'darkblue' => '0,0,139',
52
        'darkcyan' => '0,139,139',
53
        'darkgoldenrod' => '184,134,11',
54
        'darkgray' => '169,169,169',
55
        'darkgrey' => '169,169,169',
56
        'darkgreen' => '0,100,0',
57
        'darkkhaki' => '189,183,107',
58
        'darkmagenta' => '139,0,139',
59
        'darkolivegreen' => '85,107,47',
60
        'darkorange' => '255,140,0',
61
        'darkorchid' => '153,50,204',
62
        'darkred' => '139,0,0',
63
        'darksalmon' => '233,150,122',
64
        'darkseagreen' => '143,188,143',
65
        'darkslateblue' => '72,61,139',
66
        'darkslategray' => '47,79,79',
67
        'darkslategrey' => '47,79,79',
68
        'darkturquoise' => '0,206,209',
69
        'darkviolet' => '148,0,211',
70
        'deeppink' => '255,20,147',
71
        'deepskyblue' => '0,191,255',
72
        'dimgray' => '105,105,105',
73
        'dimgrey' => '105,105,105',
74
        'dodgerblue' => '30,144,255',
75
        'firebrick' => '178,34,34',
76
        'floralwhite' => '255,250,240',
77
        'forestgreen' => '34,139,34',
78
        'magenta' => '255,0,255',
79
        'fuchsia' => '255,0,255',
80
        'gainsboro' => '220,220,220',
81
        'ghostwhite' => '248,248,255',
82
        'gold' => '255,215,0',
83
        'goldenrod' => '218,165,32',
84
        'gray' => '128,128,128',
85
        'grey' => '128,128,128',
86
        'green' => '0,128,0',
87
        'greenyellow' => '173,255,47',
88
        'honeydew' => '240,255,240',
89
        'hotpink' => '255,105,180',
90
        'indianred' => '205,92,92',
91
        'indigo' => '75,0,130',
92
        'ivory' => '255,255,240',
93
        'khaki' => '240,230,140',
94
        'lavender' => '230,230,250',
95
        'lavenderblush' => '255,240,245',
96
        'lawngreen' => '124,252,0',
97
        'lemonchiffon' => '255,250,205',
98
        'lightblue' => '173,216,230',
99
        'lightcoral' => '240,128,128',
100
        'lightcyan' => '224,255,255',
101
        'lightgoldenrodyellow' => '250,250,210',
102
        'lightgray' => '211,211,211',
103
        'lightgrey' => '211,211,211',
104
        'lightgreen' => '144,238,144',
105
        'lightpink' => '255,182,193',
106
        'lightsalmon' => '255,160,122',
107
        'lightseagreen' => '32,178,170',
108
        'lightskyblue' => '135,206,250',
109
        'lightslategray' => '119,136,153',
110
        'lightslategrey' => '119,136,153',
111
        'lightsteelblue' => '176,196,222',
112
        'lightyellow' => '255,255,224',
113
        'lime' => '0,255,0',
114
        'limegreen' => '50,205,50',
115
        'linen' => '250,240,230',
116
        'maroon' => '128,0,0',
117
        'mediumaquamarine' => '102,205,170',
118
        'mediumblue' => '0,0,205',
119
        'mediumorchid' => '186,85,211',
120
        'mediumpurple' => '147,112,219',
121
        'mediumseagreen' => '60,179,113',
122
        'mediumslateblue' => '123,104,238',
123
        'mediumspringgreen' => '0,250,154',
124
        'mediumturquoise' => '72,209,204',
125
        'mediumvioletred' => '199,21,133',
126
        'midnightblue' => '25,25,112',
127
        'mintcream' => '245,255,250',
128
        'mistyrose' => '255,228,225',
129
        'moccasin' => '255,228,181',
130
        'navajowhite' => '255,222,173',
131
        'navy' => '0,0,128',
132
        'oldlace' => '253,245,230',
133
        'olive' => '128,128,0',
134
        'olivedrab' => '107,142,35',
135
        'orange' => '255,165,0',
136
        'orangered' => '255,69,0',
137
        'orchid' => '218,112,214',
138
        'palegoldenrod' => '238,232,170',
139
        'palegreen' => '152,251,152',
140
        'paleturquoise' => '175,238,238',
141
        'palevioletred' => '219,112,147',
142
        'papayawhip' => '255,239,213',
143
        'peachpuff' => '255,218,185',
144
        'peru' => '205,133,63',
145
        'pink' => '255,192,203',
146
        'plum' => '221,160,221',
147
        'powderblue' => '176,224,230',
148
        'purple' => '128,0,128',
149
        'red' => '255,0,0',
150
        'rosybrown' => '188,143,143',
151
        'royalblue' => '65,105,225',
152
        'saddlebrown' => '139,69,19',
153
        'salmon' => '250,128,114',
154
        'sandybrown' => '244,164,96',
155
        'seagreen' => '46,139,87',
156
        'seashell' => '255,245,238',
157
        'sienna' => '160,82,45',
158
        'silver' => '192,192,192',
159
        'skyblue' => '135,206,235',
160
        'slateblue' => '106,90,205',
161
        'slategray' => '112,128,144',
162
        'slategrey' => '112,128,144',
163
        'snow' => '255,250,250',
164
        'springgreen' => '0,255,127',
165
        'steelblue' => '70,130,180',
166
        'tan' => '210,180,140',
167
        'teal' => '0,128,128',
168
        'thistle' => '216,191,216',
169
        'tomato' => '255,99,71',
170
        'turquoise' => '64,224,208',
171
        'violet' => '238,130,238',
172
        'wheat' => '245,222,179',
173
        'white' => '255,255,255',
174
        'whitesmoke' => '245,245,245',
175
        'yellow' => '255,255,0',
176
        'yellowgreen' => '154,205,50',
177
        'rebeccapurple' => '102,51,153',
178
        'transparent' => '0,0,0,0',
179
    ];
180
181
    /**
182
     * Convert named color in a [r,g,b[,a]] array
183
     *
184
     * @param string $colorName
185
     *
186
     * @return array|null
187
     */
188
    public static function colorNameToRGBa($colorName)
189
    {
190
        if (\is_string($colorName) && isset(static::$cssColors[$colorName])) {
191
            $rgba = explode(',', static::$cssColors[$colorName]);
192
193
            // only case with opacity is transparent, with opacity=0, so we can intval on opacity also
194
            $rgba = array_map('intval', $rgba);
195
196
            return $rgba;
197
        }
198
199
        return null;
200
    }
201
202
    /**
203
     * Reverse conversion : from RGBA to a color name if possible
204
     *
205
     * @param integer $r
206
     * @param integer $g
207
     * @param integer $b
208
     * @param integer $a
209
     *
210
     * @return string|null
211
     */
212
    public static function RGBaToColorName($r, $g, $b, $a = 1)
213
    {
214
        static $reverseColorTable = null;
215
216
        if (! is_numeric($r) || ! is_numeric($g) || ! is_numeric($b) || ! is_numeric($a)) {
0 ignored issues
show
introduced by
The condition is_numeric($a) is always true.
Loading history...
introduced by
The condition is_numeric($g) is always true.
Loading history...
introduced by
The condition is_numeric($b) is always true.
Loading history...
introduced by
The condition is_numeric($r) is always true.
Loading history...
217
            return null;
218
        }
219
220
        if ($a < 1) {
221
            return null;
222
        }
223
224
        if (\is_null($reverseColorTable)) {
225
            $reverseColorTable = [];
226
227
            foreach (static::$cssColors as $name => $rgb_str) {
228
                $rgb_str = explode(',', $rgb_str);
229
230
                if (
231
                    \count($rgb_str) == 3 &&
232
                    ! isset($reverseColorTable[\intval($rgb_str[0])][\intval($rgb_str[1])][\intval($rgb_str[2])])
233
                ) {
234
                    $reverseColorTable[\intval($rgb_str[0])][\intval($rgb_str[1])][\intval($rgb_str[2])] = $name;
235
                }
236
            }
237
        }
238
239
        if (isset($reverseColorTable[\intval($r)][\intval($g)][\intval($b)])) {
240
            return $reverseColorTable[\intval($r)][\intval($g)][\intval($b)];
241
        }
242
243
        return null;
244
    }
245
}
246