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   527697 use v5.12;
  10         26  
6 10     10   36 use warnings;
  10         15  
  10         471  
7 10     10   3735 use Graphics::Toolkit::Color::Name;
  10         42  
  10         887  
8 10     10   106 use Graphics::Toolkit::Color::Space::Hub;
  10         15  
  10         7086  
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 188079 my ($pkg, $color_def, $space_name, $range_def, $raw) = @_;
15 172 100       339 return "Can not create color value object without color definition!" unless defined $color_def;
16 170 100       345 if (not ref $color_def) { # try to resolve color name
17 74         219 my $rgb = Graphics::Toolkit::Color::Name::get_values( $color_def );
18 74 100       151 if (ref $rgb){
19 43         135 $rgb = $RGB->clamp( $RGB->normalize( $rgb ), 'normal' );
20 43         317 return bless { color_name => $color_def, rgb_tuple => $rgb, source_tuple => '', source_space_name => ''};
21             }
22             }
23 127 50       466 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       367 return $tuple unless ref $tuple;
27 78         136 new_from_tuple( '', $tuple, $found_space_name, $range_def, $raw);
28             }
29             sub new_from_tuple { #
30 522     522 0 197376 my ($pkg, $tuple, $space_name, $range_def, $raw) = @_;
31 522         860 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
32 522 50       798 return $color_space unless ref $color_space;
33 522 100       893 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         884 $tuple = $color_space->normalize( $tuple, $range_def );
36 521 100 100     1333 $tuple = $color_space->clamp( $tuple, 'normal' ) unless defined $raw and $raw;
37              
38 521         631 my $source_tuple = '';
39 521         555 my $source_space_name = '';
40 521 100       908 if ($color_space->name ne $RGB->name){ # convert into RGB if needed
41 189         203 $source_tuple = $tuple;
42 189         279 $source_space_name = $color_space->name;
43 189         284 $tuple = Graphics::Toolkit::Color::Space::Hub::deconvert( $tuple, $color_space->name, 'normal' );
44             }
45              
46 521         1017 my $name = Graphics::Toolkit::Color::Name::from_values( $RGB->round( $RGB->denormalize( $tuple ) ) );
47 521         3510 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 1956 my ($self, $space_name) = @_;
53             Graphics::Toolkit::Color::Space::Hub::convert(
54 444         1312 $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 20939 my ($self, $space_name, $range_def, $precision_def, $raw) = @_;
59 360         663 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
60 360 50       621 return $color_space unless ref $color_space;
61 360         590 my $tuple = $self->normalized( $color_space->name );
62 360 50       581 return $tuple if not ref $tuple;
63 360         636 $tuple = $color_space->denormalize( $tuple, $range_def );
64 360 100       925 $tuple = $color_space->clamp( $tuple, $range_def ) unless $raw;
65 360 100 66     1621 $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         871 return $tuple;
68             }
69             sub formatted { # in shape values in any format # _ -- ~space, @~|~format, @~|~range, @~|~suffix --> @|%|$
70 92     92 0 15845 my ($self, $space_name, $format_name, $suffix_def, $range_def, $precision_def, $raw) = @_;
71 92         236 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
72 92 50       209 return \$color_space unless ref $color_space;
73 92         207 my $tuple = $self->shaped( $color_space->name, $range_def, $precision_def, $raw );
74 92 50       165 return \$tuple unless ref $tuple;
75 92         189 return $color_space->format( $tuple, $format_name, $suffix_def );
76             }
77 72     72 0 17558 sub name { $_[0]->{'color_name'} }
78              
79             sub is_in_gamut {
80 16     16 0 26 my ($self, $space_name) = @_;
81 16 100 100     69 $space_name = $self->{'source_space_name'} if not defined $space_name and $self->{'source_space_name'};
82 16         29 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name ); # default to RGB
83 16 50       29 return 0 unless ref $color_space;
84 16         35 my $tuple = $self->normalized( $space_name );
85 16 50       50 return 0 unless ref $tuple;
86 16         32 return $color_space->is_in_linear_bounds( $tuple, 'normal' );
87             }
88              
89             1;