/** * Apply the plugin to Css object * * @param Stylecow\Css $css The css object */ public static function apply(Css $css) { $css->executeRecursive(function ($code) { foreach ($code->getProperties() as $property) { $property->executeFunction('color', function ($parameters) { $rgba = Color::toRGBA(array_shift($parameters)); foreach ($parameters as $operation) { if (strpos($operation, ':') === false) { if (preg_match('/^[0-9]+$/', $operation)) { $function = 'tint'; $value = $operation; } else { continue; } } else { list($function, $value) = Parser::explodeTrim(':', $operation, 2); } switch ($function) { case 'hue': case 'saturation': case 'light': $rgba = Color::HSLA_RGBA(Color::editChannel(Color::RGBA_HSLA($rgba), $function, $value)); break; case 'red': case 'green': case 'blue': case 'alpha': $rgba = Color::editChannel($rgba, $function, $value); break; case 'tint': $rgba = Color::editTint($rgba, $value); break; } } if ($rgba[3] < 1) { return 'rgba(' . implode(', ', $rgba) . ')'; } return '#' . Color::RGBA_HEX($rgba); }); } }); }
/** * Converts a color declaration in rgba format. * * @param string $colors The css color declaration * * @return array The rgba color values */ public static function resolveColor($colors) { $colors = Parser::explodeTrim(' ', $colors); if (count($colors) === 1) { return Color::toRGBA($colors[0]); } $sumColors = array(0, 0, 0, 0); foreach ($colors as $k => $color) { $color = Color::toRGBA($color); $sumColors[0] += $color[0]; $sumColors[1] += $color[1]; $sumColors[2] += $color[2]; $sumColors[3] += $color[3]; } $total = count($colors); $sumColors[0] = round($sumColors[0] / $total); $sumColors[1] = round($sumColors[1] / $total); $sumColors[2] = round($sumColors[2] / $total); $sumColors[3] = round($sumColors[3] / $total); return $sumColors; }
/** * Generate the Ie filter to emulate the rotation of an element: background: linear-gradient(top, #333, #999); * * @param array &$code The piece of the parsed code * @param array $params The linear gradient parameters */ public static function getLinearGradientFilter($params) { $point = 'top'; $direction = null; if (preg_match('/(top|bottom|left|right|deg)/', $params[0])) { $point = array_shift($params); } switch ($point) { case 'top': case '90deg': $direction = 'vertical'; $reverse = false; break; case 'bottom': case '-90deg': $direction = 'vertical'; $reverse = true; break; case 'left': case '180deg': case '-180deg': $direction = 'horizontal'; $reverse = false; break; case 'right': case '0deg': case '360deg': $direction = 'vertical'; $reverse = true; break; } $colors = $params; if (isset($direction) && count($colors) === 2) { $colors[0] = Color::RGBA_HEX(Color::toRGBA($colors[0])); $colors[1] = Color::RGBA_HEX(Color::toRGBA($colors[1])); if ($reverse) { $colors = array_reverse($colors); } if ($direction === 'horizontal') { return 'progid:DXImageTransform.Microsoft.gradient(startColorStr=\'#' . $colors[0] . '\', endColorStr=\'#' . $colors[1] . '\', GradientType=1)'; } else { return 'progid:DXImageTransform.Microsoft.gradient(startColorStr=\'#' . $colors[0] . '\', endColorStr=\'#' . $colors[1] . '\')'; } } }
/** * Generate the Ie filter to emulate the background hsla color of an element: background: hsla(0, 0, 0, 0.5); * * @param array $params The rgba parameters */ public static function getHSLAFilter($params) { return self::getRGBAFilter(Color::HSLA_RGBA($params)); }