1
|
|
|
<?php namespace Arcanedev\Color; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Color\Contracts\Color as ColorContract; |
4
|
|
|
use Arcanedev\Color\Exceptions\ColorException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Color |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Color |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Color implements ColorContract |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Properties |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Red value. |
21
|
|
|
* |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $red = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Green value. |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $green = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Blue value. |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $blue = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Alpha value. |
42
|
|
|
* |
43
|
|
|
* @var float |
44
|
|
|
*/ |
45
|
|
|
protected $alpha = 1.0; |
46
|
|
|
|
47
|
|
|
/* ----------------------------------------------------------------- |
48
|
|
|
| Constructor |
49
|
|
|
| ----------------------------------------------------------------- |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Color constructor. |
54
|
|
|
* |
55
|
|
|
* @param int $red |
56
|
|
|
* @param int $green |
57
|
|
|
* @param int $blue |
58
|
|
|
* @param float|int $alpha |
59
|
|
|
*/ |
60
|
27 |
|
public function __construct($red = 0, $green = 0, $blue = 0, $alpha = 1.0) |
61
|
|
|
{ |
62
|
27 |
|
$this->setRgba($red, $green, $blue, $alpha); |
63
|
21 |
|
} |
64
|
|
|
|
65
|
|
|
/* ----------------------------------------------------------------- |
66
|
|
|
| Getters & Setters |
67
|
|
|
| ----------------------------------------------------------------- |
68
|
|
|
*/ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the RGBA values. |
72
|
|
|
* |
73
|
|
|
* @param int $red |
74
|
|
|
* @param int $green |
75
|
|
|
* @param int $blue |
76
|
|
|
* @param float|int $alpha |
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
27 |
|
public function setRgba($red, $green, $blue, $alpha) |
81
|
|
|
{ |
82
|
27 |
|
return $this->setRgb($red, $green, $blue) |
83
|
21 |
|
->setAlpha($alpha); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the RGB values. |
88
|
|
|
* |
89
|
|
|
* @param int $red |
90
|
|
|
* @param int $green |
91
|
|
|
* @param int $blue |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
27 |
|
public function setRgb($red, $green, $blue) |
96
|
|
|
{ |
97
|
27 |
|
return $this->setRed($red)->setGreen($green)->setBlue($blue); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get red value. |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
9 |
|
public function red() |
106
|
|
|
{ |
107
|
9 |
|
return $this->red; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the red value. |
112
|
|
|
* |
113
|
|
|
* @param int $red |
114
|
|
|
* |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
27 |
|
public function setRed($red) |
118
|
|
|
{ |
119
|
27 |
|
$this->checkColorValue('red', $red); |
120
|
21 |
|
$this->red = $red; |
121
|
|
|
|
122
|
21 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the green value. |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
9 |
|
public function green() |
131
|
|
|
{ |
132
|
9 |
|
return $this->green; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the green value. |
137
|
|
|
* |
138
|
|
|
* @param int $green |
139
|
|
|
* |
140
|
|
|
* @return self |
141
|
|
|
*/ |
142
|
21 |
|
public function setGreen($green) |
143
|
|
|
{ |
144
|
21 |
|
$this->checkColorValue('green', $green); |
145
|
21 |
|
$this->green = $green; |
146
|
|
|
|
147
|
21 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the blue value. |
152
|
|
|
* |
153
|
|
|
* @return int |
154
|
|
|
*/ |
155
|
9 |
|
public function blue() |
156
|
|
|
{ |
157
|
9 |
|
return $this->blue; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set the blue value. |
162
|
|
|
* |
163
|
|
|
* @param int $blue |
164
|
|
|
* |
165
|
|
|
* @return self |
166
|
|
|
*/ |
167
|
21 |
|
public function setBlue($blue) |
168
|
|
|
{ |
169
|
21 |
|
$this->checkColorValue('blue', $blue); |
170
|
21 |
|
$this->blue = $blue; |
171
|
|
|
|
172
|
21 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the alpha value. |
177
|
|
|
* |
178
|
|
|
* @return float |
179
|
|
|
*/ |
180
|
9 |
|
public function alpha() |
181
|
|
|
{ |
182
|
9 |
|
return $this->alpha; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the alpha value. |
187
|
|
|
* |
188
|
|
|
* @param float|int $alpha |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
21 |
|
public function setAlpha($alpha) |
193
|
|
|
{ |
194
|
21 |
|
$this->checkAlphaValue($alpha); |
195
|
21 |
|
$this->alpha = $alpha; |
|
|
|
|
196
|
|
|
|
197
|
21 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/* ----------------------------------------------------------------- |
201
|
|
|
| Main Methods |
202
|
|
|
| ----------------------------------------------------------------- |
203
|
|
|
*/ |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Make a Color instance. |
207
|
|
|
* |
208
|
|
|
* @param string $color |
209
|
|
|
* |
210
|
|
|
* @return self |
211
|
|
|
*/ |
212
|
6 |
|
public static function make($color) |
213
|
|
|
{ |
214
|
6 |
|
self::checkHex($color); |
215
|
|
|
|
216
|
3 |
|
list($red, $green, $blue) = ColorConverter::hexToRgb($color); |
217
|
|
|
|
218
|
3 |
|
return new self($red, $green, $blue); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Convert to hex color. |
223
|
|
|
* |
224
|
|
|
* @param bool $uppercase |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
3 |
|
public function toHex($uppercase = true) |
229
|
|
|
{ |
230
|
3 |
|
$hex = ColorConverter::rgbToHex($this->red, $this->green, $this->blue); |
231
|
|
|
|
232
|
3 |
|
return $uppercase ? strtoupper($hex) : strtolower($hex); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Parse the object to string. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
3 |
|
public function __toString() |
241
|
|
|
{ |
242
|
3 |
|
return $this->toHex(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/* ----------------------------------------------------------------- |
246
|
|
|
| Check Methods |
247
|
|
|
| ----------------------------------------------------------------- |
248
|
|
|
*/ |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Check if the color is bright. |
252
|
|
|
* |
253
|
|
|
* @param float|int $contrast |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
9 |
|
public function isBright($contrast = 150) |
258
|
|
|
{ |
259
|
9 |
|
return $contrast < sqrt( |
260
|
9 |
|
(pow($this->red, 2) * .299) + |
261
|
9 |
|
(pow($this->green, 2) * .587) + |
262
|
9 |
|
(pow($this->blue, 2) * .114) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Check if the color is dark. |
268
|
|
|
* |
269
|
|
|
* @param float|int $contrast |
270
|
|
|
* |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
9 |
|
public function isDark($contrast = 150) |
274
|
|
|
{ |
275
|
9 |
|
return ! $this->isBright($contrast); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Check if the color is valid. |
280
|
|
|
* |
281
|
|
|
* @param string $hex |
282
|
|
|
* |
283
|
|
|
* @return bool |
284
|
|
|
*/ |
285
|
6 |
|
public static function isValidHex($hex) |
286
|
|
|
{ |
287
|
6 |
|
return ColorValidator::validateHex($hex); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/* ----------------------------------------------------------------- |
291
|
|
|
| Other Methods |
292
|
|
|
| ----------------------------------------------------------------- |
293
|
|
|
*/ |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Check the color. |
297
|
|
|
* |
298
|
|
|
* @param string $value |
299
|
|
|
* |
300
|
|
|
* @throws \Arcanedev\Color\Exceptions\ColorException |
301
|
|
|
*/ |
302
|
6 |
|
private static function checkHex($value) |
303
|
|
|
{ |
304
|
6 |
|
if ( ! self::isValidHex($value)) |
305
|
3 |
|
throw new ColorException("Invalid HEX Color [$value]."); |
306
|
3 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Set color value. |
310
|
|
|
* |
311
|
|
|
* @param string $name |
312
|
|
|
* @param int $value |
313
|
|
|
* |
314
|
|
|
* @throws \Arcanedev\Color\Exceptions\ColorException |
315
|
|
|
*/ |
316
|
27 |
|
private function checkColorValue($name, $value) |
317
|
|
|
{ |
318
|
27 |
|
if ( ! is_int($value)) |
319
|
3 |
|
throw new ColorException("The $name value must be an integer."); |
320
|
|
|
|
321
|
24 |
|
if ($value < 0 || $value > 255) |
322
|
3 |
|
throw new ColorException( |
323
|
3 |
|
"The $name value must be between 0 and 255, [$value] is given." |
324
|
|
|
); |
325
|
21 |
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Check the alpha value. |
329
|
|
|
* |
330
|
|
|
* @param float|int $alpha |
331
|
|
|
* |
332
|
|
|
* @throws \Arcanedev\Color\Exceptions\ColorException |
333
|
|
|
*/ |
334
|
21 |
|
public function checkAlphaValue(&$alpha) |
335
|
|
|
{ |
336
|
21 |
|
if ( ! is_numeric($alpha)) |
337
|
3 |
|
throw new ColorException("The alpha value must be a float or an integer."); |
338
|
|
|
|
339
|
21 |
|
$alpha = (float) $alpha; |
340
|
|
|
|
341
|
21 |
|
if ($alpha < 0 || $alpha > 1) |
342
|
3 |
|
throw new ColorException( |
343
|
3 |
|
"The alpha value must be between 0 and 1, [$alpha] is given." |
344
|
|
|
); |
345
|
21 |
|
} |
346
|
|
|
} |
347
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.