| 1 |
package Imager::Color::Float; |
| 2 |
use 5.006; |
| 3 |
use Imager; |
| 4 |
use strict; |
| 5 |
use Scalar::Util (); |
| 6 |
|
| 7 |
our $VERSION = "1.008"; |
| 8 |
|
| 9 |
# It's just a front end to the XS creation functions. |
| 10 |
|
| 11 |
sub _rgb_alpha { |
| 12 |
my ($alpha) = @_; |
| 13 |
if ($alpha =~ /^(.*)%\z/) { |
| 14 |
return $1 / 100; |
| 15 |
} |
| 16 |
else { |
| 17 |
return $alpha; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
my $rgb_key = qr/rgba?/; |
| 22 |
my $rgb_samp = qr/(\d+(?:\.\d*)?)/; |
| 23 |
my $rgb_pc = qr/(\d+(?:\.\d*)?)%/; |
| 24 |
my $rgb_sep = qr/ *[, ] */; |
| 25 |
my $rgb_rgb = qr/$rgb_samp $rgb_sep $rgb_samp $rgb_sep $rgb_samp/x; |
| 26 |
my $rgb_rgb_pc = qr/$rgb_pc $rgb_sep $rgb_pc $rgb_sep $rgb_pc/x; |
| 27 |
my $rgb_alpha_sep = qr/ *[\/,] */; |
| 28 |
my $rgb_alpha = qr/((?:\.\d+|\d+(?:\.\d*)?)%?)/; |
| 29 |
|
| 30 |
# Parse color spec into an a set of 4 colors |
| 31 |
|
| 32 |
sub _pspec { |
| 33 |
if (@_ == 1 && Scalar::Util::blessed($_[0])) { |
| 34 |
if ($_[0]->isa("Imager::Color::Float")) { |
| 35 |
return $_[0]->rgba; |
| 36 |
} elsif ($_[0]->isa("Imager::Color")) { |
| 37 |
return $_[0]->as_float->rgba; |
| 38 |
} |
| 39 |
} |
| 40 |
return (@_,1) if @_ == 3; |
| 41 |
return (@_ ) if @_ == 4; |
| 42 |
if ($_[0] =~ |
| 43 |
/^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { |
| 44 |
return (hex($1)/255,hex($2)/255,hex($3)/255,hex($4)/255); |
| 45 |
} |
| 46 |
if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { |
| 47 |
return (hex($1)/255,hex($2)/255,hex($3)/255,1); |
| 48 |
} |
| 49 |
if (@_ == 1) { |
| 50 |
# CSS Color 4 says that color values are rounded to +Inf |
| 51 |
if ($_[0] =~ /\A$rgb_key\( *$rgb_rgb *\)\z/) { |
| 52 |
return ( $1 / 255, $2 / 255, $3 / 255, 1.0 ); |
| 53 |
} |
| 54 |
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc *\)\z/) { |
| 55 |
return ( $1 / 100, $2 / 100, $3 / 100, 1.0 ); |
| 56 |
} |
| 57 |
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb$rgb_alpha_sep$rgb_alpha *\)\z/) { |
| 58 |
return ( $1 / 255, $2 / 255, $3 / 255, _rgb_alpha($4) ); |
| 59 |
} |
| 60 |
elsif ($_[0] =~ /\A$rgb_key\( *$rgb_rgb_pc$rgb_alpha_sep$rgb_alpha *\)\z/) { |
| 61 |
return ( $1 / 100, $2 / 100, $3 / 100, _rgb_alpha($4) ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return (); |
| 66 |
} |
| 67 |
|
| 68 |
sub new { |
| 69 |
shift; # get rid of class name. |
| 70 |
my @arg = _pspec(@_); |
| 71 |
return @arg ? new_internal($arg[0],$arg[1],$arg[2],$arg[3]) : (); |
| 72 |
} |
| 73 |
|
| 74 |
sub set { |
| 75 |
my $self = shift; |
| 76 |
my @arg = _pspec(@_); |
| 77 |
return @arg ? set_internal($self, $arg[0],$arg[1],$arg[2],$arg[3]) : (); |
| 78 |
} |
| 79 |
|
| 80 |
sub CLONE_SKIP { 1 } |
| 81 |
|
| 82 |
sub as_8bit { |
| 83 |
my ($self) = @_; |
| 84 |
|
| 85 |
my @out; |
| 86 |
for my $s ($self->rgba) { |
| 87 |
my $result = 0+sprintf("%.f", $s * 255); |
| 88 |
$result = $result < 0 ? 0 : |
| 89 |
$result > 255 ? 255 : |
| 90 |
$result; |
| 91 |
push @out, $result; |
| 92 |
} |
| 93 |
|
| 94 |
return Imager::Color->new(@out); |
| 95 |
} |
| 96 |
|
| 97 |
sub as_css_rgb { |
| 98 |
my ($self) = @_; |
| 99 |
|
| 100 |
my (@rgb) = $self->rgba; |
| 101 |
my $alpha = pop @rgb; |
| 102 |
# check if they're all representable as byte type samples |
| 103 |
my $can_byte = 1; |
| 104 |
for my $s (@rgb) { |
| 105 |
if (abs(sprintf("%.0f", $s * 255) - $s*255) > 0.0001) { |
| 106 |
$can_byte = 0; |
| 107 |
last; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if ($alpha == 1.0) { |
| 112 |
if ($can_byte) { |
| 113 |
return sprintf("rgb(%.0f, %.0f, %.0f)", map { 255 * $_ } @rgb); |
| 114 |
} |
| 115 |
else { |
| 116 |
# avoid outputting 2 decimals unless the precision is needed |
| 117 |
my ($rpc, $gpc, $bpc) = map { 0 + sprintf("%.2f", 100 * $_) } @rgb; |
| 118 |
return "rgb($rpc% $gpc% $bpc%)"; |
| 119 |
} |
| 120 |
} |
| 121 |
else { |
| 122 |
my $apf = 0+sprintf("%.4f", $alpha); |
| 123 |
if ($can_byte) { |
| 124 |
return sprintf("rgba(%.0f, %.0f, %.0f, %s)", ( map { 255 * $_ } @rgb ), $apf); |
| 125 |
} |
| 126 |
else { |
| 127 |
# avoid outputting 2 decimals unless the precision is needed |
| 128 |
my ($rpc, $gpc, $bpc) = map { 0 + sprintf("%.2f", 100 * $_) } @rgb; |
| 129 |
return "rgba($rpc% $gpc% $bpc% / $apf)"; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
1; |
| 135 |
|
| 136 |
__END__ |
| 137 |
|
| 138 |
=head1 NAME |
| 139 |
|
| 140 |
Imager::Color::Float - Rough floating point sample color handling |
| 141 |
|
| 142 |
=head1 SYNOPSIS |
| 143 |
|
| 144 |
$color = Imager::Color->new($red, $green, $blue); |
| 145 |
$color = Imager::Color->new($red, $green, $blue, $alpha); |
| 146 |
$color = Imager::Color->new("#C0C0FF"); # html color specification |
| 147 |
|
| 148 |
$color->set($red, $green, $blue); |
| 149 |
$color->set($red, $green, $blue, $alpha); |
| 150 |
$color->set("#C0C0FF"); # html color specification |
| 151 |
|
| 152 |
($red, $green, $blue, $alpha) = $color->rgba(); |
| 153 |
@hsv = $color->hsv(); # not implemented but proposed |
| 154 |
my $c8 = $color->as_8bit; |
| 155 |
|
| 156 |
$color->info(); |
| 157 |
|
| 158 |
|
| 159 |
=head1 DESCRIPTION |
| 160 |
|
| 161 |
This module handles creating color objects used by Imager. The idea |
| 162 |
is that in the future this module will be able to handle color space |
| 163 |
calculations as well. |
| 164 |
|
| 165 |
A floating point Imager color consists of up to four components, each |
| 166 |
in the range 0.0 to 1.0. Unfortunately the meaning of the components |
| 167 |
can change depending on the type of image you're dealing with: |
| 168 |
|
| 169 |
=over |
| 170 |
|
| 171 |
=item * |
| 172 |
|
| 173 |
for 3 or 4 channel images the color components are red, green, blue, |
| 174 |
alpha. |
| 175 |
|
| 176 |
=item * |
| 177 |
|
| 178 |
for 1 or 2 channel images the color components are gray, alpha, with |
| 179 |
the other two components ignored. |
| 180 |
|
| 181 |
=back |
| 182 |
|
| 183 |
An alpha value of zero is fully transparent, an alpha value of 1.0 is |
| 184 |
fully opaque. |
| 185 |
|
| 186 |
=head1 METHODS |
| 187 |
|
| 188 |
=over 4 |
| 189 |
|
| 190 |
=item new |
| 191 |
|
| 192 |
This creates a color object to pass to functions that need a color argument. |
| 193 |
|
| 194 |
=item set |
| 195 |
|
| 196 |
This changes an already defined color. Note that this does not affect any places |
| 197 |
where the color has been used previously. |
| 198 |
|
| 199 |
=item rgba() |
| 200 |
|
| 201 |
This returns the red, green, blue and alpha channels of the color the |
| 202 |
object contains. |
| 203 |
|
| 204 |
=item info |
| 205 |
|
| 206 |
Calling info merely dumps the relevant color to the log. |
| 207 |
|
| 208 |
=item red |
| 209 |
|
| 210 |
=item green |
| 211 |
|
| 212 |
=item blue |
| 213 |
|
| 214 |
=item alpha |
| 215 |
|
| 216 |
Returns the respective component as a floating point value typically |
| 217 |
from 0 to 1.0. |
| 218 |
|
| 219 |
=item as_8bit |
| 220 |
|
| 221 |
Returns the color as the roughly equivalent 8-bit Imager::Color |
| 222 |
object. Samples below zero or above 1.0 are clipped. |
| 223 |
|
| 224 |
=item as_css_rgb |
| 225 |
|
| 226 |
Formats the color as a CSS rgb() style color. If the color is closely |
| 227 |
representable as byte style syntax, eg rgb(255, 128, 128), it will be |
| 228 |
returned in that form, otherwise the samples are formatted as |
| 229 |
percentages with up to 2 decimal places. |
| 230 |
|
| 231 |
=back |
| 232 |
|
| 233 |
=head2 Setting colors |
| 234 |
|
| 235 |
The new() and set() methods can accept the following parameters: |
| 236 |
|
| 237 |
=over |
| 238 |
|
| 239 |
=item * |
| 240 |
|
| 241 |
an Imager::Color::Float object |
| 242 |
|
| 243 |
=item * |
| 244 |
|
| 245 |
an Imager::Color object, the ranges of samples are translated from 0...255 to 0.0...1.0. |
| 246 |
|
| 247 |
=item * |
| 248 |
|
| 249 |
3 values, treated as red, green, blue |
| 250 |
|
| 251 |
=item * |
| 252 |
|
| 253 |
4 values, treated as red, green, blue, alpha |
| 254 |
|
| 255 |
=item * |
| 256 |
|
| 257 |
an 8 character hex color, optionally preceded by C<#>. |
| 258 |
|
| 259 |
=item * |
| 260 |
|
| 261 |
a 6 character hex color, optionally preceded by C<#>. |
| 262 |
|
| 263 |
=item * |
| 264 |
|
| 265 |
a CSS rgb() color, based on CSS Color 4. The C<none> keyword is not |
| 266 |
supported and numbers must be simple decimals without exponents. eg. |
| 267 |
|
| 268 |
rgb(50% 50% 100%) |
| 269 |
rgb(0, 0, 255) |
| 270 |
rgb(0.5 0.5 1.0 / 0.8) |
| 271 |
rgb(50%, 50%, 100%, 80%) |
| 272 |
|
| 273 |
This accepts some colors not accepted by the CSS rgb() specification, |
| 274 |
this may change. |
| 275 |
|
| 276 |
=back |
| 277 |
|
| 278 |
=head1 AUTHOR |
| 279 |
|
| 280 |
Arnar M. Hrafnkelsson, [email protected] |
| 281 |
And a great deal of help from others - see the C<README> for a complete |
| 282 |
list. |
| 283 |
|
| 284 |
=head1 SEE ALSO |
| 285 |
|
| 286 |
Imager(3), Imager::Color. |
| 287 |
|
| 288 |
http://imager.perl.org/ |
| 289 |
|
| 290 |
=cut |
| 291 |
|