|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kint\Object\Representation; |
|
4
|
|
|
|
|
5
|
|
|
use Kint\Object\ColorObject; |
|
6
|
|
|
|
|
7
|
|
|
class ColorRepresentation extends Representation |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
public $r = 0; |
|
|
|
|
|
|
10
|
|
|
public $g = 0; |
|
|
|
|
|
|
11
|
|
|
public $b = 0; |
|
|
|
|
|
|
12
|
|
|
public $a = 1; |
|
|
|
|
|
|
13
|
|
|
public $variant = null; |
|
14
|
|
|
public $implicit_label = true; |
|
|
|
|
|
|
15
|
|
|
public $hints = array('color'); |
|
16
|
|
|
|
|
17
|
|
|
const COLOR_NAME = 1; |
|
18
|
|
|
const COLOR_HEX_3 = 2; |
|
19
|
|
|
const COLOR_HEX_6 = 3; |
|
20
|
|
|
const COLOR_RGB = 4; |
|
21
|
|
|
const COLOR_RGBA = 5; |
|
22
|
|
|
const COLOR_HSL = 6; |
|
23
|
|
|
const COLOR_HSLA = 7; |
|
24
|
|
|
const COLOR_HEX_4 = 8; |
|
25
|
|
|
const COLOR_HEX_8 = 9; |
|
26
|
|
|
|
|
27
|
|
|
public function getColor($variant = null) |
|
28
|
|
|
{ |
|
29
|
|
|
switch ($variant) { |
|
30
|
|
|
case self::COLOR_NAME: |
|
31
|
|
|
$hex = sprintf('%02x%02x%02x', $this->r, $this->g, $this->b); |
|
32
|
|
|
|
|
33
|
|
|
return array_search($hex, ColorObject::$color_map); |
|
34
|
|
|
case self::COLOR_HEX_3: |
|
35
|
|
|
if ($this->r % 0x11 === 0 && $this->g % 0x11 === 0 && $this->b % 0x11 === 0) { |
|
36
|
|
|
return sprintf( |
|
37
|
|
|
'#%1X%1X%1X', |
|
38
|
|
|
round($this->r / 0x11), |
|
39
|
|
|
round($this->g / 0x11), |
|
40
|
|
|
round($this->b / 0x11) |
|
41
|
|
|
); |
|
42
|
|
|
} else { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
case self::COLOR_HEX_6: |
|
46
|
|
|
return sprintf('#%02X%02X%02X', $this->r, $this->g, $this->b); |
|
47
|
|
|
case self::COLOR_RGB: |
|
48
|
|
|
return sprintf('rgb(%d, %d, %d)', $this->r, $this->g, $this->b); |
|
49
|
|
View Code Duplication |
case self::COLOR_RGBA: |
|
|
|
|
|
|
50
|
|
|
return sprintf('rgba(%d, %d, %d, %s)', $this->r, $this->g, $this->b, round($this->a, 4)); |
|
51
|
|
|
case self::COLOR_HSL: |
|
52
|
|
|
$val = ColorObject::rgbToHsl($this->r, $this->g, $this->b); |
|
53
|
|
|
|
|
54
|
|
|
return vsprintf('hsl(%d, %d%%, %d%%)', $val); |
|
55
|
|
|
case self::COLOR_HSLA: |
|
56
|
|
|
$val = ColorObject::rgbToHsl($this->r, $this->g, $this->b); |
|
57
|
|
|
|
|
58
|
|
|
return sprintf('hsla(%d, %d%%, %d%%, %s)', $val[0], $val[1], $val[2], round($this->a, 4)); |
|
59
|
|
|
case self::COLOR_HEX_4: |
|
60
|
|
|
if ($this->r % 0x11 === 0 && $this->g % 0x11 === 0 && $this->b % 0x11 === 0 && ($this->a * 255) % 0x11 === 0) { |
|
61
|
|
|
return sprintf( |
|
62
|
|
|
'#%1X%1X%1X%1X', |
|
63
|
|
|
round($this->r / 0x11), |
|
64
|
|
|
round($this->g / 0x11), |
|
65
|
|
|
round($this->b / 0x11), |
|
66
|
|
|
round($this->a * 0xF) |
|
67
|
|
|
); |
|
68
|
|
|
} else { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
View Code Duplication |
case self::COLOR_HEX_8: |
|
|
|
|
|
|
72
|
|
|
return sprintf('#%02X%02X%02X%02X', $this->r, $this->g, $this->b, round($this->a * 0xFF)); |
|
73
|
|
|
case null: |
|
74
|
|
|
return $this->contents; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function __construct($value) |
|
81
|
|
|
{ |
|
82
|
|
|
parent::__construct('Color'); |
|
83
|
|
|
|
|
84
|
|
|
$this->contents = $value; |
|
85
|
|
|
$this->setValues($value); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function hasAlpha($variant = null) |
|
89
|
|
|
{ |
|
90
|
|
|
if ($variant === null) { |
|
91
|
|
|
$variant = $this->variant; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
switch ($variant) { |
|
95
|
|
|
case self::COLOR_NAME: |
|
96
|
|
|
return $this->a !== 1; |
|
97
|
|
|
case self::COLOR_RGBA: |
|
98
|
|
|
case self::COLOR_HSLA: |
|
99
|
|
|
case self::COLOR_HEX_4: |
|
100
|
|
|
case self::COLOR_HEX_8: |
|
101
|
|
|
return true; |
|
102
|
|
|
default: |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function setValues($value) |
|
108
|
|
|
{ |
|
109
|
|
|
$value = strtolower(trim($value)); |
|
|
|
|
|
|
110
|
|
|
// Find out which variant of color input it is |
|
111
|
|
|
if (isset(ColorObject::$color_map[$value])) { |
|
112
|
|
|
$variant = self::COLOR_NAME; |
|
113
|
|
|
} elseif ($value[0] === '#') { |
|
114
|
|
|
$value = substr($value, 1); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
if (dechex(hexdec($value)) !== $value) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
View Code Duplication |
switch (strlen($value)) { |
|
|
|
|
|
|
121
|
|
|
case 3: |
|
122
|
|
|
$variant = self::COLOR_HEX_3; |
|
123
|
|
|
break; |
|
124
|
|
|
case 6: |
|
125
|
|
|
$variant = self::COLOR_HEX_6; |
|
126
|
|
|
break; |
|
127
|
|
|
case 4: |
|
128
|
|
|
$variant = self::COLOR_HEX_4; |
|
129
|
|
|
break; |
|
130
|
|
|
case 8: |
|
131
|
|
|
$variant = self::COLOR_HEX_8; |
|
132
|
|
|
break; |
|
133
|
|
|
default: |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
} else { |
|
137
|
|
|
if (!preg_match('/^((?:rgb|hsl)a?)\s*\(([0-9\.%,\s]+)\)$/i', $value, $match)) { |
|
138
|
|
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
View Code Duplication |
switch ($match[1]) { |
|
|
|
|
|
|
142
|
|
|
case 'rgb': |
|
143
|
|
|
$variant = self::COLOR_RGB; |
|
144
|
|
|
break; |
|
145
|
|
|
case 'rgba': |
|
146
|
|
|
$variant = self::COLOR_RGBA; |
|
147
|
|
|
break; |
|
148
|
|
|
case 'hsl': |
|
149
|
|
|
$variant = self::COLOR_HSL; |
|
150
|
|
|
break; |
|
151
|
|
|
case 'hsla': |
|
152
|
|
|
$variant = self::COLOR_HSLA; |
|
153
|
|
|
break; |
|
154
|
|
|
default: |
|
155
|
|
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$value = explode(',', $match[2]); |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
if ($this->hasAlpha($variant)) { |
|
161
|
|
|
if (count($value) !== 4) { |
|
162
|
|
|
return; |
|
163
|
|
|
} |
|
164
|
|
|
} elseif (count($value) !== 3) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
foreach ($value as $i => &$color) { |
|
169
|
|
|
$color = trim($color); |
|
170
|
|
|
|
|
171
|
|
|
if (strpos($color, '%') !== false) { |
|
172
|
|
|
$color = str_replace('%', '', $color); |
|
173
|
|
|
|
|
174
|
|
|
if ($i === 3) { |
|
175
|
|
|
$color = $color / 100; |
|
176
|
|
|
} elseif (in_array($variant, array(self::COLOR_RGB, self::COLOR_RGBA))) { |
|
177
|
|
|
$color = round($color / 100 * 255); |
|
178
|
|
|
} elseif ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) { |
|
179
|
|
|
$color = $color / 100 * 360; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) { |
|
184
|
|
|
$color = ($color % 360 + 360) % 360; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
// Assign the correct properties based on the variant |
|
190
|
|
|
switch ($variant) { |
|
191
|
|
|
case self::COLOR_HEX_4: |
|
192
|
|
|
$this->a = hexdec($value[3]) / 0xF; |
|
|
|
|
|
|
193
|
|
|
// Fallthrough |
|
194
|
|
|
case self::COLOR_HEX_3: |
|
195
|
|
|
$this->r = hexdec($value[0]) * 0x11; |
|
|
|
|
|
|
196
|
|
|
$this->g = hexdec($value[1]) * 0x11; |
|
|
|
|
|
|
197
|
|
|
$this->b = hexdec($value[2]) * 0x11; |
|
|
|
|
|
|
198
|
|
|
break; |
|
199
|
|
|
case self::COLOR_NAME: |
|
200
|
|
|
$value = ColorObject::$color_map[$value].'FF'; |
|
|
|
|
|
|
201
|
|
|
// Fallthrough |
|
202
|
|
|
case self::COLOR_HEX_8: |
|
203
|
|
|
$this->a = hexdec(substr($value, 6, 2)) / 0xFF; |
|
|
|
|
|
|
204
|
|
|
// Fallthrough |
|
205
|
|
|
case self::COLOR_HEX_6: |
|
206
|
|
|
$value = str_split($value, 2); |
|
|
|
|
|
|
207
|
|
|
$this->r = hexdec($value[0]); |
|
|
|
|
|
|
208
|
|
|
$this->g = hexdec($value[1]); |
|
|
|
|
|
|
209
|
|
|
$this->b = hexdec($value[2]); |
|
|
|
|
|
|
210
|
|
|
break; |
|
211
|
|
|
case self::COLOR_RGBA: |
|
212
|
|
|
$this->a = $value[3]; |
|
213
|
|
|
// Fallthrough |
|
214
|
|
|
case self::COLOR_RGB: |
|
215
|
|
|
list($this->r, $this->g, $this->b) = $value; |
|
216
|
|
|
break; |
|
217
|
|
|
case self::COLOR_HSLA: |
|
218
|
|
|
$this->a = $value[3]; |
|
219
|
|
|
// Fallthrough |
|
220
|
|
|
case self::COLOR_HSL: |
|
221
|
|
|
if (min($value) < 0 || $value[0] > 360 || max($value[1], $value[2]) > 100) { |
|
222
|
|
|
return; |
|
223
|
|
|
} |
|
224
|
|
|
$value = ColorObject::hslToRgb($value[0], $value[1], $value[2]); |
|
|
|
|
|
|
225
|
|
|
list($this->r, $this->g, $this->b) = $value; |
|
226
|
|
|
break; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
// If something has gone horribly wrong |
|
230
|
|
|
if ($this->r > 0xFF || $this->g > 0xFF || $this->b > 0xFF || $this->a > 1) { |
|
231
|
|
|
$this->variant = null; |
|
232
|
|
|
} else { |
|
233
|
|
|
$this->variant = $variant; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.