| 1 | |||||||||||||||||||||||||||
| 2 | # HSL color space specific code | ||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||
| 4 | package Graphics::Toolkit::Color::Space::Instance::HSL; | ||||||||||||||||||||||||||
| 5 | 16 | ▶ | use v5.12; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 6 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Graphics::Toolkit::Color::Space qw/min max mod_real/; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||
| 9 | 97 | ▶ | sub from_hsl { | ||||||||||||||||||||||||
|
Subroutine
from_hsl: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 10 | 97 | my ($h, $s, $l) = @{$_[0]}; | |||||||||||||||||||||||||
| 11 | 97 | $h *= 6; | |||||||||||||||||||||||||
| 12 | 97 | my $C = $s * (1 - abs($l * 2 - 1)); | |||||||||||||||||||||||||
| 13 | 97 | my $X = $C * (1 - abs( mod_real($h, 2) - 1) ); | |||||||||||||||||||||||||
| 14 | 97 | my $m = $l - ($C / 2); | |||||||||||||||||||||||||
| 15 | 97 | ▶ | my @rgb = ($h < 1) ? ($C + $m, $X + $m, $m) | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 16 | : ($h < 2) ? ($X + $m, $C + $m, $m) | ||||||||||||||||||||||||||
| 17 | : ($h < 3) ? ( $m, $C + $m, $X + $m) | ||||||||||||||||||||||||||
| 18 | : ($h < 4) ? ( $m, $X + $m, $C + $m) | ||||||||||||||||||||||||||
| 19 | : ($h < 5) ? ($X + $m, $m, $C + $m) | ||||||||||||||||||||||||||
| 20 | : ($C + $m, $m, $X + $m); | ||||||||||||||||||||||||||
| 21 | 97 | return \@rgb; | |||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||
| 23 | 75 | ▶ | sub to_hsl { | ||||||||||||||||||||||||
|
Subroutine
to_hsl: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 24 | 75 | my ($r, $g, $b) = @{$_[0]}; | |||||||||||||||||||||||||
| 25 | 75 | my $vmax = max($r, $g, $b), | |||||||||||||||||||||||||
| 26 | my $vmin = min($r, $g, $b); | ||||||||||||||||||||||||||
| 27 | 75 | my $l = ($vmax + $vmin) / 2; | |||||||||||||||||||||||||
| 28 | 75 | ▶ | return ([0, 0, $l]) if $vmax == $vmin; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 29 | 45 | my $d = $vmax - $vmin; | |||||||||||||||||||||||||
| 30 | 45 | ▶ | my $s = ($l > 0.5) ? ($d / (2 - $vmax - $vmin)) : ($d / ($vmax + $vmin)); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 31 | 45 | ▶ | my $h = ($vmax == $r) ? (($g - $b) / $d + ($g < $b ? 6 : 0)) : | ||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||
| 32 | ($vmax == $g) ? (($b - $r) / $d + 2) | ||||||||||||||||||||||||||
| 33 | : (($r - $g) / $d + 4); | ||||||||||||||||||||||||||
| 34 | 45 | return ([$h/6, $s, $l]); | |||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||
| 37 | 16 | Graphics::Toolkit::Color::Space->new( | |||||||||||||||||||||||||
| 38 | family => 'HSL', | ||||||||||||||||||||||||||
| 39 | axis => [qw/hue saturation lightness/], | ||||||||||||||||||||||||||
| 40 | range => [ 360, 100, 100 ], | ||||||||||||||||||||||||||
| 41 | precision => 0, | ||||||||||||||||||||||||||
| 42 | type => [qw/angular linear linear/], | ||||||||||||||||||||||||||
| 43 | #suffix => ['', '%', '%'], | ||||||||||||||||||||||||||
| 44 | convert => {RGB => [\&from_hsl, \&to_hsl]}, | ||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||