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