/usr/share/perl5/Graphics/Color/RGB.pm is in libgraphics-color-perl 0.29-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | package Graphics::Color::RGB;
BEGIN {
$Graphics::Color::RGB::VERSION = '0.29';
}
use Moose;
use MooseX::Aliases;
extends qw(Graphics::Color);
# ABSTRACT: RGB color model
use Color::Library;
use Graphics::Color::HSL;
use Graphics::Color::HSV;
use Graphics::Color::Types qw(NumberOneOrLess);
has 'red' => (
is => 'rw',
isa => NumberOneOrLess,
default => 1,
alias => 'r'
);
has 'green' => (
is => 'rw',
isa => NumberOneOrLess,
default => 1,
alias => 'g'
);
has 'blue' => (
is => 'rw',
isa => NumberOneOrLess,
default => 1,
alias => 'b'
);
has 'alpha' => (
is => 'rw',
isa => NumberOneOrLess,
default => 1,
alias => 'a'
);
has 'name' => ( is => 'rw', isa => 'Str' );
sub as_string {
my ($self) = @_;
return sprintf('%0.2f,%0.2f,%0.2f,%0.2f',
$self->red, $self->green,
$self->blue, $self->alpha
);
}
sub as_integer_string {
my ($self) = @_;
return sprintf("%d, %d, %d, %0.2f",
$self->red * 255, $self->green * 255, $self->blue * 255, $self->alpha
);
}
sub as_css_hex {
my ($self) = @_;
return $self->as_hex_string('#');
}
sub as_hex_string {
my ($self, $prepend) = @_;
$prepend = '' unless defined($prepend);
return sprintf('%s%.2x%.2x%.2x',
$prepend, $self->red * 255, $self->green * 255, $self->blue * 255
);
}
sub as_percent_string {
my ($self) = @_;
return sprintf("%d%%, %d%%, %d%%, %0.2f",
$self->red * 100, $self->green * 100, $self->blue * 100, $self->alpha
);
}
sub as_array {
my ($self) = @_;
return ($self->red, $self->green, $self->blue);
}
sub as_array_with_alpha {
my ($self) = @_;
return ($self->red, $self->green, $self->blue, $self->alpha);
}
sub equal_to {
my ($self, $other) = @_;
return 0 unless defined($other);
unless($self->red == $other->red) {
return 0;
}
unless($self->green == $other->green) {
return 0;
}
unless($self->blue == $other->blue) {
return 0;
}
unless($self->alpha == $other->alpha) {
return 0;
}
return 1;
}
sub from_color_library {
my ($self, $id) = @_;
my $color;
if(ref($id)) {
$color = $id;
} else {
$color = Color::Library->color($id);
unless(defined($color)) {
die("Couldn't find color for '$id'!");
}
}
my ($r, $g, $b) = $color->rgb;
return Graphics::Color::RGB->new(
red => $r / 255,
green => $g / 255,
blue => $b / 255
);
}
sub from_hex_string {
my ($self, $hex) = @_;
# Get rid of the leading # if it's there
$hex =~ s/^#//g;
$hex = lc($hex);
my $len = length($hex);
if(length($hex) == 3) {
$hex =~ /([a-f0-9])([a-f0-9])([a-f0-9])/;
$hex = "$1$1$2$2$3$3";
}
if(length($hex) == 6) {
$hex =~ /([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/;
return Graphics::Color::RGB->new(
red => hex($1) / 255,
green => hex($2) / 255,
blue => hex($3) / 255
);
}
# Not a valid hex color
return undef;
}
sub to_hsl {
my ($self) = @_;
my $max = $self->red;
my $maxc = 'r';
my $min = $self->red;
if($self->green > $max) {
$max = $self->green;
$maxc = 'g';
}
if($self->blue > $max) {
$max = $self->blue;
$maxc = 'b';
}
if($self->green < $min) {
$min = $self->green;
}
if($self->blue < $min) {
$min = $self->blue;
}
my ($h, $s, $l);
if($max == $min) {
$h = 0;
} elsif($maxc eq 'r') {
$h = 60 * (($self->green - $self->blue) / ($max - $min)) % 360;
} elsif($maxc eq 'g') {
$h = (60 * (($self->blue - $self->red) / ($max - $min)) + 120);
} elsif($maxc eq 'b') {
$h = (60 * (($self->red - $self->green) / ($max - $min)) + 240);
}
$l = ($max + $min) / 2;
if($max == $min) {
$s = 0;
} elsif($l <= .5) {
$s = ($max - $min) / ($max + $min);
} else {
$s = ($max - $min) / (2 - ($max + $min));
}
return Graphics::Color::HSL->new(
hue => $h, saturation => $s, lightness => $l, alpha => $self->alpha
);
}
sub to_hsv {
my ($self) = @_;
my $max = $self->red;
my $maxc = 'r';
my $min = $self->red;
if($self->green > $max) {
$max = $self->green;
$maxc = 'g';
}
if($self->blue > $max) {
$max = $self->blue;
$maxc = 'b';
}
if($self->green < $min) {
$min = $self->green;
}
if($self->blue < $min) {
$min = $self->blue;
}
my ($h, $s, $v);
if($max == $min) {
$h = 0;
} elsif($maxc eq 'r') {
$h = 60 * (($self->green - $self->blue) / ($max - $min)) % 360;
} elsif($maxc eq 'g') {
$h = (60 * (($self->blue - $self->red) / ($max - $min)) + 120);
} elsif($maxc eq 'b') {
$h = (60 * (($self->red - $self->green) / ($max - $min)) + 240);
}
$v = $max;
if($max == 0) {
$s = 0;
} else {
$s = 1 - ($min / $max);
}
return Graphics::Color::HSV->new(
hue => $h, saturation => $s, value => $v, alpha => $self->alpha
);
}
__PACKAGE__->meta->make_immutable;
no Moose;
1;
__END__
=pod
=head1 NAME
Graphics::Color::RGB - RGB color model
=head1 VERSION
version 0.29
=head1 SYNOPSIS
use Graphics::Color::RGB;
my $color = Graphics::Color::RGB->new({
red => 1,
blue => .31,
green => .25,
});
=head1 DESCRIPTION
Graphics::Color::RGB represents a Color in the sRGB color space. Individual
color channels are expressed as decimal values from 0 to 1, 0 being a lack
of that color (or opaque in the case of alpha) and 1 being full color (or
transparent in the case of alpha). If no options are provided then new
instance of RGB are opaque white, (that is equivalent to red => 1, green => 1,
blue => 1, alpha => 1).
Convenience methods are supplied to convert to various string values.
=head1 ATTRIBUTES
=head2 red
=head2 r
Set/Get the red component of this Color. Aliased to 'r' as well.
=head2 green
=head2 g
Set/Get the green component of this Color. Aliased to 'g' as well.
=head2 blue
=head2 b
Set/Get the blue component of this Color. Aliased to 'b' as well.
=head2 alpha
=head2 a
Set/Get the alpha component of this Color. Aliased to 'a' as well.
=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 RED,GREEN,BLUE,ALPHA
=head2 as_integer_string
Return an integer formatted value for this color. This format is suitable for
CSS RGBA values.
=head2 as_css_hex
Return a hex formatted value with a prepended '#' for use in CSS and HTML.
=head2 as_hex_string ( [$prepend] )
Return a hex formatted value for this color. The output ignores the alpha
channel because, per the W3C, there is no hexadecimal notiation for an RGBA
value. Optionally allows you to include a string that will be prepended. This
is a common way to add the C<#>.
=head2 as_percent_string
Return a percent formatted value for this color. This format is suitable for
CSS RGBA values.
=head2 as_array
Get the RGB values as an array.
=head2 as_array_with_alpha
Get the RGBA 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 from_color_library ($color_id)
Attempts to retrieve the specified color-id using L<Color::Library>. The
result is then converted into a Graphics::Color::RGB object.
=head2 from_hex_string($hex)
Attempts to create a Graphics::Color::RGB object from a hex string. Works with
or without the leading # and with either 3 or 6 character hex strings.
=head2 to_hsl
Creates this RGB color in HSL space. Returns a L<Graphics::Color::HSL> object.
=head2 to_hsv
Creates this RGB color in HSV space. Returns a L<Graphics::Color::HSV> object.
=head1 AUTHOR
Cory G Watson <gphat@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 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
|