class Color

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Render/Element/Color.php \Drupal\Core\Render\Element\Color
Same name and namespace in other branches
  1. 9 core/modules/color/src/Plugin/migrate/destination/Color.php \Drupal\color\Plugin\migrate\destination\Color
  2. 9 core/modules/color/src/Plugin/migrate/source/d7/Color.php \Drupal\color\Plugin\migrate\source\d7\Color
  3. 9 core/lib/Drupal/Core/Render/Element/Color.php \Drupal\Core\Render\Element\Color
  4. 9 core/lib/Drupal/Component/Utility/Color.php \Drupal\Component\Utility\Color
  5. 8.9.x core/modules/color/src/Plugin/migrate/destination/Color.php \Drupal\color\Plugin\migrate\destination\Color
  6. 8.9.x core/modules/color/src/Plugin/migrate/source/d7/Color.php \Drupal\color\Plugin\migrate\source\d7\Color
  7. 8.9.x core/lib/Drupal/Core/Render/Element/Color.php \Drupal\Core\Render\Element\Color
  8. 8.9.x core/lib/Drupal/Component/Utility/Color.php \Drupal\Component\Utility\Color
  9. 10 core/lib/Drupal/Core/Render/Element/Color.php \Drupal\Core\Render\Element\Color
  10. 10 core/lib/Drupal/Component/Utility/Color.php \Drupal\Component\Utility\Color

Performs color conversions.

Hierarchy

  • class \Drupal\Component\Utility\Color

Expanded class hierarchy of Color

6 files declare their use of Color
Color.php in core/lib/Drupal/Core/Render/Element/Color.php
ColorTest.php in core/tests/Drupal/Tests/Component/Utility/ColorTest.php
CreateNew.php in core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
GDToolkit.php in core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
Rotate.php in core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php

... See full list

23 string references to 'Color'
AjaxFormsTestSimpleForm::buildForm in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php
Form constructor.
claro_preprocess_input in core/themes/claro/claro.theme
Implements template_preprocess_HOOK() for input.
Color::preRenderColor in core/lib/Drupal/Core/Render/Element/Color.php
Prepares a #type 'color' render element for input.html.twig.
ContentEntityNonRevisionableFieldTest::testMultiColumnNonRevisionableBaseField in core/tests/Drupal/KernelTests/Core/Entity/ContentEntityNonRevisionableFieldTest.php
Tests multi column non revisionable base field for revisionable entity.
core.data_types.schema.yml in core/config/schema/core.data_types.schema.yml
core/config/schema/core.data_types.schema.yml

... See full list

File

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

Namespace

Drupal\Component\Utility
View source
class Color {
    
    /**
     * Validates whether a hexadecimal color value is syntactically 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) {
        if (!is_string($hex)) {
            return FALSE;
        }
        return preg_match('/^[#]?([0-9a-fA-F]{3}){1,2}$/', $hex) === 1;
    }
    
    /**
     * 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 shorthands 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 [
            'red' => $c >> 16 & 0xff,
            'green' => $c >> 8 & 0xff,
            'blue' => $c & 0xff,
        ];
    }
    
    /**
     * Converts RGB color arrays or strings to lowercase CSS notation.
     *
     * @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);
    }
    
    /**
     * Normalize the hex color length to 6 characters for comparison.
     *
     * @param string $hex
     *   The hex color to normalize.
     *
     * @return string
     *   The 6 character hex color.
     */
    public static function normalizeHexLength($hex) {
        // Ignore '#' prefixes.
        $hex = ltrim($hex, '#');
        if (strlen($hex) === 3) {
            $hex[5] = $hex[2];
            $hex[4] = $hex[2];
            $hex[3] = $hex[1];
            $hex[2] = $hex[1];
            $hex[1] = $hex[0];
        }
        return '#' . $hex;
    }

}

Members

Title Sort descending Modifiers Object type Summary
Color::hexToRgb public static function Parses a hexadecimal color string like &#039;#abc&#039; or &#039;#aabbcc&#039;.
Color::normalizeHexLength public static function Normalize the hex color length to 6 characters for comparison.
Color::rgbToHex public static function Converts RGB color arrays or strings to lowercase CSS notation.
Color::validateHex public static function Validates whether a hexadecimal color value is syntactically correct.

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