Convert HSL to RGB

@api

@param integer $hue        H from 0 to 360
@param integer $saturation S from 0 to 100
@param integer $lightness  L from 0 to 100

@return array
492157
public function toRGB($hue, $saturation, $lightness)
    {
        if ($hue < 0) {
            $hue += 360;
        }

        $h = $hue / 360;
        $s = min(100, max(0, $saturation)) / 100;
        $l = min(100, max(0, $lightness)) / 100;

        $m2 = $l <= 0.5 ? $l * ($s + 1) : $l + $s - $l * $s;
        $m1 = $l * 2 - $m2;

        $r = $this->hueToRGB($m1, $m2, $h + 1/3) * 255;
        $g = $this->hueToRGB($m1, $m2, $h) * 255;
        $b = $this->hueToRGB($m1, $m2, $h - 1/3) * 255;

        $out = [Type::T_COLOR, $r, $g, $b];

        return $out;
    }
32215
private void HSVtoRGB(float hue, float sat, float bri) {

        int i;
        float f;
        float p;
        float q;
        float t;
        if (sat == 0) {
            m_red = bri;
            m_green = bri;
            m_blue = bri;
            return;
        }
        hue /= 60;
        i = (int)Math.floor(hue);
        f = hue - i;
        p = bri * (1 - sat);
        q = bri * (1 - (sat * f));
        t = bri * (1 - (sat * (1 - f)));
        switch (i) {
            case 0:
                m_red = bri;
                m_green = t;
                m_blue = p;
                break;
            case 1:
                m_red = q;
                m_green = bri;
                m_blue = p;
                break;
            case 2:
                m_red = p;
                m_green = bri;
                m_blue = t;
                break;
            case 3:
                m_red = p;
                m_green = q;
                m_blue = bri;
                break;
            case 4:
                m_red = t;
                m_green = p;
                m_blue = bri;
                break;
            default: // case 5:
                m_red = bri;
                m_green = p;
                m_blue = q;
                break;
        }
    }
312442
public function getLuminance($mode = self::HSL) {
		$r = ($this->rgb >> 16) & 0xff;
		$g = ($this->rgb >> 8) & 0xff;
		$b = $this->rgb & 0xff;

		switch ($mode) {
			case 0:
				// fastest, but less accurate.
				return (($r + $r + $r + $b + $g + $g + $g + $g) >> 3) / 255;
				break;
			case 1:
				// Digital CCIR601
				return (int)(0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
				break;
			case 2:
				// Ditigal ITU-R
				return (int)(0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255;
				break;
			case 3:
				// HSP algorithm
				return round(sqrt(0.299 * $r * $r + 0.587 * $g * $g + 0.114 * $b * $b)) / 255;
				break;
			case self::HSL:
				// HSL algorithm
				return (max($r, $g, $b) + min($r, $g, $b)) / (2 * 255);
				break;
			case self::HSV:
				// HSV algorithm
				return max($r, $g, $b) / 255;
				break;
			case self::HSI:
				// HSI algorithm
				return ($r + $g + $b) / (3 * 255);
				break;
			default:
				throw new Exception('Unknown color mode');
				break;
		}
	}
834809
public function toHSL($red, $green, $blue)
    {
        $min = min($red, $green, $blue);
        $max = max($red, $green, $blue);

        $l = $min + $max;
        $d = $max - $min;

        if ((int) $d === 0) {
            $h = $s = 0;
        } else {
            if ($l < 255) {
                $s = $d / $l;
            } else {
                $s = $d / (510 - $l);
            }

            if ($red == $max) {
                $h = 60 * ($green - $blue) / $d;
            } elseif ($green == $max) {
                $h = 60 * ($blue - $red) / $d + 120;
            } elseif ($blue == $max) {
                $h = 60 * ($red - $green) / $d + 240;
            }
        }

        return [Type::T_HSL, fmod($h, 360), $s * 100, $l / 5.1];
    }
957957
static double hsla( double h, double s, double l, double a ) {

        h = (h % 360) / 360;
        s = clamp(s); l = clamp(l); a = clamp(a);

        double m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
        double m1 = l * 2 - m2;

        return rgba( hslaHue(h + 1.0/3, m1, m2) * 255,
                     hslaHue(h        , m1, m2) * 255,
                     hslaHue(h - 1.0/3, m1, m2) * 255,
                     a);
    }
759925
public static final Color fromHSL(double h, double s, double l)
    {
        h = (((h % 360) + 360) % 360) / 360;

        s = convertPercents(s);

        l = convertPercents(l);

        return fromNormalizedHSL(h, s, l);
    }
1284827
public static function fromHsl(float $hue, float $saturation, float $lightness): Color
    {
        if (!Validator::isValidHsl($hue, $saturation, $lightness)) {
            throw new InvalidColorException('Invalid HSL value');
        }

        return new self(...array_values(self::hslToRgb($hue, $saturation, $lightness)));
    }
1221852
public function toRGB()
    {
        $h = $this->hue;
        $s = $this->saturation / 100;
        $l = $this->lightness / 100;
        $r;
        $g;
        $b;
        $c = (1 - abs(2 * $l - 1)) * $s;
        $x = $c * (1 - abs(fmod(($h / 60), 2) - 1));
        $m = $l - ($c / 2);
        if ($h < 60) {
            list($r, $g, $b) = [$c, $x, 0];
        } elseif ($h < 120) {
            list($r, $g, $b) = [$x, $c, 0];
        } elseif ($h < 180) {
            list($r, $g, $b) = [0, $c, $x];
        } elseif ($h < 240) {
            list($r, $g, $b) = [0, $x, $c];
        } elseif ($h < 300) {
            list($r, $g, $b) = [$x, 0, $c];
        } else {
            list($r, $g, $b) = [$c, 0, $x];
        }
        $r = ($r + $m) * 255;
        $g = ($g + $m) * 255;
        $b = ($b + $m) * 255;

        return [
            round($r),
            round($g),
            round($b),
        ];
    }
986037
def get_hs_color(self):
        
        # Get the color from homematic. In general this is just the hue parameter.
        hm_color = self.getCachedOrUpdatedValue("COLOR", channel=self._color_channel)

        if hm_color >= 200:
            # 200 is a special case (white), so we have a saturation of 0.
            # Larger values are undefined. For the sake of robustness we return "white" anyway.
            return 0, 0

        # For all other colors we assume saturation of 1
        return hm_color/200, 1
997220
public static function parse($color)
    {
        $hsl = '/hsl\(([0-9]{1,3}),\s*([0-9]{1,3})%,\s*([0-9]{1,3})%\)/';
        $rgb = '/rgb\(([0-9]{1,3}),\s*([0-9]{1,3}),\s*([0-9]{1,3})\)/';

        // HSL
        if (preg_match($hsl, $color, $m)) {
            return static::hsl((int)$m[1], (int)$m[2] / 100, (int)$m[3] / 100);
        } // RGB
        elseif (preg_match($rgb, $color, $m)) {
            return static::rgb((int)$m[1], (int)$m[2], (int)$m[3]);
        } // Hex
        else {
            return static::hex($color);
        };

    }
1168578
public function toHSL()
    {
        // r, g, b as fractions of 1
        $r = $this->r / 255.0;
        $g = $this->g / 255.0;
        $b = $this->b / 255.0;

        // most prominent primary color
        $max = max($r, $g, $b);
        // least prominent primary color
        $min = min($r, $g, $b);
        // maximum delta
        $dmax = $max - $min;


        // intensity = (r + g + b) / 3
        // lightness = (r + g + b - (non-extreme color)) / 2
        $l = ($min + $max) / 2;

        if ($dmax == 0) {
            // This means R=G=B, so:
            $h = 0;
            $s = 0;
        } else {
            // remember ligtness = (min+max) / 2
            $s = ($l < 0.5) ?
                 $dmax / ($l * 2) :
                 $dmax / ((1 - $l) * 2);

            $dr = ((($max - $r) / 6) + ($dmax / 2)) / $dmax;
            $dg = ((($max - $g) / 6) + ($dmax / 2)) / $dmax;
            $db = ((($max - $b) / 6) + ($dmax / 2)) / $dmax;

            if ($r == $max) {
                $h = (0.0 / 3) + $db - $dg;
            } elseif ($g == $max) {
                $h = (1.0 / 3) + $dr - $db;
            } elseif ($b == $max) {
                $h = (2.0 / 3) + $dg - $dr;
            }

            // the case of less than 0 radians
            if ($h < 0) {
                $h += 1;
            }

            if ($h > 1) {
                $h -= 1;
            }
        }

        return array($h, $s, $l);
    }
797263
public function getHsl(): array
    {
        $red   = $this->colors['r'] / 255;
        $green = $this->colors['g'] / 255;
        $blue  = $this->colors['b'] / 255;

        // Determine lowest & highest value and chroma
        $max    = max($red, $green, $blue);
        $min    = min($red, $green, $blue);
        $chroma = $max - $min;

        // Calculate Luminosity
        $lightness = ($max + $min) / 2;

        // If chroma is 0, the given color is grey
        // therefore hue and saturation are set to 0
        if ($chroma == 0) {
            return ['h' => 0, 's' => 0, 'l' => $lightness];
        }

        $saturation = $lightness > 0.5
            ? $chroma / (2 - $max - $min)
            : $chroma / ($max + $min);

        switch ($max) {
            case $red:
                $hue = ($green - $blue) / $chroma + ($green < $blue ? 6 : 0);
                break;

            case $green:
                $hue = ($blue - $red) / $chroma + 2;
                break;

            case $blue:
            default:
                $hue = ($red - $green) / $chroma + 4;
                break;
        }

        $hue /= 6;

        // Return HSL Color as array
        return [
            'h' => $hue,
            's' => $saturation,
            'l' => $lightness,
        ];
    }
1189304
function (hue, saturation, value, alpha) {
      alpha = /%/.test(alpha)
        ? parseInt(alpha, 10) / 100
        : parseFloat(alpha, 10);

      alpha = String(alpha).replace(/^0+\./, '.');

      var rgb = hsb2rgb(hue, saturation, value);
      return 'rgba(' + rgb.join(', ') + ', ' + alpha + ')';
    }
1307245
public function getHslString(Color $color)
    {
        return 'hsl('. round($color->hue) . ', ' . round($color->saturation * 100) . '%, ' . round($color->lightness * 100) . '%)';
    }
232489
public function hue($hue = null)
    {
        if (is_numeric($hue)) {
            $this->hue = $hue >= 0 && $hue <= 360 ? $hue : $this->hue;
            return $this;
        }
        return (int) $this->hue;
    }
167643
private function hue_to_rgb($v1, $v2, $vh)
    {
        $vh = $vh < 0 ? $vh + 1 : ($vh > 1 ? $vh - 1 : $vh);
        if ($vh * 6 < 1) return $v1 + ($v2 - $v1) * 6 * $vh;
        if ($vh * 2 < 1) return $v2;
        if ($vh * 3 < 2) return $v1 + ($v2 - $v1) * ((2/3) - $vh) * 6;
        return $v1;
    }
1250079
def hsl_to_rgb(h, s, l):
    """"""
    h /= 360
    s /= 100
    l /= 100

    m2 = l * (s + 1) if l <= .5 else l + s - l * s
    m1 = 2 * l - m2

    def h_to_rgb(h):
        h = h % 1
        if 6 * h < 1:
            return m1 + 6 * h * (m2 - m1)
        if 2 * h < 1:
            return m2
        if 3 * h < 2:
            return m1 + 6 * (2 / 3 - h) * (m2 - m1)
        return m1

    r, g, b = map(
        lambda x: round(x * 255), map(h_to_rgb, (h + 1 / 3, h, h - 1 / 3))
    )

    return r, g, b