1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ILess |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ILess\Node; |
11
|
|
|
|
12
|
|
|
use ILess\Color; |
13
|
|
|
use ILess\Context; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use ILess\Math; |
16
|
|
|
use ILess\Node; |
17
|
|
|
use ILess\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Color node. |
21
|
|
|
*/ |
22
|
|
|
class ColorNode extends Node implements ComparableInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Node type. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $type = 'Color'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $originalForm; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param string|array $rgb The rgb value |
40
|
|
|
* @param int $alpha Alpha channel |
41
|
|
|
* @param string $originalForm Original form of the color |
42
|
|
|
* |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
|
|
public function __construct($rgb, $alpha = 1, $originalForm = null) |
46
|
|
|
{ |
47
|
|
|
if (!$rgb instanceof Color) { |
48
|
|
|
$value = new Color($rgb, $alpha, $originalForm); |
49
|
|
|
} else { |
50
|
|
|
$value = $rgb; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* @var $value \ILess\Color */ |
54
|
|
|
parent::__construct($value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the color object. |
59
|
|
|
* |
60
|
|
|
* @return Color |
61
|
|
|
*/ |
62
|
|
|
public function getColor() |
63
|
|
|
{ |
64
|
|
|
return $this->value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function generateCSS(Context $context, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
$output->add($this->toCSS($context)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the RGB channels. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getRGB() |
81
|
|
|
{ |
82
|
|
|
return $this->value->rgb; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the HSV components of the color. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function toHSV() |
91
|
|
|
{ |
92
|
|
|
return $this->value->toHSV(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the red channel. |
97
|
|
|
* |
98
|
|
|
* @param bool $raw Return raw value? |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function getRed($raw = false) |
103
|
|
|
{ |
104
|
|
|
return $raw ? $this->value->getRed() : new DimensionNode($this->value->getRed()); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the green channel. |
109
|
|
|
* |
110
|
|
|
* @param bool $raw Return raw value? |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function getGreen($raw = false) |
115
|
|
|
{ |
116
|
|
|
return $raw ? $this->value->getGreen() : new DimensionNode($this->value->getGreen()); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the blue channel. |
121
|
|
|
* |
122
|
|
|
* @param bool $raw Return raw value? |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function getBlue($raw = false) |
127
|
|
|
{ |
128
|
|
|
return $raw ? $this->value->getBlue() : new DimensionNode($this->value->getBlue()); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the alpha channel. |
133
|
|
|
* |
134
|
|
|
* @param bool $raw Return raw value? |
135
|
|
|
* |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function getAlpha($raw = false) |
139
|
|
|
{ |
140
|
|
|
return $raw ? $this->value->getAlpha() : new DimensionNode($this->value->getAlpha()); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the color saturation. |
145
|
|
|
* |
146
|
|
|
* @param bool $raw Return raw value? |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function getSaturation($raw = false) |
151
|
|
|
{ |
152
|
|
|
return $raw ? $this->value->getSaturation() : |
|
|
|
|
153
|
|
|
new DimensionNode(round($this->value->getSaturation() * 100), '%'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the color hue. |
158
|
|
|
* |
159
|
|
|
* @param bool $raw Raw value? |
160
|
|
|
* |
161
|
|
|
* @return mixed |
162
|
|
|
*/ |
163
|
|
|
public function getHue($raw = false) |
164
|
|
|
{ |
165
|
|
|
return $raw ? $this->value->getHue() : new DimensionNode(round($this->value->getHue())); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the lightness. |
170
|
|
|
* |
171
|
|
|
* @param bool $raw Return raw value? |
172
|
|
|
* |
173
|
|
|
* @return mixed ILessNode\DimensionNode if $raw is false |
174
|
|
|
*/ |
175
|
|
|
public function getLightness($raw = false) |
176
|
|
|
{ |
177
|
|
|
return $raw ? $this->value->getLightness() : |
|
|
|
|
178
|
|
|
new DimensionNode(round($this->value->getLightness() * 100), '%'); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns the luma. |
183
|
|
|
* |
184
|
|
|
* @param bool $raw Return raw value? |
185
|
|
|
* |
186
|
|
|
* @return mixed ILessNode\DimensionNode if $raw is false |
187
|
|
|
*/ |
188
|
|
|
public function getLuma($raw = false) |
189
|
|
|
{ |
190
|
|
|
return $raw ? $this->value->getLuma() : new DimensionNode( |
|
|
|
|
191
|
|
|
$this->value->getLuma() * $this->value->getAlpha() * 100, '%' |
|
|
|
|
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns the luminance. |
197
|
|
|
* |
198
|
|
|
* @param bool $raw Return raw value? |
199
|
|
|
* |
200
|
|
|
* @return mixed ILessNode\DimensionNode if $raw is false |
201
|
|
|
*/ |
202
|
|
|
public function getLuminance($raw = false) |
203
|
|
|
{ |
204
|
|
|
return $raw ? $this->value->getLuminance() : new DimensionNode( |
|
|
|
|
205
|
|
|
$this->value->getLuminance() * $this->value->getAlpha() * 100, '%' |
|
|
|
|
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Converts the node to ARGB. |
211
|
|
|
* |
212
|
|
|
* @return AnonymousNode |
213
|
|
|
*/ |
214
|
|
|
public function toARGB() |
215
|
|
|
{ |
216
|
|
|
return new AnonymousNode($this->value->toARGB()); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns the HSL components of the color. |
221
|
|
|
* |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
public function toHSL() |
225
|
|
|
{ |
226
|
|
|
return $this->value->toHSL(); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Converts the node to string. |
231
|
|
|
* |
232
|
|
|
* @param Context $context |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
public function toCSS(Context $context) |
237
|
|
|
{ |
238
|
|
|
return $this->value->toString($context->compress, $context->compress && $context->canShortenColors); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Operations have to be done per-channel, if not, |
243
|
|
|
* channels will spill onto each other. Once we have |
244
|
|
|
* our result, in the form of an integer triplet, |
245
|
|
|
* we create a new color node to hold the result. |
246
|
|
|
* |
247
|
|
|
* @param Context $context |
248
|
|
|
* @param string $op |
249
|
|
|
* @param Node $other |
250
|
|
|
* |
251
|
|
|
* @return ColorNode |
252
|
|
|
* |
253
|
|
|
* @throws InvalidArgumentException |
254
|
|
|
*/ |
255
|
|
|
public function operate(Context $context, $op, Node $other) |
256
|
|
|
{ |
257
|
|
|
$result = []; |
258
|
|
|
|
259
|
|
|
if (!($other instanceof self)) { |
260
|
|
|
if (!$other instanceof ToColorConvertibleInterface) { |
261
|
|
|
throw new InvalidArgumentException( |
262
|
|
|
'The other node must implement toColor() method to operate, see ILess\Node\Node_ToColorConvertibleInterface' |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
$other = $other->toColor(); |
266
|
|
|
if (!$other instanceof self) { |
267
|
|
|
throw new InvalidArgumentException('The toColor() method must return an instance of ILess\Node\Node_Color'); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$t = $this->getRGB(); |
272
|
|
|
$o = $other->getRGB(); |
273
|
|
|
|
274
|
|
|
for ($c = 0; $c < 3; ++$c) { |
275
|
|
|
$result[$c] = Math::operate($op, $t[$c], $o[$c]); |
276
|
|
|
if ($result[$c] > 255) { |
277
|
|
|
$result[$c] = 255; |
278
|
|
|
} elseif ($result < 0) { |
279
|
|
|
$result[$c] = 0; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return new self($result, $this->value->getAlpha() + $other->value->getAlpha()); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Compares with another node. |
288
|
|
|
* |
289
|
|
|
* @param Node $other |
290
|
|
|
* |
291
|
|
|
* @return int |
292
|
|
|
* |
293
|
|
|
* @throws InvalidArgumentException |
294
|
|
|
*/ |
295
|
|
|
public function compare(Node $other) |
296
|
|
|
{ |
297
|
|
|
if (!($other instanceof self)) { |
298
|
|
|
if (!$other instanceof ToColorConvertibleInterface) { |
299
|
|
|
throw new InvalidArgumentException( |
300
|
|
|
'The other node must implement toColor() method to operate, see ILess\Node\Node_ToColorConvertibleInterface' |
301
|
|
|
); |
302
|
|
|
} |
303
|
|
|
$other = $other->toColor(); |
304
|
|
|
if (!$other instanceof self) { |
305
|
|
|
throw new InvalidArgumentException('The toColor() method must return an instance of ILess\Node\Node_Color'); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// cannot compare with another node |
310
|
|
|
if (!$other instanceof self) { |
311
|
|
|
return -1; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$color = $this->getColor(); |
315
|
|
|
$other = $other->getColor(); |
316
|
|
|
|
317
|
|
|
return ($color->rgb === $other->rgb && $color->alpha === $other->alpha) ? 0 : -1; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: