Completed
Push — master ( e60207...37fe77 )
by Jonathan
05:38
created

Color::getHexFromColorName()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 152

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 147
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 152
ccs 147
cts 147
cp 1
rs 8
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2

How to fix   Long Method   

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
namespace League\Glide\Manipulators\Helpers;
4
5
class Color
6
{
7
    /**
8
     * 3 digit color code expression.
9
     */
10
    const SHORT_RGB = '/^[0-9a-f]{3}$/i';
11
12
    /**
13
     * 4 digit color code expression.
14
     */
15
    const SHORT_ARGB = '/^[0-9]{1}[0-9a-f]{3}$/i';
16
17
    /**
18
     * 6 digit color code expression.
19
     */
20
    const LONG_RGB = '/^[0-9a-f]{6}$/i';
21
22
    /**
23
     * 8 digit color code expression.
24
     */
25
    const LONG_ARGB = '/^[0-9]{2}[0-9a-f]{6}$/i';
26
27
    /**
28
     * The red value.
29
     * @var int
30
     */
31
    protected $red;
32
33
    /**
34
     * The green value.
35
     * @var int
36
     */
37
    protected $green;
38
39
    /**
40
     * The blue value.
41
     * @var int
42
     */
43
    protected $blue;
44
45
    /**
46
     * The alpha value.
47
     * @var int|double
48
     */
49
    protected $alpha;
50
51
    /**
52
     * Create color helper instance.
53
     * @param string $value The color value.
54
     */
55 39
    public function __construct($value)
56
    {
57
        do {
58 39
            if ($hex = $this->getHexFromColorName($value)) {
59 15
                $rgba = $this->parseHex($hex);
60 15
                $alpha = 1;
61 15
                break;
62
            }
63
64 24
            if (preg_match(self::SHORT_RGB, $value)) {
65 3
                $rgba = $this->parseHex($value.$value);
66 3
                $alpha = 1;
67 3
                break;
68
            }
69
70 21
            if (preg_match(self::SHORT_ARGB, $value)) {
71 12
                $rgba = $this->parseHex(substr($value, 1).substr($value, 1));
72 12
                $alpha = substr($value, 0, 1) / 10;
73 12
                break;
74
            }
75
76 9
            if (preg_match(self::LONG_RGB, $value)) {
77 3
                $rgba = $this->parseHex($value);
78 3
                $alpha = 1;
79 3
                break;
80
            }
81
82 6
            if (preg_match(self::LONG_ARGB, $value)) {
83 3
                $rgba = $this->parseHex(substr($value, 2));
84 3
                $alpha = substr($value, 0, 2) / 100;
85 3
                break;
86
            }
87
88 3
            $rgba = [255, 255, 255];
89 3
            $alpha = 0;
90 3
        } while (false);
91
92 39
        $this->red = $rgba[0];
93 39
        $this->green = $rgba[1];
94 39
        $this->blue = $rgba[2];
95 39
        $this->alpha = $alpha;
96 39
    }
97
98
    /**
99
     * Parse hex color to RGB values.
100
     * @param  string $hex The hex value.
101
     * @return array  The RGB values.
102
     */
103 36
    public function parseHex($hex)
104
    {
105 36
        return array_map('hexdec', str_split($hex, 2));
106
    }
107
108
    /**
109
     * Format color for consumption.
110
     * @return string The formatted color.
111
     */
112 39
    public function formatted()
113
    {
114 39
        return 'rgba('.$this->red.', '.$this->green.', '.$this->blue.', '.$this->alpha.')';
115
    }
116
117
    /**
118
     * Get hex code by color name.
119
     * @param  string $name The color name.
120
     * @return string The hex code.
121
     */
122 39
    public function getHexFromColorName($name)
123
    {
124
        $colors = [
125 39
            'aliceblue' => 'F0F8FF',
126 39
            'antiquewhite' => 'FAEBD7',
127 39
            'aqua' => '00FFFF',
128 39
            'aquamarine' => '7FFFD4',
129 39
            'azure' => 'F0FFFF',
130 39
            'beige' => 'F5F5DC',
131 39
            'bisque' => 'FFE4C4',
132 39
            'black' => '000000',
133 39
            'blanchedalmond' => 'FFEBCD',
134 39
            'blue' => '0000FF',
135 39
            'blueviolet' => '8A2BE2',
136 39
            'brown' => 'A52A2A',
137 39
            'burlywood' => 'DEB887',
138 39
            'cadetblue' => '5F9EA0',
139 39
            'chartreuse' => '7FFF00',
140 39
            'chocolate' => 'D2691E',
141 39
            'coral' => 'FF7F50',
142 39
            'cornflowerblue' => '6495ED',
143 39
            'cornsilk' => 'FFF8DC',
144 39
            'crimson' => 'DC143C',
145 39
            'cyan' => '00FFFF',
146 39
            'darkblue' => '00008B',
147 39
            'darkcyan' => '008B8B',
148 39
            'darkgoldenrod' => 'B8860B',
149 39
            'darkgray' => 'A9A9A9',
150 39
            'darkgreen' => '006400',
151 39
            'darkkhaki' => 'BDB76B',
152 39
            'darkmagenta' => '8B008B',
153 39
            'darkolivegreen' => '556B2F',
154 39
            'darkorange' => 'FF8C00',
155 39
            'darkorchid' => '9932CC',
156 39
            'darkred' => '8B0000',
157 39
            'darksalmon' => 'E9967A',
158 39
            'darkseagreen' => '8FBC8F',
159 39
            'darkslateblue' => '483D8B',
160 39
            'darkslategray' => '2F4F4F',
161 39
            'darkturquoise' => '00CED1',
162 39
            'darkviolet' => '9400D3',
163 39
            'deeppink' => 'FF1493',
164 39
            'deepskyblue' => '00BFFF',
165 39
            'dimgray' => '696969',
166 39
            'dodgerblue' => '1E90FF',
167 39
            'firebrick' => 'B22222',
168 39
            'floralwhite' => 'FFFAF0',
169 39
            'forestgreen' => '228B22',
170 39
            'fuchsia' => 'FF00FF',
171 39
            'gainsboro' => 'DCDCDC',
172 39
            'ghostwhite' => 'F8F8FF',
173 39
            'gold' => 'FFD700',
174 39
            'goldenrod' => 'DAA520',
175 39
            'gray' => '808080',
176 39
            'green' => '008000',
177 39
            'greenyellow' => 'ADFF2F',
178 39
            'honeydew' => 'F0FFF0',
179 39
            'hotpink' => 'FF69B4',
180 39
            'indianred' => 'CD5C5C',
181 39
            'indigo' => '4B0082',
182 39
            'ivory' => 'FFFFF0',
183 39
            'khaki' => 'F0E68C',
184 39
            'lavender' => 'E6E6FA',
185 39
            'lavenderblush' => 'FFF0F5',
186 39
            'lawngreen' => '7CFC00',
187 39
            'lemonchiffon' => 'FFFACD',
188 39
            'lightblue' => 'ADD8E6',
189 39
            'lightcoral' => 'F08080',
190 39
            'lightcyan' => 'E0FFFF',
191 39
            'lightgoldenrodyellow' => 'FAFAD2',
192 39
            'lightgray' => 'D3D3D3',
193 39
            'lightgreen' => '90EE90',
194 39
            'lightpink' => 'FFB6C1',
195 39
            'lightsalmon' => 'FFA07A',
196 39
            'lightseagreen' => '20B2AA',
197 39
            'lightskyblue' => '87CEFA',
198 39
            'lightslategray' => '778899',
199 39
            'lightsteelblue' => 'B0C4DE',
200 39
            'lightyellow' => 'FFFFE0',
201 39
            'lime' => '00FF00',
202 39
            'limegreen' => '32CD32',
203 39
            'linen' => 'FAF0E6',
204 39
            'magenta' => 'FF00FF',
205 39
            'maroon' => '800000',
206 39
            'mediumaquamarine' => '66CDAA',
207 39
            'mediumblue' => '0000CD',
208 39
            'mediumorchid' => 'BA55D3',
209 39
            'mediumpurple' => '9370DB',
210 39
            'mediumseagreen' => '3CB371',
211 39
            'mediumslateblue' => '7B68EE',
212 39
            'mediumspringgreen' => '00FA9A',
213 39
            'mediumturquoise' => '48D1CC',
214 39
            'mediumvioletred' => 'C71585',
215 39
            'midnightblue' => '191970',
216 39
            'mintcream' => 'F5FFFA',
217 39
            'mistyrose' => 'FFE4E1',
218 39
            'moccasin' => 'FFE4B5',
219 39
            'navajowhite' => 'FFDEAD',
220 39
            'navy' => '000080',
221 39
            'oldlace' => 'FDF5E6',
222 39
            'olive' => '808000',
223 39
            'olivedrab' => '6B8E23',
224 39
            'orange' => 'FFA500',
225 39
            'orangered' => 'FF4500',
226 39
            'orchid' => 'DA70D6',
227 39
            'palegoldenrod' => 'EEE8AA',
228 39
            'palegreen' => '98FB98',
229 39
            'paleturquoise' => 'AFEEEE',
230 39
            'palevioletred' => 'DB7093',
231 39
            'papayawhip' => 'FFEFD5',
232 39
            'peachpuff' => 'FFDAB9',
233 39
            'peru' => 'CD853F',
234 39
            'pink' => 'FFC0CB',
235 39
            'plum' => 'DDA0DD',
236 39
            'powderblue' => 'B0E0E6',
237 39
            'purple' => '800080',
238 39
            'rebeccapurple' => '663399',
239 39
            'red' => 'FF0000',
240 39
            'rosybrown' => 'BC8F8F',
241 39
            'royalblue' => '4169E1',
242 39
            'saddlebrown' => '8B4513',
243 39
            'salmon' => 'FA8072',
244 39
            'sandybrown' => 'F4A460',
245 39
            'seagreen' => '2E8B57',
246 39
            'seashell' => 'FFF5EE',
247 39
            'sienna' => 'A0522D',
248 39
            'silver' => 'C0C0C0',
249 39
            'skyblue' => '87CEEB',
250 39
            'slateblue' => '6A5ACD',
251 39
            'slategray' => '708090',
252 39
            'snow' => 'FFFAFA',
253 39
            'springgreen' => '00FF7F',
254 39
            'steelblue' => '4682B4',
255 39
            'tan' => 'D2B48C',
256 39
            'teal' => '008080',
257 39
            'thistle' => 'D8BFD8',
258 39
            'tomato' => 'FF6347',
259 39
            'turquoise' => '40E0D0',
260 39
            'violet' => 'EE82EE',
261 39
            'wheat' => 'F5DEB3',
262 39
            'white' => 'FFFFFF',
263 39
            'whitesmoke' => 'F5F5F5',
264 39
            'yellow' => 'FFFF00',
265 39
            'yellowgreen' => '9ACD32',
266 39
        ];
267
268 39
        $name = strtolower($name);
269
270 39
        if (array_key_exists($name, $colors)) {
271 15
            return $colors[$name];
272
        }
273 24
    }
274
}
275