1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LesserPhp\Library; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* lesserphp |
7
|
|
|
* https://www.maswaba.de/lesserphp |
8
|
|
|
* |
9
|
|
|
* LESS CSS compiler, adapted from http://lesscss.org |
10
|
|
|
* |
11
|
|
|
* Copyright 2013, Leaf Corcoran <[email protected]> |
12
|
|
|
* Copyright 2016, Marcus Schwarz <[email protected]> |
13
|
|
|
* Licensed under MIT or GPLv3, see LICENSE |
14
|
|
|
* @package LesserPhp |
15
|
|
|
*/ |
16
|
|
|
class Coerce |
17
|
|
|
{ |
18
|
|
|
protected static $cssColors = [ |
19
|
|
|
'aliceblue' => '240,248,255', |
20
|
|
|
'antiquewhite' => '250,235,215', |
21
|
|
|
'aqua' => '0,255,255', |
22
|
|
|
'aquamarine' => '127,255,212', |
23
|
|
|
'azure' => '240,255,255', |
24
|
|
|
'beige' => '245,245,220', |
25
|
|
|
'bisque' => '255,228,196', |
26
|
|
|
'black' => '0,0,0', |
27
|
|
|
'blanchedalmond' => '255,235,205', |
28
|
|
|
'blue' => '0,0,255', |
29
|
|
|
'blueviolet' => '138,43,226', |
30
|
|
|
'brown' => '165,42,42', |
31
|
|
|
'burlywood' => '222,184,135', |
32
|
|
|
'cadetblue' => '95,158,160', |
33
|
|
|
'chartreuse' => '127,255,0', |
34
|
|
|
'chocolate' => '210,105,30', |
35
|
|
|
'coral' => '255,127,80', |
36
|
|
|
'cornflowerblue' => '100,149,237', |
37
|
|
|
'cornsilk' => '255,248,220', |
38
|
|
|
'crimson' => '220,20,60', |
39
|
|
|
'cyan' => '0,255,255', |
40
|
|
|
'darkblue' => '0,0,139', |
41
|
|
|
'darkcyan' => '0,139,139', |
42
|
|
|
'darkgoldenrod' => '184,134,11', |
43
|
|
|
'darkgray' => '169,169,169', |
44
|
|
|
'darkgreen' => '0,100,0', |
45
|
|
|
'darkgrey' => '169,169,169', |
46
|
|
|
'darkkhaki' => '189,183,107', |
47
|
|
|
'darkmagenta' => '139,0,139', |
48
|
|
|
'darkolivegreen' => '85,107,47', |
49
|
|
|
'darkorange' => '255,140,0', |
50
|
|
|
'darkorchid' => '153,50,204', |
51
|
|
|
'darkred' => '139,0,0', |
52
|
|
|
'darksalmon' => '233,150,122', |
53
|
|
|
'darkseagreen' => '143,188,143', |
54
|
|
|
'darkslateblue' => '72,61,139', |
55
|
|
|
'darkslategray' => '47,79,79', |
56
|
|
|
'darkslategrey' => '47,79,79', |
57
|
|
|
'darkturquoise' => '0,206,209', |
58
|
|
|
'darkviolet' => '148,0,211', |
59
|
|
|
'deeppink' => '255,20,147', |
60
|
|
|
'deepskyblue' => '0,191,255', |
61
|
|
|
'dimgray' => '105,105,105', |
62
|
|
|
'dimgrey' => '105,105,105', |
63
|
|
|
'dodgerblue' => '30,144,255', |
64
|
|
|
'firebrick' => '178,34,34', |
65
|
|
|
'floralwhite' => '255,250,240', |
66
|
|
|
'forestgreen' => '34,139,34', |
67
|
|
|
'fuchsia' => '255,0,255', |
68
|
|
|
'gainsboro' => '220,220,220', |
69
|
|
|
'ghostwhite' => '248,248,255', |
70
|
|
|
'gold' => '255,215,0', |
71
|
|
|
'goldenrod' => '218,165,32', |
72
|
|
|
'gray' => '128,128,128', |
73
|
|
|
'green' => '0,128,0', |
74
|
|
|
'greenyellow' => '173,255,47', |
75
|
|
|
'grey' => '128,128,128', |
76
|
|
|
'honeydew' => '240,255,240', |
77
|
|
|
'hotpink' => '255,105,180', |
78
|
|
|
'indianred' => '205,92,92', |
79
|
|
|
'indigo' => '75,0,130', |
80
|
|
|
'ivory' => '255,255,240', |
81
|
|
|
'khaki' => '240,230,140', |
82
|
|
|
'lavender' => '230,230,250', |
83
|
|
|
'lavenderblush' => '255,240,245', |
84
|
|
|
'lawngreen' => '124,252,0', |
85
|
|
|
'lemonchiffon' => '255,250,205', |
86
|
|
|
'lightblue' => '173,216,230', |
87
|
|
|
'lightcoral' => '240,128,128', |
88
|
|
|
'lightcyan' => '224,255,255', |
89
|
|
|
'lightgoldenrodyellow' => '250,250,210', |
90
|
|
|
'lightgray' => '211,211,211', |
91
|
|
|
'lightgreen' => '144,238,144', |
92
|
|
|
'lightgrey' => '211,211,211', |
93
|
|
|
'lightpink' => '255,182,193', |
94
|
|
|
'lightsalmon' => '255,160,122', |
95
|
|
|
'lightseagreen' => '32,178,170', |
96
|
|
|
'lightskyblue' => '135,206,250', |
97
|
|
|
'lightslategray' => '119,136,153', |
98
|
|
|
'lightslategrey' => '119,136,153', |
99
|
|
|
'lightsteelblue' => '176,196,222', |
100
|
|
|
'lightyellow' => '255,255,224', |
101
|
|
|
'lime' => '0,255,0', |
102
|
|
|
'limegreen' => '50,205,50', |
103
|
|
|
'linen' => '250,240,230', |
104
|
|
|
'magenta' => '255,0,255', |
105
|
|
|
'maroon' => '128,0,0', |
106
|
|
|
'mediumaquamarine' => '102,205,170', |
107
|
|
|
'mediumblue' => '0,0,205', |
108
|
|
|
'mediumorchid' => '186,85,211', |
109
|
|
|
'mediumpurple' => '147,112,219', |
110
|
|
|
'mediumseagreen' => '60,179,113', |
111
|
|
|
'mediumslateblue' => '123,104,238', |
112
|
|
|
'mediumspringgreen' => '0,250,154', |
113
|
|
|
'mediumturquoise' => '72,209,204', |
114
|
|
|
'mediumvioletred' => '199,21,133', |
115
|
|
|
'midnightblue' => '25,25,112', |
116
|
|
|
'mintcream' => '245,255,250', |
117
|
|
|
'mistyrose' => '255,228,225', |
118
|
|
|
'moccasin' => '255,228,181', |
119
|
|
|
'navajowhite' => '255,222,173', |
120
|
|
|
'navy' => '0,0,128', |
121
|
|
|
'oldlace' => '253,245,230', |
122
|
|
|
'olive' => '128,128,0', |
123
|
|
|
'olivedrab' => '107,142,35', |
124
|
|
|
'orange' => '255,165,0', |
125
|
|
|
'orangered' => '255,69,0', |
126
|
|
|
'orchid' => '218,112,214', |
127
|
|
|
'palegoldenrod' => '238,232,170', |
128
|
|
|
'palegreen' => '152,251,152', |
129
|
|
|
'paleturquoise' => '175,238,238', |
130
|
|
|
'palevioletred' => '219,112,147', |
131
|
|
|
'papayawhip' => '255,239,213', |
132
|
|
|
'peachpuff' => '255,218,185', |
133
|
|
|
'peru' => '205,133,63', |
134
|
|
|
'pink' => '255,192,203', |
135
|
|
|
'plum' => '221,160,221', |
136
|
|
|
'powderblue' => '176,224,230', |
137
|
|
|
'purple' => '128,0,128', |
138
|
|
|
'red' => '255,0,0', |
139
|
|
|
'rosybrown' => '188,143,143', |
140
|
|
|
'royalblue' => '65,105,225', |
141
|
|
|
'saddlebrown' => '139,69,19', |
142
|
|
|
'salmon' => '250,128,114', |
143
|
|
|
'sandybrown' => '244,164,96', |
144
|
|
|
'seagreen' => '46,139,87', |
145
|
|
|
'seashell' => '255,245,238', |
146
|
|
|
'sienna' => '160,82,45', |
147
|
|
|
'silver' => '192,192,192', |
148
|
|
|
'skyblue' => '135,206,235', |
149
|
|
|
'slateblue' => '106,90,205', |
150
|
|
|
'slategray' => '112,128,144', |
151
|
|
|
'slategrey' => '112,128,144', |
152
|
|
|
'snow' => '255,250,250', |
153
|
|
|
'springgreen' => '0,255,127', |
154
|
|
|
'steelblue' => '70,130,180', |
155
|
|
|
'tan' => '210,180,140', |
156
|
|
|
'teal' => '0,128,128', |
157
|
|
|
'thistle' => '216,191,216', |
158
|
|
|
'tomato' => '255,99,71', |
159
|
|
|
'transparent' => '0,0,0,0', |
160
|
|
|
'turquoise' => '64,224,208', |
161
|
|
|
'violet' => '238,130,238', |
162
|
|
|
'wheat' => '245,222,179', |
163
|
|
|
'white' => '255,255,255', |
164
|
|
|
'whitesmoke' => '245,245,245', |
165
|
|
|
'yellow' => '255,255,0', |
166
|
|
|
'yellowgreen' => '154,205,50', |
167
|
|
|
]; |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
24 |
|
* coerce a value for use in color operation |
172
|
|
|
* |
173
|
24 |
|
* @param array $value |
174
|
24 |
|
* |
175
|
7 |
|
* @return array|null |
176
|
24 |
|
*/ |
177
|
10 |
|
public function coerceColor(array $value) |
178
|
10 |
|
{ |
179
|
10 |
|
switch ($value[0]) { |
180
|
10 |
|
case 'color': |
181
|
|
|
return $value; |
182
|
10 |
|
case 'raw_color': |
183
|
10 |
|
$c = ['color', 0, 0, 0]; |
184
|
10 |
|
$colorStr = mb_substr($value[1], 1); |
185
|
|
|
$num = hexdec($colorStr); |
186
|
10 |
|
$width = mb_strlen($colorStr) === 3 ? 16 : 256; |
187
|
|
|
|
188
|
|
|
for ($i = 3; $i > 0; $i--) { // 3 2 1 |
189
|
10 |
|
$t = $num % $width; |
190
|
21 |
|
$num /= $width; |
191
|
5 |
|
|
192
|
5 |
|
$c[$i] = $t * (256 / $width) + $t * floor(16 / $width); |
193
|
3 |
|
} |
194
|
|
|
|
195
|
3 |
|
return $c; |
196
|
1 |
|
case 'keyword': |
197
|
|
|
$name = $value[1]; |
198
|
|
|
if (isset(static::$cssColors[$name])) { |
199
|
3 |
|
$rgba = explode(',', static::$cssColors[$name]); |
200
|
|
|
|
201
|
|
|
if (isset($rgba[3])) { |
202
|
|
|
return ['color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]]; |
203
|
|
|
} |
204
|
21 |
|
|
205
|
|
|
return ['color', $rgba[0], $rgba[1], $rgba[2]]; |
206
|
|
|
} |
207
|
|
|
|
208
|
22 |
|
} |
209
|
|
|
|
210
|
22 |
|
return null; |
211
|
22 |
|
} |
212
|
5 |
|
|
213
|
20 |
|
/** |
214
|
3 |
|
* make something string like into a string |
215
|
|
|
* |
216
|
|
|
* @param array $value |
217
|
20 |
|
* |
218
|
|
|
* @return array|null |
219
|
|
|
*/ |
220
|
|
|
public function coerceString(array $value) |
221
|
|
|
{ |
222
|
|
|
switch ($value[0]) { |
223
|
|
|
case 'string': |
224
|
|
|
return $value; |
225
|
|
|
case 'keyword': |
226
|
|
|
return ['string', '', [$value[1]]]; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return null; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|