public function ColorHex::__construct in Color Field 8.2
Create a new Hex from a string.
Parameters
string $color: The string hex value (i.e. "FFFFFF").
string $opacity: The opacity value.
Throws
Exception
File
- src/
ColorHex.php, line 27
Class
- ColorHex
- Hex represents the Hex color format.
Namespace
Drupal\color_fieldCode
public function __construct($color, $opacity) {
$color = trim(strtolower($color));
if (substr($color, 0, 1) === '#') {
$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)) {
// @throws exception.
}
$this->color = hexdec($color);
$this
->setOpacity(floatval($opacity));
return $this;
}