Exemple #1
0
 public static function parse($str)
 {
     if ($test = Color::test($str)) {
         $color = $test['value'];
         $type = $test['type'];
     } else {
         return false;
     }
     $rgba = false;
     switch ($type) {
         case 'hex':
             $rgba = Color::hexToRgb($color);
             break;
         case 'rgb':
         case 'rgba':
         case 'hsl':
         case 'hsla':
             $function = $type;
             $vals = substr($color, strlen($function) + 1);
             // Trim function name and start paren.
             $vals = substr($vals, 0, strlen($vals) - 1);
             // Trim end paren.
             $vals = array_map('trim', explode(',', $vals));
             // Explode to array of arguments.
             // Always set the alpha channel.
             $vals[3] = isset($vals[3]) ? floatval($vals[3]) : 1;
             if (strpos($function, 'rgb') === 0) {
                 $rgba = Color::normalizeCssRgb($vals);
             } else {
                 $rgba = Color::cssHslToRgb($vals);
             }
             break;
         case 'keyword':
             $keywords = self::getKeywords();
             $rgba = $keywords[$color];
             break;
     }
     return $rgba;
 }
 protected function minifyColors()
 {
     static $keywords_patt, $functions_patt;
     $minified_keywords = Color::getMinifyableKeywords();
     if (!$keywords_patt) {
         $keywords_patt = '~(?<![\\w-\\.#])(' . implode('|', array_keys($minified_keywords)) . ')(?![\\w-\\.#\\]])~iS';
         $functions_patt = Regex::make('~{{ LB }}(rgb|hsl)\\(([^\\)]{5,})\\)~iS');
     }
     $this->string->pregReplaceCallback($keywords_patt, function ($m) use($minified_keywords) {
         return $minified_keywords[strtolower($m[0])];
     });
     $this->string->pregReplaceCallback($functions_patt, function ($m) {
         $args = Functions::parseArgs(trim($m[2]));
         if (stripos($m[1], 'hsl') === 0) {
             $args = Color::cssHslToRgb($args);
         }
         return Color::rgbToHex($args);
     });
 }