You are here

public static function TwigColor::cssRgbToHex in Twig Tools 8

Returns the hexadecimal color value of a passed CSS RGB color string.

Parameters

string $string: The variable to get the hexadecimal color equivalent of.

Return value

string The hexadecimal color equivalent of the passed CSS RGB color string.

File

src/TwigExtension/TwigColor.php, line 71

Class

TwigColor
Class TwigConvert.

Namespace

Drupal\twig_tools\TwigExtension

Code

public static function cssRgbToHex($string) {

  // Match a 'rgb(xxx,xxx,xxx)' string pattern.
  $group_pattern = '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
  $pattern = '/.*[rgb]\\(' . $group_pattern . ',\\s*' . $group_pattern . ',\\s*' . $group_pattern . '\\).*/';
  preg_match($pattern, $string, $rgb);
  if (is_array($rgb) && count($rgb) === 4) {

    // Remove the first array element which has the original full string.
    array_shift($rgb);
    return self::rgbToHex($rgb);
  }
}