You are here

public function ColorHSL::toRgb in Color Field 8.2

Get the color as a RGB instance.

Return value

\Drupal\color_field\ColorRGB The color as a RGB instance.

Overrides ColorInterface::toRgb

File

src/ColorHSL.php, line 124

Class

ColorHSL
RGB represents the RGB color format.

Namespace

Drupal\color_field

Code

public function toRgb() {
  $h = $this
    ->getHue();
  $s = $this
    ->getSat();
  $l = $this
    ->getLum();
  $h /= 60;
  if ($h < 0) {
    $h = 6 - fmod(-$h, 6);
  }
  $h = fmod($h, 6);
  $s = max(0, min(1, $s / 100));
  $l = max(0, min(1, $l / 100));
  $c = (1 - abs(2 * $l - 1)) * $s;
  $x = $c * (1 - abs(fmod($h, 2) - 1));
  if ($h < 1) {
    $r = $c;
    $g = $x;
    $b = 0;
  }
  elseif ($h < 2) {
    $r = $x;
    $g = $c;
    $b = 0;
  }
  elseif ($h < 3) {
    $r = 0;
    $g = $c;
    $b = $x;
  }
  elseif ($h < 4) {
    $r = 0;
    $g = $x;
    $b = $c;
  }
  elseif ($h < 5) {
    $r = $x;
    $g = 0;
    $b = $c;
  }
  else {
    $r = $c;
    $g = 0;
    $b = $x;
  }
  $m = $l - $c / 2;
  $r = round(($r + $m) * 255);
  $g = round(($g + $m) * 255);
  $b = round(($b + $m) * 255);
  return new ColorRGB(intval($r), intval($g), intval($b), $this
    ->getOpacity());
}