HSL To RGB

This algorithm converts HSL color model to RGB color model.



									class RGB
{
	public $R;
	public $G;
	public $B;
}

class HSL
{
	public $H;
	public $S;
	public $L;
}

function HueToRGB($v1, $v2, $vH)
{
	if ($vH < 0)
		$vH += 1;

	if ($vH > 1)
		$vH -= 1;

	if ((6 * $vH) < 1)
		return ($v1 + ($v2 - $v1) * 6 * $vH);

	if ((2 * $vH) < 1)
		return $v2;

	if ((3 * $vH) < 2)
		return ($v1 + ($v2 - $v1) * ((2.0 / 3) - $vH) * 6);

	return $v1;
}

function HSLToRGB($hsl) {
	$rgb = new RGB();

	if ($hsl->S == 0)
	{
		$rgb->R = $rgb->G = $rgb->B = floor($hsl->L * 255);
	}
	else
	{
		$hue = (float)$hsl->H / 360;

		$v2 = ($hsl->L < 0.5) ? ($hsl->L * (1 + $hsl->S)) : (($hsl->L + $hsl->S) - ($hsl->L * $hsl->S));
		$v1 = 2 * $hsl->L - $v2;

		$rgb->R = floor(255 * HueToRGB($v1, $v2, $hue + (1.0 / 3)));
		$rgb->G = floor(255 * HueToRGB($v1, $v2, $hue));
		$rgb->B = floor(255 * HueToRGB($v1, $v2, $hue - (1.0 / 3)));
	}

	return $rgb;
}
								


Example

									$data = new HSL();
$data->H = 138;
$data->S = 0.50;
$data->L = 0.76;
$value = HSLToRGB($data);
								


Output

									R: 163
G: 224
B: 181