File Coverage

lib/Graphics/Toolkit/Color/Space/Instance/OKLAB.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
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 lab color space for Illuminant D65 and Observer 2,
3             # Conveter under Copyright (c) 2020 Björn Ottosson, see LICENSE.OK
4              
5             package Graphics::Toolkit::Color::Space::Instance::OKLAB;
6 16     16   266170 use v5.12;
  16         101  
7 16     16   63 use warnings;
  16         23  
  16         771  
8 16     16   438 use Graphics::Toolkit::Color::Space qw/spow mult_matrix_vector_3/;
  16         19  
  16         7269  
9             my @D65 = (0.95047, 1, 1.08883); # illuminant
10              
11             sub from_lab {
12 48     48 0 55 my (@lab) = @{$_[0]};
  48         93  
13 48         61 $lab[1] -= .5;
14 48         52 $lab[2] -= .5;
15 48         256 my @lms = mult_matrix_vector_3([[ 1, 0.396338 , 0.215804 ],
16             [ 1, -0.105561 , -0.0638542 ],
17             [ 1, -0.0894842, -1.29149 ]], @lab);
18              
19 48         113 @lms = map {spow($_, 3)} @lms;
  144         178  
20              
21 48         113 my @xyz = mult_matrix_vector_3([[ 1.22701 , -0.5578 , 0.281256 ],
22             [-0.0405802, 1.11226 ,-0.0716767],
23             [-0.0763813, -0.421482, 1.58616 ]], @lms);
24 48         105 return [map {$xyz[$_] / $D65[$_]} 0 .. 2];
  144         293  
25             }
26             sub to_lab {
27 87     87 0 100 my ($xyz) = shift;
28 87         133 my @xyz = map {$xyz->[$_] * $D65[$_]} 0 .. 2;
  261         430  
29 87         335 my @lms = mult_matrix_vector_3([[ 0.8189330101, 0.3618667424,-0.1288597137],
30             [ 0.0329845436, 0.9293118715, 0.0361456387],
31             [ 0.0482003018, 0.2643662691, 0.6338517070]], @xyz);
32              
33 87         168 @lms = map {spow($_, 1/3)} @lms;
  261         341  
34              
35 87         185 my @lab = mult_matrix_vector_3([[ 0.2104542553, 0.7936177850, -0.0040720468],
36             [ 1.9779984951, -2.4285922050, 0.4505937099],
37             [ 0.0259040371, 0.7827717662, -0.8086757660]], @lms);
38 87         162 $lab[1] += .5;
39 87         97 $lab[2] += .5;
40 87         186 return \@lab;
41             }
42              
43             Graphics::Toolkit::Color::Space->new(
44             name => 'OKLAB', # no alias, short axis name eq long
45             family => 'LAB',
46             axis => [qw/L a b/], # lightness, cyan-orange balance, magenta-green balance
47             range => [1, [-.5, .5], [-.5, .5]],
48             precision => 3,
49             convert => {XYZ => [\&from_lab, \&to_lab]}, #, {to => {in => 1, out => 1}, from => {in => 1, out => 1} }
50             );