PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/class/libraries/vendor/raveren/kint/parsers/custom/color.php

https://gitlab.com/VoyaTrax/vtCMS2
PHP | 400 lines | 211 code | 33 blank | 156 comment | 36 complexity | 6b60160b42c9ee7fb540c4d59ed29dee MD5 | raw file
  1<?php
  2class 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					// [!] name in initial variants array must go after hex
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	 * Helper function for _color_hsl2rgb().
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 * TEST DATA
251 *
252dd(array(
253'hsl(0,  100%,50%)',
254'hsl(30, 100%,50%)',
255'hsl(60, 100%,50%)',
256'hsl(90, 100%,50%)',
257'hsl(120,100%,50%)',
258'hsl(150,100%,50%)',
259'hsl(180,100%,50%)',
260'hsl(210,100%,50%)',
261'hsl(240,100%,50%)',
262'hsl(270,100%,50%)',
263'hsl(300,100%,50%)',
264'hsl(330,100%,50%)',
265'hsl(360,100%,50%)',
266'hsl(120,100%,25%)',
267'hsl(120,100%,50%)',
268'hsl(120,100%,75%)',
269'hsl(120,100%,50%)',
270'hsl(120, 67%,50%)',
271'hsl(120, 33%,50%)',
272'hsl(120,  0%,50%)',
273'hsl(120, 60%,70%)',
274'#f03',
275'#F03',
276'#ff0033',
277'#FF0033',
278'rgb(255,0,51)',
279'rgb(255, 0, 51)',
280'rgb(100%,0%,20%)',
281'rgb(100%, 0%, 20%)',
282'hsla(240,100%,50%,0.05)',
283'hsla(240,100%,50%, 0.4)',
284'hsla(240,100%,50%, 0.7)',
285'hsla(240,100%,50%,   1)',
286'rgba(255,0,0,0.1)',
287'rgba(255,0,0,0.4)',
288'rgba(255,0,0,0.7)',
289'rgba(255,0,0,  1)',
290'black',
291'silver',
292'gray',
293'white',
294'maroon',
295'red',
296'purple',
297'fuchsia',
298'green',
299'lime',
300'olive',
301'yellow',
302'navy',
303'blue',
304'teal',
305'aqua',
306'orange',
307'aliceblue',
308'antiquewhite',
309'aquamarine',
310'azure',
311'beige',
312'bisque',
313'blanchedalmond',
314'blueviolet',
315'brown',
316'burlywood',
317'cadetblue',
318'chartreuse',
319'chocolate',
320'coral',
321'cornflowerblue',
322'cornsilk',
323'crimson',
324'darkblue',
325'darkcyan',
326'darkgoldenrod',
327'darkgray',
328'darkgreen',
329'darkgrey',
330'darkkhaki',
331'darkmagenta',
332'darkolivegreen',
333'darkorange',
334'darkorchid',
335'darkred',
336'darksalmon',
337'darkseagreen',
338'darkslateblue',
339'darkslategray',
340'darkslategrey',
341'darkturquoise',
342'darkviolet',
343'deeppink',
344'deepskyblue',
345'dimgray',
346'dimgrey',
347'dodgerblue',
348'firebrick',
349'floralwhite',
350'forestgreen',
351'gainsboro',
352'ghostwhite',
353'gold',
354'goldenrod',
355'greenyellow',
356'grey',
357'honeydew',
358'hotpink',
359'indianred',
360'indigo',
361'ivory',
362'khaki',
363'lavender',
364'lavenderblush',
365'lawngreen',
366'lemonchiffon',
367'lightblue',
368'lightcoral',
369'lightcyan',
370'lightgoldenrodyellow',
371'lightgray',
372'lightgreen',
373'lightgrey',
374'lightpink',
375'lightsalmon',
376'lightseagreen',
377'lightskyblue',
378'lightslategray',
379'lightslategrey',
380'lightsteelblue',
381'lightyellow',
382'limegreen',
383'linen',
384'mediumaquamarine',
385'mediumblue',
386'mediumorchid',
387'mediumpurple',
388'mediumseagreen',
389'mediumslateblue',
390'mediumspringgreen',
391'mediumturquoise',
392'mediumvioletred',
393'midnightblue',
394'mintcream',
395'mistyrose',
396'moccasin',
397'navajowhite',
398'oldlace',
399'olivedrab',
400));*/