File Coverage

lib/Graphics/Toolkit/Color/Space/Util.pm
Criterion Covered Total %
statement 33 33 100.0
branch 15 18 83.3
condition 7 9 77.7
subroutine 11 11 100.0
pod 0 8 0.0
total 66 79 83.5


line stmt bran cond sub pod time code
1              
2             # utilities for color value calculation
3              
4             package Graphics::Toolkit::Color::Space::Util;
5 37     37   2526180 use v5.12;
  37         147  
6 37     37   196 use warnings;
  37         61  
  37         2290  
7 37     37   188 use Exporter 'import';
  37         59  
  37         22200  
8             our @EXPORT_OK = qw/round_int round_decimals mod_real min max uniq mult_matrix_vector_3 is_nr/;
9             our %EXPORT_TAGS = (all => [@EXPORT_OK]);
10              
11             #### lists #############################################################
12             sub max {
13 104     104 0 169 my $v = shift;
14 104 50       197 for (@_) { next unless defined $_; $v = $_ if $v < $_ }
  207 100       385  
  207         412  
15 104         292 return $v;
16             }
17              
18             sub min {
19 95     95 0 147 my $v = shift;
20 95 50       151 for (@_) { next unless defined $_; $v = $_ if $v > $_ }
  188 100       332  
  188         351  
21 95         178 return $v;
22             }
23              
24             sub uniq {
25 541 100   541 0 5934 return undef unless @_;
26 540         720 my %seen = ();
27 540         687 grep {not $seen{$_}++} @_;
  1355         3392  
28             }
29              
30             #### basic math ########################################################
31             my $half = 0.50000000000008;
32             my $tolerance = 0.00000000000008;
33             sub round_int {
34 4814 100   4814 0 16059 $_[0] >= 0 ? int ($_[0] + $half)
35             : int ($_[0] - $half)
36             }
37              
38             sub round_decimals {
39 4806     4806 0 168607 my ($nr, $precision) = @_;
40 4806 100 100     12436 return round_int( $nr ) unless defined $precision and $precision;
41 781         1338 $precision = 10 ** $precision;
42 781         1835 return round_int( $nr * $precision ) / $precision;
43             }
44              
45              
46             sub mod_real { # real value modulo
47 98 100 100 98 0 303 return 0 unless defined $_[1] and $_[1];
48 95         250 return $_[0] - (int($_[0] / $_[1]) * $_[1]);
49             }
50              
51 3197     3197 0 14923 sub is_nr { $_[0] =~ /^\-?\d+(\.\d+)?$/ }
52              
53             #### color computation #################################################
54             sub mult_matrix_vector_3 {
55 89     89 0 7732 my ($mat, $v1, $v2, $v3) = @_;
56 89 50 33     472 return unless ref $mat eq 'ARRAY' and defined $v3;
57 89         710 return ($v1 * $mat->[0][0] + $v2 * $mat->[0][1] + $v3 * $mat->[0][2]) ,
58             ($v1 * $mat->[1][0] + $v2 * $mat->[1][1] + $v3 * $mat->[1][2]) ,
59             ($v1 * $mat->[2][0] + $v2 * $mat->[2][1] + $v3 * $mat->[2][2]) ;
60             }
61              
62              
63             1;