| 1 |
package Imager::Color; |
| 2 |
use 5.006; |
| 3 |
use Imager; |
| 4 |
use strict; |
| 5 |
use Scalar::Util (); |
| 6 |
use POSIX (); |
| 7 |
|
| 8 |
our $VERSION = "1.015"; |
| 9 |
|
| 10 |
# It's just a front end to the XS creation functions. |
| 11 |
|
| 12 |
# used in converting hsv to rgb |
| 13 |
my @hsv_map = |
| 14 |
( |
| 15 |
'vkm', 'nvm', 'mvk', 'mnv', 'kmv', 'vmn' |
| 16 |
); |
| 17 |
|
| 18 |
sub _hsv_to_rgb { |
| 19 |
my ($hue, $sat, $val) = @_; |
| 20 |
|
| 21 |
# HSV conversions from pages 401-403 "Procedural Elements for Computer |
| 22 |
# Graphics", 1985, ISBN 0-07-053534-5. |
| 23 |
|
| 24 |
my @result; |
| 25 |
if ($sat <= 0) { |
| 26 |
return ( 255 * $val, 255 * $val, 255 * $val ); |
| 27 |
} |
| 28 |
else { |
| 29 |
$val >= 0 or $val = 0; |
| 30 |
$val <= 1 or $val = 1; |
| 31 |
$sat <= 1 or $sat = 1; |
| 32 |
$hue >= 360 and $hue %= 360; |
| 33 |
$hue < 0 and $hue += 360; |
| 34 |
$hue /= 60.0; |
| 35 |
my $i = int($hue); |
| 36 |
my $f = $hue - $i; |
| 37 |
$val *= 255; |
| 38 |
my $m = $val * (1.0 - $sat); |
| 39 |
my $n = $val * (1.0 - $sat * $f); |
| 40 |
my $k = $val * (1.0 - $sat * (1 - $f)); |
| 41 |
my $v = $val; |
| 42 |
my %fields = ( 'm'=>$m, 'n'=>$n, 'v'=>$v, 'k'=>$k, ); |
| 43 |
return @fields{split //, $hsv_map[$i]}; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
# cache of loaded gimp files |
| 48 |
# each key is a filename, under each key is a hashref with the following |
| 49 |
# keys: |
| 50 |
# mod_time => last mod_time of file |
| 51 |
# colors => hashref name to arrayref of colors |
| 52 |
my %gimp_cache; |
| 53 |
|
| 54 |
# palette search locations |
| 55 |
# this is pretty rude |
| 56 |
# $HOME is replaced at runtime |
| 57 |
my @gimp_search = |
| 58 |
( |
| 59 |
'$HOME/.gimp-1.2/palettes/Named_Colors', |
| 60 |
'$HOME/.gimp-1.1/palettes/Named_Colors', |
| 61 |
'$HOME/.gimp/palettes/Named_Colors', |
| 62 |
'/usr/share/gimp/1.2/palettes/Named_Colors', |
| 63 |
'/usr/share/gimp/1.1/palettes/Named_Colors', |
| 64 |
'/usr/share/gimp/palettes/Named_Colors', |
| 65 |
); |
| 66 |
|
| 67 |
my $default_gimp_palette; |
| 68 |
|
| 69 |
sub _load_gimp_palette { |
| 70 |
my ($filename) = @_; |
| 71 |
|
| 72 |
if (open PAL, "< $filename") { |
| 73 |
my $hdr = <PAL>; |
| 74 |
chomp $hdr; |
| 75 |
unless ($hdr =~ /GIMP Palette/) { |
| 76 |
close PAL; |
| 77 |
$Imager::ERRSTR = "$filename is not a GIMP palette file"; |
| 78 |
return; |
| 79 |
} |
| 80 |
my $line; |
| 81 |
my %pal; |
| 82 |
my $mod_time = (stat PAL)[9]; |
| 83 |
while (defined($line = <PAL>)) { |
| 84 |
next if $line =~ /^#/ || $line =~ /^\s*$/; |
| 85 |
chomp $line; |
| 86 |
my ($r,$g, $b, $name) = split ' ', $line, 4; |
| 87 |
if ($name) { |
| 88 |
$name =~ s/\s*\([\d\s]+\)\s*$//; |
| 89 |
$pal{lc $name} = [ $r, $g, $b ]; |
| 90 |
} |
| 91 |
} |
| 92 |
close PAL; |
| 93 |
|
| 94 |
$gimp_cache{$filename} = { mod_time=>$mod_time, colors=>\%pal }; |
| 95 |
|
| 96 |
return 1; |
| 97 |
} |
| 98 |
else { |
| 99 |
$Imager::ERRSTR = "Cannot open palette file $filename: $!"; |
| 100 |
return; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
sub _get_gimp_color { |
| 105 |
my %args = @_; |
| 106 |
|
| 107 |
my $filename; |
| 108 |
if ($args{palette}) { |
| 109 |
$filename = $args{palette}; |
| 110 |
} |
| 111 |
elsif (defined $default_gimp_palette) { |
| 112 |
# don't search again and again and again ... |
| 113 |
if (!length $default_gimp_palette |
| 114 |
|| !-f $default_gimp_palette) { |
| 115 |
$Imager::ERRSTR = "No GIMP palette found"; |
| 116 |
$default_gimp_palette = ""; |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$filename = $default_gimp_palette; |
| 121 |
} |
| 122 |
else { |
| 123 |
# try to make one up - this is intended to die if tainting is |
| 124 |
# enabled and $ENV{HOME} is tainted. To avoid that untaint $ENV{HOME} |
| 125 |
# or set the palette parameter |
| 126 |
for my $attempt (@gimp_search) { |
| 127 |
my $work = $attempt; # don't modify the source array |
| 128 |
$work =~ /\$HOME/ && !defined $ENV{HOME} |
| 129 |
and next; |
| 130 |
$work =~ s/\$HOME/$ENV{HOME}/; |
| 131 |
if (-e $work) { |
| 132 |
$filename = $work; |
| 133 |
last; |
| 134 |
} |
| 135 |
} |
| 136 |
if (!$filename) { |
| 137 |
$Imager::ERRSTR = "No GIMP palette found"; |
| 138 |
$default_gimp_palette = ""; |
| 139 |
return (); |
| 140 |
} |
| 141 |
|
| 142 |
$default_gimp_palette = $filename; |
| 143 |
} |
| 144 |
|
| 145 |
if ((!$gimp_cache{$filename} |
| 146 |
|| (stat $filename)[9] != $gimp_cache{$filename}) |
| 147 |
&& !_load_gimp_palette($filename)) { |
| 148 |
return (); |
| 149 |
} |
| 150 |
|
| 151 |
if (!$gimp_cache{$filename}{colors}{lc $args{name}}) { |
| 152 |
$Imager::ERRSTR = "Color '$args{name}' isn't in $filename"; |
| 153 |
return (); |
| 154 |
} |
| 155 |
|
| 156 |
return @{$gimp_cache{$filename}{colors}{lc $args{name}}}; |
| 157 |
} |
| 158 |
|
| 159 |
my @x_search = |
| 160 |
( |
| 161 |
'/usr/share/X11/rgb.txt', # newer Xorg X11 dists use this |
| 162 |
'/usr/lib/X11/rgb.txt', # seems fairly standard |
| 163 |
'/usr/local/lib/X11/rgb.txt', # seems possible |
| 164 |
'/usr/X11R6/lib/X11/rgb.txt', # probably the same as the first |
| 165 |
'/usr/openwin/lib/rgb.txt', |
| 166 |
'/usr/openwin/lib/X11/rgb.txt', |
| 167 |
); |
| 168 |
|
| 169 |
my $default_x_rgb; |
| 170 |
|
| 171 |
# called by the test code to check if we can test this stuff |
| 172 |
sub _test_x_palettes { |
| 173 |
@x_search; |
| 174 |
} |
| 175 |
|
| 176 |
# x rgb.txt cache |
| 177 |
# same structure as %gimp_cache |
| 178 |
my %x_cache; |
| 179 |
|
| 180 |
sub _load_x_rgb { |
| 181 |
my ($filename) = @_; |
| 182 |
|
| 183 |
local *RGB; |
| 184 |
if (open RGB, "< $filename") { |
| 185 |
my $line; |
| 186 |
my %pal; |
| 187 |
my $mod_time = (stat RGB)[9]; |
| 188 |
while (defined($line = <RGB>)) { |
| 189 |
# the version of rgb.txt supplied with GNU Emacs uses # for comments |
| 190 |
next if $line =~ /^[!#]/ || $line =~ /^\s*$/; |
| 191 |
chomp $line; |
| 192 |
my ($r,$g, $b, $name) = split ' ', $line, 4; |
| 193 |
if ($name) { |
| 194 |
$pal{lc $name} = [ $r, $g, $b ]; |
| 195 |
} |
| 196 |
} |
| 197 |
close RGB; |
| 198 |
|
| 199 |
$x_cache{$filename} = { mod_time=>$mod_time, colors=>\%pal }; |
| 200 |
|
| 201 |
return 1; |
| 202 |
} |
| 203 |
else { |
| 204 |
$Imager::ERRSTR = "Cannot open palette file $filename: $!"; |
| 205 |
return; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
sub _get_x_color { |
| 210 |
my %args = @_; |
| 211 |
|
| 212 |
my $filename; |
| 213 |
if ($args{palette}) { |
| 214 |
$filename = $args{palette}; |
| 215 |
} |
| 216 |
elsif (defined $default_x_rgb) { |
| 217 |
unless (length $default_x_rgb) { |
| 218 |
$Imager::ERRSTR = "No X rgb.txt palette found"; |
| 219 |
return (); |
| 220 |
} |
| 221 |
$filename = $default_x_rgb; |
| 222 |
} |
| 223 |
else { |
| 224 |
for my $attempt (@x_search) { |
| 225 |
if (-e $attempt) { |
| 226 |
$filename = $attempt; |
| 227 |
last; |
| 228 |
} |
| 229 |
} |
| 230 |
if (!$filename) { |
| 231 |
$Imager::ERRSTR = "No X rgb.txt palette found"; |
| 232 |
$default_x_rgb = ""; |
| 233 |
return (); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ((!$x_cache{$filename} |
| 238 |
|| (stat $filename)[9] != $x_cache{$filename}{mod_time}) |
| 239 |
&& !_load_x_rgb($filename)) { |
| 240 |
return (); |
| 241 |
} |
| 242 |
|
| 243 |
$default_x_rgb = $filename; |
| 244 |
|
| 245 |
if (!$x_cache{$filename}{colors}{lc $args{name}}) { |
| 246 |
$Imager::ERRSTR = "Color '$args{name}' isn't in $filename"; |
| 247 |
return (); |
| 248 |
} |
| 249 |
|
| 250 |
return @{$x_cache{$filename}{colors}{lc $args{name}}}; |
| 251 |
} |
| 252 |
|
| 253 |
sub _pc_to_byte { |
| 254 |
POSIX::ceil($_[0] * 255 / 100); |
| 255 |
} |
| 256 |
|
| 257 |
sub _rgb_alpha { |
| 258 |
my ($alpha) = @_; |
| 259 |
if ($alpha =~ /^(.*)%\z/) { |
| 260 |
return POSIX::ceil($1 * 255 / 100); |
| 261 |
} |
| 262 |
else { |
| 263 |
return POSIX::ceil($alpha * 255); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
my $rgb_key = qr/rgba?/; |
| 268 |
my $rgb_samp = qr/(\d+(?:\.\d*)?)/; |
| 269 |
my $rgb_pc = qr/(\d+(?:\.\d*)?)%/; |
| 270 |
my $rgb_sep = qr/ *[, ] */; |
| 271 |
my $rgb_rgb = qr/$rgb_samp $rgb_sep $rgb_samp $rgb_sep $rgb_samp/x; |
| 272 |
my $rgb_rgb_pc = qr/$rgb_pc $rgb_sep $rgb_pc $rgb_sep $rgb_pc/x; |
| 273 |
my $rgb_alpha_sep = qr/ *[\/,] */; |
| 274 |
my $rgb_alpha = qr/((?:\.\d+|\d+(?:\.\d*)?)%?)/; |
| 275 |
|
| 276 |
# Parse color spec into an a set of 4 colors |
| 277 |
|
| 278 |
sub _pspec { |
| 279 |
if (@_ == 1 && Scalar::Util::blessed($_[0])) { |
| 280 |
if ($_[0]->isa("Imager::Color")) { |
| 281 |
return $_[0]->rgba; |
| 282 |
} elsif ($_[0]->isa("Imager::Color::Float")) { |
| 283 |
return $_[0]->as_8bit->rgba; |
| 284 |
} |
| 285 |
} |
| 286 |
if (@_ == 1) { |
| 287 |
# CSS Color 4 says that color values are rounded to +Inf |
| 288 |
if ($_[0] =~ /\A$rgb_key\( *$rgb_rgb *\)\z/) { |
| 289 |
return ( POSIX::ceil($1), POSIX::ceil($2), POSIX::ceil($3), 255 ); |
| 290 |
} |
| 291 |
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc *\)\z/) { |
| 292 |
return ( _pc_to_byte($1), _pc_to_byte($2), _pc_to_byte($3), 255 ); |
| 293 |
} |
| 294 |
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb$rgb_alpha_sep$rgb_alpha *\)\z/) { |
| 295 |
return ( POSIX::ceil($1), POSIX::ceil($2), POSIX::ceil($3), _rgb_alpha($4) ); |
| 296 |
} |
| 297 |
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc$rgb_alpha_sep$rgb_alpha *\)\z/) { |
| 298 |
return ( _pc_to_byte($1), _pc_to_byte($2), _pc_to_byte($3), _rgb_alpha($4) ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return (@_,255) if @_ == 3 && !grep /[^\d.+eE-]/, @_; |
| 303 |
return (@_ ) if @_ == 4 && !grep /[^\d.+eE-]/, @_; |
| 304 |
if ($_[0] =~ |
| 305 |
/^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { |
| 306 |
return (hex($1),hex($2),hex($3),hex($4)); |
| 307 |
} |
| 308 |
if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { |
| 309 |
return (hex($1),hex($2),hex($3),255); |
| 310 |
} |
| 311 |
if ($_[0] =~ /^\#([\da-f])([\da-f])([\da-f])$/i) { |
| 312 |
return (hex($1) * 17, hex($2) * 17, hex($3) * 17, 255); |
| 313 |
} |
| 314 |
my %args; |
| 315 |
if (@_ == 1) { |
| 316 |
# a named color |
| 317 |
%args = ( name => @_ ); |
| 318 |
} |
| 319 |
else { |
| 320 |
%args = @_; |
| 321 |
} |
| 322 |
my @result; |
| 323 |
if (exists $args{gray}) { |
| 324 |
@result = $args{gray}; |
| 325 |
} |
| 326 |
elsif (exists $args{grey}) { |
| 327 |
@result = $args{grey}; |
| 328 |
} |
| 329 |
elsif ((exists $args{red} || exists $args{r}) |
| 330 |
&& (exists $args{green} || exists $args{g}) |
| 331 |
&& (exists $args{blue} || exists $args{b})) { |
| 332 |
@result = ( exists $args{red} ? $args{red} : $args{r}, |
| 333 |
exists $args{green} ? $args{green} : $args{g}, |
| 334 |
exists $args{blue} ? $args{blue} : $args{b} ); |
| 335 |
} |
| 336 |
elsif ((exists $args{hue} || exists $args{h}) |
| 337 |
&& (exists $args{saturation} || exists $args{'s'}) |
| 338 |
&& (exists $args{value} || exists $args{v})) { |
| 339 |
my $hue = exists $args{hue} ? $args{hue} : $args{h}; |
| 340 |
my $sat = exists $args{saturation} ? $args{saturation} : $args{'s'}; |
| 341 |
my $val = exists $args{value} ? $args{value} : $args{v}; |
| 342 |
|
| 343 |
@result = _hsv_to_rgb($hue, $sat, $val); |
| 344 |
} |
| 345 |
elsif (exists $args{web}) { |
| 346 |
if ($args{web} =~ /^#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])$/i) { |
| 347 |
@result = (hex($1),hex($2),hex($3)); |
| 348 |
} |
| 349 |
elsif ($args{web} =~ /^#?([\da-f])([\da-f])([\da-f])$/i) { |
| 350 |
@result = (hex($1) * 17, hex($2) * 17, hex($3) * 17); |
| 351 |
} |
| 352 |
} |
| 353 |
elsif ($args{name}) { |
| 354 |
unless (@result = _get_gimp_color(%args)) { |
| 355 |
unless (@result = _get_x_color(%args)) { |
| 356 |
require Imager::Color::Table; |
| 357 |
unless (@result = Imager::Color::Table->get($args{name})) { |
| 358 |
$Imager::ERRSTR = "No color named $args{name} found"; |
| 359 |
return (); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
elsif ($args{gimp}) { |
| 365 |
@result = _get_gimp_color(name=>$args{gimp}, %args); |
| 366 |
} |
| 367 |
elsif ($args{xname}) { |
| 368 |
@result = _get_x_color(name=>$args{xname}, %args); |
| 369 |
} |
| 370 |
elsif ($args{builtin}) { |
| 371 |
require Imager::Color::Table; |
| 372 |
@result = Imager::Color::Table->get($args{builtin}); |
| 373 |
} |
| 374 |
elsif ($args{rgb}) { |
| 375 |
@result = @{$args{rgb}}; |
| 376 |
} |
| 377 |
elsif ($args{rgba}) { |
| 378 |
@result = @{$args{rgba}}; |
| 379 |
return @result if @result == 4; |
| 380 |
} |
| 381 |
elsif ($args{hsv}) { |
| 382 |
@result = _hsv_to_rgb(@{$args{hsv}}); |
| 383 |
} |
| 384 |
elsif ($args{channels}) { |
| 385 |
my @ch = @{$args{channels}}; |
| 386 |
return ( @ch, (0) x (4 - @ch) ); |
| 387 |
} |
| 388 |
elsif (exists $args{channel0} || $args{c0}) { |
| 389 |
my $i = 0; |
| 390 |
while (exists $args{"channel$i"} || exists $args{"c$i"}) { |
| 391 |
push(@result, |
| 392 |
exists $args{"channel$i"} ? $args{"channel$i"} : $args{"c$i"}); |
| 393 |
++$i; |
| 394 |
} |
| 395 |
} |
| 396 |
else { |
| 397 |
$Imager::ERRSTR = "No color specification found"; |
| 398 |
return (); |
| 399 |
} |
| 400 |
if (@result) { |
| 401 |
if (exists $args{alpha} || exists $args{a}) { |
| 402 |
push(@result, exists $args{alpha} ? $args{alpha} : $args{a}); |
| 403 |
} |
| 404 |
while (@result < 4) { |
| 405 |
push(@result, 255); |
| 406 |
} |
| 407 |
return @result; |
| 408 |
} |
| 409 |
return (); |
| 410 |
} |
| 411 |
|
| 412 |
sub new { |
| 413 |
shift; # get rid of class name. |
| 414 |
my @arg = _pspec(@_); |
| 415 |
return @arg ? new_internal($arg[0],$arg[1],$arg[2],$arg[3]) : (); |
| 416 |
} |
| 417 |
|
| 418 |
sub set { |
| 419 |
my $self = shift; |
| 420 |
my @arg = _pspec(@_); |
| 421 |
return @arg ? set_internal($self, $arg[0],$arg[1],$arg[2],$arg[3]) : (); |
| 422 |
} |
| 423 |
|
| 424 |
sub equals { |
| 425 |
my ($self, %opts) = @_; |
| 426 |
|
| 427 |
my $other = $opts{other} |
| 428 |
or return Imager->_set_error("'other' parameter required"); |
| 429 |
my $ignore_alpha = $opts{ignore_alpha} || 0; |
| 430 |
|
| 431 |
my @left = $self->rgba; |
| 432 |
my @right = $other->rgba; |
| 433 |
my $last_chan = $ignore_alpha ? 2 : 3; |
| 434 |
for my $ch (0 .. $last_chan) { |
| 435 |
$left[$ch] == $right[$ch] |
| 436 |
or return; |
| 437 |
} |
| 438 |
|
| 439 |
return 1; |
| 440 |
} |
| 441 |
|
| 442 |
sub CLONE_SKIP { 1 } |
| 443 |
|
| 444 |
# Lifted from Graphics::Color::RGB |
| 445 |
# Thank you very much |
| 446 |
sub hsv { |
| 447 |
my( $self ) = @_; |
| 448 |
|
| 449 |
my( $red, $green, $blue, $alpha ) = $self->rgba; |
| 450 |
my $max = $red; |
| 451 |
my $maxc = 'r'; |
| 452 |
my $min = $red; |
| 453 |
|
| 454 |
if($green > $max) { |
| 455 |
$max = $green; |
| 456 |
$maxc = 'g'; |
| 457 |
} |
| 458 |
if($blue > $max) { |
| 459 |
$max = $blue; |
| 460 |
$maxc = 'b'; |
| 461 |
} |
| 462 |
|
| 463 |
if($green < $min) { |
| 464 |
$min = $green; |
| 465 |
} |
| 466 |
if($blue < $min) { |
| 467 |
$min = $blue; |
| 468 |
} |
| 469 |
|
| 470 |
my ($h, $s, $v); |
| 471 |
|
| 472 |
if($max == $min) { |
| 473 |
$h = 0; |
| 474 |
} |
| 475 |
elsif($maxc eq 'r') { |
| 476 |
$h = 60 * (($green - $blue) / ($max - $min)) % 360; |
| 477 |
} |
| 478 |
elsif($maxc eq 'g') { |
| 479 |
$h = (60 * (($blue - $red) / ($max - $min)) + 120); |
| 480 |
} |
| 481 |
elsif($maxc eq 'b') { |
| 482 |
$h = (60 * (($red - $green) / ($max - $min)) + 240); |
| 483 |
} |
| 484 |
|
| 485 |
$v = $max/255; |
| 486 |
if($max == 0) { |
| 487 |
$s = 0; |
| 488 |
} |
| 489 |
else { |
| 490 |
$s = 1 - ($min / $max); |
| 491 |
} |
| 492 |
|
| 493 |
return int($h), $s, $v, $alpha; |
| 494 |
} |
| 495 |
|
| 496 |
sub as_float { |
| 497 |
my ($self) = @_; |
| 498 |
|
| 499 |
return Imager::Color::Float->new(map { $_ / 255 } $self->rgba); |
| 500 |
} |
| 501 |
|
| 502 |
sub as_css_rgb { |
| 503 |
my ($self) = @_; |
| 504 |
|
| 505 |
my ($r, $g, $b, $alpha) = $self->rgba; |
| 506 |
|
| 507 |
if ($alpha == 255) { |
| 508 |
return "rgb($r, $g, $b)"; |
| 509 |
} |
| 510 |
else { |
| 511 |
my $ac = POSIX::floor($alpha * 1000 / 255) / 10; |
| 512 |
if (POSIX::ceil(POSIX::floor($ac/10) * 10 * 255 / 100) == $alpha) { |
| 513 |
# simple one decimal fraction |
| 514 |
$ac = POSIX::floor($ac/10)/10; |
| 515 |
} |
| 516 |
elsif (POSIX::ceil(POSIX::floor($ac) * 255 / 100) == $alpha) { |
| 517 |
$ac = POSIX::floor($ac) . "%"; |
| 518 |
} |
| 519 |
else { |
| 520 |
$ac = "$ac%"; |
| 521 |
} |
| 522 |
return "rgba($r, $g, $b, $ac)"; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
1; |
| 527 |
|
| 528 |
__END__ |
| 529 |
|
| 530 |
=head1 NAME |
| 531 |
|
| 532 |
Imager::Color - Color handling for Imager. |
| 533 |
|
| 534 |
=head1 SYNOPSIS |
| 535 |
|
| 536 |
use Imager; |
| 537 |
|
| 538 |
$color = Imager::Color->new($red, $green, $blue); |
| 539 |
$color = Imager::Color->new($red, $green, $blue, $alpha); |
| 540 |
$color = Imager::Color->new("#C0C0FF"); # html color specification |
| 541 |
|
| 542 |
$color->set($red, $green, $blue); |
| 543 |
$color->set($red, $green, $blue, $alpha); |
| 544 |
$color->set("#C0C0FF"); # html color specification |
| 545 |
|
| 546 |
($red, $green, $blue, $alpha) = $color->rgba(); |
| 547 |
@hsv = $color->hsv(); |
| 548 |
|
| 549 |
$color->info(); |
| 550 |
|
| 551 |
if ($color->equals(other=>$other_color)) { |
| 552 |
... |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
=head1 DESCRIPTION |
| 557 |
|
| 558 |
This module handles creating color objects used by Imager. The idea |
| 559 |
is that in the future this module will be able to handle color space |
| 560 |
calculations as well. |
| 561 |
|
| 562 |
An Imager color consists of up to four components, each in the range 0 |
| 563 |
to 255. Unfortunately the meaning of the components can change |
| 564 |
depending on the type of image you're dealing with: |
| 565 |
|
| 566 |
=over |
| 567 |
|
| 568 |
=item * |
| 569 |
|
| 570 |
for 3 or 4 channel images the color components are red, green, blue, |
| 571 |
alpha. |
| 572 |
|
| 573 |
=item * |
| 574 |
|
| 575 |
for 1 or 2 channel images the color components are gray, alpha, with |
| 576 |
the other two components ignored. |
| 577 |
|
| 578 |
=back |
| 579 |
|
| 580 |
An alpha value of zero is fully transparent, an alpha value of 255 is |
| 581 |
fully opaque. |
| 582 |
|
| 583 |
=head1 METHODS |
| 584 |
|
| 585 |
=over 4 |
| 586 |
|
| 587 |
=item new |
| 588 |
|
| 589 |
This creates a color object to pass to functions that need a color argument. |
| 590 |
|
| 591 |
=item set |
| 592 |
|
| 593 |
This changes an already defined color. Note that this does not affect any places |
| 594 |
where the color has been used previously. |
| 595 |
|
| 596 |
=item rgba() |
| 597 |
|
| 598 |
This returns the red, green, blue and alpha channels of the color the |
| 599 |
object contains. |
| 600 |
|
| 601 |
=item info |
| 602 |
|
| 603 |
Calling info merely dumps the relevant color to the log. |
| 604 |
|
| 605 |
=item equals(other=>$other_color) |
| 606 |
|
| 607 |
=item equals(other=>$other_color, ignore_alpha=>1) |
| 608 |
|
| 609 |
Compares $self and color $other_color returning true if the color |
| 610 |
components are the same. |
| 611 |
|
| 612 |
Compares all four channels unless C<ignore_alpha> is set. If |
| 613 |
C<ignore_alpha> is set only the first three channels are compared. |
| 614 |
|
| 615 |
=back |
| 616 |
|
| 617 |
You can specify colors in several different ways, you can just supply |
| 618 |
simple values: |
| 619 |
|
| 620 |
=over |
| 621 |
|
| 622 |
=item * |
| 623 |
|
| 624 |
an Imager::Color object |
| 625 |
|
| 626 |
=item * |
| 627 |
|
| 628 |
an Imager::Color::Float object, the ranges of samples are translated from 0.0...1.0 to 0...255. |
| 629 |
|
| 630 |
=item * |
| 631 |
|
| 632 |
simple numeric parameters - if you supply 3 or 4 numeric arguments, you get a color made up of those RGB (and possibly A) components. |
| 633 |
|
| 634 |
=item * |
| 635 |
|
| 636 |
a six hex digit web color, either C<RRGGBB> or C<#RRGGBB> |
| 637 |
|
| 638 |
=item * |
| 639 |
|
| 640 |
an eight hex digit web color, either C<RRGGBBAA> or C<#RRGGBBAA>. |
| 641 |
|
| 642 |
=item * |
| 643 |
|
| 644 |
a CSS rgb() color, based on CSS Color 4. The C<none> keyword is not |
| 645 |
supported and numbers must be simple decimals without exponents. eg. |
| 646 |
|
| 647 |
rgb(50% 50% 100%) |
| 648 |
rgb(0, 0, 255) |
| 649 |
rgb(0.5 0.5 1.0 / 0.8) |
| 650 |
rgb(50%, 50%, 100%, 80%) |
| 651 |
|
| 652 |
Samples from percentages or decimals are rounded up per CSS Color 3 and 4. |
| 653 |
|
| 654 |
This accepts some colors not accepted by the CSS rgb() specification, |
| 655 |
this may change. |
| 656 |
|
| 657 |
=item * |
| 658 |
|
| 659 |
a 3 hex digit web color, C<#RGB> - a value of F becomes 255. |
| 660 |
|
| 661 |
=item * |
| 662 |
|
| 663 |
a color name, from whichever of the gimp C<Named_Colors> file or X |
| 664 |
C<rgb.txt> is found first. The same as using the C<name> keyword. |
| 665 |
|
| 666 |
=back |
| 667 |
|
| 668 |
You can supply named parameters: |
| 669 |
|
| 670 |
=over |
| 671 |
|
| 672 |
=item * |
| 673 |
|
| 674 |
'red', 'green' and 'blue', optionally shortened to 'r', 'g' and 'b'. |
| 675 |
The color components in the range 0 to 255. |
| 676 |
|
| 677 |
# all of the following are equivalent |
| 678 |
my $c1 = Imager::Color->new(red=>100, blue=>255, green=>0); |
| 679 |
my $c2 = Imager::Color->new(r=>100, b=>255, g=>0); |
| 680 |
my $c3 = Imager::Color->new(r=>100, blue=>255, g=>0); |
| 681 |
|
| 682 |
=item * |
| 683 |
|
| 684 |
C<hue>, C<saturation> and C<value>, optionally shortened to C<h>, C<s> and |
| 685 |
C<v>, to specify a HSV color. 0 <= hue < 360, 0 <= s <= 1 and 0 <= v |
| 686 |
<= 1. |
| 687 |
|
| 688 |
# the same as RGB(127,255,127) |
| 689 |
my $c1 = Imager::Color->new(hue=>120, v=>1, s=>0.5); |
| 690 |
my $c1 = Imager::Color->new(hue=>120, value=>1, saturation=>0.5); |
| 691 |
|
| 692 |
=item * |
| 693 |
|
| 694 |
C<web>, which can specify a 6 or 3 hex digit web color, in any of the |
| 695 |
forms C<#RRGGBB>, C<#RGB>, C<RRGGBB> or C<RGB>. |
| 696 |
|
| 697 |
my $c1 = Imager::Color->new(web=>'#FFC0C0'); # pale red |
| 698 |
|
| 699 |
=item * |
| 700 |
|
| 701 |
C<gray> or C<grey> which specifies a single channel, from 0 to 255. |
| 702 |
|
| 703 |
# exactly the same |
| 704 |
my $c1 = Imager::Color->new(gray=>128); |
| 705 |
my $c1 = Imager::Color->new(grey=>128); |
| 706 |
|
| 707 |
=item * |
| 708 |
|
| 709 |
C<rgb> which takes a 3 member arrayref, containing each of the red, |
| 710 |
green and blue values. |
| 711 |
|
| 712 |
# the same |
| 713 |
my $c1 = Imager::Color->new(rgb=>[255, 100, 0]); |
| 714 |
my $c1 = Imager::Color->new(r=>255, g=>100, b=>0); |
| 715 |
|
| 716 |
=item * |
| 717 |
|
| 718 |
C<hsv> which takes a 3 member arrayref, containing each of hue, |
| 719 |
saturation and value. |
| 720 |
|
| 721 |
# the same |
| 722 |
my $c1 = Imager::Color->new(hsv=>[120, 0.5, 1]); |
| 723 |
my $c1 = Imager::Color->new(hue=>120, v=>1, s=>0.5); |
| 724 |
|
| 725 |
=item * |
| 726 |
|
| 727 |
C<gimp> which specifies a color from a GIMP palette file. You can |
| 728 |
specify the file name of the palette file with the 'palette' |
| 729 |
parameter, or let Imager::Color look in various places, typically |
| 730 |
C<$HOME/gimp-1.x/palettes/Named_Colors> with and without the version |
| 731 |
number, and in C</usr/share/gimp/palettes/>. The palette file must |
| 732 |
have color names. |
| 733 |
|
| 734 |
my $c1 = Imager::Color->new(gimp=>'snow'); |
| 735 |
my $c1 = Imager::Color->new(gimp=>'snow', palette=>'testimg/test_gimp_pal); |
| 736 |
|
| 737 |
=item * |
| 738 |
|
| 739 |
C<xname> which specifies a color from an X11 C<rgb.txt> file. You can |
| 740 |
specify the file name of the C<rgb.txt> file with the C<palette> |
| 741 |
parameter, or let Imager::Color look in various places, typically |
| 742 |
C</usr/lib/X11/rgb.txt>. |
| 743 |
|
| 744 |
my $c1 = Imager::Color->new(xname=>'blue') # usually RGB(0, 0, 255) |
| 745 |
|
| 746 |
=item * |
| 747 |
|
| 748 |
C<builtin> which specifies a color from the built-in color table in |
| 749 |
Imager::Color::Table. The colors in this module are the same as the |
| 750 |
default X11 C<rgb.txt> file. |
| 751 |
|
| 752 |
my $c1 = Imager::Color->new(builtin=>'black') # always RGB(0, 0, 0) |
| 753 |
|
| 754 |
=item * |
| 755 |
|
| 756 |
C<name> which specifies a name from either a GIMP palette, an X |
| 757 |
C<rgb.txt> file or the built-in color table, whichever is found first. |
| 758 |
|
| 759 |
=item * |
| 760 |
|
| 761 |
'channel0', 'channel1', etc, each of which specifies a single channel. These can be abbreviated to 'c0', 'c1' etc. |
| 762 |
|
| 763 |
=item * |
| 764 |
|
| 765 |
'channels' which takes an arrayref of the channel values. |
| 766 |
|
| 767 |
=back |
| 768 |
|
| 769 |
Optionally you can add an alpha channel to a color with the 'alpha' or |
| 770 |
'a' parameter. |
| 771 |
|
| 772 |
These color specifications can be used for both constructing new |
| 773 |
colors with the new() method and modifying existing colors with the |
| 774 |
set() method. |
| 775 |
|
| 776 |
=head1 METHODS |
| 777 |
|
| 778 |
=over |
| 779 |
|
| 780 |
=item hsv() |
| 781 |
|
| 782 |
my($h, $s, $v, $alpha) = $color->hsv(); |
| 783 |
|
| 784 |
Returns the color as a Hue/Saturation/Value/Alpha tuple. |
| 785 |
|
| 786 |
=item red |
| 787 |
|
| 788 |
=item green |
| 789 |
|
| 790 |
=item blue |
| 791 |
|
| 792 |
=item alpha |
| 793 |
|
| 794 |
Returns the respective component as an integer from 0 to 255. |
| 795 |
|
| 796 |
=item as_float |
| 797 |
|
| 798 |
Returns the color as a L<Imager::Color::Float> object. |
| 799 |
|
| 800 |
=item as_css_rgb |
| 801 |
|
| 802 |
Returns the color as a CSS rgb() format color. This is always |
| 803 |
returned in the byte form, eg. rgb(255 128 64). |
| 804 |
|
| 805 |
If the alpha is not full coverage (255) it will be rounded if the |
| 806 |
result of converting the color back to an 8 bit color would return the |
| 807 |
same alpha, eg. if the color alpha is 128, it will be formatted as |
| 808 |
0.5, not as the more precise 50.2%. |
| 809 |
|
| 810 |
=back |
| 811 |
|
| 812 |
=head1 AUTHOR |
| 813 |
|
| 814 |
Arnar M. Hrafnkelsson, [email protected] |
| 815 |
And a great deal of help from others - see the C<README> for a complete |
| 816 |
list. |
| 817 |
|
| 818 |
=head1 SEE ALSO |
| 819 |
|
| 820 |
Imager(3), Imager::Color |
| 821 |
http://imager.perl.org/ |
| 822 |
|
| 823 |
=cut |