Same name in other branches
  1. 8.x-2.x src/ColorHex.php \Drupal\color_field\ColorHex

Hex represents the Hex color format.

Hierarchy

Expanded class hierarchy of ColorHex

5 files declare their use of ColorHex
ColorFieldFormatterCss.php in src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php
ColorFieldFormatterSwatch.php in src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatch.php
ColorFieldFormatterSwatchOptions.php in src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatchOptions.php
ColorFieldFormatterText.php in src/Plugin/Field/FieldFormatter/ColorFieldFormatterText.php
color_field.module in ./color_field.module

File

src/ColorHex.php, line 10

Namespace

Drupal\color_field
View source
class ColorHex extends ColorBase {
    
    /**
     * The Hex triplet of the color as an int.
     *
     * @var int
     */
    protected int $color;
    
    /**
     * Create a new Hex from a string.
     *
     * @param string $color
     *   The string hex value (i.e. "FFFFFF").
     * @param string $opacity
     *   The opacity value.
     *
     * @throws \Exception
     *   If the color doesn't appear to be a valid hex value.
     */
    public function __construct(string $color, ?string $opacity) {
        $color = trim(strtolower($color));
        if (str_starts_with($color, '#')) {
            $color = substr($color, 1);
        }
        if (strlen($color) === 3) {
            $color = str_repeat($color[0], 2) . str_repeat($color[1], 2) . str_repeat($color[2], 2);
        }
        if (!preg_match('/[0-9A-F]{6}/i', $color)) {
            throw new \Exception("Color {$color} doesn't appear to be a valid hex value");
        }
        $this->color = hexdec($color);
        $opacity = $opacity ?? '1';
        $this->setOpacity((double) $opacity);
        return $this;
    }
    
    /**
     * A string representation of this color in the current format.
     *
     * @param bool $opacity
     *   Whether to display the opacity.
     *
     * @return string
     *   The color in format: #RRGGBB.
     */
    public function toString(bool $opacity = TRUE) : string {
        $rgb = $this->toRgb();
        $hex = '#';
        $hex .= str_pad(dechex($rgb->getRed()), 2, "0", STR_PAD_LEFT);
        $hex .= str_pad(dechex($rgb->getGreen()), 2, "0", STR_PAD_LEFT);
        $hex .= str_pad(dechex($rgb->getBlue()), 2, "0", STR_PAD_LEFT);
        if ($opacity) {
            $hex .= ' ' . $this->getOpacity();
        }
        return strtolower($hex);
    }
    
    /**
     * {@inheritdoc}
     */
    public function toHex() : ColorHex {
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function toRgb() : ColorRGB {
        $red = ($this->color & 0xff0000) >> 16;
        $green = ($this->color & 0xff00) >> 8;
        $blue = $this->color & 0xff;
        $opacity = $this->getOpacity();
        return new ColorRGB($red, $green, $blue, $opacity);
    }
    
    /**
     * {@inheritdoc}
     */
    public function toHsl() : ColorHsl {
        return $this->toRGB()
            ->toHsl();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ColorBase::$namedColors public static property Named HTML colors.
ColorBase::$opacity protected property The opacity of the color.
ColorBase::$patterns public static property Regexes to match various color formats.
ColorBase::getOpacity public function Get the opacity.
ColorBase::setOpacity public function Set the opacity.
ColorHex::$color protected property The Hex triplet of the color as an int.
ColorHex::toHex public function Get the color as a hex instance. Overrides ColorInterface::toHex
ColorHex::toHsl public function Get the color as an HSL instance. Overrides ColorInterface::toHsl
ColorHex::toRgb public function Get the color as an RGB instance. Overrides ColorInterface::toRgb
ColorHex::toString public function A string representation of this color in the current format. Overrides ColorInterface::toString
ColorHex::__construct public function Create a new Hex from a string.