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 18     18   884 use v5.12;
  18         53  
2 18     18   109 use warnings;
  18         28  
  18         630  
3              
4             # common code of Graphics::Toolkit::Color::Space::Instance::*
5              
6             package Graphics::Toolkit::Color::Space;
7 18     18   8165 use Graphics::Toolkit::Color::Space::Basis;
  18         46  
  18         644  
8 18     18   8404 use Graphics::Toolkit::Color::Space::Shape;
  18         50  
  18         23080  
9              
10             sub new {
11 68     68 0 2187 my $pkg = shift;
12 68         227 my %args = @_;
13 68         462 my $basis = Graphics::Toolkit::Color::Space::Basis->new( $args{'axis'}, $args{'short'} );
14 68 100       230 return unless ref $basis;
15 67         356 my $shape = Graphics::Toolkit::Color::Space::Shape->new( $basis, $args{'range'}, $args{'type'} );
16 67 50       198 return unless ref $shape;
17              
18             # which formats the constructor will accept, that can be deconverted into list
19 650 100   650   1215 my %deformats = ( hash => sub { $basis->list_from_hash(@_) if $basis->is_hash(@_) },
20 667 100   667   1288 named_array => sub { @{$_[0]}[1 .. $#{$_[0]}] if $basis->is_named_array(@_) },
  106         349  
  106         285  
21 593 100   593   1153 string => sub { $basis->list_from_string(@_) if $basis->is_string(@_) },
22 541 100   541   955 css_string => sub { $basis->list_from_css(@_) if $basis->is_css_string(@_) },
23 67         821 );
24             # which formats we can output
25 518     518   1588 my %formats = (list => sub { @_ }, # 1,2,3
26 12     12   50 hash => sub { $basis->key_hash_from_list(@_) }, # { red => 1, green => 2, blue => 3 }
27 1     1   5 char_hash => sub { $basis->shortcut_hash_from_list(@_) },# { r =>1, g => 2, b => 3 }
28 1     1   5 array => sub { $basis->named_array_from_list(@_) }, # ['rgb',1,2,3]
29 42     42   129 string => sub { $basis->named_string_from_list(@_) }, # rgb: 1, 2, 3
30 4     4   15 css_string => sub { $basis->css_string_from_list(@_) }, # rgb(1,2,3)
31 67         703 );
32              
33 67         414 bless { basis => $basis, shape => $shape, format => \%formats, deformat => \%deformats, convert => {} };
34             }
35 6662     6662 0 12234 sub basis { $_[0]{'basis'}}
36 1738     1738 0 17926 sub name { $_[0]->basis->name }
37 3037     3037 0 4250 sub dimensions { $_[0]->basis->count }
38 839     839 0 1375 sub is_array { $_[0]->basis->is_array( $_[1] ) }
39 3     3 0 10 sub is_partial_hash { $_[0]->basis->is_partial_hash( $_[1] ) }
40 620 100 66 620 0 3948 sub has_format { (defined $_[1] and exists $_[0]{'format'}{ lc $_[1] }) ? 1 : 0 }
41 390 100 66 390 0 6295 sub can_convert { (defined $_[1] and exists $_[0]{'convert'}{ uc $_[1] }) ? 1 : 0 }
42              
43             ########################################################################
44              
45 106     106 0 10270 sub delta { shift->{'shape'}->delta( @_ ) } # @values -- @vector, @vector --> |@vector # on normalize values
46 282     282 0 11713 sub check { shift->{'shape'}->check( @_ ) } # @values -- @range --> ? # pos if carp
47 256     256 0 31344 sub clamp { shift->{'shape'}->clamp( @_ ) } # @values -- @range --> |@vector
48 234     234 0 516 sub normalize { shift->{'shape'}->normalize(@_)} # @values -- @range --> |@vector
49 597     597 0 3587 sub denormalize{ shift->{'shape'}->denormalize(@_)} # @values -- @range --> |@vector
50 94     94 0 221 sub denormalize_range{ shift->{'shape'}->denormalize_range(@_)} # @values -- @range --> |@vector
51              
52             ########################################################################
53              
54             sub add_formatter {
55 9     9 0 25 my ($self, $format, $code) = @_;
56 9 50 33     96 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         43 $self->{'format'}{ $format } = $code;
59             }
60             sub format {
61 604     604 0 8666 my ($self, $values, $format) = @_;
62 604 50       937 return unless $self->basis->is_array( $values );
63 604 100       1232 $self->{'format'}{ lc $format }->(@$values) if $self->has_format( $format );
64             }
65              
66             sub add_deformatter {
67 17     17 0 512 my ($self, $format, $code) = @_;
68 17 50 33     176 return 0 if not defined $format or ref $format or exists $self->{'deformat'}{$format} or ref $code ne 'CODE';
      33        
      33        
69 17         57 $self->{'deformat'}{ lc $format } = $code;
70             }
71             sub deformat {
72 706     706 0 27152 my ($self, $values) = @_;
73 706 50       1201 return undef unless defined $values;
74 706         805 for my $deformatter (values %{$self->{'deformat'}}){
  706         1687  
75 2897         4564 my @values = $deformatter->($values);
76 2897 100       4973 return @values if @values == $self->dimensions;
77             }
78 458         874 return undef;
79             }
80              
81             ########################################################################
82              
83             sub add_converter {
84 59     59 0 141 my ($self, $space_name, $to_code, $from_code, $mode) = @_;
85 59 50 33     494 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 59 50       154 return 0 if $self->can_convert( $space_name );
87 59         297 $self->{'convert'}{ uc $space_name } = { from => $from_code, to => $to_code, mode => $mode };
88             }
89             sub convert {
90 144     144 0 26680 my ($self, $values, $space_name) = @_;
91 144 50 33     366 return unless $self->{'basis'}->is_array( $values ) and defined $space_name;
92 144 50       364 $self->{'convert'}{ uc $space_name }{'to'}->(@$values) if $self->can_convert( $space_name );
93             }
94              
95             sub deconvert {
96 179     179 0 27520 my ($self, $values, $space_name) = @_;
97 179 50 33     685 return unless ref $values eq 'ARRAY' and defined $space_name;
98 179 50       346 $self->{'convert'}{ uc $space_name }{'from'}->(@$values) if $self->can_convert( $space_name );
99             }
100              
101             1;