File Coverage

lib/Graphics/Toolkit/Color/Space/Hub.pm
Criterion Covered Total %
statement 61 61 100.0
branch 34 38 89.4
condition 7 8 87.5
subroutine 16 16 100.0
pod 11 12 91.6
total 129 135 95.5


line stmt bran cond sub pod time code
1 7     7   793 use v5.12;
  7         20  
2 7     7   33 use warnings;
  7         11  
  7         355  
3              
4             # check, convert and measure color values
5              
6             package Graphics::Toolkit::Color::Space::Hub;
7 7     7   39 use Carp;
  7         14  
  7         7298  
8             our $base_package = 'RGB';
9             my @space_packages = ($base_package, qw/CMY CMYK HSL HSV HSB HWB YIQ /); # search order # HCL LUV Ncol ?XYZ LAB
10             my %space_obj = map { $_ => require "Graphics/Toolkit/Color/Space/Instance/$_.pm" } @space_packages;
11              
12 3489 100   3489 1 13711 sub get_space { $space_obj{ uc $_[0] } if exists $space_obj{ uc $_[0] } }
13 1202 100 100 1202 1 2415 sub is_space { (defined $_[0] and ref get_space($_[0])) ? 1 : 0 }
14 232     232 1 422 sub base_space { $space_obj{$base_package} }
15 265     265 1 722 sub space_names { @space_packages }
16              
17             ########################################################################
18              
19             sub check_space_name {
20 1298 100   1298 0 2309 return unless defined $_[0];
21 1148         3225 my $error = "called with unknown color space name '$_[0]', please try one of: " . join (', ', @space_packages);
22 1148 100       1711 is_space( $_[0] ) ? 0 : carp $error;
23             }
24             sub _check_values_and_space {
25 605     605   890 my ($sub_name, $values, $space_name) = @_;
26 605   66     999 $space_name //= $base_package;
27 605 100       779 check_space_name( $space_name ) and return;
28 603         901 my $space = get_space($space_name);
29 603 100       1207 $space->is_array( $values ) ? $space
30             : carp 'need an ARRAY ref with '.$space->dimensions." $space_name values as first argument of $sub_name";
31             }
32              
33             ########################################################################
34              
35             sub partial_hash_deformat { # convert partial hash into
36 22     22 1 6880 my ($value_hash) = @_;
37 22 100       94 return unless ref $value_hash eq 'HASH';
38 21         46 for my $space_name (space_names()) {
39 80         107 my $color_space = get_space( $space_name );
40 80         154 my $pos_hash = $color_space->basis->deformat_partial_hash( $value_hash );
41 80 100       187 return $pos_hash, $color_space->name if ref $pos_hash eq 'HASH';
42             }
43 4         12 return undef;
44             }
45              
46             sub deformat { # convert from any format into list of values of any space
47 244     244 1 28170 my ($formated_values) = @_;
48 244         396 for my $space_name (space_names()) {
49 686         990 my $color_space = get_space( $space_name );
50 686         1395 my @val = $color_space->deformat( $formated_values );
51 686 100       1891 return \@val, $space_name if defined $val[0];
52             }
53             }
54              
55             sub format { # @tuple --> % | % |~ ...
56 590     590 1 11648 my ($values, $space_name, $format_name) = @_;
57              
58 590         928 my $space = _check_values_and_space( 'format', $values, $space_name );
59 590 100       3130 return unless ref $space;
60 587   100     1723 my @values = $space->format( $values, $format_name // 'list' );
61 587 50       1005 return @values, carp "got unknown format name: '$format_name'" unless defined $values[0];
62 587 100       2920 return @values == 1 ? $values[0] : @values;
63             }
64              
65             sub deconvert { # @... --> @RGB (base color space) # normalized values only
66 1     1 1 2692 my ($values, $space_name) = @_;
67 1         6 my $space = _check_values_and_space( 'deconvert', $values, $space_name );
68 1 50       5 return unless ref $space;
69 1         4 my @values = $space->clamp( $values, 'normal');
70 1 50       4 return @values if $space->name eq base_space->name;
71 1         5 $space->convert( \@values, $base_package);
72             }
73              
74             sub convert { # @RGB --> @... # normalized values only
75 2     2 1 3858 my ($values, $space_name) = @_;
76 2         7 my $space = _check_values_and_space( 'convert', $values, $space_name );
77 2 50       6 return unless ref $space;
78 2         6 my @values = base_space->clamp( $values, 'normal');
79 2 100       7 return @values if $space->name eq base_space->name;
80 1         4 $space->deconvert( \@values, $base_package);
81             }
82              
83             sub denormalize { # result clamped, alway in space
84 5     5 1 4604 my ($values, $space_name, $range) = @_;
85 5         14 my $space = _check_values_and_space( 'denormalize', $values, $space_name );
86 5 100       1638 return unless ref $space;
87 2         7 my @values = $space->clamp($values, 'normal');
88 2         11 $space->denormalize( \@values, $range);
89             }
90              
91             sub normalize {
92 7     7 1 6465 my ($values, $space_name, $range) = @_;
93 7         19 my $space = _check_values_and_space( 'normalize', $values, $space_name );
94 7 100       1814 return unless ref $space;
95 4         12 my @values = $space->clamp($values, $range);
96 4 100       475 return unless defined $values[0];
97 3         9 $space->normalize( $values, $range);
98             }
99              
100              
101             1;
102              
103             __END__