File Coverage

lib/Graphics/Toolkit/Color/Space.pm
Criterion Covered Total %
statement 72 72 100.0
branch 30 42 71.4
condition 14 36 38.8
subroutine 35 35 100.0
pod 0 21 0.0
total 151 206 73.3


line stmt bran cond sub pod time code
1 16     16   813 use v5.12;
  16         50  
2 16     16   110 use warnings;
  16         50  
  16         572  
3              
4             # common code of Graphics::Toolkit::Color::Space::Instance::*
5              
6             package Graphics::Toolkit::Color::Space;
7 16     16   7431 use Graphics::Toolkit::Color::Space::Basis;
  16         55  
  16         480  
8 16     16   7343 use Graphics::Toolkit::Color::Space::Shape;
  16         46  
  16         21009  
9              
10             sub new {
11 66     66 0 1505 my $pkg = shift;
12 66         221 my %args = @_;
13 66         465 my $basis = Graphics::Toolkit::Color::Space::Basis->new( $args{'axis'}, $args{'short'} );
14 66 100       213 return unless ref $basis;
15 65         335 my $shape = Graphics::Toolkit::Color::Space::Shape->new( $basis, $args{'range'}, $args{'type'} );
16 65 50       186 return unless ref $shape;
17              
18             # which formats the constructor will accept, that can be deconverted into list
19 557 100   557   1053 my %deformats = ( hash => sub { $basis->list_from_hash(@_) if $basis->is_hash(@_) },
20 629 100   629   1157 named_array => sub { @{$_[0]}[1 .. $#{$_[0]}] if $basis->is_named_array(@_) },
  96         321  
  96         230  
21 581 100   581   1073 string => sub { $basis->list_from_string(@_) if $basis->is_string(@_) },
22 558 100   558   998 css_string => sub { $basis->list_from_css(@_) if $basis->is_css_string(@_) },
23 65         763 );
24             # which formats we can output
25 518     518   1687 my %formats = (list => sub { @_ }, # 1,2,3
26 12     12   40 hash => sub { $basis->key_hash_from_list(@_) }, # { red => 1, green => 2, blue => 3 }
27 1     1   3 char_hash => sub { $basis->shortcut_hash_from_list(@_) },# { r =>1, g => 2, b => 3 }
28 1     1   4 array => sub { $basis->named_array_from_list(@_) }, # ['rgb',1,2,3]
29 38     38   117 string => sub { $basis->named_string_from_list(@_) }, # rgb: 1, 2, 3
30 2     2   9 css_string => sub { $basis->css_string_from_list(@_) }, # rgb(1,2,3)
31 65         655 );
32              
33 65         420 bless { basis => $basis, shape => $shape, format => \%formats, deformat => \%deformats, convert => {} };
34             }
35 6386     6386 0 11494 sub basis { $_[0]{'basis'}}
36 1673     1673 0 14927 sub name { $_[0]->basis->name }
37 2889     2889 0 3977 sub dimensions { $_[0]->basis->count }
38 812     812 0 1339 sub is_array { $_[0]->basis->is_array( $_[1] ) }
39 1     1 0 4 sub is_partial_hash { $_[0]->basis->is_partial_hash( $_[1] ) }
40 614 100 66 614 0 3744 sub has_format { (defined $_[1] and exists $_[0]{'format'}{ lc $_[1] }) ? 1 : 0 }
41 371 100 66 371 0 5438 sub can_convert { (defined $_[1] and exists $_[0]{'convert'}{ uc $_[1] }) ? 1 : 0 }
42              
43             ########################################################################
44              
45 102     102 0 9664 sub delta { shift->{'shape'}->delta( @_ ) } # @values -- @vector, @vector --> |@vector # on normalize values
46 253     253 0 7361 sub check { shift->{'shape'}->check( @_ ) } # @values -- @range --> ? # pos if carp
47 245     245 0 31525 sub clamp { shift->{'shape'}->clamp( @_ ) } # @values -- @range --> |@vector
48 223     223 0 495 sub normalize { shift->{'shape'}->normalize(@_)} # @values -- @range --> |@vector
49 593     593 0 3562 sub denormalize{ shift->{'shape'}->denormalize(@_)} # @values -- @range --> |@vector
50 90     90 0 194 sub denormalize_range{ shift->{'shape'}->denormalize_range(@_)} # @values -- @range --> |@vector
51              
52             ########################################################################
53              
54             sub add_formatter {
55 9     9 0 21 my ($self, $format, $code) = @_;
56 9 50 33     103 return 0 if not defined $format or ref $format or ref $code ne 'CODE';
      33        
57 9 50       26 return 0 if $self->has_format( $format );
58 9         32 $self->{'format'}{ $format } = $code;
59             }
60             sub format {
61 598     598 0 8585 my ($self, $values, $format) = @_;
62 598 50       920 return unless $self->basis->is_array( $values );
63 598 100       1128 $self->{'format'}{ lc $format }->(@$values) if $self->has_format( $format );
64             }
65              
66             sub add_deformatter {
67 17     17 0 435 my ($self, $format, $code) = @_;
68 17 50 33     213 return 0 if not defined $format or ref $format or exists $self->{'deformat'}{$format} or ref $code ne 'CODE';
      33        
      33        
69 17         74 $self->{'deformat'}{ lc $format } = $code;
70             }
71             sub deformat {
72 657     657 0 25422 my ($self, $values) = @_;
73 657 50       1095 return undef unless defined $values;
74 657         741 for my $deformatter (values %{$self->{'deformat'}}){
  657         1892  
75 2755         4375 my @values = $deformatter->($values);
76 2755 100       4454 return @values if @values == $self->dimensions;
77             }
78 422         891 return undef;
79             }
80              
81             ########################################################################
82              
83             sub add_converter {
84 57     57 0 148 my ($self, $space_name, $to_code, $from_code, $mode) = @_;
85 57 50 33     468 return 0 if not defined $space_name or ref $space_name or ref $from_code ne 'CODE' or ref $to_code ne 'CODE';
      33        
      33        
86 57 50       125 return 0 if $self->can_convert( $space_name );
87 57         297 $self->{'convert'}{ uc $space_name } = { from => $from_code, to => $to_code, mode => $mode };
88             }
89             sub convert {
90 128     128 0 22866 my ($self, $values, $space_name) = @_;
91 128 50 33     270 return unless $self->{'basis'}->is_array( $values ) and defined $space_name;
92 128 50       282 $self->{'convert'}{ uc $space_name }{'to'}->(@$values) if $self->can_convert( $space_name );
93             }
94              
95             sub deconvert {
96 182     182 0 23174 my ($self, $values, $space_name) = @_;
97 182 50 33     696 return unless ref $values eq 'ARRAY' and defined $space_name;
98 182 50       348 $self->{'convert'}{ uc $space_name }{'from'}->(@$values) if $self->can_convert( $space_name );
99             }
100              
101             1;