You are here

public function ColorRGB::toHsl in Color Field 8.2

Get the color as a HSL instance.

Return value

\Drupal\color_field\ColorHSL The color as a HSL instance.

Overrides ColorInterface::toHsl

File

src/ColorRGB.php, line 129

Class

ColorRGB
RGB represents the RGB color format.

Namespace

Drupal\color_field

Code

public function toHsl() {
  $r = $this
    ->getRed() / 255;
  $g = $this
    ->getGreen() / 255;
  $b = $this
    ->getBlue() / 255;
  $max = max($r, $g, $b);
  $min = min($r, $g, $b);
  $l = ($max + $min) / 2;
  if ($max == $min) {

    // Achromatic.
    $h = $s = 0;
  }
  else {
    $d = $max - $min;
    $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
    switch ($max) {
      case $r:
        $h = ($g - $b) / $d + ($g < $b ? 6 : 0);
        break;
      case $g:
        $h = ($b - $r) / $d + 2;
        break;
      case $b:
        $h = ($r - $g) / $d + 4;
        break;
    }
    $h /= 6;
  }
  $h = floor($h * 360);
  $s = floor($s * 100);
  $l = floor($l * 100);
  return new ColorHSL(intval($h), intval($s), intval($l), $this
    ->getOpacity());
}