You are here

private function JQueryColorpickerRawRgbDisplayFormatter::hexToRgb in Jquery Colorpicker 8

Helper function to convert hex to rgb.

1 call to JQueryColorpickerRawRgbDisplayFormatter::hexToRgb()
JQueryColorpickerRawRgbDisplayFormatter::viewElements in src/Plugin/Field/FieldFormatter/JQueryColorpickerRawRgbDisplayFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/JQueryColorpickerRawRgbDisplayFormatter.php, line 52

Class

JQueryColorpickerRawRgbDisplayFormatter
Formatter class for jquery_colorpicker field.

Namespace

Drupal\jquery_colorpicker\Plugin\Field\FieldFormatter

Code

private function hexToRgb($hex) {
  $hex = str_replace("#", "", $hex);
  if (strlen($hex) == 3) {
    $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
    $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
    $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
  }
  else {
    $r = hexdec(substr($hex, 0, 2));
    $g = hexdec(substr($hex, 2, 2));
    $b = hexdec(substr($hex, 4, 2));
  }
  $rgb = [
    $r,
    $g,
    $b,
  ];

  // Returns the rgb values separated by commas.
  return implode(",", $rgb);
}