File Coverage

lib/Graphics/Toolkit/Color/Values.pm
Criterion Covered Total %
statement 60 60 100.0
branch 26 34 76.4
condition 13 15 86.6
subroutine 11 11 100.0
pod 0 7 0.0
total 110 127 86.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 9     9   745318 use v5.12;
  9         38  
6 9     9   53 use warnings;
  9         19  
  9         562  
7 9     9   4767 use Graphics::Toolkit::Color::Name;
  9         45  
  9         722  
8 9     9   91 use Graphics::Toolkit::Color::Space::Hub;
  9         18  
  9         7414  
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 159     159 0 340000 my ($pkg, $color_def, $range_def, $raw) = @_;
15 159 50       394 return "Can not create color value object without color definition!" unless defined $color_def;
16 159 100       348 if (not ref $color_def) { # try to resolve color name
17 74         253 my $rgb = Graphics::Toolkit::Color::Name::get_values( $color_def );
18 74 100       182 if (ref $rgb){
19 39         127 $rgb = $RGB->clamp( $RGB->normalize( $rgb ), 'normal' );
20 39         379 return bless { color_name => $color_def, rgb_tuple => $rgb, source_tuple => '', source_space_name => ''};
21             }
22             }
23 120         326 my ($tuple, $space_name) = Graphics::Toolkit::Color::Space::Hub::deformat( $color_def );
24 120 100       387 return "could not recognize color value format or color name: $color_def" unless ref $tuple;
25 78         189 new_from_tuple( '', $tuple, $space_name, $range_def, $raw);
26             }
27             sub new_from_tuple { #
28 513     513 0 316850 my ($pkg, $tuple, $space_name, $range_def, $raw) = @_;
29 513         1021 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
30 513 50       844 return $color_space unless ref $color_space;
31 513 100       1177 return "Need ARRAY of ".$color_space->axis_count." ".$color_space->name." values as first argument!"
32             unless $color_space->is_value_tuple( $tuple );
33 512         1115 $tuple = $color_space->normalize( $tuple, $range_def );
34 512 100 66     1783 $tuple = $color_space->clamp( $tuple, 'normal' ) unless defined $raw and $raw;
35              
36 512         819 my $source_tuple = '';
37 512         672 my $source_space_name = '';
38 512 100       1278 if ($color_space->name ne $RGB->name){ # convert into RGB if needed
39 138         210 $source_tuple = $tuple;
40 138         256 $source_space_name = $color_space->name;
41 138         284 $tuple = Graphics::Toolkit::Color::Space::Hub::deconvert( $tuple, $color_space->name, 'normal' );
42             }
43              
44 512         1260 my $name = Graphics::Toolkit::Color::Name::from_values( $RGB->round( $RGB->denormalize( $tuple ) ) );
45 512         4758 bless { rgb_tuple => $tuple, source_tuple => $source_tuple, source_space_name => $source_space_name, color_name => $name };
46             }
47              
48             #### getter ############################################################
49             sub normalized { # normalized (0..1) value tuple in any color space
50 418     418 0 2315 my ($self, $space_name) = @_;
51             Graphics::Toolkit::Color::Space::Hub::convert(
52 418         1383 $self->{'rgb_tuple'}, $space_name, 'normal', $self->{'source_tuple'}, $self->{'source_space_name'},
53             );
54             }
55             sub shaped { # in any color space, range and precision
56 343     343 0 21768 my ($self, $space_name, $range_def, $precision_def, $raw) = @_;
57 343         769 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
58 343 50       703 return $color_space unless ref $color_space;
59 343         655 my $tuple = $self->normalized( $color_space->name );
60 343 50       791 return $tuple if not ref $tuple;
61 343         812 $tuple = $color_space->denormalize( $tuple, $range_def );
62 343 100       1263 $tuple = $color_space->clamp( $tuple, $range_def ) unless $raw;
63 343 100 66     1660 $tuple = $color_space->round( $tuple, $precision_def ) unless ($raw and not defined $precision_def)
      100        
      100        
64             or (defined $range_def and not defined $precision_def);
65 343         1132 return $tuple;
66             }
67             sub formatted { # in shape values in any format # _ -- ~space, @~|~format, @~|~range, @~|~suffix
68 86     86 0 19716 my ($self, $space_name, $format_name, $suffix_def, $range_def, $precision_def, $raw) = @_;
69 86         246 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
70 86 50       177 return $color_space unless ref $color_space;
71 86         220 my $tuple = $self->shaped( $color_space->name, $range_def, $precision_def, $raw );
72 86 50       195 return $tuple unless ref $tuple;
73 86         199 return $color_space->format( $tuple, $format_name, $suffix_def );
74             }
75 69     69 0 21361 sub name { $_[0]->{'color_name'} }
76              
77             sub is_in_gamut {
78 15     15 0 39 my ($self, $space_name) = @_;
79 15 100 100     82 $space_name = $self->{'source_space_name'} if not defined $space_name and $self->{'source_space_name'};
80 15         37 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name ); # default to RGB
81 15 50       44 return 0 unless ref $color_space;
82 15         44 my $tuple = $self->normalized( $space_name );
83 15 50       36 return 0 unless ref $tuple;
84 15         71 return $color_space->is_in_linear_bounds( $tuple, 'normal' );
85             }
86              
87             1;