| 1 | |||||||||||||||||||||||||||
| 2 | # HWB color space | ||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||
| 4 | package Graphics::Toolkit::Color::Space::Instance::HWB; | ||||||||||||||||||||||||||
| 5 | 16 | ▶ | use v5.12; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 6 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Graphics::Toolkit::Color::Space qw/min max/; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||
| 9 | 7 | ▶ | sub from_rgb { | ||||||||||||||||||||||||
|
Subroutine
from_rgb: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||
| 10 | 7 | my ($r, $g, $b) = @{$_[0]}; | |||||||||||||||||||||||||
| 11 | 7 | my $vmax = max($r, $g, $b); | |||||||||||||||||||||||||
| 12 | 7 | my $white = my $vmin = min($r, $g, $b); | |||||||||||||||||||||||||
| 13 | 7 | ▶ | return ([0,1,0]) if $white == 1; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 14 | 4 | my $black = 1 - ($vmax); | |||||||||||||||||||||||||
| 15 | 4 | ▶ | return ([0,0,1]) if $black == 1; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | 4 | my $d = $vmax - $vmin; | |||||||||||||||||||||||||
| 18 | 4 | my $s = $d / $vmax; | |||||||||||||||||||||||||
| 19 | 4 | ▶ | my $h = ($d == 0) ? 0 : | ||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||
| 20 | ($vmax == $r) ? (($g - $b) / $d + ($g < $b ? 6 : 0)) : | ||||||||||||||||||||||||||
| 21 | ($vmax == $g) ? (($b - $r) / $d + 2) | ||||||||||||||||||||||||||
| 22 | : (($r - $g) / $d + 4); | ||||||||||||||||||||||||||
| 23 | 4 | return ([$h/6, $white, $black]); | |||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||
| 25 | 9 | ▶ | sub to_rgb { | ||||||||||||||||||||||||
|
Subroutine
to_rgb: called CC=10 SCAR=23.4
Pod
undocumented
| |||||||||||||||||||||||||||
| 26 | 9 | my ($h, $w, $b) = @{$_[0]}; | |||||||||||||||||||||||||
| 27 | 9 | ▶ | return ([0, 0, 0]) if $b == 1; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 28 | 5 | ▶ | return ([1, 1, 1]) if $w == 1; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 29 | 5 | my $v = 1 - $b; | |||||||||||||||||||||||||
| 30 | 5 | my $s = 1 - ($w / $v); | |||||||||||||||||||||||||
| 31 | 5 | ▶ | $s = 0 if $s < 0; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 32 | 5 | ▶ | return ([$v, $v, $v]) if $s == 0; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||
| 34 | 3 | my $hi = int( $h * 6 ); | |||||||||||||||||||||||||
| 35 | 3 | my $f = ( $h * 6 ) - $hi; | |||||||||||||||||||||||||
| 36 | 3 | my $p = $v * (1 - $s ); | |||||||||||||||||||||||||
| 37 | 3 | my $q = $v * (1 - ($s * $f)); | |||||||||||||||||||||||||
| 38 | 3 | my $t = $v * (1 - ($s * (1 - $f))); | |||||||||||||||||||||||||
| 39 | 3 | ▶ | my @rgb = ($hi == 1) ? ($q, $v, $p) | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 40 | : ($hi == 2) ? ($p, $v, $t) | ||||||||||||||||||||||||||
| 41 | : ($hi == 3) ? ($p, $q, $v) | ||||||||||||||||||||||||||
| 42 | : ($hi == 4) ? ($t, $p, $v) | ||||||||||||||||||||||||||
| 43 | : ($hi == 5) ? ($v, $p, $q) | ||||||||||||||||||||||||||
| 44 | : ($v, $t, $p); | ||||||||||||||||||||||||||
| 45 | 3 | return \@rgb; | |||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||
| 48 | 16 | Graphics::Toolkit::Color::Space->new( | |||||||||||||||||||||||||
| 49 | family => 'HWB', | ||||||||||||||||||||||||||
| 50 | axis => [qw/hue whiteness blackness/], | ||||||||||||||||||||||||||
| 51 | type => [qw/angular linear linear/], | ||||||||||||||||||||||||||
| 52 | range => [360, 100, 100], | ||||||||||||||||||||||||||
| 53 | precision => 0, | ||||||||||||||||||||||||||
| 54 | suffix => ['', '%', '%'], | ||||||||||||||||||||||||||
| 55 | convert => {RGB => [\&to_rgb, \&from_rgb]}, | ||||||||||||||||||||||||||
| 56 | constraint => {cone => {checker => '$_[0][1] + $_[0][2] <= 1', | ||||||||||||||||||||||||||
| 57 | error => 'The sum of whiteness and blackness can not exceed 100%.', | ||||||||||||||||||||||||||
| 58 | remedy => 'my $s = $_[0][1] + $_[0][2];[$_[0][0], $_[0][1]/$s, $_[0][2]/$s]', }}, | ||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||