2 class Kint_Parsers_Color extends kintParser
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'
41 protected function _parse( & $variable )
43 if ( !self::_fits( $variable ) ) return false;
45 $this->type = 'CSS color';
46 $variants = self::_convert( $variable );
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']}";
56 private static function _fits( $variable )
58 if ( !is_string( $variable ) ) return false;
60 $var = strtolower( trim( $variable ) );
62 return isset( self::$_css3Named[$var] )
64 '/^(?:#[0-9A-Fa-f]{3}|#[0-9A-Fa-f]{6}|(?:rgb|hsl)a?\s*\((?:\s*[0-9.%]+\s*,?){3,4}\))$/',
69 private static function _convert( $color )
71 $color = strtolower( $color );
72 $decimalColors = array();
80 if ( isset( self::$_css3Named[ $color ] ) ) {
81 $variants['name'] = $color;
82 $color = self::$_css3Named[ $color ];
85 if ( $color{0} === '#' ) {
86 $variants['hex'] = $color;
87 $color = substr( $color, 1 );
88 if ( strlen( $color ) === 6 ) {
89 $colors = str_split( $color, 2 );
92 $color{0} . $color{0},
93 $color{1} . $color{1},
94 $color{2} . $color{2},
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;
110 } elseif ( substr( $color, 0, 3 ) === 'hsl' ) {
111 $variants['hsl'] = $color;
112 preg_match_all( '#([0-9.%]+)#', $color, $matches );
114 $colors = $matches[1];
116 $colors[1] = str_replace( '%', '', $colors[1] ) / 100;
117 $colors[2] = str_replace( '%', '', $colors[2] ) / 100;
119 $decimalColors = self::_HSLtoRGB( $colors );
120 if ( isset( $colors[3] ) ) {
121 $decimalColors[] = $colors[3];
125 if ( isset( $decimalColors[3] ) ) {
126 $alpha = $decimalColors[3];
127 unset( $decimalColors[3] );
131 foreach ( $variants as $type => &$variant ) {
132 if ( isset( $variant ) ) continue;
137 foreach ( $decimalColors as &$color ) {
138 $variant .= str_pad( dechex( $color ), 2, "0", STR_PAD_LEFT );
140 $variant .= isset( $alpha ) ? ' (alpha omitted)' : '';
143 $rgb = $decimalColors;
144 if ( isset( $alpha ) ) {
150 $variant = "rgb{$a}( " . implode( ', ', $rgb ) . " )";
153 $rgb = self::_RGBtoHSL( $decimalColors );
154 if ( $rgb === null ) {
155 unset( $variants[ $type ] );
158 if ( isset( $alpha ) ) {
165 $variant = "hsl{$a}( " . implode( ', ', $rgb ) . " )";
168 // [!] name in initial variants array must go after hex
169 if ( ( $key = array_search( $variants['hex'], self::$_css3Named, true ) ) !== false ) {
172 unset( $variants[ $type ] );
183 private static function _HSLtoRGB( array $hsl )
185 list( $h, $s, $l ) = $hsl;
186 $m2 = ( $l <= 0.5 ) ? $l * ( $s + 1 ) : $l + $s - $l * $s;
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 ),
197 * Helper function for _color_hsl2rgb().
199 private static function _hue2rgb( $m1, $m2, $h )
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;
209 private static function _RGBtoHSL( array $rgb )
211 list( $clrR, $clrG, $clrB ) = $rgb;
213 $clrMin = min( $clrR, $clrG, $clrB );
214 $clrMax = max( $clrR, $clrG, $clrB );
215 $deltaMax = $clrMax - $clrMin;
217 $L = ( $clrMax + $clrMin ) / 510;
219 if ( 0 == $deltaMax ) {
224 $S = $deltaMax / ( $clrMax + $clrMin );
226 $S = $deltaMax / ( 510 - $clrMax - $clrMin );
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 );
234 $H = 2 / 3 + ( $clrR - $clrG ) / ( 6.0 * $deltaMax );
237 if ( 0 > $H ) $H += 1;
238 if ( 1 < $H ) $H -= 1;
242 round( $S * 100 ) . '%',
243 round( $L * 100 ) . '%'
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)',
370 'lightgoldenrodyellow',