You are here

private function SassColour::rgb2hsl in Sassy 7

Same name and namespace in other branches
  1. 7.3 phpsass/script/literals/SassColour.php \SassColour::rgb2hsl()

* Converts from RGB to HSL colourspace * Algorithm adapted from {@link http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_RGB_to_HSL_or_HSV}

3 calls to SassColour::rgb2hsl()
SassColour::getHue in phamlp/sass/script/literals/SassColour.php
* Returns the hue of this colour. *
SassColour::getLightness in phamlp/sass/script/literals/SassColour.php
* Returns the lightness of this colour. *
SassColour::getSaturation in phamlp/sass/script/literals/SassColour.php
* Returns the saturation of this colour. *

File

phamlp/sass/script/literals/SassColour.php, line 808

Class

SassColour
SassColour class. A SassScript object representing a CSS colour.

Code

private function rgb2hsl() {
  $rgb = array(
    $this->red / 255,
    $this->green / 255,
    $this->blue / 255,
  );
  $max = max($rgb);
  $min = min($rgb);
  $c = $max - $min;

  // Lightness
  $l = ($max + $min) / 2;
  $this->lightness = $l * 100;

  // Saturation
  $this->saturation = ($c ? $l <= 0.5 ? $c / (2 * $l) : $c / (2 - 2 * $l) : 0) * 100;

  // Hue
  switch ($max) {
    case $min:
      $h = 0;
      break;
    case $rgb[0]:
      $h = 60 * ($rgb[1] - $rgb[2]) / $c;
      break;
    case $rgb[1]:
      $h = 60 * ($rgb[2] - $rgb[0]) / $c + 120;
      break;
    case $rgb[2]:
      $h = 60 * ($rgb[0] - $rgb[1]) / $c + 240;
      break;
  }
  $this->hue = fmod($h, 360);
}