public static function Color::hexToRgb

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

2 calls to Color::hexToRgb()
ColorTest::testHexToRgb in drupal/core/modules/system/lib/Drupal/system/Tests/Common/ColorTest.php
Tests Color::hexToRgb().
form_validate_color in drupal/core/includes/form.inc
Form element validation handler for #type 'color'.

File

drupal/core/lib/Drupal/Core/Utility/Color.php, line 49
Definition of Drupal\Core\Utility\Color.

Class

Color
Performs color conversions.

Namespace

Drupal\Core\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 shorhands 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,
  );
}