public static function Color::hexToRgb

public static Color::hexToRgb($hex)

Parses a hexadecimal color string like '#abc' or '#aabbcc'.

Parameters

string $hex: The hexadecimal color string to parse.

Return value

array An array containing the values for 'red', 'green', 'blue'.

Throws

\InvalidArgumentException

File

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

Class

Color
Performs color conversions.

Namespace

Drupal\Component\Utility

Code

public static function hexToRgb($hex) {
  if (!self::validateHex($hex)) {
    throw new \InvalidArgumentException("'$hex' is not a valid hex value.");
  }

  // Ignore '#' prefixes.
  $hex = ltrim($hex, '#');

  // Convert shorthands like '#abc' to '#aabbcc'.
  if (strlen($hex) == 3) {
    $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
  }

  $c = hexdec($hex);

  return array(
    'red' => $c >> 16 & 0xFF,
    'green' => $c >> 8 & 0xFF,
    'blue' => $c & 0xFF,
  );
}

© 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::hexToRgb/8.1.x