You are here

public function ColorHex::toString in Color Field 8.2

A string representation of this color in the current format.

Parameters

bool $opacity: Whether or not to display the opacity.

Return value

string The color in format: #RRGGBB.

Overrides ColorInterface::toString

File

src/ColorHex.php, line 57

Class

ColorHex
Hex represents the Hex color format.

Namespace

Drupal\color_field

Code

public function toString($opacity = TRUE) {
  $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);
}