| 1 | ||||||||||||||||||||||||||||||||||||||
| 2 | # store all color space objects, to convert check, convert and measure color values | |||||||||||||||||||||||||||||||||||||
| 3 | ||||||||||||||||||||||||||||||||||||||
| 4 | package Graphics::Toolkit::Color::Space::Hub; | |||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use v5.12; | |||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use warnings; | |||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||
| 7 | ||||||||||||||||||||||||||||||||||||||
| 8 | #### internal space loading ############################################ | |||||||||||||||||||||||||||||||||||||
| 9 | 15 | our $default_space_name = 'RGB'; | ||||||||||||||||||||||||||||||||||||
| 10 | 15 | our @load_order = ($default_space_name, | ||||||||||||||||||||||||||||||||||||
| 11 | qw/RGBLinear CMY CMYK HSL HSV HSB HWB NCol YIQ YPbPr/, | |||||||||||||||||||||||||||||||||||||
| 12 | qw/CIEXYZ CIERGB CIELAB CIELUV CIELCHab CIELCHuv HunterLAB/, | |||||||||||||||||||||||||||||||||||||
| 13 | qw/AppleRGB AdobeRGB ProPhotoRGB WideGamutRGB/, | |||||||||||||||||||||||||||||||||||||
| 14 | qw/DisplayP3Linear DisplayP3 DCIP3Linear DCIP3 Rec709 Rec2020/, | |||||||||||||||||||||||||||||||||||||
| 15 | qw/OKLAB OKLCH OKHSL OKHSV OKHWB/); | |||||||||||||||||||||||||||||||||||||
| 16 | 15 | add_space( require "Graphics/Toolkit/Color/Space/Instance/$_.pm" ) for @load_order; | ||||||||||||||||||||||||||||||||||||
| 17 | 15 | my ($default_space, @search_order, %space_obj, %next_conversion_node, %space_family); | ||||||||||||||||||||||||||||||||||||
| 18 | ||||||||||||||||||||||||||||||||||||||
| 19 | #### space API ######################################################### | |||||||||||||||||||||||||||||||||||||
| 20 | 146 | ▶ | sub is_space_name { | |||||||||||||||||||||||||||||||||||
|
Subroutine
is_space_name: called CC=2 SCAR=6.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 21 | 146 | ▶ | (ref get_space( $default_space->normalize_name( $_[0] ))) ? 1 : 0 } | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 22 | 11 | ▶ | sub all_space_names { sort keys %space_obj } | |||||||||||||||||||||||||||||||||||
|
Subroutine
all_space_names: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | sub default_space_name { $default_space_name } | |||||||||||||||||||||||||||||||||||
|
Subroutine
default_space_name: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 24 | 10512 | ▶ | sub default_space { $default_space } | |||||||||||||||||||||||||||||||||||
|
Subroutine
default_space: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 25 | 7550 | ▶ | sub get_space { # takes only normal names or alias names | |||||||||||||||||||||||||||||||||||
|
Subroutine
get_space: called CC=4 SCAR=13.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 26 | 7550 | my $name = shift; | ||||||||||||||||||||||||||||||||||||
| 27 | 7550 | ▶ | return unless defined $name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 28 | 7548 | ▶ | $name = default_space()->normalize_name( $name ) if ref $default_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 29 | 7548 | ▶ | exists $space_obj{ $name } ? $space_obj{ $name } : ''; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 30 | } | |||||||||||||||||||||||||||||||||||||
| 31 | 3026 | ▶ | sub try_get_space { # takes any name variant and defaults to $default_space_name | |||||||||||||||||||||||||||||||||||
|
Subroutine
try_get_space: called CC=5 SCAR=16.2
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 32 | 3026 | ▶ | my $name = shift || $default_space_name; | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $Graphics::Toolkit::Color::Space::Hub::default_space_name
MC/DC0%0/2
shift() || $Graphics::Toolkit::Color::Space::Hub::default_space_name
shift()
$Graphics::Toolkit::Color::Space::Hub::default_space_name
Truth table66%2/3
A || B
A= shift()B= $Graphics::Toolkit::Color::Space::Hub::default_space_name
| ||||||||||||||||||||||||||||||||||||||
| 33 | 3026 | ▶ | return $name if ref $name eq 'Graphics::Toolkit::Color::Space' and is_space_name( $name->name ); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $name eq "Graphics::Toolkit::Color::Space" and is_space_name($name->name)
MC/DC50%1/2
ref $name eq "Graphics::Toolkit::Color::Space" and is_space_name($name->name)
ref $name eq "Graphics::Toolkit::Color::Space"
is_space_name($name->name)
Truth table66%2/3
A and B
A= ref $name eq "Graphics::Toolkit::Color::Space"B= is_space_name($name->name)
| ||||||||||||||||||||||||||||||||||||||
| 34 | 2966 | $name = default_space()->normalize_name( $name ); | ||||||||||||||||||||||||||||||||||||
| 35 | 2966 | my $space = get_space( $name ); | ||||||||||||||||||||||||||||||||||||
| 36 | 2966 | ▶ | return (ref $space) ? $space | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 37 | : "$name is an unknown color space, try one of: ".(join ', ', all_space_names()); | |||||||||||||||||||||||||||||||||||||
| 38 | } | |||||||||||||||||||||||||||||||||||||
| 39 | ||||||||||||||||||||||||||||||||||||||
| 40 | 496 | ▶ | sub add_space { | |||||||||||||||||||||||||||||||||||
|
Subroutine
add_space: called CC=13 SCAR=26.1
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 41 | 496 | my $space = shift; | ||||||||||||||||||||||||||||||||||||
| 42 | 496 | ▶ | return 'add_space got no Graphics::Toolkit::Color::Space object as argument' if ref $space ne 'Graphics::Toolkit::Color::Space'; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 43 | 496 | my $name = $space->name; | ||||||||||||||||||||||||||||||||||||
| 44 | 496 | my $alias = $space->name('alias'); | ||||||||||||||||||||||||||||||||||||
| 45 | 496 | ▶ | return "can not add color space object without a name" unless $name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 46 | 496 | ▶ | return "color space name $name is already taken" if ref get_space( $name ); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 47 | 496 | ▶ | if ($name eq $default_space_name) { # there is no parent | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 48 | 15 | $default_space = $space; | ||||||||||||||||||||||||||||||||||||
| 49 | } else { | |||||||||||||||||||||||||||||||||||||
| 50 | 481 | my $conversion_parent = $space->conversion_tree_parent; | ||||||||||||||||||||||||||||||||||||
| 51 | 481 | ▶ | return "can not add color space $name, it has no converter" unless defined $conversion_parent and $conversion_parent; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $conversion_parent and $conversion_parent
MC/DC0%0/2
defined $conversion_parent and $conversion_parent
defined $conversion_parent
$conversion_parent
Truth table33%1/3
A and B
A= defined $conversion_parentB= $conversion_parent
| ||||||||||||||||||||||||||||||||||||||
| 52 | 481 | $conversion_parent = $space->normalize_name( $conversion_parent ); | ||||||||||||||||||||||||||||||||||||
| 53 | 481 | my $parent_space = get_space( $conversion_parent ); | ||||||||||||||||||||||||||||||||||||
| 54 | 481 | ▶ | return "color space $name does only convert into '$conversion_parent', which is no known color space" unless ref $parent_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 55 | 481 | my $parent_name = $parent_space->name; | ||||||||||||||||||||||||||||||||||||
| 56 | 481 | $next_conversion_node{ $parent_name }{ $name } = $name; | ||||||||||||||||||||||||||||||||||||
| 57 | 481 | ▶ | unless ($parent_name eq $default_space_name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 58 | 330 | my $upper_space_name = $default_space_name; | ||||||||||||||||||||||||||||||||||||
| 59 | 330 | ▶ | while ($upper_space_name ne $parent_name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 60 | $upper_space_name = $next_conversion_node{ $upper_space_name }{ $name } | |||||||||||||||||||||||||||||||||||||
| 61 | 660 | = $next_conversion_node{ $upper_space_name }{ $parent_name }; | ||||||||||||||||||||||||||||||||||||
| 62 | } | |||||||||||||||||||||||||||||||||||||
| 63 | } | |||||||||||||||||||||||||||||||||||||
| 64 | } | |||||||||||||||||||||||||||||||||||||
| 65 | 496 | push @search_order, $name; | ||||||||||||||||||||||||||||||||||||
| 66 | 496 | $space_obj{ $name } = $space; | ||||||||||||||||||||||||||||||||||||
| 67 | 496 | ▶ | $space_obj{ $alias } = $space if $alias and not ref get_space( $alias ); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$alias and not ref get_space($alias)
MC/DC50%1/2
$alias and not ref get_space($alias)
$alias
not ref get_space($alias)
Truth table66%2/3
A and not(B)
A= $aliasB= not ref get_space($alias)
| ||||||||||||||||||||||||||||||||||||||
| 68 | 496 | ▶ | push @{$space_family{ $space->family }}, $space if $space->family; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 69 | 496 | return 1; | ||||||||||||||||||||||||||||||||||||
| 70 | } | |||||||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub remove_space { | |||||||||||||||||||||||||||||||||||
|
Subroutine
remove_space: called CC=8 SCAR=21.6
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 72 | 3 | my $name = shift; | ||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | return "need name of color space as argument in order to remove the space" unless defined $name and $name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and $name
MC/DC0%0/2
defined $name and $name
defined $name
$name
Truth table33%1/3
A and B
A= defined $nameB= $name
| ||||||||||||||||||||||||||||||||||||||
| 74 | 3 | my $space = try_get_space( $name ); | ||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | return "can not remove unknown color space: $name" if not ref $space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | return "can not remove default color space: $name" if $space->name eq $default_space_name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 77 | ||||||||||||||||||||||||||||||||||||||
| 78 | 1 | $name = $space->name; | ||||||||||||||||||||||||||||||||||||
| 79 | 1 | my $upper_space_name = $default_space_name; | ||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | while ($upper_space_name ne $name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $upper_space_name = delete $next_conversion_node{ $upper_space_name }{ $name }; | ||||||||||||||||||||||||||||||||||||
| 82 | } | |||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | delete $space_family{ $space->family } if $space->family; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | delete $space_obj{ $space->name('alias') } if $space->name('alias'); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 85 | 1 | delete $space_obj{ $name }; | ||||||||||||||||||||||||||||||||||||
| 86 | } | |||||||||||||||||||||||||||||||||||||
| 87 | ||||||||||||||||||||||||||||||||||||||
| 88 | #### tuple API ########################################################## | |||||||||||||||||||||||||||||||||||||
| 89 | 530 | ▶ | sub convert { # normalized RGB tuple, ~space_name --> |normalized tuple in wanted space | |||||||||||||||||||||||||||||||||||
|
Subroutine
convert: called CC=24 SCAR=32.7
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 90 | 530 | my ($tuple, $target_space_name, $want_result_normalized, $source_tuple, $source_space_name) = @_; | ||||||||||||||||||||||||||||||||||||
| 91 | 530 | ▶ | return "need an ARRAY ref with 3 normalized RGB values as first argument in order to convert them" | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 92 | unless $default_space->is_number_tuple( $tuple ); | |||||||||||||||||||||||||||||||||||||
| 93 | 527 | my $target_space = try_get_space( $target_space_name ); | ||||||||||||||||||||||||||||||||||||
| 94 | 527 | ▶ | return "got unknown space name: '$target_space_name' as second argument, can not convert " unless ref $target_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 95 | 526 | my $source_space = try_get_space( $source_space_name ); | ||||||||||||||||||||||||||||||||||||
| 96 | 526 | ▶ | return "did not found target color space !'$target_space_name'" unless ref $target_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 97 | 526 | ▶ | if ($source_space_name xor $source_tuple){ | |||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%2/4
$source_space_name xor $source_tuple
MC/DC0%0/2
$source_space_name xor $source_tuple
$source_space_name
$source_tuple
Truth table50%2/4
A xor B
A= $source_space_nameB= $source_tuple
| ||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return "arguments source_space_name and source_values (nr. 4 and 5) have to be provided both or none of them"; | ||||||||||||||||||||||||||||||||||||
| 99 | ▶ | } elsif ($source_space_name and $source_tuple) { | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$source_space_name and $source_tuple
MC/DC50%1/2
$source_space_name and $source_tuple
$source_space_name
$source_tuple
Truth table66%2/3
A and B
A= $source_space_nameB= $source_tuple
| ||||||||||||||||||||||||||||||||||||||
| 100 | 72 | ▶ | return "got unknown source color space $source_space_name" if not ref $source_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 101 | 72 | ▶ | return "argument source_values has to be a tuple, if provided" unless $source_space->is_number_tuple( $source_tuple ); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 102 | } | |||||||||||||||||||||||||||||||||||||
| 103 | ||||||||||||||||||||||||||||||||||||||
| 104 | 526 | $tuple = [@$tuple]; # unwrap ref to avoid spooky action | ||||||||||||||||||||||||||||||||||||
| 105 | 526 | my $current_space_name = $default_space_name; # we start in RGB | ||||||||||||||||||||||||||||||||||||
| 106 | 526 | $target_space_name = $target_space->name; # use only normalized name | ||||||||||||||||||||||||||||||||||||
| 107 | 526 | ▶ | $want_result_normalized //= 0; # normal flags to start state | |||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$want_result_normalized //= 0
MC/DC100%1/1
$want_result_normalized //= 0
$want_result_normalized
Truth table100%2/2
A
A= $want_result_normalized
| ||||||||||||||||||||||||||||||||||||||
| 108 | 526 | my $tuple_is_normal = 1; | ||||||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||||||
| 110 | 526 | ▶ | while ($current_space_name ne $target_space_name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 111 | 525 | my $next_space_name = $next_conversion_node{ $current_space_name }{ $target_space_name }; | ||||||||||||||||||||||||||||||||||||
| 112 | 525 | my $next_space = get_space( $next_space_name ); | ||||||||||||||||||||||||||||||||||||
| 113 | # replace tuple with values from constructor if possible | |||||||||||||||||||||||||||||||||||||
| 114 | 525 | ▶ | if (defined $source_space_name and $next_space->is_name($source_space_name)){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $source_space_name and $next_space->is_name($source_space_name)
MC/DC100%2/2
defined $source_space_name and $next_space->is_name($source_space_name)
defined $source_space_name
$next_space->is_name($source_space_name)
Truth table100%3/3
A and B
A= defined $source_space_nameB= $next_space->is_name($source_space_name)
| ||||||||||||||||||||||||||||||||||||||
| 115 | 49 | $tuple = [@$source_tuple]; | ||||||||||||||||||||||||||||||||||||
| 116 | 49 | $tuple_is_normal = 1; | ||||||||||||||||||||||||||||||||||||
| 117 | } else { | |||||||||||||||||||||||||||||||||||||
| 118 | 476 | my @normal_in_out = $next_space->converter_normal_states( 'from', $current_space_name ); | ||||||||||||||||||||||||||||||||||||
| 119 | 476 | ▶ | $tuple = $next_space->normalize( $tuple ) if not $tuple_is_normal and $normal_in_out[0]; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $tuple_is_normal and $normal_in_out[0]
MC/DC0%0/2
not $tuple_is_normal and $normal_in_out[0]
not $tuple_is_normal
$normal_in_out[0]
Truth table33%1/3
not(A) and B
A= not $tuple_is_normalB= $normal_in_out[0]
| ||||||||||||||||||||||||||||||||||||||
| 120 | 476 | ▶ | $tuple = $next_space->denormalize( $tuple ) if $tuple_is_normal and not $normal_in_out[0]; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tuple_is_normal and not $normal_in_out[0]
MC/DC0%0/2
$tuple_is_normal and not $normal_in_out[0]
$tuple_is_normal
not $normal_in_out[0]
Truth table33%1/3
A and not(B)
A= $tuple_is_normalB= not $normal_in_out[0]
| ||||||||||||||||||||||||||||||||||||||
| 121 | 476 | $tuple = $next_space->convert_from( $current_space_name, $tuple ); | ||||||||||||||||||||||||||||||||||||
| 122 | 476 | $tuple_is_normal = $normal_in_out[1]; | ||||||||||||||||||||||||||||||||||||
| 123 | 476 | ▶ | if (not $tuple_is_normal and $next_space_name ne $target_space_name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $tuple_is_normal and $next_space_name ne $target_space_name
MC/DC100%2/2
not $tuple_is_normal and $next_space_name ne $target_space_name
not $tuple_is_normal
$next_space_name ne $target_space_name
Truth table100%3/3
not(A) and B
A= not $tuple_is_normalB= $next_space_name ne $target_space_name
| ||||||||||||||||||||||||||||||||||||||
| 124 | 106 | $tuple_is_normal = 1; | ||||||||||||||||||||||||||||||||||||
| 125 | 106 | $tuple = $next_space->normalize( $tuple ); | ||||||||||||||||||||||||||||||||||||
| 126 | } | |||||||||||||||||||||||||||||||||||||
| 127 | } | |||||||||||||||||||||||||||||||||||||
| 128 | 525 | $current_space_name = $next_space_name; | ||||||||||||||||||||||||||||||||||||
| 129 | } | |||||||||||||||||||||||||||||||||||||
| 130 | 526 | ▶ | $tuple = $target_space->normalize( $tuple ) if not $tuple_is_normal and $want_result_normalized; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $tuple_is_normal and $want_result_normalized
MC/DC100%2/2
not $tuple_is_normal and $want_result_normalized
not $tuple_is_normal
$want_result_normalized
Truth table100%3/3
not(A) and B
A= not $tuple_is_normalB= $want_result_normalized
| ||||||||||||||||||||||||||||||||||||||
| 131 | 526 | ▶ | $tuple = $target_space->denormalize( $tuple ) if $tuple_is_normal and not $want_result_normalized; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tuple_is_normal and not $want_result_normalized
MC/DC100%2/2
$tuple_is_normal and not $want_result_normalized
$tuple_is_normal
not $want_result_normalized
Truth table100%3/3
A and not(B)
A= $tuple_is_normalB= not $want_result_normalized
| ||||||||||||||||||||||||||||||||||||||
| 132 | 526 | return $tuple; | ||||||||||||||||||||||||||||||||||||
| 133 | } | |||||||||||||||||||||||||||||||||||||
| 134 | 214 | ▶ | sub deconvert { # normalized value tuple --> RGB tuple | |||||||||||||||||||||||||||||||||||
|
Subroutine
deconvert: called CC=28 SCAR=44.2
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 135 | 214 | my ($tuple, $original_space_name, $want_result_normalized, $source_tuple, $source_space_name) = @_; | ||||||||||||||||||||||||||||||||||||
| 136 | 214 | my $original_space = try_get_space( $original_space_name ); | ||||||||||||||||||||||||||||||||||||
| 137 | 214 | my $source_space = try_get_space( $source_space_name ); | ||||||||||||||||||||||||||||||||||||
| 138 | 214 | ▶ | $want_result_normalized //= 0; | |||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$want_result_normalized //= 0
MC/DC100%1/1
$want_result_normalized //= 0
$want_result_normalized
Truth table100%2/2
A
A= $want_result_normalized
| ||||||||||||||||||||||||||||||||||||||
| 139 | 214 | ▶ | return "need a space name to convert from as second argument" unless defined $original_space_name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 140 | 212 | ▶ | return "got unknown color space name as second argument" unless ref $original_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 141 | 210 | ▶ | return "need as first argument an ARRAY with valid number of normalized values from the color space ". $original_space->name | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 142 | unless $original_space->is_number_tuple( $tuple ); | |||||||||||||||||||||||||||||||||||||
| 143 | ||||||||||||||||||||||||||||||||||||||
| 144 | 210 | ▶ | if ($source_space_name xor $source_tuple){ | |||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition25%1/4
$source_space_name xor $source_tuple
MC/DC0%0/2
$source_space_name xor $source_tuple
$source_space_name
$source_tuple
Truth table25%1/4
A xor B
A= $source_space_nameB= $source_tuple
| ||||||||||||||||||||||||||||||||||||||
| 145 | 0 | return "arguments source_space_name and source_values (nr. 4 and 5) have to be provided both or none of them"; | ||||||||||||||||||||||||||||||||||||
| 146 | ▶ | } elsif ($source_space_name and $source_tuple) { | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$source_space_name and $source_tuple
MC/DC0%0/2
$source_space_name and $source_tuple
$source_space_name
$source_tuple
Truth table33%1/3
A and B
A= $source_space_nameB= $source_tuple
| ||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | return "got unknown source color space $source_space_name" if not ref $source_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return "argument source_values has to be a tuple, if provided" unless $source_space->is_number_tuple( $source_tuple ); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 149 | } | |||||||||||||||||||||||||||||||||||||
| 150 | ||||||||||||||||||||||||||||||||||||||
| 151 | # none conversion cases | |||||||||||||||||||||||||||||||||||||
| 152 | 210 | ▶ | if ($original_space->name eq $default_space_name) { # nothing to convert | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return ($want_result_normalized) ? $tuple : $original_space->denormalize( $tuple ); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 154 | } | |||||||||||||||||||||||||||||||||||||
| 155 | 210 | my $current_space = $original_space; | ||||||||||||||||||||||||||||||||||||
| 156 | 210 | my $tuple_is_normal = 1; | ||||||||||||||||||||||||||||||||||||
| 157 | # actual conversion | |||||||||||||||||||||||||||||||||||||
| 158 | 210 | ▶ | while ($current_space->name ne $default_space_name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 159 | 372 | my ($next_space_name, @next_options) = $current_space->conversion_tree_parent; | ||||||||||||||||||||||||||||||||||||
| 160 | 372 | ▶ | $next_space_name = shift @next_options while @next_options and $next_space_name ne $default_space_name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@next_options and $next_space_name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
MC/DC0%0/2
@next_options and $next_space_name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
@next_options
$next_space_name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
Truth table33%1/3
A and B
A= @next_optionsB= $next_space_name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
| ||||||||||||||||||||||||||||||||||||||
| 161 | # replace tuple with values from constructor if possible | |||||||||||||||||||||||||||||||||||||
| 162 | 372 | ▶ | if (defined $source_space_name and $current_space->is_name( $source_space_name )){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $source_space_name and $current_space->is_name($source_space_name)
MC/DC0%0/2
defined $source_space_name and $current_space->is_name($source_space_name)
defined $source_space_name
$current_space->is_name($source_space_name)
Truth table33%1/3
A and B
A= defined $source_space_nameB= $current_space->is_name($source_space_name)
| ||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $tuple = [@$source_tuple]; | ||||||||||||||||||||||||||||||||||||
| 164 | 0 | $tuple_is_normal = 1; | ||||||||||||||||||||||||||||||||||||
| 165 | } else { | |||||||||||||||||||||||||||||||||||||
| 166 | 372 | my @normal_in_out = $current_space->converter_normal_states( 'to', $next_space_name ); | ||||||||||||||||||||||||||||||||||||
| 167 | 372 | ▶ | $tuple = $current_space->normalize( $tuple ) if not $tuple_is_normal and $normal_in_out[0]; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $tuple_is_normal and $normal_in_out[0]
MC/DC0%0/2
not $tuple_is_normal and $normal_in_out[0]
not $tuple_is_normal
$normal_in_out[0]
Truth table33%1/3
not(A) and B
A= not $tuple_is_normalB= $normal_in_out[0]
| ||||||||||||||||||||||||||||||||||||||
| 168 | 372 | ▶ | $tuple = $current_space->denormalize( $tuple ) if $tuple_is_normal and not $normal_in_out[0]; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tuple_is_normal and not $normal_in_out[0]
MC/DC50%1/2
$tuple_is_normal and not $normal_in_out[0]
$tuple_is_normal
not $normal_in_out[0]
Truth table66%2/3
A and not(B)
A= $tuple_is_normalB= not $normal_in_out[0]
| ||||||||||||||||||||||||||||||||||||||
| 169 | 372 | $tuple = $current_space->convert_to( $next_space_name, $tuple); | ||||||||||||||||||||||||||||||||||||
| 170 | 372 | $tuple_is_normal = $normal_in_out[1]; | ||||||||||||||||||||||||||||||||||||
| 171 | 372 | ▶ | if (not $tuple_is_normal and $current_space->name ne $default_space_name){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $tuple_is_normal and $current_space->name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
MC/DC0%0/2
not $tuple_is_normal and $current_space->name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
not $tuple_is_normal
$current_space->name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
Truth table33%1/3
not(A) and B
A= not $tuple_is_normalB= $current_space->name ne $Graphics::Toolkit::Color::Space::Hub::default_space_name
| ||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $tuple_is_normal = 1; | ||||||||||||||||||||||||||||||||||||
| 173 | 0 | $tuple = $current_space->normalize( $tuple ); | ||||||||||||||||||||||||||||||||||||
| 174 | } | |||||||||||||||||||||||||||||||||||||
| 175 | } | |||||||||||||||||||||||||||||||||||||
| 176 | 372 | $current_space = get_space( $next_space_name ); | ||||||||||||||||||||||||||||||||||||
| 177 | } | |||||||||||||||||||||||||||||||||||||
| 178 | 210 | ▶ | $tuple = $current_space->normalize( $tuple ) if not $tuple_is_normal and $want_result_normalized; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $tuple_is_normal and $want_result_normalized
MC/DC0%0/2
not $tuple_is_normal and $want_result_normalized
not $tuple_is_normal
$want_result_normalized
Truth table33%1/3
not(A) and B
A= not $tuple_is_normalB= $want_result_normalized
| ||||||||||||||||||||||||||||||||||||||
| 179 | 210 | ▶ | $tuple = $current_space->denormalize( $tuple ) if $tuple_is_normal and not $want_result_normalized; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tuple_is_normal and not $want_result_normalized
MC/DC50%1/2
$tuple_is_normal and not $want_result_normalized
$tuple_is_normal
not $want_result_normalized
Truth table66%2/3
A and not(B)
A= $tuple_is_normalB= not $want_result_normalized
| ||||||||||||||||||||||||||||||||||||||
| 180 | 210 | return $tuple; | ||||||||||||||||||||||||||||||||||||
| 181 | } | |||||||||||||||||||||||||||||||||||||
| 182 | ||||||||||||||||||||||||||||||||||||||
| 183 | 2264 | ▶ | sub deformat { # formatted color def --> tuple | |||||||||||||||||||||||||||||||||||
|
Subroutine
deformat: called CC=4 SCAR=14.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 184 | 2264 | my ($color_def, $space_name, $suffix) = @_; | ||||||||||||||||||||||||||||||||||||
| 185 | 2264 | ▶ | return 'Got no color definition!' unless defined $color_def; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 186 | 2264 | my ($tuple, $original_space, $format_name); | ||||||||||||||||||||||||||||||||||||
| 187 | 2264 | my $color_space = get_space( $space_name ); | ||||||||||||||||||||||||||||||||||||
| 188 | 2264 | ▶ | return "$color_space is an unknown space!" unless ref $color_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 189 | 2264 | ($tuple, $format_name) = $color_space->deformat( $color_def ); #, $suffix | ||||||||||||||||||||||||||||||||||||
| 190 | 2264 | ▶ | return "Could not deformat color definition: '$color_def' in space: ".$color_space->name unless defined $format_name; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 191 | 89 | return $tuple, $color_space->name, $format_name; | ||||||||||||||||||||||||||||||||||||
| 192 | } | |||||||||||||||||||||||||||||||||||||
| 193 | 142 | ▶ | sub deformat_search { # formatted color def --> tuple | |||||||||||||||||||||||||||||||||||
|
Subroutine
deformat_search: called CC=6 SCAR=17.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 194 | 142 | my ($color_def, $suffix) = @_; | ||||||||||||||||||||||||||||||||||||
| 195 | 142 | ▶ | return 'got no color definition to deformat!' unless defined $color_def; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 196 | 142 | for my $space_name (@search_order) { | ||||||||||||||||||||||||||||||||||||
| 197 | 2264 | my @tuple_space_format = deformat( $color_def, $space_name, $suffix); | ||||||||||||||||||||||||||||||||||||
| 198 | 2264 | ▶ | return @tuple_space_format if @tuple_space_format > 1; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 199 | } | |||||||||||||||||||||||||||||||||||||
| 200 | 53 | ▶ | return "Could not deformat color definition in any space: '@$color_def'!" if ref $color_def eq 'ARRAY'; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 201 | 24 | ▶ | return "Could not deformat color definition in any space: '%$color_def'!" if ref $color_def eq 'HASH'; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 202 | 15 | return "Could not deformat color definition in any space: '$color_def'!"; | ||||||||||||||||||||||||||||||||||||
| 203 | } | |||||||||||||||||||||||||||||||||||||
| 204 | 49 | ▶ | sub deformat_search_partial_hash { # convert partial hash into | |||||||||||||||||||||||||||||||||||
|
Subroutine
deformat_search_partial_hash: called CC=8 SCAR=20.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 205 | 49 | my ($value_hash, $space_name) = @_; | ||||||||||||||||||||||||||||||||||||
| 206 | 49 | ▶ | return unless ref $value_hash eq 'HASH'; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 207 | 48 | my $space = try_get_space( $space_name ); | ||||||||||||||||||||||||||||||||||||
| 208 | 48 | ▶ | return $space unless ref $space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 209 | 48 | ▶ | my @space_name_options = (defined $space_name and $space_name) ? ($space->name) : (@search_order); | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $space_name and $space_name
MC/DC50%1/2
defined $space_name and $space_name
defined $space_name
$space_name
Truth table66%2/3
A and B
A= defined $space_nameB= $space_name
| ||||||||||||||||||||||||||||||||||||||
| 210 | 48 | for my $space_name (@space_name_options) { | ||||||||||||||||||||||||||||||||||||
| 211 | 379 | my $color_space = try_get_space( $space_name ); | ||||||||||||||||||||||||||||||||||||
| 212 | 379 | my $tuple = $color_space->tuple_from_partial_hash( $value_hash ); | ||||||||||||||||||||||||||||||||||||
| 213 | 379 | ▶ | next unless ref $tuple; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 214 | 33 | ▶ | return wantarray ? ($tuple, $color_space->name) : $tuple; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 215 | } | |||||||||||||||||||||||||||||||||||||
| 216 | 15 | return undef; | ||||||||||||||||||||||||||||||||||||
| 217 | } | |||||||||||||||||||||||||||||||||||||
| 218 | ||||||||||||||||||||||||||||||||||||||
| 219 | 28 | ▶ | sub distance { # @c1 @c2 -- ~space ~select @range --> + | |||||||||||||||||||||||||||||||||||
|
Subroutine
distance: called CC=5 SCAR=16.1
Pod
undocumented
| ||||||||||||||||||||||||||||||||||||||
| 220 | 28 | my ($tuple_a, $tuple_b, $space_name, $select_axis, $range) = @_; | ||||||||||||||||||||||||||||||||||||
| 221 | 28 | my $color_space = try_get_space( $space_name ); | ||||||||||||||||||||||||||||||||||||
| 222 | 28 | ▶ | return $color_space unless ref $color_space; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 223 | 28 | $tuple_a = convert( $tuple_a, $space_name, 'normal' ); | ||||||||||||||||||||||||||||||||||||
| 224 | 28 | $tuple_b = convert( $tuple_b, $space_name, 'normal' ); | ||||||||||||||||||||||||||||||||||||
| 225 | 28 | my $delta = $color_space->delta( $tuple_a, $tuple_b ); | ||||||||||||||||||||||||||||||||||||
| 226 | 28 | $delta = $color_space->denormalize_delta( $delta, $range ); | ||||||||||||||||||||||||||||||||||||
| 227 | 28 | ▶ | if (defined $select_axis){ | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 228 | 17 | ▶ | $select_axis = [$select_axis] unless ref $select_axis; | |||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||
| 229 | 21 | my @selected_values = grep {defined $_} map {$delta->[$_]} | ||||||||||||||||||||||||||||||||||||
| 230 | 17 | grep {defined $_} map {$color_space->pos_from_axis_name($_)} @$select_axis; | ||||||||||||||||||||||||||||||||||||
| 231 | 17 | $delta = \@selected_values; | ||||||||||||||||||||||||||||||||||||
| 232 | } | |||||||||||||||||||||||||||||||||||||
| 233 | 28 | my $d = 0; | ||||||||||||||||||||||||||||||||||||
| 234 | 28 | $d += $_ * $_ for @$delta; | ||||||||||||||||||||||||||||||||||||
| 235 | 28 | return sqrt $d; | ||||||||||||||||||||||||||||||||||||
| 236 | } | |||||||||||||||||||||||||||||||||||||
| 237 | ||||||||||||||||||||||||||||||||||||||
| 238 | 15 | 1; | ||||||||||||||||||||||||||||||||||||