class Color

Performs color conversions.

Hierarchy

  • class \Drupal\Core\Utility\Color

Expanded class hierarchy of Color

2 files declare their use of Color
ColorTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Common/ColorTest.php
Definition of Drupal\system\Tests\Common\ColorTest.
form.inc in drupal/core/includes/form.inc
Functions for form and batch generation and processing.
5 string references to 'Color'
ColorTest::getInfo in drupal/core/modules/color/lib/Drupal/color/Tests/ColorTest.php
color_library_info in drupal/core/modules/color/color.module
Implements hook_library_info().
FormTest::testColorValidation in drupal/core/modules/system/lib/Drupal/system/Tests/Form/FormTest.php
Tests validation of #type 'color' elements.
form_test_color in drupal/core/modules/system/tests/modules/form_test/form_test.module
Form constructor for testing #type 'color' elements.
form_test_menu in drupal/core/modules/system/tests/modules/form_test/form_test.module
Implements hook_menu().

File

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

Namespace

Drupal\Core\Utility
View source
class Color {

  /**
   * Validates whether a hexadecimal color value is syntatically correct.
   *
   * @param $hex
   *   The hexadecimal string to validate. May contain a leading '#'. May use
   *   the shorthand notation (e.g., '123' for '112233').
   *
   * @return bool
   *   TRUE if $hex is valid or FALSE if it is not.
   */
  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;
  }

  /**
   * Parses a hexadecimal color string like '#abc' or '#aabbcc'.
   *
   * @param string $hex
   *   The hexadecimal color string to parse.
   *
   * @return array
   *   An array containing the values for 'red', 'green', 'blue'.
   *
   * @throws \InvalidArgumentException
   */
  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,
    );
  }

  /**
   * Converts RGB color arrays and RGB strings in CSS notation to lowercase
   * simple colors like '#aabbcc'.
   *
   * @param array|string $input
   *   The value to convert. If the value is an array the first three elements
   *   will be used as the red, green and blue components. String values in CSS
   *   notation like '10, 20, 30' are also supported.
   *
   * @return string
   *   The lowercase simple color representation of the given color.
   */
  public static function rgbToHex($input) {

    // Remove named array keys if input comes from Color::hex2rgb().
    if (is_array($input)) {
      $rgb = array_values($input);
    }
    elseif (is_string($input)) {
      preg_match('/(\\d+), ?(\\d+), ?(\\d+)/', $input, $rgb);
      array_shift($rgb);
    }
    $out = 0;
    foreach ($rgb as $k => $v) {
      $out |= $v << 16 - $k * 8;
    }
    return '#' . str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Color::hexToRgb public static function Parses a hexadecimal color string like '#abc' or '#aabbcc'.
Color::rgbToHex public static function Converts RGB color arrays and RGB strings in CSS notation to lowercase simple colors like '#aabbcc'.
Color::validateHex public static function Validates whether a hexadecimal color value is syntatically correct.