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   520306 use v5.12;
  10         34  
6 10     10   40 use warnings;
  10         13  
  10         428  
7 10     10   3771 use Graphics::Toolkit::Color::Name;
  10         39  
  10         778  
8 10     10   100 use Graphics::Toolkit::Color::Space::Hub;
  10         16  
  10         6713  
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 190984 my ($pkg, $color_def, $space_name, $range_def, $raw) = @_;
15 172 100       320 return "Can not create color value object without color definition!" unless defined $color_def;
16 170 100       306 if (not ref $color_def) { # try to resolve color name
17 74         198 my $rgb = Graphics::Toolkit::Color::Name::get_values( $color_def );
18 74 100       161 if (ref $rgb){
19 43         107 $rgb = $RGB->clamp( $RGB->normalize( $rgb ), 'normal' );
20 43         267 return bless { color_name => $color_def, rgb_tuple => $rgb, source_tuple => '', source_space_name => ''};
21             }
22             }
23 127 50       417 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       331 return $tuple unless ref $tuple;
27 78         147 new_from_tuple( '', $tuple, $found_space_name, $range_def, $raw);
28             }
29             sub new_from_tuple { #
30 522     522 0 205049 my ($pkg, $tuple, $space_name, $range_def, $raw) = @_;
31 522         916 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
32 522 50       708 return $color_space unless ref $color_space;
33 522 100       815 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         974 $tuple = $color_space->normalize( $tuple, $range_def );
36 521 100 100     1313 $tuple = $color_space->clamp( $tuple, 'normal' ) unless defined $raw and $raw;
37              
38 521         672 my $source_tuple = '';
39 521         601 my $source_space_name = '';
40 521 100       981 if ($color_space->name ne $RGB->name){ # convert into RGB if needed
41 189         244 $source_tuple = $tuple;
42 189         292 $source_space_name = $color_space->name;
43 189         326 $tuple = Graphics::Toolkit::Color::Space::Hub::deconvert( $tuple, $color_space->name, 'normal' );
44             }
45              
46 521         1045 my $name = Graphics::Toolkit::Color::Name::from_values( $RGB->round( $RGB->denormalize( $tuple ) ) );
47 521         3786 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 2253 my ($self, $space_name) = @_;
53             Graphics::Toolkit::Color::Space::Hub::convert(
54 444         1182 $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 25412 my ($self, $space_name, $range_def, $precision_def, $raw) = @_;
59 360         635 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
60 360 50       553 return $color_space unless ref $color_space;
61 360         554 my $tuple = $self->normalized( $color_space->name );
62 360 50       676 return $tuple if not ref $tuple;
63 360         662 $tuple = $color_space->denormalize( $tuple, $range_def );
64 360 100       937 $tuple = $color_space->clamp( $tuple, $range_def ) unless $raw;
65 360 100 66     1624 $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         868 return $tuple;
68             }
69             sub formatted { # in shape values in any format # _ -- ~space, @~|~format, @~|~range, @~|~suffix --> @|%|$
70 92     92 0 19823 my ($self, $space_name, $format_name, $suffix_def, $range_def, $precision_def, $raw) = @_;
71 92         212 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
72 92 50       158 return \$color_space unless ref $color_space;
73 92         189 my $tuple = $self->shaped( $color_space->name, $range_def, $precision_def, $raw );
74 92 50       188 return \$tuple unless ref $tuple;
75 92         230 return $color_space->format( $tuple, $format_name, $suffix_def );
76             }
77 72     72 0 22178 sub name { $_[0]->{'color_name'} }
78              
79             sub is_in_gamut {
80 16     16 0 25 my ($self, $space_name) = @_;
81 16 100 100     56 $space_name = $self->{'source_space_name'} if not defined $space_name and $self->{'source_space_name'};
82 16         30 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name ); # default to RGB
83 16 50       27 return 0 unless ref $color_space;
84 16         34 my $tuple = $self->normalized( $space_name );
85 16 50       30 return 0 unless ref $tuple;
86 16         37 return $color_space->is_in_linear_bounds( $tuple, 'normal' );
87             }
88              
89             1;