File Coverage

lib/Graphics/Toolkit/Color/Space/Instance/CIELCHab.pm
Criterion Covered Total %
statement 21 21 100.0
branch 6 6 100.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 32 34 94.1


line stmt bran cond sub pod time code
1              
2             # CIE LCh(ab) cylindrical color space variant of CIELAB
3              
4             package Graphics::Toolkit::Color::Space::Instance::CIELCHab;
5 15     15   241993 use v5.12;
  15         62  
6 15     15   86 use warnings;
  15         41  
  15         1040  
7 15     15   524 use Graphics::Toolkit::Color::Space qw/round_decimals/;
  15         29  
  15         6122  
8              
9             my $TAU = 6.283185307;
10              
11             sub from_lab {
12 8     8 0 17 my ($lab) = shift;
13 8         34 my $a = $lab->[1] * 1000 - 500;
14 8         27 my $b = $lab->[2] * 400 - 200;
15              
16 8 100       33 $a = 0 if round_decimals($a, 5) == 0;
17 8 100       23 $b = 0 if round_decimals($b, 5) == 0;
18 8         38 my $c = sqrt( ($a**2) + ($b**2));
19 8         96 my $h = atan2($b, $a);
20 8 100       32 $h += $TAU if $h < 0;
21 8         47 return ([$lab->[0], $c / 539, $h / $TAU]);
22             }
23             sub to_lab {
24 10     10 0 28 my ($lch) = shift;
25 10         76 my $a = $lch->[1] * cos($lch->[2] * $TAU) * 539;
26 10         35 my $b = $lch->[1] * sin($lch->[2] * $TAU) * 539;
27 10         63 return ([$lch->[0], ($a+500) / 1000, ($b+200) / 400 ]);
28             }
29              
30             Graphics::Toolkit::Color::Space->new(
31             name => 'LCH',
32             alias => 'CIELCHab',
33             axis => [qw/luminance chroma hue/],
34             type => [qw/linear linear angular/],
35             range => [100, 539, 360],
36             precision => 3,
37             convert => { LAB => [\&to_lab, \&from_lab] },
38             );