Created
November 16, 2015 21:48
-
-
Save zoffixznet/72e6d8221832a282ad8c to your computer and use it in GitHub Desktop.
Convert RGB to HSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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