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   3144213 use v5.12;
  37         134  
6 37     37   198 use warnings;
  37         85  
  37         2737  
7 37     37   271 use Exporter 'import';
  37         70  
  37         26404  
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 197 my $v = shift;
14 104 50       250 for (@_) { next unless defined $_; $v = $_ if $v < $_ }
  207 100       457  
  207         496  
15 104         362 return $v;
16             }
17              
18             sub min {
19 95     95 0 186 my $v = shift;
20 95 50       227 for (@_) { next unless defined $_; $v = $_ if $v > $_ }
  188 100       357  
  188         467  
21 95         280 return $v;
22             }
23              
24             sub uniq {
25 525 100   525 0 8811 return undef unless @_;
26 524         883 my %seen = ();
27 524         942 grep {not $seen{$_}++} @_;
  1335         4981  
28             }
29              
30             #### basic math ########################################################
31             my $half = 0.50000000000008;
32             my $tolerance = 0.00000000000008;
33             sub round_int {
34 3245 100   3245 0 17786 $_[0] >= 0 ? int ($_[0] + $half)
35             : int ($_[0] - $half)
36             }
37              
38             sub round_decimals {
39 3237     3237 0 228840 my ($nr, $precision) = @_;
40 3237 100 100     12321 return round_int( $nr ) unless defined $precision and $precision;
41 781         1628 $precision = 10 ** $precision;
42 781         2228 return round_int( $nr * $precision ) / $precision;
43             }
44              
45              
46             sub mod_real { # real value modulo
47 97 100 100 97 0 453 return 0 unless defined $_[1] and $_[1];
48 94         367 return $_[0] - (int($_[0] / $_[1]) * $_[1]);
49             }
50              
51 2867     2867 0 28516 sub is_nr { $_[0] =~ /^\-?\d+(\.\d+)?$/ }
52              
53             #### color computation #################################################
54             sub mult_matrix_vector_3 {
55 89     89 0 9605 my ($mat, $v1, $v2, $v3) = @_;
56 89 50 33     529 return unless ref $mat eq 'ARRAY' and defined $v3;
57 89         797 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;