Library/Color.pm

filter: Statementstmt 57.3% 39 / 68 Branchbran 9.0% 2 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 8 / 12 Podpod 100% 6 / 6 totaltotal 46.6% 55 / 118 CC 14 SCAR 29.2
CC
14
Cov
43%
parse_rgb_color
56.0
new
6.6
rgb2hex
6.0
CRAP 18.6
↑ Summary Library/Dictionary.pm »
1 package Color::Library::Color;
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 ▶use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 __PACKAGE__->mk_accessors(qw/_id _name _title _dictionary /);
9 2 __PACKAGE__->mk_accessors(qw/_rgb _html _value _hex/);
10  
11 use overload
12 2 ▶ '""' => \&html,
Subroutine BEGIN: called CC=1 SCAR=0.0
13 fallback => 1,
14 2 ;
15  
16 sub rgb;
17 sub rgb2hex;
18 sub rgb2value;
19 sub value2rgb($);
20 sub parse_rgb_color;
21 sub integer2rgb($);
22  
23 =head1 NAME
24  
25 Color::Library::Color - Color entry for a Color::Library color dictionary
26  
27 =head1 METHODS
28  
29 =over 4
30  
31 =item $id = $color->id
32  
33 Returns the id of the color
34  
35 A color id is in the format of <dictionary_id:color_name>, e.g.
36  
37 svg:aliceblue
38 x11:bisque2
39 nbs-iscc-f:chromeyellow.66
40 vaccc:darkspringyellow
41  
42 =item $name = $color->name
43  
44 Returns the name of the color, e.g.
45  
46 aliceblue
47 bisque2
48 chromeyellow
49 darkspringyellow
50  
51 =item $title = $color->title
52  
53 Returns the title of the color, e.g.
54  
55 aliceblue
56 bisque2
57 chrome yellow
58 Dark Spring-Yellow
59  
60 =item $dictionary = $color->dictionary
61  
62 Returns the Color::Library::Dictionary object that the color belongs to
63  
64 =item $hex = $color->hex
65  
66 Returns the hex value of the color, e.g.
67  
68 ff08ff
69 eed5b7
70 eaa221
71 669900
72  
73 Note that $hex does NOT include the leading #, for that use $color->html, $color->css, or $color->svg
74  
75 =item $html = $color->html
76  
77 =item $css = $color->css
78  
79 =item $svg = $color->svg
80  
81 Returns the hex value of the color with a leading #, suitable for use in HTML, CSS, or SVG documents, e.g.
82  
83 #ff08ff
84 #eed5b7
85 #eaa221
86 #669900
87  
88 =cut
89  
90 =item $value = $color->value
91  
92 Returns the numeric value of the color, e.g.
93  
94 15792383
95 15652279
96 15376929
97 6723840
98  
99 =cut
100  
101 2 for my $method (qw/id name title dictionary html value hex/) {
102 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 14 my $accessor = "_$method";
104 89786 ▶ *$method = sub { return $_[0]->$accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
105 }
106 2 *css = \&html;
107 2 *svg = \&html;
108  
109 =item ($r, $g, $b) = $color->rgb
110  
111 Returns r, g, and b values of the color as a 3 element list (list context), e.g.
112  
113 (240, 248, 255)
114  
115 =item $rgb = $color->rgb
116  
117 Returns r, g, and b values of the color in a 3 element array (scalar context), e.g.
118  
119 [ 240, 248, 255 ]
120  
121 =cut
122  
123 2 ▶sub rgb {
Subroutine rgb: called CC=2 SCAR=8.1
Pod documented
124 0 ▶ return wantarray ? @{ $_[0]->_rgb } : [ @{ $_[0]->_rgb } ]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
125 }
126  
127 =item $color = Color::Library::Color->new( id => $id, name => $name, title => $title, value => $value )
128  
129 =item $color = Color::Library::Color->new( { id => $id, name => $name, title => $title, value => $value } )
130  
131 =item $color = Color::Library::Color->new( [[ $id, $name, $title, $rgb, $hex, $value ]] )
132  
133 Returns a new Color::Library::Color object representing the specified color
134  
135 You probably don't want/need to call this yourself
136  
137 =cut
138  
139 # FUTURE Note that $value may be a numeric value, a hex value, or a 3 element r-g-b array
140  
141 12801 ▶sub new {
Subroutine new: called CC=4 SCAR=18.9
Pod documented
142 12801 my $self = bless {}, shift;
143 12801 ▶ if (ref $_[0] eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 12801 0 12801
144 12801 my ($id, $name, $title, $rgb, $hex, $value) = @{ shift() };
145 12801 $self->_id($id);
146 12801 $self->_name($name);
147 12801 $self->_title($title);
148 12801 $self->_rgb($rgb);
149 12801 $self->_hex($hex);
150 12801 $self->_html("#" . $hex);
151 12801 $self->_value($value);
152 12801 $self->_dictionary(shift);
153 }
154 else {
155 0 ▶ local %_ = ref $_[0] eq "HASH" ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
156 0 $self->_id($_{id});
157 0 $self->_name($_{name});
158 0 $self->_title($_{title});
159 0 $self->_dictionary($_{dictionary});
160  
161 0 ▶ my ($r, $g, $b) = parse_rgb_color(ref $_{value} eq "ARRAY" ? @{ $_{value} } : $_{value});
Branch: 1 branch
BranchTrueFalseTotal
ref $_{'value'} eq 'ARRAY' ? : 0 0 0
162  
163 0 my $rgb = $self->_rgb([ $r, $g, $b ]);
164 0 my $hex = $self->_hex(rgb2hex $rgb);
165 0 $self->_html("#" . $hex);
166 0 $self->_value(rgb2value $rgb);
167 }
168 12801 return $self;
169 }
170  
171 =back
172  
173 =head2 FUNCTIONS
174  
175 =over 4
176  
177 =item $hex = Color::Library::Color::rgb2hex( $rgb )
178  
179 =item $hex = Color::Library::Color::rgb2hex( $r, $g, $b )
180  
181 Converts an rgb value to its hex representation
182  
183 =cut
184  
185 0 ▶sub rgb2hex {
Subroutine rgb2hex: not called CC=2 SCAR=17.9
Pod documented
186 0 ▶ return ref $_[0] eq "ARRAY" ?
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
187 sprintf("%02lx%02lx%02lx", $_[0][0], $_[0][1], $_[0][2]) :
188 sprintf("%02lx%02lx%02lx", $_[0], $_[1], $_[2]);
189 }
190  
191 =item $value = Color::Library::Color::rgb2value( $rgb )
192  
193 =item $value = Color::Library::Color::rgb2value( $r, $g, $b )
194  
195 Converts an rgb value to its numeric representation
196  
197 =cut
198  
199 0 ▶sub rgb2value {
Subroutine rgb2value: not called CC=2 SCAR=17.9
Pod documented
200 0 ▶ my ($r, $g, $b) = ref $_[0] eq "ARRAY" ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
201 0 return $b + ($g << 8) + ($r << 16);
202 }
203  
204 =item $rgb = Color::Library::Color::value2rgb( $value )
205  
206 =item ($r, $g, $b) = Color::Library::Color::value2rgb( $value )
207  
208 Converts a numeric color value to its rgb representation
209  
210 =cut
211  
212 0 ▶sub value2rgb($) {
Subroutine value2rgb: not called CC=2 SCAR=17.9
Pod documented
213 0 my $value = shift;
214 0 my ($r, $g, $b);
215 0 $b = ($value & 0x0000ff);
216
217 0 $g = ($value & 0x00ff00) >> 8;
218 0 $r = ($value & 0xff0000) >> 16;
219 0 ▶ return wantarray ? ($r, $g, $b) : [ $r, $g, $b ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
220 }
221  
222 =item ($r, $g, $b) = Color::Library::Color::parse_rgb_color( $hex )
223  
224 =item ($r, $g, $b) = Color::Library::Color::parse_rgb_color( $value )
225  
226 Makes a best effort to convert a hex or numeric color value to its rgb representation
227  
228 =cut
229  
230 # Partly taken from Imager/Color.pm
231 0 ▶sub parse_rgb_color {
Subroutine parse_rgb_color: not called CC=7 SCAR=40.3
Pod documented
232 0 ▶ return (@_) if @_ == 3 && ! grep /[^\d.+eE-]/, @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 3 and not grep(/[^\d.+eE-]/, @_) 0 0 0
Condition0%0/3
@_ == 3 and not grep(/[^\d.+eE-]/, @_)
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 3 and not grep(/[^\d.+eE-]/, @_)
@_ == 3 not grep(/[^\d.+eE-]/, @_)
Truth table0%0/3
A and not(B)
A= @_ == 3B= not grep(/[^\d.+eE-]/, @_)
ABresult
0X0
100
111
233 0 ▶ if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])$/i) 0 0 0
234 0 return (hex($1), hex($2), hex($3));
235 }
236 0 ▶ if ($_[0] =~ /^\#?([\da-f])([\da-f])([\da-f])$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\#?([\da-f])([\da-f])([\da-f])$/i) 0 0 0
237 0 return (hex($1) * 17, hex($2) * 17, hex($3) * 17);
238 }
239 0 ▶ return value2rgb $_[0] if 1 == @_ && $_[0] =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == @_ and $_[0] =~ /^\d+$/ 0 0 0
Condition0%0/3
1 == @_ and $_[0] =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @_ and $_[0] =~ /^\d+$/
1 == @_ $_[0] =~ /^\d+$/
Truth table0%0/3
A and B
A= 1 == @_B= $_[0] =~ /^\d+$/
ABresult
0X0
100
111
240 }
241  
242 2 1;
243  
244 __END__
↑ Summary Library/Dictionary.pm »