You are here

public static function Color::validateHex in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Component/Utility/Color.php \Drupal\Component\Utility\Color::validateHex()

Validates whether a hexadecimal color value is syntactically 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.

3 calls to Color::validateHex()
Color::hexToRgb in core/lib/Drupal/Component/Utility/Color.php
Parses a hexadecimal color string like '#abc' or '#aabbcc'.
CreateNew::validateArguments in core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
Validates the arguments.
RotateImageEffect::validateConfigurationForm in core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php
Form validation handler.

File

core/lib/Drupal/Component/Utility/Color.php, line 25
Contains \Drupal\Component\Utility\Color.

Class

Color
Performs color conversions.

Namespace

Drupal\Component\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 = Unicode::strlen($hex);
  $valid = $valid && ($length === 3 || $length === 6);

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