You are here

class ColorapiService in Color API 8

Service class for the Color API module.

Hierarchy

Expanded class hierarchy of ColorapiService

1 string reference to 'ColorapiService'
colorapi.services.yml in ./colorapi.services.yml
colorapi.services.yml

File

src/Service/ColorapiService.php, line 10

Namespace

Drupal\colorapi\Service
View source
class ColorapiService implements ColorapiServiceInterface {

  /**
   * {@inheritdoc}
   */
  public function isValidHexadecimalColorString($string) {
    return preg_match(HexColorInterface::HEXADECIMAL_COLOR_REGEX, $string);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members