Color   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 11

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Test Coverage

Coverage 92.78%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 270
ccs 180
cts 194
cp 0.9278
rs 10
c 3
b 0
f 0

4 Methods

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