public static function Color::rgbToHex

public static Color::rgbToHex($input)

Converts RGB color arrays and RGB strings in CSS notation to lowercase simple colors like '#aabbcc'.

Parameters

array|string $input: The value to convert. If the value is an array the first three elements will be used as the red, green and blue components. String values in CSS notation like '10, 20, 30' are also supported.

Return value

string The lowercase simple color representation of the given color.

File

core/lib/Drupal/Component/Utility/Color.php, line 78

Class

Color
Performs color conversions.

Namespace

Drupal\Component\Utility

Code

public static function rgbToHex($input) {
  // Remove named array keys if input comes from Color::hex2rgb().
  if (is_array($input)) {
    $rgb = array_values($input);
  }
  // Parse string input in CSS notation ('10, 20, 30').
  elseif (is_string($input)) {
    preg_match('/(\d+), ?(\d+), ?(\d+)/', $input, $rgb);
    array_shift($rgb);
  }

  $out = 0;
  foreach ($rgb as $k => $v) {
    $out |= $v << (16 - $k * 8);
  }

  return '#' . str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!Color.php/function/Color::rgbToHex/8.1.x