File Coverage

lib/Graphics/Toolkit/Color/Values.pm
Criterion Covered Total %
statement 60 60 100.0
branch 28 36 77.7
condition 14 15 93.3
subroutine 11 11 100.0
pod 0 7 0.0
total 113 129 87.6


line stmt bran cond sub pod time code
1              
2             # read only store of a single color: name + values in default and original space
3              
4             package Graphics::Toolkit::Color::Values;
5 10     10   530781 use v5.12;
  10         25  
6 10     10   38 use warnings;
  10         17  
  10         459  
7 10     10   4296 use Graphics::Toolkit::Color::Name;
  10         52  
  10         736  
8 10     10   98 use Graphics::Toolkit::Color::Space::Hub;
  10         18  
  10         6463  
9              
10             my $RGB = Graphics::Toolkit::Color::Space::Hub::default_space();
11              
12             #### constructor #######################################################
13             sub new_from_any_input { # values => %space_name => tuple , ~origin_space, ~color_name
14 172     172 0 182257 my ($pkg, $color_def, $space_name, $range_def, $raw) = @_;
15 172 100       370 return "Can not create color value object without color definition!" unless defined $color_def;
16 170 100       335 if (not ref $color_def) { # try to resolve color name
17 74         189 my $rgb = Graphics::Toolkit::Color::Name::get_values( $color_def );
18 74 100       152 if (ref $rgb){
19 43         126 $rgb = $RGB->clamp( $RGB->normalize( $rgb ), 'normal' );
20 43         375 return bless { color_name => $color_def, rgb_tuple => $rgb, source_tuple => '', source_space_name => ''};
21             }
22             }
23 127 50       477 my ($tuple, $found_space_name, $format) = (defined $space_name)
24             ? Graphics::Toolkit::Color::Space::Hub::deformat( $color_def, $space_name )
25             : Graphics::Toolkit::Color::Space::Hub::deformat_search( $color_def );
26 127 100       346 return $tuple unless ref $tuple;
27 78         142 new_from_tuple( '', $tuple, $found_space_name, $range_def, $raw);
28             }
29             sub new_from_tuple { #
30 522     522 0 198598 my ($pkg, $tuple, $space_name, $range_def, $raw) = @_;
31 522         899 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
32 522 50       805 return $color_space unless ref $color_space;
33 522 100       856 return "Need ARRAY of ".$color_space->axis_count." ".$color_space->name." values as first argument!"
34             unless $color_space->is_value_tuple( $tuple );
35 521         989 $tuple = $color_space->normalize( $tuple, $range_def );
36 521 100 100     1397 $tuple = $color_space->clamp( $tuple, 'normal' ) unless defined $raw and $raw;
37              
38 521         715 my $source_tuple = '';
39 521         607 my $source_space_name = '';
40 521 100       1081 if ($color_space->name ne $RGB->name){ # convert into RGB if needed
41 189         260 $source_tuple = $tuple;
42 189         321 $source_space_name = $color_space->name;
43 189         323 $tuple = Graphics::Toolkit::Color::Space::Hub::deconvert( $tuple, $color_space->name, 'normal' );
44             }
45              
46 521         1149 my $name = Graphics::Toolkit::Color::Name::from_values( $RGB->round( $RGB->denormalize( $tuple ) ) );
47 521         3816 bless { rgb_tuple => $tuple, source_tuple => $source_tuple, source_space_name => $source_space_name, color_name => $name };
48             }
49              
50             #### getter ############################################################
51             sub normalized { # normalized (0..1) value tuple in any color space
52 444     444 0 2158 my ($self, $space_name) = @_;
53             Graphics::Toolkit::Color::Space::Hub::convert(
54 444         1265 $self->{'rgb_tuple'}, $space_name, 'normal', $self->{'source_tuple'}, $self->{'source_space_name'},
55             );
56             }
57             sub shaped { # in any color space, range and precision
58 360     360 0 21676 my ($self, $space_name, $range_def, $precision_def, $raw) = @_;
59 360         674 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
60 360 50       588 return $color_space unless ref $color_space;
61 360         608 my $tuple = $self->normalized( $color_space->name );
62 360 50       606 return $tuple if not ref $tuple;
63 360         675 $tuple = $color_space->denormalize( $tuple, $range_def );
64 360 100       989 $tuple = $color_space->clamp( $tuple, $range_def ) unless $raw;
65 360 100 66     1566 $tuple = $color_space->round( $tuple, $precision_def ) unless ($raw and not defined $precision_def)
      100        
      100        
66             or (defined $range_def and not defined $precision_def);
67 360         911 return $tuple;
68             }
69             sub formatted { # in shape values in any format # _ -- ~space, @~|~format, @~|~range, @~|~suffix --> @|%|$
70 92     92 0 14231 my ($self, $space_name, $format_name, $suffix_def, $range_def, $precision_def, $raw) = @_;
71 92         218 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
72 92 50       162 return \$color_space unless ref $color_space;
73 92         162 my $tuple = $self->shaped( $color_space->name, $range_def, $precision_def, $raw );
74 92 50       163 return \$tuple unless ref $tuple;
75 92         196 return $color_space->format( $tuple, $format_name, $suffix_def );
76             }
77 72     72 0 19922 sub name { $_[0]->{'color_name'} }
78              
79             sub is_in_gamut {
80 16     16 0 20 my ($self, $space_name) = @_;
81 16 100 100     67 $space_name = $self->{'source_space_name'} if not defined $space_name and $self->{'source_space_name'};
82 16         28 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name ); # default to RGB
83 16 50       23 return 0 unless ref $color_space;
84 16         33 my $tuple = $self->normalized( $space_name );
85 16 50       26 return 0 unless ref $tuple;
86 16         30 return $color_space->is_in_linear_bounds( $tuple, 'normal' );
87             }
88              
89             1;