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   3124027 use v5.12;
  37         183  
6 37     37   213 use warnings;
  37         83  
  37         2543  
7 37     37   246 use Exporter 'import';
  37         86  
  37         26666  
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 179 my $v = shift;
14 104 50       247 for (@_) { next unless defined $_; $v = $_ if $v < $_ }
  207 100       451  
  207         514  
15 104         350 return $v;
16             }
17              
18             sub min {
19 95     95 0 243 my $v = shift;
20 95 50       183 for (@_) { next unless defined $_; $v = $_ if $v > $_ }
  188 100       5981  
  188         432  
21 95         245 return $v;
22             }
23              
24             sub uniq {
25 526 100   526 0 5820 return undef unless @_;
26 525         1001 my %seen = ();
27 525         1021 grep {not $seen{$_}++} @_;
  1333         4534  
28             }
29              
30             #### basic math ########################################################
31             my $half = 0.50000000000008;
32             my $tolerance = 0.00000000000008;
33             sub round_int {
34 3266 100   3266 0 18513 $_[0] >= 0 ? int ($_[0] + $half)
35             : int ($_[0] - $half)
36             }
37              
38             sub round_decimals {
39 3258     3258 0 228628 my ($nr, $precision) = @_;
40 3258 100 100     13781 return round_int( $nr ) unless defined $precision and $precision;
41 781         1671 $precision = 10 ** $precision;
42 781         2205 return round_int( $nr * $precision ) / $precision;
43             }
44              
45              
46             sub mod_real { # real value modulo
47 97 100 100 97 0 412 return 0 unless defined $_[1] and $_[1];
48 94         395 return $_[0] - (int($_[0] / $_[1]) * $_[1]);
49             }
50              
51 2876     2876 0 41558 sub is_nr { $_[0] =~ /^\-?\d+(\.\d+)?$/ }
52              
53             #### color computation #################################################
54             sub mult_matrix_vector_3 {
55 89     89 0 5464 my ($mat, $v1, $v2, $v3) = @_;
56 89 50 33     829 return unless ref $mat eq 'ARRAY' and defined $v3;
57 89         846 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;