/** * Parses a css property code and creates a new Property object * * @param string $string The css code * @return Stylecow\Property */ public static function createFromString($string) { $pieces = Parser::explodeTrim(':', $string, 2); if (isset($pieces[1])) { return new static($pieces[0], $pieces[1]); } return new static($pieces[0], null); }
/** * Parses a css selector code and creates a new Selector object * * @param string $string The css code * @return Stylecow\Selector */ public static function createFromString($string) { $string = trim($string); if ($string[0] === '@') { $pieces = Parser::explodeTrim(' ', $string, 2); return new static($pieces[0], isset($pieces[1]) ? Parser::explodeTrim(',', $pieces[1]) : null); } return new static(null, Parser::explodeTrim(',', $string)); }
/** * Apply the plugin to Css object * * @param Stylecow\Css $css The css object */ public static function apply(Css $css) { $css->executeRecursive(function ($code) { while (strpos((string) $code->selector, ':matches(') !== false) { foreach ($code->selector->get() as $key => $selector) { if (preg_match('/:matches\\(([^\\)]*)\\)/', $selector, $match)) { $code->selector->delete($key); foreach (Parser::explodeTrim(',', $match[1]) as $matchSelector) { $code->selector->add(str_replace($match[0], $matchSelector, $selector)); } } } } }); }
/** * 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; }
public static function parseMediaQueries(array $selectors) { $parsed = array(); foreach ($selectors as $k => $selector) { $parsed[$k] = array(); foreach (Parser::explodeTrim(' and ', strtolower($selector)) as $rule) { $expressions = Parser::explodeTrim(' ', $rule); while ($expressions) { $expression = array_shift($expressions); if ($expression[0] === '(' && substr($expression, -1) === ')') { $expression = trim(substr($expression, 1, -1)); } if ($expression === 'not') { $parsed[$k]['not'] = true; continue; } if ($expression === 'only') { $parsed[$k]['only'] = true; continue; } if ($expression === 'all') { continue; } if (in_array($expression, self::$types)) { $parsed[$k]['type'] = $expression; continue; } $expression = Parser::explodeTrim(':', $expression, 2); $name = $expression[0]; $value = isset($expression[1]) ? $expression[1] : true; if (isset(self::$expressions[$name])) { $fn = self::$expressions[$name]['value']; $parsed[$k][$name] = self::$fn($value); continue; } if ((strpos($name, 'min-') === 0 || strpos($name, 'max-') === 0) && ($subname = substr($name, 4)) && isset(self::$expressions[$subname]) && self::$expressions[$subname]['min-max'] === true) { $fn = self::$expressions[$subname]['value']; $parsed[$k][$name] = self::$fn($value); continue; } } } } return $parsed; }