File Coverage

blib/lib/Graphics/Color/RGB.pm
Criterion Covered Total %
statement 2 4 50.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 4 6 66.6


line stmt bran cond sub pod time code
1             package Graphics::Color::RGB;
2             BEGIN {
3 1     1   33357 $Graphics::Color::RGB::VERSION = '0.29';
4             }
5 1     1   1908 use Moose;
  0            
  0            
6             use MooseX::Aliases;
7              
8             extends qw(Graphics::Color);
9              
10             # ABSTRACT: RGB color model
11              
12             use Color::Library;
13             use Graphics::Color::HSL;
14             use Graphics::Color::HSV;
15              
16             use Graphics::Color::Types qw(NumberOneOrLess);
17              
18              
19             has 'red' => (
20             is => 'rw',
21             isa => NumberOneOrLess,
22             default => 1,
23             alias => 'r'
24             );
25              
26              
27             has 'green' => (
28             is => 'rw',
29             isa => NumberOneOrLess,
30             default => 1,
31             alias => 'g'
32             );
33              
34              
35             has 'blue' => (
36             is => 'rw',
37             isa => NumberOneOrLess,
38             default => 1,
39             alias => 'b'
40             );
41              
42              
43             has 'alpha' => (
44             is => 'rw',
45             isa => NumberOneOrLess,
46             default => 1,
47             alias => 'a'
48             );
49              
50              
51             has 'name' => ( is => 'rw', isa => 'Str' );
52              
53              
54             sub as_string {
55             my ($self) = @_;
56              
57             return sprintf('%0.2f,%0.2f,%0.2f,%0.2f',
58             $self->red, $self->green,
59             $self->blue, $self->alpha
60             );
61             }
62              
63              
64             sub as_integer_string {
65             my ($self) = @_;
66              
67             return sprintf("%d, %d, %d, %0.2f",
68             $self->red * 255, $self->green * 255, $self->blue * 255, $self->alpha
69             );
70             }
71              
72              
73             sub as_css_hex {
74             my ($self) = @_;
75              
76             return $self->as_hex_string('#');
77             }
78              
79              
80             sub as_hex_string {
81             my ($self, $prepend) = @_;
82              
83             $prepend = '' unless defined($prepend);
84              
85             return sprintf('%s%.2x%.2x%.2x',
86             $prepend, $self->red * 255, $self->green * 255, $self->blue * 255
87             );
88             }
89              
90              
91             sub as_percent_string {
92             my ($self) = @_;
93              
94             return sprintf("%d%%, %d%%, %d%%, %0.2f",
95             $self->red * 100, $self->green * 100, $self->blue * 100, $self->alpha
96             );
97             }
98              
99              
100             sub as_array {
101             my ($self) = @_;
102              
103             return ($self->red, $self->green, $self->blue);
104             }
105              
106              
107             sub as_array_with_alpha {
108             my ($self) = @_;
109              
110             return ($self->red, $self->green, $self->blue, $self->alpha);
111             }
112              
113              
114             sub equal_to {
115             my ($self, $other) = @_;
116              
117             return 0 unless defined($other);
118              
119             unless($self->red == $other->red) {
120             return 0;
121             }
122             unless($self->green == $other->green) {
123             return 0;
124             }
125             unless($self->blue == $other->blue) {
126             return 0;
127             }
128             unless($self->alpha == $other->alpha) {
129             return 0;
130             }
131              
132             return 1;
133             }
134              
135              
136             sub from_color_library {
137             my ($self, $id) = @_;
138              
139             my $color;
140             if(ref($id)) {
141             $color = $id;
142             } else {
143             $color = Color::Library->color($id);
144             unless(defined($color)) {
145             die("Couldn't find color for '$id'!");
146             }
147             }
148              
149             my ($r, $g, $b) = $color->rgb;
150             return Graphics::Color::RGB->new(
151             red => $r / 255,
152             green => $g / 255,
153             blue => $b / 255
154             );
155             }
156              
157              
158             sub from_hex_string {
159             my ($self, $hex) = @_;
160              
161             # Get rid of the leading # if it's there
162             $hex =~ s/^#//g;
163             $hex = lc($hex);
164              
165             my $len = length($hex);
166              
167             if(length($hex) == 3) {
168             $hex =~ /([a-f0-9])([a-f0-9])([a-f0-9])/;
169             $hex = "$1$1$2$2$3$3";
170             }
171              
172             if(length($hex) == 6) {
173             $hex =~ /([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/;
174              
175             return Graphics::Color::RGB->new(
176             red => hex($1) / 255,
177             green => hex($2) / 255,
178             blue => hex($3) / 255
179             );
180             }
181              
182             # Not a valid hex color
183             return undef;
184             }
185              
186              
187             sub to_hsl {
188             my ($self) = @_;
189              
190             my $max = $self->red;
191             my $maxc = 'r';
192             my $min = $self->red;
193              
194             if($self->green > $max) {
195             $max = $self->green;
196             $maxc = 'g';
197             }
198             if($self->blue > $max) {
199             $max = $self->blue;
200             $maxc = 'b';
201             }
202              
203             if($self->green < $min) {
204             $min = $self->green;
205             }
206             if($self->blue < $min) {
207             $min = $self->blue;
208             }
209              
210             my ($h, $s, $l);
211              
212             if($max == $min) {
213             $h = 0;
214             } elsif($maxc eq 'r') {
215             $h = 60 * (($self->green - $self->blue) / ($max - $min)) % 360;
216             } elsif($maxc eq 'g') {
217             $h = (60 * (($self->blue - $self->red) / ($max - $min)) + 120);
218             } elsif($maxc eq 'b') {
219             $h = (60 * (($self->red - $self->green) / ($max - $min)) + 240);
220             }
221              
222             $l = ($max + $min) / 2;
223              
224             if($max == $min) {
225             $s = 0;
226             } elsif($l <= .5) {
227             $s = ($max - $min) / ($max + $min);
228             } else {
229             $s = ($max - $min) / (2 - ($max + $min));
230             }
231              
232             return Graphics::Color::HSL->new(
233             hue => $h, saturation => $s, lightness => $l, alpha => $self->alpha
234             );
235             }
236              
237              
238             sub to_hsv {
239             my ($self) = @_;
240              
241             my $max = $self->red;
242             my $maxc = 'r';
243             my $min = $self->red;
244              
245             if($self->green > $max) {
246             $max = $self->green;
247             $maxc = 'g';
248             }
249             if($self->blue > $max) {
250             $max = $self->blue;
251             $maxc = 'b';
252             }
253              
254             if($self->green < $min) {
255             $min = $self->green;
256             }
257             if($self->blue < $min) {
258             $min = $self->blue;
259             }
260              
261             my ($h, $s, $v);
262              
263             if($max == $min) {
264             $h = 0;
265             } elsif($maxc eq 'r') {
266             $h = 60 * (($self->green - $self->blue) / ($max - $min)) % 360;
267             } elsif($maxc eq 'g') {
268             $h = (60 * (($self->blue - $self->red) / ($max - $min)) + 120);
269             } elsif($maxc eq 'b') {
270             $h = (60 * (($self->red - $self->green) / ($max - $min)) + 240);
271             }
272              
273             $v = $max;
274             if($max == 0) {
275             $s = 0;
276             } else {
277             $s = 1 - ($min / $max);
278             }
279              
280             return Graphics::Color::HSV->new(
281             hue => $h, saturation => $s, value => $v, alpha => $self->alpha
282             );
283             }
284              
285             __PACKAGE__->meta->make_immutable;
286              
287             no Moose;
288             1;
289             __END__
290             =pod
291              
292             =head1 NAME
293              
294             Graphics::Color::RGB - RGB color model
295              
296             =head1 VERSION
297              
298             version 0.29
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) 2011 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
424