Color/Calculator.pm

filter: Statementstmt 75.2% 91 / 121 Branchbran 71.7% 56 / 78 Conditioncond 61.1% 11 / 18 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 37.5% 9 / 24 Podpod 0.0% 0 / 19 totaltotal 62.5% 170 / 272 CC 55 SCAR 24.7
CC
55
Cov
73%
invert
22.1
_clear_values_amount_space_name
20.0
mix
12.1
CRAP 11.8
« Color.pm ↑ Summary Color/SetCalculator.pm »
1  
2 # methods to compute one related color
3  
4 package Graphics::Toolkit::Color::Calculator;
5 7 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Graphics::Toolkit::Color::Space::Util qw/is_nr spow/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Graphics::Toolkit::Color::Values;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶sub apply_gamma {
Subroutine apply_gamma: called CC=10 SCAR=23.5
Pod undocumented
11 9 my ($color_values, $gamma, $color_space) = @_;
12 9 my $gamma_array = '';
13 9 ▶ return "need a color space as third argument" if ref $color_space ne 'Graphics::Toolkit::Color::Space';
Branch: 1 branch
BranchTrueFalseTotal
if ref $color_space ne "Graphics::Toolkit::Color::Space" 0 9 9
14 9 ▶ if (ref $gamma eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gamma eq "HASH") 3 6 9
15 3 ($gamma_array, my $deduced_space_name) =
16 Graphics::Toolkit::Color::Space::Hub::deformat_search_partial_hash( $gamma, $color_space->name );
17 3 ▶ return 'axis names: '.join(', ', keys %$gamma).' do not correlate to the selected color space: '.
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gamma_array 1 2 3
18 ($color_space->name).'!' unless ref $gamma_array;
19 }
20 8 ▶ $gamma_array = [ ($gamma) x $color_space->axis_count] if is_nr( $gamma );
Branch: 1 branch
BranchTrueFalseTotal
if is_nr($gamma) 6 2 8
21 8 ▶ $gamma_array = $gamma if not defined $gamma_array and ref $gamma eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $gamma_array and ref $gamma eq "ARRAY" 0 8 8
Condition33%1/3
not defined $gamma_array and ref $gamma eq "ARRAY"
!ll&&!rl&&r
800
MC/DC0%0/2
not defined $gamma_array and ref $gamma eq "ARRAY"
not defined $gamma_array ref $gamma eq "ARRAY"
Truth table33%1/3
not(A) and B
A= not defined $gamma_arrayB= ref $gamma eq "ARRAY"
ABresult
0X0
100
111
22 8 ▶ return 'got badly formatted gamma value' if ref $gamma_array ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $gamma_array ne "ARRAY" 0 8 8
23
24 8 my $tuple = $color_values->normalized( $color_space->name );
25 8 for my $axis_nr ($color_space->basis->axis_iterator){
26 24 ▶ $tuple->[$axis_nr] = spow($tuple->[$axis_nr], $gamma_array->[$axis_nr]) if exists $gamma_array->[$axis_nr];
Branch: 1 branch
BranchTrueFalseTotal
if exists $gamma_array->[$axis_nr] 22 2 24
27 }
28 8 return $color_values->new_from_tuple( $tuple, $color_space->name, 'normal' );
29 }
30  
31 13 ▶sub set_value { # .values, %newval -- ~space_name --> _
Subroutine set_value: called CC=5 SCAR=16.1
Pod undocumented
32 13 my ($color_values, $partial_hash, $preselected_space_name) = @_;
33 13 my ($new_values, $deduced_space_name) =
34 Graphics::Toolkit::Color::Space::Hub::deformat_search_partial_hash( $partial_hash, $preselected_space_name );
35 13 ▶ unless (ref $new_values){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $new_values) 4 9 13
36 4 my $help_start = 'axis names: '.join(', ', keys %$partial_hash).' do not correlate to ';
37 4 ▶ return (defined $preselected_space_name) ? $help_start.'the selected color space: '.$preselected_space_name.'!'
Branch: 1 branch
BranchTrueFalseTotal
defined $preselected_space_name ? : 1 3 4
38 : $help_start.'any supported color space!';
39 }
40 9 my $tuple = $color_values->shaped( $deduced_space_name );
41 9 my $color_space = Graphics::Toolkit::Color::Space::Hub::get_space( $deduced_space_name );
42 9 for my $pos ($color_space->basis->axis_iterator) {
43 27 ▶ $tuple->[$pos] = $new_values->[$pos] if defined $new_values->[$pos];
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_values->[$pos] 11 16 27
44 }
45 9 return $color_values->new_from_tuple( $tuple, $color_space->name );
46 }
47  
48 13 ▶sub add_value { # .values, %newval -- ~space_name --> _
Subroutine add_value: called CC=5 SCAR=16.1
Pod undocumented
49 13 my ($color_values, $partial_hash, $preselected_space_name) = @_;
50 13 my ($new_values, $deduced_space_name) =
51 Graphics::Toolkit::Color::Space::Hub::deformat_search_partial_hash( $partial_hash, $preselected_space_name );
52 13 ▶ unless (ref $new_values){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $new_values) 5 8 13
53 5 my $help_start = 'axis names: '.join(', ', keys %$partial_hash).' do not correlate to ';
54 5 ▶ return (defined $preselected_space_name) ? $help_start.'the selected color space: '.$preselected_space_name.'!'
Branch: 1 branch
BranchTrueFalseTotal
defined $preselected_space_name ? : 3 2 5
55 : $help_start.'any supported color space!';
56 }
57 8 my $tuple = $color_values->shaped( $deduced_space_name );
58 8 my $color_space = Graphics::Toolkit::Color::Space::Hub::get_space( $deduced_space_name );
59 8 for my $pos ($color_space->basis->axis_iterator) {
60 24 ▶ $tuple->[$pos] += $new_values->[$pos] if defined $new_values->[$pos];
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_values->[$pos] 9 15 24
61 }
62 8 return $color_values->new_from_tuple( $tuple, $color_space->name );
63 }
64  
65  
66 0 ▶sub derive {
Subroutine derive: not called CC=1 SCAR=6.9
Pod undocumented
67 0 my ($color_values, $mult, $add, $set, $raw, $space_name) = @_;
68 }
69  
70  
71 #### light designer API ################################################
72 0 ▶sub _clear_values_amount_space_name {
Subroutine _clear_values_amount_space_name: not called CC=4 SCAR=30.0
73 0 my ($color_values, $amount, $space_name, @more) = @_;
74 0 ▶ return "need a G::T::Color::Values object as first argument"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $color_values eq "Graphics::Toolkit::Color::Values" 0 0 0
75 unless ref $color_values eq 'Graphics::Toolkit::Color::Values';
76 0 ▶ return "need a numeric amount between 0 and 1 as first argument" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 0 0
77 0 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
78 0 ▶ return "$space_name is not a known color space" unless ref $color_space;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $color_space 0 0 0
79 0 return ($color_values, $amount, $color_space, @more);
80 }
81  
82 0 ▶sub lighten { add_axis_value( @_, 'lightness') }
Subroutine lighten: not called CC=1 SCAR=6.9
Pod undocumented
83 0 ▶sub darken {
Subroutine darken: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my ($color_values, $amount, $color_space) = @_;
85 0 add_axis_value($color_values, -$amount, $color_space, 'lightness');
86 }
87 0 ▶sub saturate { add_axis_value( @_, 'saturation') }
Subroutine saturate: not called CC=1 SCAR=6.9
Pod undocumented
88 0 ▶sub desaturate {
Subroutine desaturate: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ($color_values, $amount, $color_space) = @_;
90 0 add_axis_value($color_values, -$amount, $color_space, 'saturation');
91 }
92 0 ▶sub add_axis_value {
Subroutine add_axis_value: not called CC=3 SCAR=24.8
Pod undocumented
93 0 my ($color_values, $amount, $color_space, $axis_name) = _clear_values_amount_space_name(@_);
94 0 ▶ return $color_values unless ref $color_values;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $color_values 0 0 0
95 0 my $axis_nr = $color_space->pos_from_axis_role( $axis_name );
96 0 ▶ return "color space: '".$color_space->name."' has no $axis_name axis" unless defined $axis_nr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $axis_nr 0 0 0
97 0 my $tuple = $color_values->normalized( $color_space->name );
98 0 $tuple->[$axis_nr] += $amount;
99 0 return $color_values->new_from_tuple( $tuple, $color_space->name, 'normal' );
100 }
101  
102 0 ▶sub tint { mix_with(@_, [255 ,255 ,255 ]) } # white
Subroutine tint: not called CC=1 SCAR=6.9
Pod undocumented
103 0 ▶sub tone { mix_with(@_, [127.5,127.5,127.5]) } # grey50
Subroutine tone: not called CC=1 SCAR=6.9
Pod undocumented
104 0 ▶sub shade { mix_with(@_, [ 0, 0, 0 ]) } # black
Subroutine shade: not called CC=1 SCAR=6.9
Pod undocumented
105 0 ▶sub mix_with {
Subroutine mix_with: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my ($color_values, $amount, $color_space, $tuple) = _clear_values_amount_space_name(@_);
107 0 ▶ return $color_values unless ref $color_values;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $color_values 0 0 0
108 0 return mix( $color_values, [Graphics::Toolkit::Color::Values->new_from_tuple( $tuple )], $amount, $color_space);
109 }
110  
111 0 ▶sub brightness {
Subroutine brightness: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my ($color_values, $mult, $add, $set, $space_name) = @_;
113  
114 }
115  
116 0 ▶sub saturation {
Subroutine saturation: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my ($color_values, $mult, $add, $set, $space_name) = @_;
118  
119 }
120  
121 0 ▶sub contrast {
Subroutine contrast: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my ($color_values, $mult, $add, $set, $space_name) = @_;
123  
124 }
125  
126 0 ▶sub vibrance {
Subroutine vibrance: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my ($color_values, $mult, $add, $set, $space_name) = @_;
128  
129 }
130  
131 #### deep designer methods #############################################
132 79 ▶sub mix { # .base_color_vals, @.added_volor_vals, @+|+add_amount, .space --> .color_values
Subroutine mix: called CC=12 SCAR=24.9
Pod undocumented
133 79 my ($base_color, $added_color, $add_amount, $color_space ) = @_;
134 79 ▶ return "need color value object as first argument !\n" unless ref $base_color eq 'Graphics::Toolkit::Color::Values';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base_color eq "Graphics::Toolkit::Color::Values" 0 79 79
135 79 ▶ return "second argument has to be an ARRAY !\n" unless ref $added_color eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $added_color eq "ARRAY" 0 79 79
136 79 ▶ return "need a color space object !\n" unless ref $color_space eq 'Graphics::Toolkit::Color::Space';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $color_space eq "Graphics::Toolkit::Color::Space" 0 79 79
137  
138 79 my $color_count = @$added_color + 1;
139 79 ▶ $add_amount = 1 / $color_count unless defined $add_amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $add_amount 11 68 79
140 79 ▶ $add_amount = [($add_amount) x ($color_count - 1)] unless ref $add_amount eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $add_amount eq "ARRAY" 71 8 79
141 79 ▶ return "ARRAY of mix amounts needs a value for every color !\n" unless @$add_amount == $color_count - 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$add_amount == $color_count - 1 2 77 79
142 77 my $mix_sum = 0;
143 77 $mix_sum += $_ for @$add_amount;
144 77 ▶ if ($mix_sum > 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($mix_sum > 1) { } 4 73 77
145 4 for my $reciepe_index (0 .. $#$add_amount){
146 7 $add_amount->[$reciepe_index] = $add_amount->[$reciepe_index] / $mix_sum;
147 }
148 } else {
149 73 push @$add_amount, 1 - $mix_sum;
150 73 push @$added_color, $base_color;
151 }
152
153 77 my $result_values = [(0) x $color_space->axis_count];
154 77 for my $color_nr (0 .. $#$added_color){
155 158 my $tuple = $added_color->[$color_nr]->shaped( $color_space->name );
156 158 $result_values->[$_] += $tuple->[$_] * $add_amount->[$color_nr] for 0 .. $#$tuple;
157 }
158 77 return $base_color->new_from_tuple( $result_values, $color_space->name );
159 }
160  
161 25 ▶sub invert {
Subroutine invert: called CC=21 SCAR=30.9
Pod undocumented
162 25 my ($color_values, $only, $color_space, $default_color_space ) = @_;
163 25 ▶ $only = [$only] if defined $only and not ref $only; # selected axes
Branch: 1 branch
BranchTrueFalseTotal
if defined $only and not ref $only 5 20 25
Condition100%3/3
defined $only and not ref $only
!ll&&!rl&&r
1645
MC/DC100%2/2
defined $only and not ref $only
defined $only not ref $only
Truth table100%3/3
A and not(B)
A= defined $onlyB= not ref $only
ABresult
0X0
100
111
164 25 ▶ return "need argument only as axis name (short or long) or as ARRAY of names!"
Branch: 1 branch
BranchTrueFalseTotal
if defined $only and ref $only ne "ARRAY" 0 25 25
Condition66%2/3
defined $only and ref $only ne "ARRAY"
!ll&&!rl&&r
1690
MC/DC0%0/2
defined $only and ref $only ne "ARRAY"
defined $only ref $only ne "ARRAY"
Truth table66%2/3
A and B
A= defined $onlyB= ref $only ne "ARRAY"
ABresult
0X0
100
111
165 if defined $only and ref $only ne 'ARRAY';
166 25 ▶ if (defined $only){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $only) 9 16 25
167 9 my %partial_hash = map { $_ => 1 } @$only;
168 9 ▶ my $preselected_space_name = defined($color_space) ? $color_space->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $color_space ? : 6 3 9
169 9 my ($new_values, $deduced_space_name) =
170 Graphics::Toolkit::Color::Space::Hub::deformat_search_partial_hash( \%partial_hash, $preselected_space_name );
171 9 ▶ return "could not find any color space that contains the axes: ". join(', ', @$only).' !'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $deduced_space_name or defined $color_space 1 8 9
Condition66%2/3
defined $deduced_space_name or defined $color_space
l!l&&r!l&&!r
801
MC/DC50%1/2
defined $deduced_space_name or defined $color_space
defined $deduced_space_name defined $color_space
Truth table66%2/3
A or B
A= defined $deduced_space_nameB= defined $color_space
ABresult
1X1
011
000
172 if not defined $deduced_space_name and not defined $color_space;
173 8 ▶ return "axes ". join(', ', @$only) . 'do not match color space '.$color_space->name.' !'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $deduced_space_name and ref $color_space 0 8 8
Condition33%1/3
not defined $deduced_space_name and ref $color_space
!ll&&!rl&&r
800
MC/DC0%0/2
not defined $deduced_space_name and ref $color_space
not defined $deduced_space_name ref $color_space
Truth table33%1/3
not(A) and B
A= not defined $deduced_space_nameB= ref $color_space
ABresult
0X0
100
111
174 if not defined $deduced_space_name and ref $color_space;
175 8 $color_space = Graphics::Toolkit::Color::Space::Hub::get_space( $deduced_space_name );
176 }
177 24 ▶ $color_space //= $default_color_space;
Condition66%2/3
$color_space //= $default_color_space
l!l&&r!l&&!r
2220
MC/DC0%0/2
$color_space //= $default_color_space
$color_space $default_color_space
Truth table66%2/3
A //= B
A= $color_spaceB= $default_color_space
ABresult
1X1
011
000
178
179 24 ▶ my $selected_axis = (defined $only) ? [ ] : [$color_space->basis->axis_iterator];
Branch: 1 branch
BranchTrueFalseTotal
defined $only ? : 8 16 24
180 24 ▶ if (defined $only) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $only) 8 16 24
181 8 for my $axis_name (@$only){
182 11 my $pos = $color_space->pos_from_axis_role( $axis_name );
183 11 $selected_axis->[$pos] = $pos;
184 }
185 }
186 24 my $tuple = $color_values->normalized( $color_space->name );
187 24 for my $axis_nr ($color_space->basis->axis_iterator){
188 72 ▶ next unless defined $selected_axis->[$axis_nr];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $selected_axis->[$axis_nr] 13 59 72
189 59 ▶ if ($color_space->shape->is_axis_euclidean( $axis_nr )){
Branch: 1 branch
BranchTrueFalseTotal
if ($color_space->shape->is_axis_euclidean($axis_nr)) { } 50 9 59
190 50 $tuple->[$axis_nr] = 0.5 - ($tuple->[$axis_nr] - 0.5);
191 } else {
192 9 ▶ $tuple->[$axis_nr]++ while $tuple->[$axis_nr] < 0;
Branch: 1 branch
BranchTrueFalseTotal
$tuple->[$axis_nr] < 0 and do { ++$tuple->[$axis_nr] } 0 9 9
193 9 ▶ $tuple->[$axis_nr]-- while $tuple->[$axis_nr] > 1;
Branch: 1 branch
BranchTrueFalseTotal
$tuple->[$axis_nr] > 1 and do { --$tuple->[$axis_nr] } 0 9 9
194 9 ▶ $tuple->[$axis_nr] = ($tuple->[$axis_nr] < 0.5)
Branch: 1 branch
BranchTrueFalseTotal
$tuple->[$axis_nr] < 0.5 ? : 7 2 9
195 ? $tuple->[$axis_nr] + 0.5
196 : $tuple->[$axis_nr] - 0.5;
197 }
198 }
199 24 return $color_values->new_from_tuple( $tuple, $color_space->name, 'normal' );
200 }
201
202 7 1;
« Color.pm ↑ Summary Color/SetCalculator.pm »