function Color::validateHex

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Color.php \Drupal\Component\Utility\Color::validateHex()
  2. 8.9.x core/lib/Drupal/Component/Utility/Color.php \Drupal\Component\Utility\Color::validateHex()
  3. 10 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.

4 calls to Color::validateHex()
Color::hexToRgb in core/lib/Drupal/Component/Utility/Color.php
Parses a hexadecimal color string like '#abc' or '#aabbcc'.
ColorTest::testValidateHex in core/tests/Drupal/Tests/Component/Utility/ColorTest.php
@covers \Drupal\Component\Utility\Color::validateHex
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

File

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

Class

Color
Performs color conversions.

Namespace

Drupal\Component\Utility

Code

public static function validateHex($hex) {
    if (!is_string($hex)) {
        return FALSE;
    }
    return preg_match('/^[#]?([0-9a-fA-F]{3}){1,2}$/', $hex) === 1;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.