You are here

private function CSSmin::shortenHexColors in Advanced CSS/JS Aggregation 7.2

Compresses HEX color values of the form #AABBCC to #ABC or short color name.

DOES NOT compress CSS ID selectors which match the above pattern (which would break things). e.g. #AddressForm { ... }

DOES NOT compress IE filters, which have hex color values (which would break things). e.g. filter: chroma(color="#FFFFFF");

DOES NOT compress invalid hex values. e.g. background-color: #aabbccdd

Parameters

string $css:

Return value

string

1 call to CSSmin::shortenHexColors()
CSSmin::minify in advagg_css_compress/yui/CSSMin.inc
Minifies the given input CSS string

File

advagg_css_compress/yui/CSSMin.inc, line 864

Class

CSSmin

Code

private function shortenHexColors($css) {

  // Look for hex colors inside { ... } (to avoid IDs) and
  // which don't have a =, or a " in front of them (to avoid filters)
  $pattern = '/(=\\s*?["\']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(\\}|[^0-9a-f{][^{]*?\\})/iS';
  $_index = $index = $lastIndex = $offset = 0;
  $longHexColors = array(
    '#f0ffff' => 'azure',
    '#f5f5dc' => 'beige',
    '#ffe4c4' => 'bisque',
    '#a52a2a' => 'brown',
    '#ff7f50' => 'coral',
    '#ffd700' => 'gold',
    '#808080' => 'gray',
    '#008000' => 'green',
    '#4b0082' => 'indigo',
    '#fffff0' => 'ivory',
    '#f0e68c' => 'khaki',
    '#faf0e6' => 'linen',
    '#800000' => 'maroon',
    '#000080' => 'navy',
    '#fdf5e6' => 'oldlace',
    '#808000' => 'olive',
    '#ffa500' => 'orange',
    '#da70d6' => 'orchid',
    '#cd853f' => 'peru',
    '#ffc0cb' => 'pink',
    '#dda0dd' => 'plum',
    '#800080' => 'purple',
    '#f00' => 'red',
    '#fa8072' => 'salmon',
    '#a0522d' => 'sienna',
    '#c0c0c0' => 'silver',
    '#fffafa' => 'snow',
    '#d2b48c' => 'tan',
    '#008080' => 'teal',
    '#ff6347' => 'tomato',
    '#ee82ee' => 'violet',
    '#f5deb3' => 'wheat',
  );
  $sb = array();
  while (preg_match($pattern, $css, $m, 0, $offset)) {
    $index = $this
      ->indexOf($css, $m[0], $offset);
    $lastIndex = $index + strlen($m[0]);
    $isFilter = $m[1] !== null && $m[1] !== '';
    $sb[] = $this
      ->strSlice($css, $_index, $index);
    if ($isFilter) {

      // Restore, maintain case, otherwise filter will break
      $sb[] = $m[1] . '#' . $m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7];
    }
    else {
      if (strtolower($m[2]) == strtolower($m[3]) && strtolower($m[4]) == strtolower($m[5]) && strtolower($m[6]) == strtolower($m[7])) {

        // Compress.
        $hex = '#' . strtolower($m[3] . $m[5] . $m[7]);
      }
      else {

        // Non compressible color, restore but lower case.
        $hex = '#' . strtolower($m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]);
      }

      // replace Hex colors with shorter color names
      $sb[] = array_key_exists($hex, $longHexColors) ? $longHexColors[$hex] : $hex;
    }
    $_index = $offset = $lastIndex - strlen($m[8]);
  }
  $sb[] = $this
    ->strSlice($css, $_index);
  return implode('', $sb);
}