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