1 | <?php |
||
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) |
|
107 | |||
108 | /** |
||
109 | * Format color for consumption. |
||
110 | * @return string The formatted color. |
||
111 | */ |
||
112 | 26 | public function formatted() |
|
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) |
|
274 | } |
||
275 |