Color/HSV.pm

filter: Statementstmt 74.7% 62 / 83 Branchbran 50.0% 15 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 72.3% 94 / 130 SCAR 31.9
CC
16
Cov
68%
to_rgb
25.4
equal_to
7.3
BEGIN
1.0
CRAP 24.3
« Color/RGB.pm ↑ Summary Color/CMYK.pm »
1 package Graphics::Color::HSV;
2 4 $Graphics::Color::HSV::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 4 with 'Graphics::Color::Equal';
9  
10 4 ▶use Graphics::Color::Types qw(Number360OrLess NumberOneOrLess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Graphics::Color::RGB;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 4 has 'hue' => (
15 is => 'rw',
16 isa => Number360OrLess,
17 default => 1,
18 alias => 'h'
19 );
20  
21  
22 4 has 'saturation' => (
23 is => 'rw',
24 isa => NumberOneOrLess,
25 default => 1,
26 alias => 's'
27 );
28  
29  
30 4 has 'value' => (
31 is => 'rw',
32 isa => NumberOneOrLess,
33 default => 1,
34 alias => 'v'
35 );
36  
37  
38 4 has 'alpha' => (
39 is => 'rw',
40 isa => NumberOneOrLess,
41 default => 1,
42 alias => 'a'
43 );
44  
45  
46 4 has 'name' => ( is => 'rw', isa => 'Str' );
47  
48  
49 sub as_string {
50 1 ▶ my ($self) = @_;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
51  
52 1 return sprintf('%d,%0.2f,%0.2f,%0.2f',
53 $self->hue, $self->saturation, $self->value, $self->alpha
54 );
55 }
56  
57  
58 sub as_percent_string {
59 1 ▶ my ($self) = @_;
Subroutine as_percent_string: called CC=1 SCAR=0.0
Pod documented
60  
61 1 return sprintf("%d, %d%%, %d%%, %0.2f",
62 $self->hue, $self->saturation * 100, $self->value * 100,
63 $self->alpha
64 );
65 }
66  
67  
68 sub as_array {
69 1 ▶ my ($self) = @_;
Subroutine as_array: called CC=1 SCAR=0.0
Pod documented
70  
71 1 return ($self->hue, $self->saturation, $self->value);
72 }
73  
74  
75 sub as_array_with_alpha {
76 1 ▶ my ($self) = @_;
Subroutine as_array_with_alpha: called CC=1 SCAR=0.0
Pod documented
77  
78 1 return ($self->hue, $self->saturation, $self->value, $self->alpha);
79 }
80  
81  
82 sub equal_to {
83 2 ▶ my ($self, $other) = @_;
Subroutine equal_to: called CC=6 SCAR=19.9
Pod documented
84  
85 2 ▶ return 0 unless defined($other);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other 0 2 2
86  
87 2 ▶ unless($self->hue == $other->hue) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hue == $other->hue) 0 2 2
88 0 return 0;
89 }
90 2 ▶ unless($self->saturation == $other->saturation) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->saturation == $other->saturation) 1 1 2
91 1 return 0;
92 }
93 1 ▶ unless($self->value == $other->value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->value == $other->value) 0 1 1
94 0 return 0;
95 }
96 1 ▶ unless($self->alpha == $other->alpha) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->alpha == $other->alpha) 0 1 1
97 0 return 0;
98 }
99  
100 1 return 1;
101 }
102  
103  
104  
105 sub to_rgb {
106 3 ▶ my ($self) = @_;
Subroutine to_rgb: called CC=11 SCAR=32.3
Pod documented
107  
108 3 my ($h, $s, $v) = ($self->h, $self->s, $self->v);
109  
110 3 my ($red, $green, $blue);
111  
112 3 ▶ if($v == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v == 0) { } 0 3 3
elsif ($s == 0) { } 0 3 3
113 0 ($red, $green, $blue) = (0, 0, 0);
114 } elsif($s == 0) {
115 0 ($red, $green, $blue) = ($v, $v, $v);
116 } else {
117 3 my $hf = $h / 60;
118 3 my $i = int($hf);
119 3 my $f = $hf - $i;
120 3 my $pv = $v * (1 - $s);
121 3 my $qv = $v * (1 - $s * $f);
122 3 my $tv = $v * (1 - $s * (1 - $f));
123  
124 3 ▶ if($i == 0) {
Branch: 8 branches
BranchTrueFalseTotal
if ($i == 0) { } 1 2 3
elsif ($i == 1) { } 0 2 2
elsif ($i == 2) { } 1 1 2
elsif ($i == 3) { } 0 1 1
elsif ($i == 4) { } 1 0 1
elsif ($i == 5) { } 0 0 0
elsif ($i == 6) { } 0 0 0
elsif ($i == -1) { } 0 0 0
125 1 $red = $v;
126 1 $green = $tv;
127 1 $blue = $pv;
128 } elsif($i == 1) {
129 0 $red = $qv;
130 0 $green = $v;
131 0 $blue = $pv;
132 } elsif($i == 2) {
133 1 $red = $pv;
134 1 $green = $v;
135 1 $blue = $tv;
136 } elsif($i == 3) {
137 0 $red = $pv;
138 0 $green = $qv;
139 0 $blue = $v;
140 } elsif($i == 4) {
141 1 $red = $tv;
142 1 $green = $pv;
143 1 $blue = $v;
144 } elsif($i == 5) {
145 0 $red = $v;
146 0 $green = $pv;
147 0 $blue = $qv;
148 } elsif($i == 6) {
149 0 $red = $v;
150 0 $blue = $tv;
151 0 $green = $pv;
152 } elsif($i == -1) {
153 0 $red = $v;
154 0 $green = $pv;
155 0 $blue = $qv;
156 } else {
157 0 die('Invalid HSV -> RGB conversion.')
158 }
159 }
160  
161 3 return Graphics::Color::RGB->new(
162 red => $red, green => $green, blue => $blue
163 );
164 }
165  
166 4 __PACKAGE__->meta->make_immutable;
167  
168 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 4 1;
170  
171 __END__
172  
173 =pod
174  
175 =head1 NAME
176  
177 Graphics::Color::HSV
178  
179 =head1 VERSION
180  
181 version 0.30
182  
183 =head1 SYNOPSIS
184  
185 use Graphics::Color::HSV;
186  
187 my $color = Graphics::Color::HSV->new({
188 hue => 120,
189 saturation => .5,
190 value => .25,
191 });
192  
193 =head1 DESCRIPTION
194  
195 Graphics::Color::HSV represents a Color in an RGB color space. HSLV stands for
196 B<Hue> B<Saturation> and B<Value>. HSV is closely related to HSL.
197  
198 =head1 ATTRIBUTES
199  
200 =head2 hue
201  
202 =head2 h
203  
204 Set/Get the hue component of this Color.
205  
206 =head2 saturation
207  
208 =head2 s
209  
210 Set/Get the saturation component of this Color.
211  
212 =head2 value
213  
214 =head2 v
215  
216 Set/Get the value component of this Color.
217  
218 =head2 alpha
219  
220 Set/Get the alpha component of this Color.
221  
222 =head2 name
223  
224 Get the name of this color. Only valid if the color was created by name.
225  
226 =head1 METHODS
227  
228 =head2 as_string
229  
230 Get a string version of this Color in the form of
231 HUE,SATURATION,VALUE,ALPHA.
232  
233 =head2 as_percent_string
234  
235 Return a percent formatted value for this color. This format is suitable for
236 CSS HSV values.
237  
238 =head2 as_array
239  
240 Get the HSV values as an array
241  
242 =head2 as_array_with_alpha>
243  
244 Get the HSVA values as an array
245  
246 =head2 equal_to
247  
248 Compares this color to the provided one. Returns 1 if true, else 0;
249  
250 =head2 not_equal_to
251  
252 The opposite of equal_to.
253  
254 =head2 to_rgb
255  
256 Creates this HSV color in RGB space. Returns a L<Graphics::Color::RGB> object.
257  
258 =head1 AUTHOR
259  
260 Cory G Watson <gphat@cpan.org>
261  
262 =head1 COPYRIGHT AND LICENSE
263  
264 This software is copyright (c) 2014 by Cold Hard Code, LLC.
265  
266 This is free software; you can redistribute it and/or modify it under
267 the same terms as the Perl 5 programming language system itself.
268  
269 =cut
« Color/RGB.pm ↑ Summary Color/CMYK.pm »