File Coverage

lib/Graphics/Toolkit/Color/Values.pm
Criterion Covered Total %
statement 64 64 100.0
branch 18 26 69.2
condition n/a
subroutine 12 12 100.0
pod 0 7 0.0
total 94 109 86.2


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   421072 use v5.12;
  9         34  
6 9     9   33 use warnings;
  9         10  
  9         436  
7 9     9   3329 use Graphics::Toolkit::Color::Name;
  9         40  
  9         771  
8 9     9   89 use Graphics::Toolkit::Color::Space::Hub;
  9         16  
  9         6861  
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 138     138 0 194619 my ($pkg, $color_def) = @_;
15 138 50       319 return "Can not create color value object without color definition!" unless defined $color_def;
16 138 100       343 if (not ref $color_def) { # try to resolve color name
17 65         152 my $rgb = Graphics::Toolkit::Color::Name::get_values( $color_def );
18 65 100       136 if (ref $rgb){
19 39         102 $rgb = $RGB->clamp( $RGB->normalize( $rgb ), 'normal' );
20 39         230 return bless { color_name => $color_def, rgb => $rgb, source_values => '', source_space_name => ''};
21             }
22             }
23 99         286 my ($values, $space_name) = Graphics::Toolkit::Color::Space::Hub::deformat( $color_def );
24 99 100       436 return "could not recognize color value format or color name: $color_def" unless ref $values;
25 56         186 new_from_tuple( '', $values, $space_name);
26             }
27             sub new_from_tuple { #
28 492     492 0 171794 my ($pkg, $values, $space_name, $range_def) = @_;
29 492         837 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
30 492 50       770 return $color_space unless ref $color_space;
31 492 100       910 return "Need ARRAY of ".$color_space->axis_count." ".$color_space->name." values as first argument!"
32             unless $color_space->is_value_tuple( $values );
33             # $values = $color_space->clamp( $values, $range_def);
34 491         912 $values = $color_space->normalize( $values, $range_def );
35 491         926 $values = $color_space->clamp( $values, 'normal');
36 491         950 _new_from_normal_tuple($values, $color_space);
37             }
38             sub _new_from_normal_tuple { #
39 491     491   613 my ($values, $color_space) = @_;
40 491         575 my $source_values = '';
41 491         600 my $source_space_name = '';
42 491 100       866 if ($color_space->name ne $RGB->name){
43 126         205 $source_values = $values;
44 126         213 $source_space_name = $color_space->name;
45 126         241 $values = Graphics::Toolkit::Color::Space::Hub::deconvert( $color_space->name, $values, 'normal' );
46             }
47 491         1040 $values = $RGB->clamp( $values, 'normal' );
48 491         1044 my $nv = $RGB->round( $RGB->denormalize( $values ) );
49 491         1160 my $name = Graphics::Toolkit::Color::Name::from_values( $RGB->round( $RGB->denormalize( $values ) ) );
50 491         4018 bless { rgb => $values, source_values => $source_values, source_space_name => $source_space_name, color_name => $name };
51             }
52              
53             sub is_in_gamut {
54 4     4 0 12 my ($color_def, $range_def) = @_;
55 4         22 my $rgb = Graphics::Toolkit::Color::Name::get_values( $color_def );
56 4 50       16 return 1 if ref $rgb;
57 4         29 my ($values, $space_name) = Graphics::Toolkit::Color::Space::Hub::deformat( $color_def );
58 4         15 my $color_space = Graphics::Toolkit::Color::Space::Hub::get_space( $space_name );
59 4 50       14 return 0 unless ref $color_space;
60 4         17 return $color_space->is_in_bounds( $values ); # , $range_def
61             }
62              
63             #### getter ############################################################
64             sub normalized { # normalized (0..1) value tuple in any color space
65 398     398 0 8353 my ($self, $space_name) = @_;
66             Graphics::Toolkit::Color::Space::Hub::convert(
67 398         1086 $self->{'rgb'}, $space_name, 'normal', $self->{'source_space_name'}, $self->{'source_values'},
68             );
69             }
70             sub shaped { # in any color space, range and precision
71 337     337 0 56762 my ($self, $space_name, $range_def, $precision_def) = @_;
72 337         646 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
73 337 50       585 return $color_space unless ref $color_space;
74 337         606 my $values = $self->normalized( $color_space->name );
75 337 50       574 return $values if not ref $values;
76 337         651 $values = $color_space->denormalize( $values, $range_def );
77 337         762 $values = $color_space->clamp( $values, $range_def );
78 337         594 $values = $color_space->round( $values, $precision_def );
79 337         767 return $values;
80             }
81             sub formatted { # in shape values in any format # _ -- ~space, @~|~format, @~|~range, @~|~suffix
82 81     81 0 44678 my ($self, $space_name, $format_name, $suffix_def, $range_def, $precision_def) = @_;
83 81         262 my $color_space = Graphics::Toolkit::Color::Space::Hub::try_get_space( $space_name );
84 81 50       173 return $color_space unless ref $color_space;
85 81         264 my $values = $self->shaped( $color_space->name, $range_def, $precision_def );
86 81 50       155 return $values unless ref $values;
87 81         188 return $color_space->format( $values, $format_name, $suffix_def );
88             }
89 69     69 0 37204 sub name { $_[0]->{'color_name'} }
90              
91              
92             1;