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   256125 use v5.12;
  16         107  
7 16     16   65 use warnings;
  16         59  
  16         754  
8 16     16   529 use Graphics::Toolkit::Color::Space qw/spow mult_matrix_vector_3/;
  16         21  
  16         7042  
9             my @D65 = (0.95047, 1, 1.08883); # illuminant
10              
11             sub from_lab {
12 48     48 0 64 my (@lab) = @{$_[0]};
  48         98  
13 48         65 $lab[1] -= .5;
14 48         59 $lab[2] -= .5;
15 48         279 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         123 @lms = map {spow($_, 3)} @lms;
  144         218  
20              
21 48         137 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         129 return [map {$xyz[$_] / $D65[$_]} 0 .. 2];
  144         313  
25             }
26             sub to_lab {
27 87     87 0 133 my ($xyz) = shift;
28 87         135 my @xyz = map {$xyz->[$_] * $D65[$_]} 0 .. 2;
  261         412  
29 87         324 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         181 @lms = map {spow($_, 1/3)} @lms;
  261         325  
34              
35 87         206 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         166 $lab[1] += .5;
39 87         108 $lab[2] += .5;
40 87         236 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             );