blib/lib/Number/RGB.pm

filter: Statementstmt 98.1% 103 / 105 Branchbran 88.4% 23 / 26 Conditioncond 73.9% 17 / 23 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 94.7% 36 / 38 Podpod 100% 7 / 7 totaltotal 90.5% 192 / 212 CC 22 SCAR 12.0
CC
22
Cov
93%
new_from_guess
8.1
_op_math
7.0
new
6.0
CRAP 3.3
↑ Summary
1 package Number::RGB;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.001001'; # VERSION
7  
8 2 ▶use vars qw[$CONSTRUCTOR_SPEC];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util qw[looks_like_number];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Params::Validate qw[:all];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use base qw[Class::Accessor::Fast];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Attribute::Handlers 0.99;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @CARP_NOT = ('Attribute::Handlers', __PACKAGE__);
15 2 $Carp::Internal{'attributes'}++; # no idea why doesn't work in @CARP_NOT
16  
17 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
18 1 my $class = shift;
19 1 my $caller = (caller)[0];
20 1 ▶ eval qq[
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
21 package $caller;
22 use Attribute::Handlers;
23 sub RGB :ATTR(RAWDATA) { goto &$class\::RGB }
24 package $class;
25 ];
26 }
27  
28 2 ▶use overload fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
29 '""' => \&as_string,
30 22 ▶ '+' => sub { shift->_op_math('+', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 4 ▶ '-' => sub { shift->_op_math('-', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 3 ▶ '*' => sub { shift->_op_math('*', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 3 ▶ '/' => sub { shift->_op_math('/', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 0 ▶ '%' => sub { shift->_op_math('%', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 ▶ '**' => sub { shift->_op_math('**', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 2 ▶ '<<' => sub { shift->_op_math('<<', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 ▶ '>>' => sub { shift->_op_math('>>', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 2 ▶ '&' => sub { shift->_op_math('&', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 2 ▶ '^' => sub { shift->_op_math('^', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 ▶ '|' => sub { shift->_op_math('|', @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 1617 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
43 1617 my $class = shift;
44 1617 my %params = validate( @_, $CONSTRUCTOR_SPEC );
45 1591 ▶ croak "$class->new() requires parameters" unless keys %params;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %params 1 1590 1591
46  
47 1590 my %rgb;
48 1590 ▶ if ( defined $params{rgb} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $params{'rgb'}) { } 802 788 1590
elsif (defined $params{'rgb_number'}) { } 775 13 788
elsif (defined $params{'hex'}) { } 13 0 13
49 802 @rgb{qw[r g b]} = @{$params{rgb}};
50 } elsif ( defined $params{rgb_number} ) {
51 775 return $class->new(rgb => [($params{rgb_number})x3]);
52 } elsif ( defined $params{hex} ) {
53 13 my $hex = $params{hex};
54 13 $hex =~ s/^#//;
55 13 ▶ $hex =~ s/(.)/$1$1/g if length($hex) == 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $hex == 3 9 4 13
56 13 @rgb{qw[r g b]} = map hex, $hex =~ /(.{2})/g;
57 }
58  
59 815 $class->SUPER::new(\%rgb);
60 }
61  
62 2 __PACKAGE__->mk_accessors( qw[r g b] );
63  
64 1 ▶sub rgb { [ map $_[0]->$_, qw[r g b] ] }
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
65 2 ▶sub hex { '#' . join '', map { substr sprintf('0%x',$_[0]->$_), -2 } qw[r g b] }
Subroutine hex: called CC=1 SCAR=0.0
Pod documented
66 1 ▶sub hex_uc { uc shift->hex }
Subroutine hex_uc: called CC=1 SCAR=0.0
Pod documented
67 808 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
68 808 join ',', map $_[0]->$_, qw[r g b]
69 }
70  
71 22 ▶sub _op_math {
Subroutine _op_math: called CC=7 SCAR=19.5
72 22 my ($self,$op, $other, $reversed) = @_;
73 ref($self)->new(rgb => [
74 map {
75 22 my $x = $self->$_;
76 66 ▶ my $y = ref($other) && overload::Overloaded($other) ? $other->$_ : $other;
Branch: 1 branch
BranchTrueFalseTotal
ref $other && overload::Overloaded($other) ? : 30 36 66
Condition66%2/3
ref $other and overload::Overloaded($other)
!ll&&!rl&&r
36030
MC/DC50%1/2
ref $other and overload::Overloaded($other)
ref $other overload::Overloaded($other)
Truth table66%2/3
A and B
A= ref $otherB= overload::Overloaded($other)
ABresult
0X0
100
111
77 66 ▶ my $ans = eval ($reversed ? "$y $op $x" : "$x $op $y");
Branch: 1 branch
BranchTrueFalseTotal
$reversed ? : 3 63 66
78 66 ▶ $ans = sprintf '%.0f', $ans||0;
Condition100%2/2
$ans || 0
l!l
588
MC/DC100%1/1
$ans || 0
$ans
Truth table100%2/2
A
A= $ans
Aresult
11
00
79 66 ▶ $ans = 0 if $ans < 0; $ans = 255 if $ans > 255;
Branch: 2 branches
BranchTrueFalseTotal
if $ans < 0 6 60 66
if $ans > 255 4 62 66
80 66 $ans;
81 } qw[r g b]
82 ] );
83 }
84  
85 788 ▶sub new_from_guess {
Subroutine new_from_guess: called CC=8 SCAR=20.9
Pod documented
86 788 my ($class, $value) = @_;
87 788 ▶ $value = [ $value =~ /\d+/g ] if $value =~ /,/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /,/ 5 783 788
88 788 ▶ my $is_single_rgb = looks_like_number($value) && $value>=0 && $value<=255;
Condition83%5/6
&looks_like_number($value) && $value >= 0 && $value <= 255
!ll&&!rl&&r
123773
&looks_like_number($value) and $value >= 0
!ll&&!rl&&r
120776
MC/DC66%2/3
&looks_like_number($value) && $value >= 0 && $value <= 255
&looks_like_number($value) $value >= 0 $value <= 255
Truth table75%3/4
A and B && C
A= &looks_like_number($value)B= $value >= 0C= $value <= 255
ABCresult
0XX0
10X0
1100
1111
89  
90 788 foreach my $param ( keys %{$CONSTRUCTOR_SPEC} ) {
91 810 ▶ next if $param eq 'hex' and $is_single_rgb;
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "hex" and $is_single_rgb 0 810 810
Condition66%2/3
$param eq "hex" and $is_single_rgb
!ll&&!rl&&r
795150
MC/DC0%0/2
$param eq "hex" and $is_single_rgb
$param eq "hex" $is_single_rgb
Truth table66%2/3
A and B
A= $param eq "hex"B= $is_single_rgb
ABresult
0X0
100
111
92 810 my $self = eval { $class->new($param => $value) };
93 810 ▶ return $self if defined $self;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self 785 25 810
94 }
95 3 croak q{couldn't guess value type};
96 }
97  
98 20 ▶sub RGB :ATTR(RAWDATA) {
Subroutine RGB: called CC=1 SCAR=0.0
Pod documented
99 20 my ($var, $data) = @_[2,4];
100 20 $$var = __PACKAGE__->new_from_guess($data);
101 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 $CONSTRUCTOR_SPEC = {
104 rgb => {
105 type => ARRAYREF,
106 optional => 1,
107 callbacks => {
108 802 ▶ 'three elements' => sub { 3 == @{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 803 ▶ 'only digits' => sub { 0 == grep /\D/, @{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
110 803 ▶ 'between 0 and 255' => sub { 3 == grep { $_ >= 0 && $_ <= 255 } @{$_[0]} },
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition66%2/3
$_ >= 0 && $_ <= 255
!ll&&!rl&&r
032406
MC/DC50%1/2
$_ >= 0 && $_ <= 255
$_ >= 0 $_ <= 255
Truth table66%2/3
A && B
A= $_ >= 0B= $_ <= 255
ABresult
0X0
100
111
111 },
112 },
113 rgb_number => {
114 type => SCALAR,
115 optional => 1,
116 callbacks => {
117 785 ▶ 'only digits' => sub { $_[0] !~ /\D/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 778 ▶ 'between 0 and 255' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
119 778 ▶ looks_like_number($_[0]) and $_[0] >= 0 && $_[0] <= 255
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[0]) and ($_[0] >= 0 and $_[0] <= 255) 778 0 778
Condition66%4/6
&looks_like_number($_[0]) && ($_[0] >= 0 && $_[0] <= 255)
!ll&&!rl&&r
03775
$_[0] >= 0 and $_[0] <= 255
!ll&&!rl&&r
03775
MC/DC33%1/3
&looks_like_number($_[0]) && ($_[0] >= 0 && $_[0] <= 255)
&looks_like_number($_[0]) $_[0] >= 0 $_[0] <= 255
Truth table50%2/4
A && B and C
A= &looks_like_number($_[0])B= $_[0] >= 0C= $_[0] <= 255
ABCresult
0XX0
10X0
1100
1111
120 },
121 },
122 },
123 hex => {
124 type => SCALAR,
125 optional => 1,
126 callbacks => {
127 15 ▶ 'hex format' => sub { $_[0] =~ /^#?(?:[\da-f]{3}|[\da-f]{6})$/i },
Subroutine __ANON__: called CC=1 SCAR=0.0
128 },
129 }
130 2 };
131  
132 2 1;
133  
134 __END__
↑ Summary