line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Graphics::Color::HSV; |
2
|
|
|
|
|
|
|
$Graphics::Color::HSV::VERSION = '0.31'; |
3
|
1
|
|
|
1
|
|
18194
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use MooseX::Aliases; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
extends qw(Graphics::Color); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Graphics::Color::Equal'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Graphics::Color::Types qw(Number360OrLess NumberOneOrLess); |
11
|
|
|
|
|
|
|
use Graphics::Color::RGB; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'hue' => ( |
15
|
|
|
|
|
|
|
is => 'rw', |
16
|
|
|
|
|
|
|
isa => Number360OrLess, |
17
|
|
|
|
|
|
|
default => 1, |
18
|
|
|
|
|
|
|
alias => 'h' |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'saturation' => ( |
23
|
|
|
|
|
|
|
is => 'rw', |
24
|
|
|
|
|
|
|
isa => NumberOneOrLess, |
25
|
|
|
|
|
|
|
default => 1, |
26
|
|
|
|
|
|
|
alias => 's' |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has 'value' => ( |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
isa => NumberOneOrLess, |
33
|
|
|
|
|
|
|
default => 1, |
34
|
|
|
|
|
|
|
alias => 'v' |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has 'alpha' => ( |
39
|
|
|
|
|
|
|
is => 'rw', |
40
|
|
|
|
|
|
|
isa => NumberOneOrLess, |
41
|
|
|
|
|
|
|
default => 1, |
42
|
|
|
|
|
|
|
alias => 'a' |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has 'name' => ( is => 'rw', isa => 'Str' ); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub as_string { |
50
|
|
|
|
|
|
|
my ($self) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my ($self) = @_; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my ($self) = @_; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
return ($self->hue, $self->saturation, $self->value); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub as_array_with_alpha { |
76
|
|
|
|
|
|
|
my ($self) = @_; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return ($self->hue, $self->saturation, $self->value, $self->alpha); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub equal_to { |
83
|
|
|
|
|
|
|
my ($self, $other) = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
return 0 unless defined($other); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
unless($self->hue == $other->hue) { |
88
|
|
|
|
|
|
|
return 0; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
unless($self->saturation == $other->saturation) { |
91
|
|
|
|
|
|
|
return 0; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
unless($self->value == $other->value) { |
94
|
|
|
|
|
|
|
return 0; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
unless($self->alpha == $other->alpha) { |
97
|
|
|
|
|
|
|
return 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub to_rgb { |
106
|
|
|
|
|
|
|
my ($self) = @_; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my ($h, $s, $v) = ($self->h, $self->s, $self->v); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my ($red, $green, $blue); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
if($v == 0) { |
113
|
|
|
|
|
|
|
($red, $green, $blue) = (0, 0, 0); |
114
|
|
|
|
|
|
|
} elsif($s == 0) { |
115
|
|
|
|
|
|
|
($red, $green, $blue) = ($v, $v, $v); |
116
|
|
|
|
|
|
|
} else { |
117
|
|
|
|
|
|
|
my $hf = $h / 60; |
118
|
|
|
|
|
|
|
my $i = int($hf); |
119
|
|
|
|
|
|
|
my $f = $hf - $i; |
120
|
|
|
|
|
|
|
my $pv = $v * (1 - $s); |
121
|
|
|
|
|
|
|
my $qv = $v * (1 - $s * $f); |
122
|
|
|
|
|
|
|
my $tv = $v * (1 - $s * (1 - $f)); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
if($i == 0) { |
125
|
|
|
|
|
|
|
$red = $v; |
126
|
|
|
|
|
|
|
$green = $tv; |
127
|
|
|
|
|
|
|
$blue = $pv; |
128
|
|
|
|
|
|
|
} elsif($i == 1) { |
129
|
|
|
|
|
|
|
$red = $qv; |
130
|
|
|
|
|
|
|
$green = $v; |
131
|
|
|
|
|
|
|
$blue = $pv; |
132
|
|
|
|
|
|
|
} elsif($i == 2) { |
133
|
|
|
|
|
|
|
$red = $pv; |
134
|
|
|
|
|
|
|
$green = $v; |
135
|
|
|
|
|
|
|
$blue = $tv; |
136
|
|
|
|
|
|
|
} elsif($i == 3) { |
137
|
|
|
|
|
|
|
$red = $pv; |
138
|
|
|
|
|
|
|
$green = $qv; |
139
|
|
|
|
|
|
|
$blue = $v; |
140
|
|
|
|
|
|
|
} elsif($i == 4) { |
141
|
|
|
|
|
|
|
$red = $tv; |
142
|
|
|
|
|
|
|
$green = $pv; |
143
|
|
|
|
|
|
|
$blue = $v; |
144
|
|
|
|
|
|
|
} elsif($i == 5) { |
145
|
|
|
|
|
|
|
$red = $v; |
146
|
|
|
|
|
|
|
$green = $pv; |
147
|
|
|
|
|
|
|
$blue = $qv; |
148
|
|
|
|
|
|
|
} elsif($i == 6) { |
149
|
|
|
|
|
|
|
$red = $v; |
150
|
|
|
|
|
|
|
$blue = $tv; |
151
|
|
|
|
|
|
|
$green = $pv; |
152
|
|
|
|
|
|
|
} elsif($i == -1) { |
153
|
|
|
|
|
|
|
$red = $v; |
154
|
|
|
|
|
|
|
$green = $pv; |
155
|
|
|
|
|
|
|
$blue = $qv; |
156
|
|
|
|
|
|
|
} else { |
157
|
|
|
|
|
|
|
die('Invalid HSV -> RGB conversion.') |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
return Graphics::Color::RGB->new( |
162
|
|
|
|
|
|
|
red => $red, green => $green, blue => $blue |
163
|
|
|
|
|
|
|
); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
no Moose; |
169
|
|
|
|
|
|
|
1; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
__END__ |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=pod |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 NAME |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Graphics::Color::HSV |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 VERSION |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
version 0.31 |
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 |