Color/RGB.pm

filter: Statementstmt 88.9% 113 / 127 Branchbran 72.4% 42 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 100% 12 / 12 totaltotal 86.1% 186 / 216 SCAR 35.2
CC
30
Cov
84%
to_hsl
11.4
to_hsv
10.4
equal_to
7.3
CRAP 33.8
↑ Summary Color/HSV.pm »
1 package Graphics::Color::RGB;
2 4 $Graphics::Color::RGB::VERSION = '0.30';
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use MooseX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 extends qw(Graphics::Color);
7  
8 # ABSTRACT: RGB color model
9  
10 4 ▶use Color::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Graphics::Color::HSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Graphics::Color::HSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use Graphics::Color::Types qw(NumberOneOrLess);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 4 has 'red' => (
18 is => 'rw',
19 isa => NumberOneOrLess,
20 default => 1,
21 alias => 'r'
22 );
23  
24  
25 4 has 'green' => (
26 is => 'rw',
27 isa => NumberOneOrLess,
28 default => 1,
29 alias => 'g'
30 );
31  
32  
33 4 has 'blue' => (
34 is => 'rw',
35 isa => NumberOneOrLess,
36 default => 1,
37 alias => 'b'
38 );
39  
40  
41 4 has 'alpha' => (
42 is => 'rw',
43 isa => NumberOneOrLess,
44 default => 1,
45 alias => 'a'
46 );
47  
48  
49 4 has 'name' => ( is => 'rw', isa => 'Str' );
50  
51  
52 sub as_string {
53 1 ▶ my ($self) = @_;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
54  
55 1 return sprintf('%0.2f,%0.2f,%0.2f,%0.2f',
56 $self->red, $self->green,
57 $self->blue, $self->alpha
58 );
59 }
60  
61  
62 sub as_integer_string {
63 1 ▶ my ($self) = @_;
Subroutine as_integer_string: called CC=1 SCAR=0.0
Pod documented
64  
65 1 return sprintf("%d, %d, %d, %0.2f",
66 $self->red * 255, $self->green * 255, $self->blue * 255, $self->alpha
67 );
68 }
69  
70  
71 sub as_css_hex {
72 1 ▶ my ($self) = @_;
Subroutine as_css_hex: called CC=1 SCAR=0.0
Pod documented
73  
74 1 return $self->as_hex_string('#');
75 }
76  
77  
78 sub as_hex_string {
79 4 ▶ my ($self, $prepend) = @_;
Subroutine as_hex_string: called CC=2 SCAR=6.9
Pod documented
80  
81 4 ▶ $prepend = '' unless defined($prepend);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prepend 2 2 4
82  
83 4 return sprintf('%s%.2x%.2x%.2x',
84 $prepend, $self->red * 255, $self->green * 255, $self->blue * 255
85 );
86 }
87  
88  
89 sub as_percent_string {
90 1 ▶ my ($self) = @_;
Subroutine as_percent_string: called CC=1 SCAR=0.0
Pod documented
91  
92 1 return sprintf("%d%%, %d%%, %d%%, %0.2f",
93 $self->red * 100, $self->green * 100, $self->blue * 100, $self->alpha
94 );
95 }
96  
97  
98 sub as_array {
99 1 ▶ my ($self) = @_;
Subroutine as_array: called CC=1 SCAR=0.0
Pod documented
100  
101 1 return ($self->red, $self->green, $self->blue);
102 }
103  
104  
105 sub as_array_with_alpha {
106 1 ▶ my ($self) = @_;
Subroutine as_array_with_alpha: called CC=1 SCAR=0.0
Pod documented
107  
108 1 return ($self->red, $self->green, $self->blue, $self->alpha);
109 }
110  
111  
112 sub equal_to {
113 2 ▶ my ($self, $other) = @_;
Subroutine equal_to: called CC=6 SCAR=19.9
Pod documented
114  
115 2 ▶ return 0 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 2 2
116  
117 2 ▶ unless($self->red == $other->red) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->red == $other->red) 0 2 2
118 0 return 0;
119 }
120 2 ▶ unless($self->green == $other->green) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->green == $other->green) 1 1 2
121 1 return 0;
122 }
123 1 ▶ unless($self->blue == $other->blue) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->blue == $other->blue) 0 1 1
124 0 return 0;
125 }
126 1 ▶ unless($self->alpha == $other->alpha) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->alpha == $other->alpha) 0 1 1
127 0 return 0;
128 }
129  
130 1 return 1;
131 }
132  
133  
134 sub from_color_library {
135 1 ▶ my ($self, $id) = @_;
Subroutine from_color_library: called CC=3 SCAR=11.8
Pod documented
136  
137 1 my $color;
138 1 ▶ if(ref($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id) { } 0 1 1
139 0 $color = $id;
140 } else {
141 1 $color = Color::Library->color($id);
142 1 ▶ unless(defined($color)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 1 1
143 0 die("Couldn't find color for '$id'!");
144 }
145 }
146  
147 1 my ($r, $g, $b) = $color->rgb;
148 1 return Graphics::Color::RGB->new(
149 red => $r / 255,
150 green => $g / 255,
151 blue => $b / 255
152 );
153 }
154  
155  
156 sub from_hex_string {
157 2 ▶ my ($self, $hex) = @_;
Subroutine from_hex_string: called CC=3 SCAR=12.0
Pod documented
158  
159 # Get rid of the leading # if it's there
160 2 $hex =~ s/^#//g;
161 2 $hex = lc($hex);
162  
163 2 my $len = length($hex);
164  
165 2 ▶ if(length($hex) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $hex == 3) 0 2 2
166 0 $hex =~ /([a-f0-9])([a-f0-9])([a-f0-9])/;
167 0 $hex = "$1$1$2$2$3$3";
168 }
169  
170 2 ▶ if(length($hex) == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $hex == 6) 2 0 2
171 2 $hex =~ /([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/;
172  
173 2 return Graphics::Color::RGB->new(
174 red => hex($1) / 255,
175 green => hex($2) / 255,
176 blue => hex($3) / 255
177 );
178 }
179  
180 # Not a valid hex color
181 0 return undef;
182 }
183  
184  
185 sub to_hsl {
186 3 ▶ my ($self) = @_;
Subroutine to_hsl: called CC=11 SCAR=24.4
Pod documented
187  
188 3 my $max = $self->red;
189 3 my $maxc = 'r';
190 3 my $min = $self->red;
191  
192 3 ▶ if($self->green > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->green > $max) 1 2 3
193 1 $max = $self->green;
194 1 $maxc = 'g';
195 }
196 3 ▶ if($self->blue > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->blue > $max) 1 2 3
197 1 $max = $self->blue;
198 1 $maxc = 'b';
199 }
200  
201 3 ▶ if($self->green < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->green < $min) 1 2 3
202 1 $min = $self->green;
203 }
204 3 ▶ if($self->blue < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->blue < $min) 0 3 3
205 0 $min = $self->blue;
206 }
207  
208 3 my ($h, $s, $l);
209  
210 3 ▶ if($max == $min) {
Branch: 4 branches
BranchTrueFalseTotal
if ($max == $min) { } 0 3 3
elsif ($maxc eq 'r') { } 1 2 3
elsif ($maxc eq 'g') { } 1 1 2
elsif ($maxc eq 'b') { } 1 0 1
211 0 $h = 0;
212 } elsif($maxc eq 'r') {
213 1 $h = 60 * (($self->green - $self->blue) / ($max - $min)) % 360;
214 } elsif($maxc eq 'g') {
215 1 $h = (60 * (($self->blue - $self->red) / ($max - $min)) + 120);
216 } elsif($maxc eq 'b') {
217 1 $h = (60 * (($self->red - $self->green) / ($max - $min)) + 240);
218 }
219  
220 3 $l = ($max + $min) / 2;
221  
222 3 ▶ if($max == $min) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max == $min) { } 0 3 3
elsif ($l <= 0.5) { } 2 1 3
223 0 $s = 0;
224 } elsif($l <= .5) {
225 2 $s = ($max - $min) / ($max + $min);
226 } else {
227 1 $s = ($max - $min) / (2 - ($max + $min));
228 }
229  
230 3 return Graphics::Color::HSL->new(
231 hue => $h, saturation => $s, lightness => $l, alpha => $self->alpha
232 );
233 }
234  
235  
236 sub to_hsv {
237 3 ▶ my ($self) = @_;
Subroutine to_hsv: called CC=10 SCAR=23.4
Pod documented
238  
239 3 my $max = $self->red;
240 3 my $maxc = 'r';
241 3 my $min = $self->red;
242  
243 3 ▶ if($self->green > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->green > $max) 1 2 3
244 1 $max = $self->green;
245 1 $maxc = 'g';
246 }
247 3 ▶ if($self->blue > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->blue > $max) 1 2 3
248 1 $max = $self->blue;
249 1 $maxc = 'b';
250 }
251  
252 3 ▶ if($self->green < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->green < $min) 1 2 3
253 1 $min = $self->green;
254 }
255 3 ▶ if($self->blue < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->blue < $min) 0 3 3
256 0 $min = $self->blue;
257 }
258  
259 3 my ($h, $s, $v);
260  
261 3 ▶ if($max == $min) {
Branch: 4 branches
BranchTrueFalseTotal
if ($max == $min) { } 0 3 3
elsif ($maxc eq 'r') { } 1 2 3
elsif ($maxc eq 'g') { } 1 1 2
elsif ($maxc eq 'b') { } 1 0 1
262 0 $h = 0;
263 } elsif($maxc eq 'r') {
264 1 $h = 60 * (($self->green - $self->blue) / ($max - $min)) % 360;
265 } elsif($maxc eq 'g') {
266 1 $h = (60 * (($self->blue - $self->red) / ($max - $min)) + 120);
267 } elsif($maxc eq 'b') {
268 1 $h = (60 * (($self->red - $self->green) / ($max - $min)) + 240);
269 }
270  
271 3 $v = $max;
272 3 ▶ if($max == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == 0) { } 0 3 3
273 0 $s = 0;
274 } else {
275 3 $s = 1 - ($min / $max);
276 }
277  
278 3 return Graphics::Color::HSV->new(
279 hue => $h, saturation => $s, value => $v, alpha => $self->alpha
280 );
281 }
282  
283 4 __PACKAGE__->meta->make_immutable;
284  
285 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 4 1;
287  
288 __END__
289  
290 =pod
291  
292 =head1 NAME
293  
294 Graphics::Color::RGB - RGB color model
295  
296 =head1 VERSION
297  
298 version 0.30
299  
300 =head1 SYNOPSIS
301  
302 use Graphics::Color::RGB;
303  
304 my $color = Graphics::Color::RGB->new({
305 red => 1,
306 blue => .31,
307 green => .25,
308 });
309  
310 =head1 DESCRIPTION
311  
312 Graphics::Color::RGB represents a Color in the sRGB color space. Individual
313 color channels are expressed as decimal values from 0 to 1, 0 being a lack
314 of that color (or opaque in the case of alpha) and 1 being full color (or
315 transparent in the case of alpha). If no options are provided then new
316 instance of RGB are opaque white, (that is equivalent to red => 1, green => 1,
317 blue => 1, alpha => 1).
318  
319 Convenience methods are supplied to convert to various string values.
320  
321 =head1 ATTRIBUTES
322  
323 =head2 red
324  
325 =head2 r
326  
327 Set/Get the red component of this Color. Aliased to 'r' as well.
328  
329 =head2 green
330  
331 =head2 g
332  
333 Set/Get the green component of this Color. Aliased to 'g' as well.
334  
335 =head2 blue
336  
337 =head2 b
338  
339 Set/Get the blue component of this Color. Aliased to 'b' as well.
340  
341 =head2 alpha
342  
343 =head2 a
344  
345 Set/Get the alpha component of this Color. Aliased to 'a' as well.
346  
347 =head2 name
348  
349 Get the name of this color. Only valid if the color was created by name.
350  
351 =head1 METHODS
352  
353 =head2 as_string
354  
355 Get a string version of this Color in the form of RED,GREEN,BLUE,ALPHA
356  
357 =head2 as_integer_string
358  
359 Return an integer formatted value for this color. This format is suitable for
360 CSS RGBA values.
361  
362 =head2 as_css_hex
363  
364 Return a hex formatted value with a prepended '#' for use in CSS and HTML.
365  
366 =head2 as_hex_string ( [$prepend] )
367  
368 Return a hex formatted value for this color. The output ignores the alpha
369 channel because, per the W3C, there is no hexadecimal notiation for an RGBA
370 value. Optionally allows you to include a string that will be prepended. This
371 is a common way to add the C<#>.
372  
373 =head2 as_percent_string
374  
375 Return a percent formatted value for this color. This format is suitable for
376 CSS RGBA values.
377  
378 =head2 as_array
379  
380 Get the RGB values as an array.
381  
382 =head2 as_array_with_alpha
383  
384 Get the RGBA values as an array
385  
386 =head2 equal_to
387  
388 Compares this color to the provided one. Returns 1 if true, else 0;
389  
390 =head2 not_equal_to
391  
392 The opposite of equal_to.
393  
394 =head2 from_color_library ($color_id)
395  
396 Attempts to retrieve the specified color-id using L<Color::Library>. The
397 result is then converted into a Graphics::Color::RGB object.
398  
399 =head2 from_hex_string($hex)
400  
401 Attempts to create a Graphics::Color::RGB object from a hex string. Works with
402 or without the leading # and with either 3 or 6 character hex strings.
403  
404 =head2 to_hsl
405  
406 Creates this RGB color in HSL space. Returns a L<Graphics::Color::HSL> object.
407  
408 =head2 to_hsv
409  
410 Creates this RGB color in HSV space. Returns a L<Graphics::Color::HSV> object.
411  
412 =head1 AUTHOR
413  
414 Cory G Watson <gphat@cpan.org>
415  
416 =head1 COPYRIGHT AND LICENSE
417  
418 This software is copyright (c) 2014 by Cold Hard Code, LLC.
419  
420 This is free software; you can redistribute it and/or modify it under
421 the same terms as the Perl 5 programming language system itself.
422  
423 =cut
↑ Summary Color/HSV.pm »