Exemple #1
0
 protected static function colorAdjust($color, array $adjustments)
 {
     $fn_matched = preg_match('!^(#|rgba?)!', $color, $m);
     $keywords = CssCrush_Color::getKeywords();
     // Support for Hex, RGB, RGBa and keywords
     // HSL and HSLa are passed over
     if ($fn_matched or array_key_exists($color, $keywords)) {
         $alpha = null;
         $rgb = null;
         // Get an RGB value from the color argument
         if ($fn_matched) {
             switch ($m[1]) {
                 case '#':
                     $rgb = CssCrush_Color::hexToRgb($color);
                     break;
                 case 'rgb':
                 case 'rgba':
                     $rgba = $m[1] == 'rgba' ? true : false;
                     $rgb = substr($color, $rgba ? 5 : 4);
                     $rgb = substr($rgb, 0, strlen($rgb) - 1);
                     $rgb = array_map('trim', explode(',', $rgb));
                     $alpha = $rgba ? array_pop($rgb) : null;
                     $rgb = CssCrush_Color::normalizeCssRgb($rgb);
                     break;
             }
         } else {
             $rgb = $keywords[$color];
         }
         $hsl = CssCrush_Color::rgbToHsl($rgb);
         // Clean up adjustment parameters to floating point numbers
         // Calculate the new HSL value
         $counter = 0;
         foreach ($adjustments as &$_val) {
             $index = $counter++;
             $_val = $_val ? trim(str_replace('%', '', $_val)) : 0;
             // Reduce value to float
             $_val /= 100;
             // Calculate new HSL value
             $hsl[$index] = max(0, min(1, $hsl[$index] + $_val));
         }
         // Finally convert new HSL value to RGB
         $rgb = CssCrush_Color::hslToRgb($hsl);
         // Return as hex if there is no alpha channel
         // Otherwise return RGBa string
         if (is_null($alpha)) {
             return CssCrush_Color::rgbToHex($rgb);
         }
         $rgb[] = $alpha;
         return 'rgba(' . implode(',', $rgb) . ')';
     } else {
         return $color;
     }
 }
function csscrush_hsl(CssCrush_Rule $rule)
{
    foreach ($rule as &$declaration) {
        if (!$declaration->skip and (!empty($declaration->functions) and in_array('hsl', $declaration->functions))) {
            while (preg_match('!hsl(___p\\d+___)!', $declaration->value, $m)) {
                $full_match = $m[0];
                $token = $m[1];
                $hsl = trim($rule->parens[$token], '()');
                $hsl = array_map('trim', explode(',', $hsl));
                $rgb = CssCrush_Color::cssHslToRgb($hsl);
                $hex = CssCrush_Color::rgbToHex($rgb);
                $declaration->value = str_replace($full_match, $hex, $declaration->value);
            }
        }
    }
}