You are here

private function CSSCompression_Color::rgb2hex in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg_css_compress/css-compressor-3.x/src/lib/Color.inc \CSSCompression_Color::rgb2hex()

Converts rgb values to hex codes

Parameters

(string) val: Color to be converted:

1 call to CSSCompression_Color::rgb2hex()
CSSCompression_Color::color in advagg_css_compress/css-compressor-3.x/src/lib/Color.inc
Central handler for all color conversions.

File

advagg_css_compress/css-compressor-3.x/src/lib/Color.inc, line 97

Class

CSSCompression_Color
CSS Compressor [VERSION] [DATE] Corey Hart @ http://www.codenothing.com

Code

private function rgb2hex($val) {
  if (!preg_match($this->rrgb, $val, $match)) {
    return $val;
  }

  // locals
  $hex = '0123456789abcdef';
  $str = explode(',', $match[1]);
  $new = '';

  // Incase rgb was defined with single val
  if (!$str) {
    $str = array(
      $match[1],
    );
  }
  foreach ($str as $x) {
    $x = strpos($x, '%') !== false ? intval(intval($x) / 100 * 255) : intval($x);
    if ($x > 255) {
      $x = 255;
    }
    if ($x < 0) {
      $x = 0;
    }
    $new .= $hex[($x - $x % 16) / 16];
    $new .= $hex[$x % 16];
  }

  // Repeat hex code to complete 6 digit hex requirement for single definitions
  if (count($str) == 1) {
    $new .= $new . $new;
  }

  // Replace with hex value
  return "#{$new}";
}