File Coverage

lib/Graphics/Toolkit/Color/Space/Instance/OKLCH.pm
Criterion Covered Total %
statement 22 22 100.0
branch 6 6 100.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 33 35 94.2


line stmt bran cond sub pod time code
1              
2             # OK LCH cylindrical color space variant of OKLAB
3             # Conveter under Copyright (c) 2021 Björn Ottosson, see LICENSE.OK
4              
5             package Graphics::Toolkit::Color::Space::Instance::OKLCH;
6 16     16   255383 use v5.12;
  16         45  
7 16     16   79 use warnings;
  16         25  
  16         694  
8 16     16   467 use Graphics::Toolkit::Color::Space qw/round_decimals/;
  16         385  
  16         4597  
9              
10             my $TAU = 6.283185307;
11              
12             sub from_lab {
13 6     6 0 7 my ($lab) = shift;
14 6         27 my $a = $lab->[1] - .5;
15 6         7 my $b = $lab->[2] - .5;
16              
17 6 100       13 $a = 0 if round_decimals($a, 5) == 0;
18 6 100       9 $b = 0 if round_decimals($b, 5) == 0;
19 6         14 my $c = sqrt( ($a**2) + ($b**2));
20 6         26 my $h = atan2($b, $a);
21 6 100       11 $h += $TAU if $h < 0;
22 6         17 return ([$lab->[0], $c * 2, $h / $TAU]);
23             }
24             sub to_lab {
25 6     6 0 7 my ($lch) = shift;
26 6         12 my $c = $lch->[1] / 2;
27 6         13 my $a = $c * cos($lch->[2] * $TAU);
28 6         8 my $b = $c * sin($lch->[2] * $TAU);
29 6         17 return ([$lch->[0], $a + .5, $b + .5 ]);
30             }
31              
32             Graphics::Toolkit::Color::Space->new(
33             name => 'OKLCH',
34             family => 'HSL',
35             axis => [qw/luminance chroma hue/],
36             role => [qw/lightness saturation hue/],
37             type => [qw/linear linear angular/],
38             range => [1, .5, 360],
39             precision => [5, 5, 2],
40             convert => { OKLAB => [\&to_lab, \&from_lab] },
41             );