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   808 use v5.12;
  7         21  
2 7     7   30 use warnings;
  7         12  
  7         285  
3              
4             # check, convert and measure color values
5              
6             package Graphics::Toolkit::Color::Space::Hub;
7 7     7   40 use Carp;
  7         8  
  7         7673  
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 13864 sub get_space { $space_obj{ uc $_[0] } if exists $space_obj{ uc $_[0] } }
13 1202 100 100 1202 1 2705 sub is_space { (defined $_[0] and ref get_space($_[0])) ? 1 : 0 }
14 232     232 1 434 sub base_space { $space_obj{$base_package} }
15 265     265 1 763 sub space_names { @space_packages }
16              
17             ########################################################################
18              
19             sub check_space_name {
20 1298 100   1298 0 2390 return unless defined $_[0];
21 1148         3504 my $error = "called with unknown color space name '$_[0]', please try one of: " . join (', ', @space_packages);
22 1148 100       1787 is_space( $_[0] ) ? 0 : carp $error;
23             }
24             sub _check_values_and_space {
25 605     605   915 my ($sub_name, $values, $space_name) = @_;
26 605   66     1068 $space_name //= $base_package;
27 605 100       871 check_space_name( $space_name ) and return;
28 603         990 my $space = get_space($space_name);
29 603 100       1289 $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 5376 my ($value_hash) = @_;
37 22 100       62 return unless ref $value_hash eq 'HASH';
38 21         38 for my $space_name (space_names()) {
39 80         123 my $color_space = get_space( $space_name );
40 80         167 my $pos_hash = $color_space->basis->deformat_partial_hash( $value_hash );
41 80 100       237 return $pos_hash, $color_space->name if ref $pos_hash eq 'HASH';
42             }
43 4         17 return undef;
44             }
45              
46             sub deformat { # convert from any format into list of values of any space
47 244     244 1 22153 my ($formated_values) = @_;
48 244         409 for my $space_name (space_names()) {
49 686         1043 my $color_space = get_space( $space_name );
50 686         1451 my @val = $color_space->deformat( $formated_values );
51 686 100       2025 return \@val, $space_name if defined $val[0];
52             }
53             }
54              
55             sub format { # @tuple --> % | % |~ ...
56 590     590 1 9488 my ($values, $space_name, $format_name) = @_;
57              
58 590         949 my $space = _check_values_and_space( 'format', $values, $space_name );
59 590 100       2995 return unless ref $space;
60 587   100     1744 my @values = $space->format( $values, $format_name // 'list' );
61 587 50       1111 return @values, carp "got unknown format name: '$format_name'" unless defined $values[0];
62 587 100       3348 return @values == 1 ? $values[0] : @values;
63             }
64              
65             sub deconvert { # @... --> @RGB (base color space) # normalized values only
66 1     1 1 2179 my ($values, $space_name) = @_;
67 1         3 my $space = _check_values_and_space( 'deconvert', $values, $space_name );
68 1 50       4 return unless ref $space;
69 1         4 my @values = $space->clamp( $values, 'normal');
70 1 50       3 return @values if $space->name eq base_space->name;
71 1         4 $space->convert( \@values, $base_package);
72             }
73              
74             sub convert { # @RGB --> @... # normalized values only
75 2     2 1 2961 my ($values, $space_name) = @_;
76 2         6 my $space = _check_values_and_space( 'convert', $values, $space_name );
77 2 50       7 return unless ref $space;
78 2         4 my @values = base_space->clamp( $values, 'normal');
79 2 100       7 return @values if $space->name eq base_space->name;
80 1         3 $space->deconvert( \@values, $base_package);
81             }
82              
83             sub denormalize { # result clamped, alway in space
84 5     5 1 3636 my ($values, $space_name, $range) = @_;
85 5         13 my $space = _check_values_and_space( 'denormalize', $values, $space_name );
86 5 100       1656 return unless ref $space;
87 2         6 my @values = $space->clamp($values, 'normal');
88 2         7 $space->denormalize( \@values, $range);
89             }
90              
91             sub normalize {
92 7     7 1 5220 my ($values, $space_name, $range) = @_;
93 7         19 my $space = _check_values_and_space( 'normalize', $values, $space_name );
94 7 100       1772 return unless ref $space;
95 4         13 my @values = $space->clamp($values, $range);
96 4 100       479 return unless defined $values[0];
97 3         9 $space->normalize( $values, $range);
98             }
99              
100              
101             1;
102              
103             __END__