You are here

public static function TwigColor::rgbToHex in Twig Tools 8

Returns the hexadecimal color value of an RGB array of values.

Parameters

array $rgb: An array of RGB (red, green, blue) values.

Return value

string The hexadecimal color equivalent of the passed RGB color.

1 call to TwigColor::rgbToHex()
TwigColor::cssRgbToHex in src/TwigExtension/TwigColor.php
Returns the hexadecimal color value of a passed CSS RGB color string.

File

src/TwigExtension/TwigColor.php, line 38

Class

TwigColor
Class TwigConvert.

Namespace

Drupal\twig_tools\TwigExtension

Code

public static function rgbToHex(array $rgb) {
  $rgb = array_reduce($rgb, function ($carry, $color) {
    $options = [
      'options' => [
        'min_range' => 0,
        'max_range' => 255,
      ],
    ];

    // Validate that each value is between 0-255.
    $int_color = filter_var($color, FILTER_VALIDATE_INT, $options);
    if ($int_color !== FALSE) {
      $carry[] = $int_color;
    }
    return $carry;
  }, []);
  if (count($rgb) === 3) {
    return sprintf("#%02x%02x%02x", $rgb[0], $rgb[1], $rgb[2]);
  }
}