1 <?php
2 class Kint_Parsers_Color extends kintParser
3 {
4 private static $_css3Named = array(
5 'aliceblue'=>'#f0f8ff','antiquewhite'=>'#faebd7','aqua'=>'#00ffff','aquamarine'=>'#7fffd4','azure'=>'#f0ffff',
6 'beige'=>'#f5f5dc','bisque'=>'#ffe4c4','black'=>'#000000','blanchedalmond'=>'#ffebcd','blue'=>'#0000ff',
7 'blueviolet'=>'#8a2be2','brown'=>'#a52a2a','burlywood'=>'#deb887','cadetblue'=>'#5f9ea0','chartreuse'=>'#7fff00',
8 'chocolate'=>'#d2691e','coral'=>'#ff7f50','cornflowerblue'=>'#6495ed','cornsilk'=>'#fff8dc','crimson'=>'#dc143c',
9 'cyan'=>'#00ffff','darkblue'=>'#00008b','darkcyan'=>'#008b8b','darkgoldenrod'=>'#b8860b','darkgray'=>'#a9a9a9',
10 'darkgrey'=>'#a9a9a9','darkgreen'=>'#006400','darkkhaki'=>'#bdb76b','darkmagenta'=>'#8b008b',
11 'darkolivegreen'=>'#556b2f','darkorange'=>'#ff8c00','darkorchid'=>'#9932cc','darkred'=>'#8b0000',
12 'darksalmon'=>'#e9967a','darkseagreen'=>'#8fbc8f','darkslateblue'=>'#483d8b','darkslategray'=>'#2f4f4f',
13 'darkslategrey'=>'#2f4f4f','darkturquoise'=>'#00ced1','darkviolet'=>'#9400d3','deeppink'=>'#ff1493',
14 'deepskyblue'=>'#00bfff','dimgray'=>'#696969','dimgrey'=>'#696969','dodgerblue'=>'#1e90ff',
15 'firebrick'=>'#b22222','floralwhite'=>'#fffaf0','forestgreen'=>'#228b22','fuchsia'=>'#ff00ff',
16 'gainsboro'=>'#dcdcdc','ghostwhite'=>'#f8f8ff','gold'=>'#ffd700','goldenrod'=>'#daa520','gray'=>'#808080',
17 'grey'=>'#808080','green'=>'#008000','greenyellow'=>'#adff2f','honeydew'=>'#f0fff0','hotpink'=>'#ff69b4',
18 'indianred'=>'#cd5c5c','indigo'=>'#4b0082','ivory'=>'#fffff0','khaki'=>'#f0e68c','lavender'=>'#e6e6fa',
19 'lavenderblush'=>'#fff0f5','lawngreen'=>'#7cfc00','lemonchiffon'=>'#fffacd','lightblue'=>'#add8e6',
20 'lightcoral'=>'#f08080','lightcyan'=>'#e0ffff','lightgoldenrodyellow'=>'#fafad2','lightgray'=>'#d3d3d3',
21 'lightgrey'=>'#d3d3d3','lightgreen'=>'#90ee90','lightpink'=>'#ffb6c1','lightsalmon'=>'#ffa07a',
22 'lightseagreen'=>'#20b2aa','lightskyblue'=>'#87cefa','lightslategray'=>'#778899','lightslategrey'=>'#778899',
23 'lightsteelblue'=>'#b0c4de','lightyellow'=>'#ffffe0','lime'=>'#00ff00','limegreen'=>'#32cd32','linen'=>'#faf0e6',
24 'magenta'=>'#ff00ff','maroon'=>'#800000','mediumaquamarine'=>'#66cdaa','mediumblue'=>'#0000cd',
25 'mediumorchid'=>'#ba55d3','mediumpurple'=>'#9370d8','mediumseagreen'=>'#3cb371','mediumslateblue'=>'#7b68ee',
26 'mediumspringgreen'=>'#00fa9a','mediumturquoise'=>'#48d1cc','mediumvioletred'=>'#c71585',
27 'midnightblue'=>'#191970','mintcream'=>'#f5fffa','mistyrose'=>'#ffe4e1','moccasin'=>'#ffe4b5',
28 'navajowhite'=>'#ffdead','navy'=>'#000080','oldlace'=>'#fdf5e6','olive'=>'#808000','olivedrab'=>'#6b8e23',
29 'orange'=>'#ffa500','orangered'=>'#ff4500','orchid'=>'#da70d6','palegoldenrod'=>'#eee8aa','palegreen'=>'#98fb98',
30 'paleturquoise'=>'#afeeee','palevioletred'=>'#d87093','papayawhip'=>'#ffefd5','peachpuff'=>'#ffdab9',
31 'peru'=>'#cd853f','pink'=>'#ffc0cb','plum'=>'#dda0dd','powderblue'=>'#b0e0e6','purple'=>'#800080',
32 'red'=>'#ff0000','rosybrown'=>'#bc8f8f','royalblue'=>'#4169e1','saddlebrown'=>'#8b4513','salmon'=>'#fa8072',
33 'sandybrown'=>'#f4a460','seagreen'=>'#2e8b57','seashell'=>'#fff5ee','sienna'=>'#a0522d','silver'=>'#c0c0c0',
34 'skyblue'=>'#87ceeb','slateblue'=>'#6a5acd','slategray'=>'#708090','slategrey'=>'#708090','snow'=>'#fffafa',
35 'springgreen'=>'#00ff7f','steelblue'=>'#4682b4','tan'=>'#d2b48c','teal'=>'#008080','thistle'=>'#d8bfd8',
36 'tomato'=>'#ff6347','turquoise'=>'#40e0d0','violet'=>'#ee82ee','wheat'=>'#f5deb3','white'=>'#ffffff',
37 'whitesmoke'=>'#f5f5f5','yellow'=>'#ffff00','yellowgreen'=>'#9acd32'
38 );
39
40
41 protected function _parse( & $variable )
42 {
43 if ( !self::_fits( $variable ) ) return false;
44
45 $this->type = 'CSS color';
46 $variants = self::_convert( $variable );
47 $this->value =
48 "<div style=\"background:{$variable}\" class=\"kint-color-preview\">{$variable}</div>"
49 . "<strong>hex :</strong> {$variants['hex']}\n"
50 . "<strong>rgb :</strong> {$variants['rgb']}\n"
51 . ( isset( $variants['name'] ) ? "<strong>name:</strong> {$variants['name']}\n" : '' )
52 . "<strong>hsl :</strong> {$variants['hsl']}";
53 }
54
55
56 private static function _fits( $variable )
57 {
58 if ( !is_string( $variable ) ) return false;
59
60 $var = strtolower( trim( $variable ) );
61
62 return isset( self::$_css3Named[$var] )
63 || preg_match(
64 '/^(?:#[0-9A-Fa-f]{3}|#[0-9A-Fa-f]{6}|(?:rgb|hsl)a?\s*\((?:\s*[0-9.%]+\s*,?){3,4}\))$/',
65 $var
66 );
67 }
68
69 private static function _convert( $color )
70 {
71 $color = strtolower( $color );
72 $decimalColors = array();
73 $variants = array(
74 'hex' => null,
75 'rgb' => null,
76 'name' => null,
77 'hsl' => null,
78 );
79
80 if ( isset( self::$_css3Named[ $color ] ) ) {
81 $variants['name'] = $color;
82 $color = self::$_css3Named[ $color ];
83 }
84
85 if ( $color{0} === '#' ) {
86 $variants['hex'] = $color;
87 $color = substr( $color, 1 );
88 if ( strlen( $color ) === 6 ) {
89 $colors = str_split( $color, 2 );
90 } else {
91 $colors = array(
92 $color{0} . $color{0},
93 $color{1} . $color{1},
94 $color{2} . $color{2},
95 );
96 }
97
98 $decimalColors = array_map( 'hexdec', $colors );
99 } elseif ( substr( $color, 0, 3 ) === 'rgb' ) {
100 $variants['rgb'] = $color;
101 preg_match_all( '#([0-9.%]+)#', $color, $matches );
102 $decimalColors = $matches[1];
103 foreach ( $decimalColors as &$color ) {
104 if ( strpos( $color, '%' ) !== false ) {
105 $color = str_replace( '%', '', $color ) * 2.55;
106 }
107 }
108
109
110 } elseif ( substr( $color, 0, 3 ) === 'hsl' ) {
111 $variants['hsl'] = $color;
112 preg_match_all( '#([0-9.%]+)#', $color, $matches );
113
114 $colors = $matches[1];
115 $colors[0] /= 360;
116 $colors[1] = str_replace( '%', '', $colors[1] ) / 100;
117 $colors[2] = str_replace( '%', '', $colors[2] ) / 100;
118
119 $decimalColors = self::_HSLtoRGB( $colors );
120 if ( isset( $colors[3] ) ) {
121 $decimalColors[] = $colors[3];
122 }
123 }
124
125 if ( isset( $decimalColors[3] ) ) {
126 $alpha = $decimalColors[3];
127 unset( $decimalColors[3] );
128 } else {
129 $alpha = null;
130 }
131 foreach ( $variants as $type => &$variant ) {
132 if ( isset( $variant ) ) continue;
133
134 switch ( $type ) {
135 case 'hex':
136 $variant = '#';
137 foreach ( $decimalColors as &$color ) {
138 $variant .= str_pad( dechex( $color ), 2, "0", STR_PAD_LEFT );
139 }
140 $variant .= isset( $alpha ) ? ' (alpha omitted)' : '';
141 break;
142 case 'rgb':
143 $rgb = $decimalColors;
144 if ( isset( $alpha ) ) {
145 $rgb[] = $alpha;
146 $a = 'a';
147 } else {
148 $a = '';
149 }
150 $variant = "rgb{$a}( " . implode( ', ', $rgb ) . " )";
151 break;
152 case 'hsl':
153 $rgb = self::_RGBtoHSL( $decimalColors );
154 if ( $rgb === null ) {
155 unset( $variants[ $type ] );
156 break;
157 }
158 if ( isset( $alpha ) ) {
159 $rgb[] = $alpha;
160 $a = 'a';
161 } else {
162 $a = '';
163 }
164
165 $variant = "hsl{$a}( " . implode( ', ', $rgb ) . " )";
166 break;
167 case 'name':
168
169 if ( ( $key = array_search( $variants['hex'], self::$_css3Named, true ) ) !== false ) {
170 $variant = $key;
171 } else {
172 unset( $variants[ $type ] );
173 }
174 break;
175 }
176
177 }
178
179 return $variants;
180 }
181
182
183 private static function _HSLtoRGB( array $hsl )
184 {
185 list( $h, $s, $l ) = $hsl;
186 $m2 = ( $l <= 0.5 ) ? $l * ( $s + 1 ) : $l + $s - $l * $s;
187 $m1 = $l * 2 - $m2;
188 return array(
189 round( self::_hue2rgb( $m1, $m2, $h + 0.33333 ) * 255 ),
190 round( self::_hue2rgb( $m1, $m2, $h ) * 255 ),
191 round( self::_hue2rgb( $m1, $m2, $h - 0.33333 ) * 255 ),
192 );
193 }
194
195
196 197 198
199 private static function _hue2rgb( $m1, $m2, $h )
200 {
201 $h = ( $h < 0 ) ? $h + 1 : ( ( $h > 1 ) ? $h - 1 : $h );
202 if ( $h * 6 < 1 ) return $m1 + ( $m2 - $m1 ) * $h * 6;
203 if ( $h * 2 < 1 ) return $m2;
204 if ( $h * 3 < 2 ) return $m1 + ( $m2 - $m1 ) * ( 0.66666 - $h ) * 6;
205 return $m1;
206 }
207
208
209 private static function _RGBtoHSL( array $rgb )
210 {
211 list( $clrR, $clrG, $clrB ) = $rgb;
212
213 $clrMin = min( $clrR, $clrG, $clrB );
214 $clrMax = max( $clrR, $clrG, $clrB );
215 $deltaMax = $clrMax - $clrMin;
216
217 $L = ( $clrMax + $clrMin ) / 510;
218
219 if ( 0 == $deltaMax ) {
220 $H = 0;
221 $S = 0;
222 } else {
223 if ( 0.5 > $L ) {
224 $S = $deltaMax / ( $clrMax + $clrMin );
225 } else {
226 $S = $deltaMax / ( 510 - $clrMax - $clrMin );
227 }
228
229 if ( $clrMax == $clrR ) {
230 $H = ( $clrG - $clrB ) / ( 6.0 * $deltaMax );
231 } else if ( $clrMax == $clrG ) {
232 $H = 1 / 3 + ( $clrB - $clrR ) / ( 6.0 * $deltaMax );
233 } else {
234 $H = 2 / 3 + ( $clrR - $clrG ) / ( 6.0 * $deltaMax );
235 }
236
237 if ( 0 > $H ) $H += 1;
238 if ( 1 < $H ) $H -= 1;
239 }
240 return array(
241 round( $H * 360 ),
242 round( $S * 100 ) . '%',
243 round( $L * 100 ) . '%'
244 );
245
246 }
247 }
248
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400