Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created November 16, 2015 21:48
Show Gist options
  • Save zoffixznet/72e6d8221832a282ad8c to your computer and use it in GitHub Desktop.
Save zoffixznet/72e6d8221832a282ad8c to your computer and use it in GitHub Desktop.
Convert RGB to HSL
use v6;
sub rgb2hsl ( $r is copy, $g is copy, $b is copy ) {
$_ /= 255 for $r, $g, $b;
# Formula cortesy http://www.rapidtables.com/convert/color/rgb-to-hsl.htm
my $c_max = max $r, $g, $b;
my $c_min = min $r, $g, $b;
my \Δ = $c_max - $c_min;
my $h = Δ == 0 ?? 0 !! do {
given $c_max {
when $r { 60 * ( ($g - $b)/Δ % 6 ) }
when $g { 60 * ( ($b - $r)/Δ + 2 ) }
when $b { 60 * ( ($r - $g)/Δ + 4 ) }
}
};
my $l = Δ / 2;
my $s = Δ / (1 - abs(2*$l - 1));
return %(:$h, :$s, :$l);
}
say rgb2hsl(10, 188, 222).perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment