1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MikeAlmond\TwigColorExtension; |
5
|
|
|
|
6
|
|
|
use MikeAlmond\Color\Color; |
7
|
|
|
use MikeAlmond\Color\CssGenerator; |
8
|
|
|
use MikeAlmond\Color\PaletteGenerator; |
9
|
|
|
use MikeAlmond\Color\Validator; |
10
|
|
|
use Twig\Extension\AbstractExtension as TwigExtension; |
11
|
|
|
use Twig\TwigFilter; |
12
|
|
|
use Twig\TwigTest; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ColorExtension |
16
|
|
|
* @package MikeAlmond\TwigColorExtension |
17
|
|
|
*/ |
18
|
|
|
class ColorExtension extends TwigExtension |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
7 |
|
public function getTests() |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
7 |
|
new TwigTest('color_valid', [$this, 'isValid']), |
28
|
7 |
|
new TwigTest('color_readable', [$this, 'isReadable']), |
29
|
7 |
|
new TwigTest('color_low_contrast', [$this, 'isLowContrast']), |
30
|
7 |
|
new TwigTest('color_dark', [$this, 'isDark']), |
31
|
7 |
|
new TwigTest('color_light', [$this, 'isLight']), |
32
|
7 |
|
new TwigTest('color_has_name', [$this, 'hasColorName']), |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
7 |
|
public function getFilters() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
7 |
|
new TwigFilter('color_darken', [$this, 'darken']), |
43
|
7 |
|
new TwigFilter('color_lighten', [$this, 'lighten']), |
44
|
7 |
|
new TwigFilter('color_adjust_hue', [$this, 'adjustHue']), |
45
|
7 |
|
new TwigFilter('color_text_color', [$this, 'getMatchingTextColor']), |
46
|
7 |
|
new TwigFilter('color_complementary', [$this, 'getComplementary']), |
47
|
7 |
|
new TwigFilter('color_css_rgb', [$this, 'getCssRgb']), |
48
|
7 |
|
new TwigFilter('color_css_rgba', [$this, 'getCssRgba']), |
49
|
7 |
|
new TwigFilter('color_css_hsl', [$this, 'getCssHsl']), |
50
|
7 |
|
new TwigFilter('color_css_hsla', [$this, 'getCssHsla']), |
51
|
7 |
|
new TwigFilter('color_css_hex', [$this, 'getCssHex']), |
52
|
7 |
|
new TwigFilter('color_css_name', [$this, 'getCssColorName']), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Extension name. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
**/ |
61
|
1 |
|
public function getName() : string |
62
|
|
|
{ |
63
|
1 |
|
return 'mikealmond/twig-color-extension'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string|Color $color |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
6 |
|
public function isValid($color) : bool |
72
|
|
|
{ |
73
|
6 |
|
return $color instanceof Color || Validator::isValidHex($color); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string|Color $color |
78
|
|
|
* @param string|Color $backgroundColor |
79
|
|
|
* @param string $level |
80
|
|
|
* @param int $fontSize |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
2 |
|
public function isReadable($color, $backgroundColor, $level = 'AA', int $fontSize = 12) : bool |
85
|
|
|
{ |
86
|
2 |
|
if (!$this->isValid($color) || !$this->isValid($backgroundColor)) { |
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $this->parseColor($color) |
91
|
1 |
|
->isReadable( |
92
|
1 |
|
$this->parseColor($backgroundColor), |
93
|
1 |
|
$level, |
94
|
1 |
|
$fontSize |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string|Color $color |
100
|
|
|
* @param string|Color $backgroundColor |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
2 |
|
public function isLowContrast($color, $backgroundColor = 'FFFFFF') : bool |
105
|
|
|
{ |
106
|
2 |
|
if (!$this->isValid($color) || !$this->isValid($backgroundColor)) { |
107
|
1 |
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this->parseColor($color)->luminosityContrast($this->parseColor($backgroundColor)) < 4.5; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string|Color $color |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
2 |
|
public function isDark($color) : bool |
119
|
|
|
{ |
120
|
2 |
|
if (!$this->isValid($color)) { |
121
|
2 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return $this->parseColor($color)->isDark(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string|Color $color |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
2 |
|
public function isLight($color) : bool |
133
|
|
|
{ |
134
|
2 |
|
if (!$this->isValid($color)) { |
135
|
1 |
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return !$this->parseColor($color)->isDark(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string|Color $color |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
2 |
|
public function hasColorName($color) : bool |
147
|
|
|
{ |
148
|
2 |
|
if (!$this->isValid($color)) { |
149
|
1 |
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return CssGenerator::hasName($this->parseColor($color)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string|Color $color |
157
|
|
|
* @param float $percentage |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
2 |
|
public function darken($color, float $percentage) : string |
162
|
|
|
{ |
163
|
2 |
|
if (!$this->isValid($color)) { |
164
|
1 |
|
return $color; |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return $this->parseColor($color)->darken($percentage)->getHex(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string|Color $color |
172
|
|
|
* @param float $percentage |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
2 |
|
public function lighten($color, float $percentage) : string |
177
|
|
|
{ |
178
|
2 |
|
if (!$this->isValid($color)) { |
179
|
1 |
|
return $color; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
return $this->parseColor($color)->lighten($percentage)->getHex(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string|Color $color |
187
|
|
|
* @param float $degrees |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
2 |
|
public function adjustHue($color, float $degrees = 30) : string |
192
|
|
|
{ |
193
|
2 |
|
if (!$this->isValid($color)) { |
194
|
1 |
|
return $color; |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
return $this->parseColor($color)->adjustHue($degrees)->getHex(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string|Color $color |
202
|
|
|
* @param string $default |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
2 |
|
public function getMatchingTextColor($color, string $default = 'CCCCCC') : string |
207
|
|
|
{ |
208
|
2 |
|
if (!$this->isValid($color)) { |
209
|
1 |
|
return $default; |
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
return $this->parseColor($color)->getMatchingTextColor($default)->getHex(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string|Color $color |
217
|
|
|
* @param int $distance |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
2 |
|
public function getComplementary($color, int $distance = 45) : string |
222
|
|
|
{ |
223
|
2 |
|
if (!$this->isValid($color)) { |
224
|
1 |
|
return $color; |
225
|
|
|
} |
226
|
|
|
|
227
|
1 |
|
$generator = new PaletteGenerator($this->parseColor($color)); |
228
|
1 |
|
$palette = $generator->triad($distance); |
229
|
|
|
|
230
|
1 |
|
return $palette[1]->getHex(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string|Color $color |
235
|
|
|
* |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
2 |
|
public function getCssColorName($color) : string |
239
|
|
|
{ |
240
|
2 |
|
if (!$this->isValid($color)) { |
241
|
1 |
|
return $color; |
242
|
|
|
} |
243
|
|
|
|
244
|
1 |
|
$color = $this->parseColor($color); |
245
|
|
|
|
246
|
1 |
|
if (!CssGenerator::hasName($color)) { |
247
|
1 |
|
return CssGenerator::hex($color); |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
return CssGenerator::name($color); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param string|Color $color |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
2 |
|
public function getCssRgb($color) : string |
259
|
|
|
{ |
260
|
2 |
|
if (!$this->isValid($color)) { |
261
|
1 |
|
return $color; |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
return CssGenerator::rgb($this->parseColor($color)); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param string|Color $color |
269
|
|
|
* @param float $alpha |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
2 |
|
public function getCssRgba($color, float $alpha = 1.0) : string |
274
|
|
|
{ |
275
|
2 |
|
if (!$this->isValid($color)) { |
276
|
1 |
|
return $color; |
277
|
|
|
} |
278
|
|
|
|
279
|
1 |
|
return CssGenerator::rgba($this->parseColor($color), $alpha); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string|Color $color |
284
|
|
|
* |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
2 |
|
public function getCssHsl($color) : string |
288
|
|
|
{ |
289
|
2 |
|
if (!$this->isValid($color)) { |
290
|
1 |
|
return $color; |
291
|
|
|
} |
292
|
|
|
|
293
|
1 |
|
return CssGenerator::hsl($this->parseColor($color)); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param string|Color $color |
298
|
|
|
* @param float $alpha |
299
|
|
|
* |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
2 |
|
public function getCssHsla($color, float $alpha = 1.0) : string |
303
|
|
|
{ |
304
|
2 |
|
if (!$this->isValid($color)) { |
305
|
1 |
|
return $color; |
306
|
|
|
} |
307
|
|
|
|
308
|
1 |
|
return CssGenerator::hsla($this->parseColor($color), $alpha); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param string|Color $color |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
2 |
|
public function getCssHex($color) : string |
317
|
|
|
{ |
318
|
2 |
|
if (!$this->isValid($color)) { |
319
|
1 |
|
return $color; |
320
|
|
|
} |
321
|
|
|
|
322
|
1 |
|
return CssGenerator::hex($this->parseColor($color)); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param $color |
327
|
|
|
* |
328
|
|
|
* @return Color |
329
|
|
|
*/ |
330
|
3 |
|
private function parseColor($color) : Color |
331
|
|
|
{ |
332
|
3 |
|
return $color instanceof Color ? $color : Color::fromHex($color); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|