/** * 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::resolveColor(array_shift($parameters)); foreach ($parameters as $operation) { if (strpos($operation, ':') === false) { if (preg_match('/^[\\+\\-]?[0-9]+$/', $operation)) { $function = 'tint'; $value = $operation; } else { if (preg_match('/^[\\+\\-]?[0-9\\.]+$/', $operation)) { $function = 'alpha'; $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); }); } }); }
/** * 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 rgba color of an element: background: rgba(0, 0, 0, 0.5); * * @param array $params The rgba parameters */ public static function getRGBAFilter($params) { $color = '#' . dechex(round(255 * floatval($params[3]))) . Color::RGBA_HEX($params); return 'progid:DXImageTransform.Microsoft.gradient(startColorStr=\'' . $color . '\', endColorStr=\'' . $color . '\')'; }