ColorapiService.php in Color API 8
File
src/Service/ColorapiService.php
View source
<?php
namespace Drupal\colorapi\Service;
use Drupal\colorapi\Plugin\DataType\HexColorInterface;
class ColorapiService implements ColorapiServiceInterface {
public function isValidHexadecimalColorString($string) {
return preg_match(HexColorInterface::HEXADECIMAL_COLOR_REGEX, $string);
}
public function hexToRgb($hex, $colorIndex) {
$hex = str_replace("#", "", $hex);
if ($colorIndex == 'red') {
if (strlen($hex) == 3) {
$rgb_value = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
}
else {
$rgb_value = hexdec(substr($hex, 0, 2));
}
}
elseif ($colorIndex == 'green') {
if (strlen($hex) == 3) {
$rgb_value = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
}
else {
$rgb_value = hexdec(substr($hex, 2, 2));
}
}
if ($colorIndex == 'blue') {
if (strlen($hex) == 3) {
$rgb_value = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
}
else {
$rgb_value = hexdec(substr($hex, 4, 2));
}
}
return $rgb_value;
}
}