private static function Kint_Parsers_Color::_convert in Devel 8.2
Same name and namespace in other branches
- 8 kint/kint/parsers/custom/color.php \Kint_Parsers_Color::_convert()
1 call to Kint_Parsers_Color::_convert()
- Kint_Parsers_Color::_parse in kint/
kint/ parsers/ custom/ color.php - * main and usually single method a custom parser must implement * *
File
- kint/
kint/ parsers/ custom/ color.php, line 69
Class
Code
private static function _convert($color) {
$color = strtolower($color);
$decimalColors = array();
$variants = array(
'hex' => null,
'rgb' => null,
'name' => null,
'hsl' => null,
);
if (isset(self::$_css3Named[$color])) {
$variants['name'] = $color;
$color = self::$_css3Named[$color];
}
if ($color[0] === '#') {
$variants['hex'] = $color;
$color = substr($color, 1);
if (strlen($color) === 6) {
$colors = str_split($color, 2);
}
else {
$colors = array(
$color[0] . $color[0],
$color[1] . $color[1],
$color[2] . $color[2],
);
}
$decimalColors = array_map('hexdec', $colors);
}
elseif (substr($color, 0, 3) === 'rgb') {
$variants['rgb'] = $color;
preg_match_all('#([0-9.%]+)#', $color, $matches);
$decimalColors = $matches[1];
foreach ($decimalColors as &$color) {
if (strpos($color, '%') !== false) {
$color = str_replace('%', '', $color) * 2.55;
}
}
}
elseif (substr($color, 0, 3) === 'hsl') {
$variants['hsl'] = $color;
preg_match_all('#([0-9.%]+)#', $color, $matches);
$colors = $matches[1];
$colors[0] /= 360;
$colors[1] = str_replace('%', '', $colors[1]) / 100;
$colors[2] = str_replace('%', '', $colors[2]) / 100;
$decimalColors = self::_HSLtoRGB($colors);
if (isset($colors[3])) {
$decimalColors[] = $colors[3];
}
}
if (isset($decimalColors[3])) {
$alpha = $decimalColors[3];
unset($decimalColors[3]);
}
else {
$alpha = null;
}
foreach ($variants as $type => &$variant) {
if (isset($variant)) {
continue;
}
switch ($type) {
case 'hex':
$variant = '#';
foreach ($decimalColors as &$color) {
$variant .= str_pad(dechex($color), 2, "0", STR_PAD_LEFT);
}
$variant .= isset($alpha) ? ' (alpha omitted)' : '';
break;
case 'rgb':
$rgb = $decimalColors;
if (isset($alpha)) {
$rgb[] = $alpha;
$a = 'a';
}
else {
$a = '';
}
$variant = "rgb{$a}( " . implode(', ', $rgb) . " )";
break;
case 'hsl':
$rgb = self::_RGBtoHSL($decimalColors);
if ($rgb === null) {
unset($variants[$type]);
break;
}
if (isset($alpha)) {
$rgb[] = $alpha;
$a = 'a';
}
else {
$a = '';
}
$variant = "hsl{$a}( " . implode(', ', $rgb) . " )";
break;
case 'name':
// [!] name in initial variants array must go after hex
if (($key = array_search($variants['hex'], self::$_css3Named, true)) !== false) {
$variant = $key;
}
else {
unset($variants[$type]);
}
break;
}
}
return $variants;
}