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 CC 30 SCAR 18.7
CC
30
Cov
84%
to_hsl
11.4
to_hsv
10.4
equal_to
7.3
CRAP 6.5
« Color/HSV.pm ↑ Summary Color/CMYK.pm »
1 package Graphics::Color::RGB;
2 4 $Graphics::Color::RGB::VERSION = '0.31';
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 1 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
53 1 my ($self) = @_;
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 1 ▶sub as_integer_string {
Subroutine as_integer_string: called CC=1 SCAR=0.0
Pod documented
63 1 my ($self) = @_;
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 1 ▶sub as_css_hex {
Subroutine as_css_hex: called CC=1 SCAR=0.0
Pod documented
72 1 my ($self) = @_;
73  
74 1 return $self->as_hex_string('#');
75 }
76  
77  
78 4 ▶sub as_hex_string {
Subroutine as_hex_string: called CC=2 SCAR=6.9
Pod documented
79 4 my ($self, $prepend) = @_;
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 1 ▶sub as_percent_string {
Subroutine as_percent_string: called CC=1 SCAR=0.0
Pod documented
90 1 my ($self) = @_;
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 1 ▶sub as_array {
Subroutine as_array: called CC=1 SCAR=0.0
Pod documented
99 1 my ($self) = @_;
100  
101 1 return ($self->red, $self->green, $self->blue);
102 }
103  
104  
105 1 ▶sub as_array_with_alpha {
Subroutine as_array_with_alpha: called CC=1 SCAR=0.0
Pod documented
106 1 my ($self) = @_;
107  
108 1 return ($self->red, $self->green, $self->blue, $self->alpha);
109 }
110  
111  
112 2 ▶sub equal_to {
Subroutine equal_to: called CC=6 SCAR=19.9
Pod documented
113 2 my ($self, $other) = @_;
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 1 ▶sub from_color_library {
Subroutine from_color_library: called CC=3 SCAR=11.8
Pod documented
135 1 my ($self, $id) = @_;
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 2 ▶sub from_hex_string {
Subroutine from_hex_string: called CC=3 SCAR=12.0
Pod documented
157 2 my ($self, $hex) = @_;
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 3 ▶sub to_hsl {
Subroutine to_hsl: called CC=11 SCAR=24.4
Pod documented
186 3 my ($self) = @_;
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 3 ▶sub to_hsv {
Subroutine to_hsv: called CC=10 SCAR=23.4
Pod documented
237 3 my ($self) = @_;
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__
« Color/HSV.pm ↑ Summary Color/CMYK.pm »