File Coverage

lib/Graphics/Toolkit/Color/Space/Basis.pm
Criterion Covered Total %
statement 93 93 100.0
branch 52 58 89.6
condition 27 32 84.3
subroutine 28 28 100.0
pod 0 25 0.0
total 200 236 84.7


line stmt bran cond sub pod time code
1              
2             # count and names of color space axis (short and long), space name = usr | prefix + axis initials
3              
4             package Graphics::Toolkit::Color::Space::Basis;
5 36     36   487950 use v5.12;
  36         114  
6 36     36   174 use warnings;
  36         89  
  36         1771  
7 36     36   5738 use Graphics::Toolkit::Color::Space::Util qw/is_nr/;
  36         84  
  36         45862  
8              
9             sub new {
10 289     289 0 8789 my ($pkg, $axis_long_names, $axis_short_names, $space_name, $alias_name) = @_;
11 289 100       892 return 'first argument (axis names) has to be an ARRAY reference' unless ref $axis_long_names eq 'ARRAY';
12 286 100 66     761 return 'amount of short axis names have to match the count of long axis names'
      100        
13             if defined $axis_short_names and (ref $axis_short_names ne 'ARRAY' or @$axis_long_names != @$axis_short_names);
14              
15 285         609 my @axis_long_name = map {lc} @$axis_long_names;
  882         1803  
16 285 100       743 my @axis_short_name = map { color_key_shortcut($_) } (defined $axis_short_names) ? @$axis_short_names : @axis_long_name;
  882         1219  
17 285 50       626 return 'need some axis names to create a color space' unless @axis_long_name > 0;
18 285 50       605 return 'need same amount of axis short names and long names' unless @axis_long_name == @axis_short_name;
19              
20 285         619 my @iterator = 0 .. $#axis_long_name;
21 285         396 my %long_name_order = map { $axis_long_name[$_] => $_ } @iterator;
  882         1929  
22 285         534 my %short_name_order = map { $axis_short_name[$_] => $_ } @iterator;
  882         1520  
23 285         812 my $axis_initials = uc join( '', @axis_short_name );
24 285   66     883 $space_name //= $axis_initials;
25 285   100     720 $alias_name //= '';
26              
27 285         2254 bless { space_name => uc $space_name, alias_name => uc $alias_name,
28             axis_long_name => \@axis_long_name, axis_short_name => \@axis_short_name,
29             long_name_order => \%long_name_order, short_name_order => \%short_name_order,
30             axis_iterator => \@iterator }
31             }
32 882 50   882 0 2828 sub color_key_shortcut { lc substr($_[0], 0, 1) if defined $_[0] }
33              
34             #### getter ############################################################
35 5896     5896 0 16682 sub space_name { $_[0]{'space_name'} } # color space name
36 2360     2360 0 4627 sub alias_name { $_[0]{'alias_name'} } # alternative space name
37              
38 191     191 0 237 sub long_axis_names { @{$_[0]{'axis_long_name'}} } # axis full names
  191         753  
39 9     9 0 21 sub short_axis_names { @{$_[0]{'axis_short_name'}} } # axis short names
  9         98  
40 8433     8433 0 7832 sub axis_iterator { @{$_[0]{'axis_iterator'}} } # counting all axis 0 .. -1
  8433         15196  
41 14029     14029 0 16874 sub axis_count { int @{$_[0]{'axis_iterator'}} } # amount of axis
  14029         45036  
42              
43             #### predicates ########################################################
44             sub is_name { # --> ? # is this a valid name of this space
45 333 50   333 0 863 return 0 if not defined $_[1];
46 333 100       1095 return 1 if uc $_[1] eq $_[0]{'space_name'};
47 280 100 100     1075 return 1 if $_[0]{'alias_name'} and uc $_[1] eq $_[0]{'alias_name'};
48 263         791 return 0;
49             }
50 1069 100 66 1069 0 5922 sub is_long_axis_name { (defined $_[1] and exists $_[0]->{'long_name_order'}{ lc $_[1] }) ? 1 : 0 } # ~long_name --> ?
51 814 100 66 814 0 6400 sub is_short_axis_name { (defined $_[1] and exists $_[0]->{'short_name_order'}{ lc $_[1] }) ? 1 : 0 }# ~short_name --> ?
52 1061 100   1061 0 2008 sub is_axis_name { $_[0]->is_long_axis_name($_[1]) or $_[0]->is_short_axis_name($_[1]) } # ~name --> ?
53              
54 711 50   711 0 1992 sub pos_from_long_axis_name { defined $_[1] ? $_[0]->{'long_name_order'}{ lc $_[1] } : undef } # ~long_name --> +pos
55 357 50   357 0 1077 sub pos_from_short_axis_name { defined $_[1] ? $_[0]->{'short_name_order'}{ lc $_[1] } : undef } # ~short_name --> +pos
56 208   100 208 0 413 sub pos_from_axis_name { pos_from_long_axis_name(@_) // pos_from_short_axis_name(@_) }
57              
58             sub is_hash { # with all axis names as keys
59 2292     2292 0 3549 my ($self, $value_hash) = @_;
60 2292 100       4251 $self->is_partial_hash( $value_hash ) and (keys %$value_hash == $self->axis_count);
61             }
62             sub is_partial_hash { # with some axis names as keys
63 2565     2565 0 3832 my ($self, $value_hash) = @_;
64 2565 100       8965 return 0 unless ref $value_hash eq 'HASH';
65 872         1406 my $key_count = keys %$value_hash;
66 872         1224 my @axis_visited;
67 872 100 100     5867 return 0 unless $key_count and $key_count <= $self->axis_count;
68 718         1804 for my $axis_name (keys %$value_hash) {
69 1049 100 66     1983 return 0 unless $self->is_axis_name( $axis_name ) and defined $value_hash->{$axis_name};
70 494         950 my $axis_pos = $self->pos_from_long_axis_name( $axis_name );
71 494 100       1248 $axis_pos = $self->pos_from_short_axis_name( $axis_name ) unless defined $axis_pos;
72 494         808 $axis_visited[ $axis_pos ]++;
73 494 100       1127 return 0 if $axis_visited[ $axis_pos ] > 1;
74             }
75 154         675 return 1;
76             }
77 5679 100 100 5679 0 10727 sub is_value_tuple { (ref $_[1] eq 'ARRAY' and @{$_[1]} == $_[0]->axis_count) ? 1 : 0 }
78             sub is_number_tuple {
79 365     365 0 569 my ($self, $tuple) = @_;
80 365 100       579 return 0 unless $self->is_value_tuple( $tuple );
81 360 100       594 map { return 0 unless is_nr( $tuple->[$_] ) } $self->axis_iterator;
  1085         1687  
82 356         713 return 1;
83             }
84              
85             #### converter #########################################################
86             sub short_axis_name_from_long {
87 4     4 0 13 my ($self, $name) = @_;
88 4 100       14 return unless $self->is_long_axis_name( $name );
89 3         10 ($self->short_axis_names)[ $self->pos_from_long_axis_name( $name ) ];
90             }
91             sub long_axis_name_from_short {
92 4     4 0 11 my ($self, $name) = @_;
93 4 100       15 return unless $self->is_short_axis_name( $name );
94 3         12 ($self->long_axis_names)[ $self->pos_from_short_axis_name( $name ) ];
95             }
96              
97             sub long_name_hash_from_tuple {
98 13     13 0 27 my ($self, $values) = @_;
99 13 100       37 return unless $self->is_value_tuple( $values );
100 11         28 return { map { $self->{'axis_long_name'}[$_] => $values->[$_]} $self->axis_iterator };
  49         158  
101             }
102             sub short_name_hash_from_tuple {
103 10     10 0 25 my ($self, $values) = @_;
104 10 100       28 return unless $self->is_value_tuple( $values );
105 8         27 return { map {$self->{'axis_short_name'}[$_] => $values->[$_]} $self->axis_iterator };
  25         108  
106             }
107              
108             sub tuple_from_hash {
109 40     40 0 11979 my ($self, $value_hash) = @_;
110 40 100       103 return unless $self->is_hash( $value_hash );
111 39         118 my $values = [ (0) x $self->axis_count ];
112 39         102 for my $key (keys %$value_hash) {
113 122         278 $values->[ $self->pos_from_axis_name( $key ) ] = $value_hash->{ $key };
114             }
115 39         146 return $values;
116             }
117             sub tuple_from_partial_hash {
118 217     217 0 10327 my ($self, $value_hash) = @_;
119 217 100       384 return unless $self->is_partial_hash( $value_hash );
120 29         53 my $values = [];
121 29         61 for my $key (keys %$value_hash) {
122 46         102 $values->[ $self->pos_from_axis_name( $key ) ] = $value_hash->{ $key };
123             }
124 29         73 return $values;
125             }
126              
127             1;