Same name and namespace in other branches
  1. 8.x-2.x src/ColorHex.php \Drupal\color_field\ColorHex::toString() 1 comment

A string representation of this color in the current format.

Parameters

bool $opacity: Whether to display the opacity.

Return value

string The color in format: #RRGGBB.

Overrides ColorInterface::toString

File

src/ColorHex.php, line 61

Class

ColorHex
Hex represents the Hex color format.

Namespace

Drupal\color_field

Code

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);
}