RGB To HSL
This algorithm converts RGB color model to HSL color model.
class HSL
{
public $H;
public $S;
public $L;
}
class RGB
{
public $R;
public $G;
public $B;
}
function RGBToHSL($rgb) {
$hsl = new HSL();
$r = ($rgb->R / 255.0);
$g = ($rgb->G / 255.0);
$b = ($rgb->B / 255.0);
$min = min(min($r, $g), $b);
$max = max(max($r, $g), $b);
$delta = $max - $min;
$hsl->L = ($max + $min) / 2;
if ($delta == 0)
{
$hsl->H = 0;
$hsl->S = 0.0;
}
else
{
$hsl->S = ($hsl->L <= 0.5) ? ($delta / ($max + $min)) : ($delta / (2 - $max - $min));
if ($r == $max)
{
$hue = (($g - $b) / 6) / $delta;
}
else if ($g == $max)
{
$hue = (1.0 / 3) + (($b - $r) / 6) / $delta;
}
else
{
$hue = (2.0 / 3) + (($r - $g) / 6) / $delta;
}
if ($hue < 0)
$hue += 1;
if ($hue > 1)
$hue -= 1;
$hsl->H = (int)($hue * 360);
}
return $hsl;
}
Example
$data = new RGB();
$data->R = 82;
$data->G = 0;
$data->B = 87;
$value = RGBToHSL($data);
Output
H: 296
S: 1
L: 0.17058823529412