| 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 | ▶ | 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/round_decimals/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 16 | my $TAU = 6.283185307; | |||||||||
| 11 | |||||||||||
| 12 | 6 | ▶ | sub from_lab { | ||||||||
|
Subroutine
from_lab: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 13 | 6 | my ($lab) = shift; | |||||||||
| 14 | 6 | my $a = $lab->[1] - .5; | |||||||||
| 15 | 6 | my $b = $lab->[2] - .5; | |||||||||
| 16 | |||||||||||
| 17 | 6 | ▶ | $a = 0 if round_decimals($a, 5) == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 6 | ▶ | $b = 0 if round_decimals($b, 5) == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 6 | my $c = sqrt( ($a**2) + ($b**2)); | |||||||||
| 20 | 6 | my $h = atan2($b, $a); | |||||||||
| 21 | 6 | ▶ | $h += $TAU if $h < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 6 | return ([$lab->[0], $c * 2, $h / $TAU]); | |||||||||
| 23 | } | ||||||||||
| 24 | 6 | ▶ | sub to_lab { | ||||||||
|
Subroutine
to_lab: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 6 | my ($lch) = shift; | |||||||||
| 26 | 6 | my $c = $lch->[1] / 2; | |||||||||
| 27 | 6 | my $a = $c * cos($lch->[2] * $TAU); | |||||||||
| 28 | 6 | my $b = $c * sin($lch->[2] * $TAU); | |||||||||
| 29 | 6 | return ([$lch->[0], $a + .5, $b + .5 ]); | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 16 | 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 | ); | ||||||||||