| 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 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 16 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 16 | ▶ | use Graphics::Toolkit::Color::Space qw/spow mult_matrix_vector_3/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 16 | my @D65 = (0.95047, 1, 1.08883); # illuminant | |
| 10 | |||
| 11 | 48 | ▶ | sub from_lab { |
|
Subroutine
from_lab: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 12 | 48 | my (@lab) = @{$_[0]}; | |
| 13 | 48 | $lab[1] -= .5; | |
| 14 | 48 | $lab[2] -= .5; | |
| 15 | 48 | 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 | @lms = map {spow($_, 3)} @lms; | |
| 20 | |||
| 21 | 48 | 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 | return [map {$xyz[$_] / $D65[$_]} 0 .. 2]; | |
| 25 | } | ||
| 26 | 87 | ▶ | sub to_lab { |
|
Subroutine
to_lab: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 27 | 87 | my ($xyz) = shift; | |
| 28 | 87 | my @xyz = map {$xyz->[$_] * $D65[$_]} 0 .. 2; | |
| 29 | 87 | 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 | @lms = map {spow($_, 1/3)} @lms; | |
| 34 | |||
| 35 | 87 | 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 | $lab[1] += .5; | |
| 39 | 87 | $lab[2] += .5; | |
| 40 | 87 | return \@lab; | |
| 41 | } | ||
| 42 | |||
| 43 | 16 | 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 | ); | ||