public static function Color::validateHex

Validates whether a hexadecimal color value is syntatically correct.

Parameters

$hex: The hexadecimal string to validate. May contain a leading '#'. May use the shorthand notation (e.g., '123' for '112233').

Return value

bool TRUE if $hex is valid or FALSE if it is not.

2 calls to Color::validateHex()
Color::hexToRgb in drupal/core/lib/Drupal/Core/Utility/Color.php
Parses a hexadecimal color string like '#abc' or '#aabbcc'.
ColorTest::testHexToRgb in drupal/core/modules/system/lib/Drupal/system/Tests/Common/ColorTest.php
Tests Color::hexToRgb().

File

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

Class

Color
Performs color conversions.

Namespace

Drupal\Core\Utility

Code

public static function validateHex($hex) {

  // Must be a string.
  $valid = is_string($hex);

  // Hash prefix is optional.
  $hex = ltrim($hex, '#');

  // Must be either RGB or RRGGBB.
  $length = drupal_strlen($hex);
  $valid = $valid && ($length === 3 || $length === 6);

  // Must be a valid hex value.
  $valid = $valid && ctype_xdigit($hex);
  return $valid;
}