| 1 |
#!perl -w |
| 2 |
# Before `make install' is performed this script should be runnable with |
| 3 |
# `make test'. After `make install' it should work as `perl test.pl' |
| 4 |
|
| 5 |
######################### We start with some black magic to print on failure. |
| 6 |
|
| 7 |
use Test::More; |
| 8 |
|
| 9 |
use Imager; |
| 10 |
use Imager::Test qw(is_fcolor3 is_fcolor4 is_color4); |
| 11 |
|
| 12 |
-d "testout" or mkdir "testout"; |
| 13 |
|
| 14 |
Imager->open_log(log => "testout/t15color.log"); |
| 15 |
|
| 16 |
my $c1 = Imager::Color->new(100, 150, 200, 250); |
| 17 |
is_color4($c1, 100, 150, 200, 250, 'simple 4-arg'); |
| 18 |
my $c2 = Imager::Color->new(100, 150, 200); |
| 19 |
is_color4($c2, 100, 150, 200, 255, 'simple 3-arg'); |
| 20 |
my $c3 = Imager::Color->new("#6496C8"); |
| 21 |
is_color4($c3, 100, 150, 200, 255, 'web color'); |
| 22 |
# crashes in Imager-0.38pre8 and earlier |
| 23 |
my @foo; |
| 24 |
for (1..1000) { |
| 25 |
push(@foo, Imager::Color->new("#FFFFFF")); |
| 26 |
} |
| 27 |
my $fail; |
| 28 |
for (@foo) { |
| 29 |
Imager::Color::set_internal($_, 128, 128, 128, 128) == $_ or ++$fail; |
| 30 |
Imager::Color::set_internal($_, 128, 129, 130, 131) == $_ or ++$fail; |
| 31 |
test_col($_, 128, 129, 130, 131) or ++$fail; |
| 32 |
} |
| 33 |
ok(!$fail, 'consitency check'); |
| 34 |
|
| 35 |
# test the new OO methods |
| 36 |
SKIP: |
| 37 |
{ |
| 38 |
skip "no X rgb.txt found", 1 |
| 39 |
unless grep -r, Imager::Color::_test_x_palettes(); |
| 40 |
is_color4(Imager::Color->new(xname=>'blue'), 0, 0, 255, 255, 'xname'); |
| 41 |
} |
| 42 |
|
| 43 |
my @oo_tests = |
| 44 |
( |
| 45 |
[ |
| 46 |
[ r=>100, g=>150, b=>200 ], |
| 47 |
100, 150, 200, 255, |
| 48 |
'r g b' |
| 49 |
], |
| 50 |
[ |
| 51 |
[ red=>101, green=>151, blue=>201 ], |
| 52 |
101, 151, 201, 255, |
| 53 |
'red green blue' |
| 54 |
], |
| 55 |
[ |
| 56 |
[ grey=>102 ], |
| 57 |
102, 255, 255, 255, |
| 58 |
'grey' |
| 59 |
], |
| 60 |
[ |
| 61 |
[ gray=>103 ], |
| 62 |
103, 255, 255, 255, |
| 63 |
'gray' |
| 64 |
], |
| 65 |
[ |
| 66 |
[ gimp=>'snow' , palette=>'testimg/test_gimp_pal' ], |
| 67 |
255, 250, 250, 255, |
| 68 |
'gimp' |
| 69 |
], |
| 70 |
[ |
| 71 |
[ h=>0, 's'=>0, 'v'=>1.0 ], |
| 72 |
255, 255, 255, 255, |
| 73 |
'h s v' |
| 74 |
], |
| 75 |
[ |
| 76 |
[ h=>0, 's'=>1, v=>1 ], |
| 77 |
255, 0, 0, 255, |
| 78 |
'h s v again' |
| 79 |
], |
| 80 |
[ |
| 81 |
[ web=>'#808182' ], |
| 82 |
128, 129, 130, 255, |
| 83 |
'web 6 digit' |
| 84 |
], |
| 85 |
[ |
| 86 |
[ web=>'#123' ], |
| 87 |
0x11, 0x22, 0x33, 255, |
| 88 |
'web 3 digit' |
| 89 |
], |
| 90 |
[ |
| 91 |
[ rgb=>[ 255, 150, 121 ] ], |
| 92 |
255, 150, 121, 255, |
| 93 |
'rgb arrayref' |
| 94 |
], |
| 95 |
[ |
| 96 |
[ rgba=>[ 255, 150, 121, 128 ] ], |
| 97 |
255, 150, 121, 128, |
| 98 |
'rgba arrayref' |
| 99 |
], |
| 100 |
[ |
| 101 |
[ hsv=>[ 0, 1, 1 ] ], |
| 102 |
255, 0, 0, 255, |
| 103 |
'hsv arrayref' |
| 104 |
], |
| 105 |
[ |
| 106 |
[ channel0=>129, channel1=>130, channel2=>131, channel3=>134 ], |
| 107 |
129, 130, 131, 134, |
| 108 |
'channel0-3' |
| 109 |
], |
| 110 |
[ |
| 111 |
[ c0=>129, c1=>130, c2=>131, c3=>134 ], |
| 112 |
129, 130, 131, 134, |
| 113 |
'c0-3', |
| 114 |
], |
| 115 |
[ |
| 116 |
[ channels=>[ 200, ] ], |
| 117 |
200, 0, 0, 0, |
| 118 |
'channels arrayref (1)' |
| 119 |
], |
| 120 |
[ |
| 121 |
[ channels=>[ 200, 201 ] ], |
| 122 |
200, 201, 0, 0, |
| 123 |
'channels arrayref (2)' |
| 124 |
], |
| 125 |
[ |
| 126 |
[ channels=>[ 200, 201, 203 ] ], |
| 127 |
200, 201, 203, 0, |
| 128 |
'channels arrayref (3)' |
| 129 |
], |
| 130 |
[ |
| 131 |
[ channels=>[ 200, 201, 203, 204 ] ], |
| 132 |
200, 201, 203, 204, |
| 133 |
'channels arrayref (4)' |
| 134 |
], |
| 135 |
[ |
| 136 |
[ name=>'snow', palette=>'testimg/test_gimp_pal' ], |
| 137 |
255, 250, 250, 255, |
| 138 |
'name' |
| 139 |
], |
| 140 |
|
| 141 |
# rgb() without alpha |
| 142 |
[ |
| 143 |
[ "rgb(255 128 128)" ], |
| 144 |
255, 128, 128, 255, |
| 145 |
"rgb non-percent, spaces" |
| 146 |
], |
| 147 |
[ |
| 148 |
[ "rgb(255, 128, 128)" ], |
| 149 |
255, 128, 128, 255, |
| 150 |
"rgb non-percent, commas simple" |
| 151 |
], |
| 152 |
[ |
| 153 |
[ "rgb(255 ,128 ,128)" ], |
| 154 |
255, 128, 128, 255, |
| 155 |
"rgb non-percent, commas less simple" |
| 156 |
], |
| 157 |
[ |
| 158 |
[ "rgb(254.5 127.5 127.5)" ], |
| 159 |
255, 128, 128, 255, |
| 160 |
"rgb non-percent with decimals, spaces" |
| 161 |
], |
| 162 |
[ |
| 163 |
[ "rgb(254.5,127.5,126.2)" ], |
| 164 |
255, 128, 127, 255, |
| 165 |
"rgb non-percent decimals, commas" |
| 166 |
], |
| 167 |
[ |
| 168 |
[ "rgb(254.5 , 127.5 , 126.2)" ], |
| 169 |
255, 128, 127, 255, |
| 170 |
"rgb non-percent decimals, commas more spaces" |
| 171 |
], |
| 172 |
[ |
| 173 |
[ "rgb(100% 50% 50%)" ], |
| 174 |
255, 128, 128, 255, |
| 175 |
"rgb percent, spaces" |
| 176 |
], |
| 177 |
[ |
| 178 |
[ "rgb(100%, 50%, 50%)" ], |
| 179 |
255, 128, 128, 255, |
| 180 |
"rgb percent, commas" |
| 181 |
], |
| 182 |
[ |
| 183 |
[ "rgb(99.99% 49.99% 74.98%)" ], |
| 184 |
255, 128, 192, 255, |
| 185 |
"rgb percent decimals, spaces" |
| 186 |
], |
| 187 |
[ |
| 188 |
[ "rgb(99.99%, 49.99%, 49.98%)" ], |
| 189 |
255, 128, 128, 255, |
| 190 |
"rgb percent decimals, commas" |
| 191 |
], |
| 192 |
# rgb() with alpha |
| 193 |
[ |
| 194 |
[ "rgb(255 128 128 / 0.5)" ], |
| 195 |
255, 128, 128, 128, |
| 196 |
"rgba non-percent, spaces" |
| 197 |
], |
| 198 |
[ |
| 199 |
[ "rgb(255, 128, 128, 0.25)" ], |
| 200 |
255, 128, 128, 64, |
| 201 |
"rgba non-percent, commas simple" |
| 202 |
], |
| 203 |
[ |
| 204 |
[ "rgb(255 ,128 ,128 , 0.75)" ], |
| 205 |
255, 128, 128, 192, |
| 206 |
"rgba non-percent, commas less simple" |
| 207 |
], |
| 208 |
[ |
| 209 |
[ "rgba(254.5 127.5 127.5 / 0.1)" ], |
| 210 |
255, 128, 128, 26, |
| 211 |
"rgba non-percent with decimals, spaces" |
| 212 |
], |
| 213 |
[ |
| 214 |
[ "rgb(254.5,127.5,126.2,1.0)" ], |
| 215 |
255, 128, 127, 255, |
| 216 |
"rgba non-percent decimals, commas" |
| 217 |
], |
| 218 |
[ |
| 219 |
[ "rgb(254.5 , 127.5 , 126.2, 0.9)" ], |
| 220 |
255, 128, 127, 230, |
| 221 |
"rgba non-percent decimals, commas more spaces" |
| 222 |
], |
| 223 |
[ |
| 224 |
[ "rgb(100% 50% 50% / 0.2)" ], |
| 225 |
255, 128, 128, 51, |
| 226 |
"rgba percent, spaces" |
| 227 |
], |
| 228 |
[ |
| 229 |
[ "rgb(100%, 50%, 50%, 30%)" ], |
| 230 |
255, 128, 128, 77, |
| 231 |
"rgba percent, commas" |
| 232 |
], |
| 233 |
[ |
| 234 |
[ "rgb(99.99% 49.99% 74.98% / 49.9%)" ], |
| 235 |
255, 128, 192, 128, |
| 236 |
"rgba percent decimals, spaces" |
| 237 |
], |
| 238 |
[ |
| 239 |
[ "rgb(99.99%, 49.99%, 49.98%, 50.0%)" ], |
| 240 |
255, 128, 128, 128, |
| 241 |
"rgba percent decimals, commas" |
| 242 |
], |
| 243 |
); |
| 244 |
|
| 245 |
for my $test (@oo_tests) { |
| 246 |
my ($parms, $r, $g, $b, $a, $name) = @$test; |
| 247 |
my $c = Imager::Color->new(@$parms); |
| 248 |
is_color4($c, $r, $g, $b, $a, $name); |
| 249 |
|
| 250 |
my $rgb = $c->as_css_rgb; |
| 251 |
is_color4(Imager::Color->new($rgb), $r, $g, $b, $a, "$name via rgb $rgb"); |
| 252 |
} |
| 253 |
|
| 254 |
{ |
| 255 |
# test our promises for as_css_rgb |
| 256 |
is(Imager::Color->new(255, 128, 64)->as_css_rgb, "rgb(255, 128, 64)", |
| 257 |
"as_css_rgb: simple rgb"); |
| 258 |
is(Imager::Color->new(255, 128, 64, 128)->as_css_rgb, "rgba(255, 128, 64, 0.5)", |
| 259 |
"as_css_rgb: simple rgb with 1 decimal alpha"); |
| 260 |
is(Imager::Color->new(255, 128, 64, 64)->as_css_rgb, "rgba(255, 128, 64, 25%)", |
| 261 |
"as_css_rgb: simple rgb with 2 decimal alpha"); |
| 262 |
is(Imager::Color->new(255, 128, 64, 24)->as_css_rgb, "rgba(255, 128, 64, 9.4%)", |
| 263 |
"as_css_rgb: simple rgb with alpha we don't simplify"); |
| 264 |
} |
| 265 |
|
| 266 |
# test the internal HSV <=> RGB conversions |
| 267 |
# these values were generated using the GIMP |
| 268 |
# all but hue is 0..360, saturation and value from 0 to 1 |
| 269 |
# rgb from 0 to 255 |
| 270 |
my @hsv_vs_rgb = |
| 271 |
( |
| 272 |
{ hsv => [ 0, 0.2, 0.1 ], rgb=> [ 25, 20, 20 ] }, |
| 273 |
{ hsv => [ 0, 0.5, 1.0 ], rgb => [ 255, 127, 127 ] }, |
| 274 |
{ hsv => [ 100, 0.5, 1.0 ], rgb => [ 170, 255, 127 ] }, |
| 275 |
{ hsv => [ 100, 1.0, 1.0 ], rgb=> [ 85, 255, 0 ] }, |
| 276 |
{ hsv => [ 335, 0.5, 0.5 ], rgb=> [127, 63, 90 ] }, |
| 277 |
); |
| 278 |
|
| 279 |
use Imager::Color::Float; |
| 280 |
my $test_num = 23; |
| 281 |
my $index = 0; |
| 282 |
for my $entry (@hsv_vs_rgb) { |
| 283 |
print "# color index $index\n"; |
| 284 |
my $hsv = $entry->{hsv}; |
| 285 |
my $rgb = $entry->{rgb}; |
| 286 |
my $fhsvo = Imager::Color::Float->new($hsv->[0]/360.0, $hsv->[1], $hsv->[2]); |
| 287 |
my $fc = Imager::Color::Float::i_hsv_to_rgb($fhsvo); |
| 288 |
is_fcolor3($fc, $rgb->[0]/255, $rgb->[1]/255, $rgb->[2]/255, 0.01, |
| 289 |
"i_hsv_to_rgbf $index"); |
| 290 |
my $fc2 = Imager::Color::Float::i_rgb_to_hsv($fc); |
| 291 |
is_fcolor3($fc2, $hsv->[0]/360.0, $hsv->[1], $hsv->[2], "i_rgbf_to_hsv $index"); |
| 292 |
|
| 293 |
my $hsvo = Imager::Color->new($hsv->[0]*255/360.0, $hsv->[1] * 255, |
| 294 |
$hsv->[2] * 255); |
| 295 |
my $c = Imager::Color::i_hsv_to_rgb($hsvo); |
| 296 |
color_close_enough("i_hsv_to_rgb $index", @$rgb, $c); |
| 297 |
my $c2 = Imager::Color::i_rgb_to_hsv($c); |
| 298 |
color_close_enough_hsv("i_rgb_to_hsv $index", $hsv->[0]*255/360.0, $hsv->[1] * 255, |
| 299 |
$hsv->[2] * 255, $c2); |
| 300 |
++$index; |
| 301 |
} |
| 302 |
|
| 303 |
# check the built-ins table |
| 304 |
is_color4(Imager::Color->new(builtin=>'black'), 0, 0, 0, 255, 'builtin black'); |
| 305 |
|
| 306 |
{ |
| 307 |
my $c1 = Imager::Color->new(255, 255, 255, 0); |
| 308 |
my $c2 = Imager::Color->new(255, 255, 255, 255); |
| 309 |
ok(!$c1->equals(other=>$c2), "not equal no ignore alpha"); |
| 310 |
ok(scalar($c1->equals(other=>$c2, ignore_alpha=>1)), |
| 311 |
"equal with ignore alpha"); |
| 312 |
ok($c1->equals(other=>$c1), "equal to itself"); |
| 313 |
} |
| 314 |
|
| 315 |
{ # http://rt.cpan.org/NoAuth/Bug.html?id=13143 |
| 316 |
# Imager::Color->new(color_name) warning if HOME environment variable not set |
| 317 |
local $ENV{HOME}; |
| 318 |
my @warnings; |
| 319 |
local $SIG{__WARN__} = sub { push @warnings, "@_" }; |
| 320 |
|
| 321 |
# presumably no-one will name a color like this. |
| 322 |
my $c1 = Imager::Color->new(gimp=>"ABCDEFGHIJKLMNOP"); |
| 323 |
is(@warnings, 0, "Should be no warnings") |
| 324 |
or do { print "# $_" for @warnings }; |
| 325 |
} |
| 326 |
|
| 327 |
{ |
| 328 |
# float color from hex triple |
| 329 |
my $f3white = Imager::Color::Float->new("#FFFFFF"); |
| 330 |
is_fcolor4($f3white, 1.0, 1.0, 1.0, 1.0, "check color #FFFFFF"); |
| 331 |
my $f3black = Imager::Color::Float->new("#000000"); |
| 332 |
is_fcolor4($f3black, 0, 0, 0, 1.0, "check color #000000"); |
| 333 |
my $f3grey = Imager::Color::Float->new("#808080"); |
| 334 |
is_fcolor4($f3grey, 0x80/0xff, 0x80/0xff, 0x80/0xff, 1.0, "check color #808080"); |
| 335 |
|
| 336 |
my $f4white = Imager::Color::Float->new("#FFFFFF80"); |
| 337 |
is_fcolor4($f4white, 1.0, 1.0, 1.0, 0x80/0xff, "check color #FFFFFF80"); |
| 338 |
} |
| 339 |
|
| 340 |
{ |
| 341 |
# fail to make a color |
| 342 |
ok(!Imager::Color::Float->new("-unknown-"), "try to make float color -unknown-"); |
| 343 |
} |
| 344 |
|
| 345 |
{ |
| 346 |
# set after creation |
| 347 |
my $c = Imager::Color::Float->new(0, 0, 0); |
| 348 |
is_fcolor4($c, 0, 0, 0, 1.0, "check simple init of float color"); |
| 349 |
ok($c->set(1.0, 0.5, 0.25, 1.0), "set() the color"); |
| 350 |
is_fcolor4($c, 1.0, 0.5, 0.25, 1.0, "check after set"); |
| 351 |
|
| 352 |
ok(!$c->set("-unknown-"), "set to unknown"); |
| 353 |
} |
| 354 |
|
| 355 |
{ |
| 356 |
# test ->hsv |
| 357 |
my $c = Imager::Color->new(255, 0, 0); |
| 358 |
my($h,$s,$v) = $c->hsv; |
| 359 |
is($h,0,'red hue'); |
| 360 |
is($s,1,'red saturation'); |
| 361 |
is($v,1,'red value'); |
| 362 |
|
| 363 |
$c = Imager::Color->new(0, 255, 0); |
| 364 |
($h,$s,$v) = $c->hsv; |
| 365 |
is($h,120,'green hue'); |
| 366 |
is($s,1,'green saturation'); |
| 367 |
is($v,1,'green value'); |
| 368 |
|
| 369 |
$c = Imager::Color->new(0, 0, 255); |
| 370 |
($h,$s,$v) = $c->hsv; |
| 371 |
is($h,240,'blue hue'); |
| 372 |
is($s,1,'blue saturation'); |
| 373 |
is($v,1,'blue value'); |
| 374 |
|
| 375 |
$c = Imager::Color->new(255, 255, 255); |
| 376 |
($h,$s,$v) = $c->hsv; |
| 377 |
is($h,0,'white hue'); |
| 378 |
is($s,0,'white saturation'); |
| 379 |
is($v,1,'white value'); |
| 380 |
|
| 381 |
$c = Imager::Color->new(0, 0, 0); |
| 382 |
($h,$s,$v) = $c->hsv; |
| 383 |
is($h,0,'black hue'); |
| 384 |
is($s,0,'black saturation'); |
| 385 |
is($v,0,'black value'); |
| 386 |
} |
| 387 |
|
| 388 |
# test conversion to float color |
| 389 |
{ |
| 390 |
my $c = Imager::Color->new(255, 128, 64); |
| 391 |
my $cf = $c->as_float; |
| 392 |
is_fcolor4($cf, 1.0, 128/255, 64/255, 1.0, "check color converted to float"); |
| 393 |
} |
| 394 |
|
| 395 |
# test conversion to 8bit color |
| 396 |
{ |
| 397 |
# more cases than above |
| 398 |
my @tests = |
| 399 |
( |
| 400 |
[ "simple black", [ 0, 0, 0 ], [ 0, 0, 0, 255 ] ], |
| 401 |
[ "range clip", [ -0.01, 0, 1.01 ], [ 0, 0, 255, 255 ] ], |
| 402 |
); |
| 403 |
for my $test (@tests) { |
| 404 |
my ($name, $float, $expect) = @$test; |
| 405 |
my $f = Imager::Color::Float->new(@$float); |
| 406 |
{ |
| 407 |
# test as_8bit |
| 408 |
my $as8 = $f->as_8bit; |
| 409 |
is_deeply([ $as8->rgba ], $expect, $name); |
| 410 |
} |
| 411 |
{ |
| 412 |
# test construction |
| 413 |
my $as8 = Imager::Color->new($f); |
| 414 |
is_deeply([ $as8->rgba ], $expect, "constructed: $name"); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
# test conversion from 8bit to float color |
| 420 |
{ |
| 421 |
my @tests = |
| 422 |
( |
| 423 |
[ "black", [ 0,0,0 ], [ 0,0,0, 1.0 ] ], |
| 424 |
[ "white", [ 255, 255, 255 ], [ 1.0, 1.0, 1.0, 1.0 ] ], |
| 425 |
[ "dark red", [ 128, 0, 0 ], [ 128/255, 0, 0, 1.0 ] ], |
| 426 |
[ "green", [ 255, 128, 128, 64 ], [ 1.0, 128/255, 128/255, 64/255 ] ], |
| 427 |
); |
| 428 |
for my $test (@tests) { |
| 429 |
my ($name, $as8, $float) = @$test; |
| 430 |
my $c = Imager::Color->new(@$as8); |
| 431 |
{ |
| 432 |
# test as_float |
| 433 |
my $f = $c->as_float; |
| 434 |
is_fcolor4($f, $float->[0], $float->[1], $float->[2], $float->[3], "as_float: $name"); |
| 435 |
} |
| 436 |
{ |
| 437 |
# test construction |
| 438 |
my $f = Imager::Color::Float->new($c); |
| 439 |
is_fcolor4($f, $float->[0], $float->[1], $float->[2], $float->[3], "construction: $name"); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
{ |
| 445 |
# CSS rgb() support for float colors |
| 446 |
my @tests = |
| 447 |
( |
| 448 |
# rgb() without alpha |
| 449 |
[ |
| 450 |
[ "rgb(255 128 128)" ], |
| 451 |
1.0, 128/255, 128/255, 1.0, |
| 452 |
"rgb non-percent, spaces" |
| 453 |
], |
| 454 |
[ |
| 455 |
[ "rgb(255, 128, 128)" ], |
| 456 |
1.0, 128/255, 128/255, 1.0, |
| 457 |
"rgb non-percent, commas simple" |
| 458 |
], |
| 459 |
[ |
| 460 |
[ "rgb(255 ,128 ,128)" ], |
| 461 |
1.0, 128/255, 128/255, 1.0, |
| 462 |
"rgb non-percent, commas less simple" |
| 463 |
], |
| 464 |
[ |
| 465 |
[ "rgb(254.5 127.5 127.5)" ], |
| 466 |
254.5/255, 127.5/255, 127.5/255, 1.0, |
| 467 |
"rgb non-percent with decimals, spaces" |
| 468 |
], |
| 469 |
[ |
| 470 |
[ "rgb(254.5,127.5,126.2)" ], |
| 471 |
254.5/255, 127.5/255, 126.2/255, 1.0, |
| 472 |
"rgb non-percent decimals, commas" |
| 473 |
], |
| 474 |
[ |
| 475 |
[ "rgb(254.5 , 127.5 , 126.2)" ], |
| 476 |
254.5/255, 127.5/255, 126.2/255, 1.0, |
| 477 |
"rgb non-percent decimals, commas more spaces" |
| 478 |
], |
| 479 |
[ |
| 480 |
[ "rgb(100% 50% 50%)" ], |
| 481 |
1.0, 0.5, 0.5, 1.0, |
| 482 |
"rgb percent, spaces" |
| 483 |
], |
| 484 |
[ |
| 485 |
[ "rgb(100%, 50%, 50%)" ], |
| 486 |
1.0, 0.5, 0.5, 1.0, |
| 487 |
"rgb percent, commas" |
| 488 |
], |
| 489 |
[ |
| 490 |
[ "rgb(99.99% 49.99% 74.98%)" ], |
| 491 |
0.9999, 0.4999, 0.7498, 1.0, |
| 492 |
"rgb percent decimals, spaces" |
| 493 |
], |
| 494 |
[ |
| 495 |
[ "rgb(99.99%, 49.99%, 49.98%)" ], |
| 496 |
0.9999, 0.4999, 0.4998, 1.0, |
| 497 |
"rgb percent decimals, commas" |
| 498 |
], |
| 499 |
# rgb() with alpha |
| 500 |
[ |
| 501 |
[ "rgb(255 128 128 / 0.5)" ], |
| 502 |
1.0, 128/255, 128/255, 0.5, |
| 503 |
"rgba non-percent, spaces" |
| 504 |
], |
| 505 |
[ |
| 506 |
[ "rgb(255, 128, 128, 0.25)" ], |
| 507 |
1.0, 128/255, 128/255, 0.25, |
| 508 |
"rgba non-percent, commas simple" |
| 509 |
], |
| 510 |
[ |
| 511 |
[ "rgb(255 ,128 ,128 , 0.75)" ], |
| 512 |
1.0, 128/255, 128/255, 0.75, |
| 513 |
"rgba non-percent, commas less simple" |
| 514 |
], |
| 515 |
[ |
| 516 |
[ "rgba(254.5 127.5 127.5 / 0.1)" ], |
| 517 |
254.5/255, 127.5/255, 127.5/255, 0.1, |
| 518 |
"rgba non-percent with decimals, spaces" |
| 519 |
], |
| 520 |
[ |
| 521 |
[ "rgb(254.5,127.5,126.2,1.0)" ], |
| 522 |
254.5/255, 127.5/255, 126.2/255, 1.0, |
| 523 |
"rgba non-percent decimals, commas" |
| 524 |
], |
| 525 |
[ |
| 526 |
[ "rgb(254.5 , 127.5 , 126.2, 0.9)" ], |
| 527 |
254.5/255, 127.5/255, 126.2/255, 0.9, |
| 528 |
"rgba non-percent decimals, commas more spaces" |
| 529 |
], |
| 530 |
[ |
| 531 |
[ "rgb(100% 50% 50% / 0.2)" ], |
| 532 |
1.0, 0.5, 0.5, 0.2, |
| 533 |
"rgba percent, spaces" |
| 534 |
], |
| 535 |
[ |
| 536 |
[ "rgb(100%, 50%, 50%, 30%)" ], |
| 537 |
1.0, 0.5, 0.5, 0.3, |
| 538 |
"rgba percent, commas" |
| 539 |
], |
| 540 |
[ |
| 541 |
[ "rgb(99.99% 49.99% 74.98% / 49.9%)" ], |
| 542 |
0.9999, 0.4999, 0.7498, 0.499, |
| 543 |
"rgba percent decimals, spaces" |
| 544 |
], |
| 545 |
[ |
| 546 |
[ "rgb(99.99%, 49.99%, 49.98%, 50.0%)" ], |
| 547 |
0.9999, 0.4999, 0.4998, 0.5, |
| 548 |
"rgba percent decimals, commas" |
| 549 |
], |
| 550 |
); |
| 551 |
for my $test (@tests) { |
| 552 |
my ($parms, $r, $g, $b, $a, $name) = @$test; |
| 553 |
my $f = Imager::Color::Float->new(@$parms); |
| 554 |
is_fcolor4($f, $r, $g, $b, $a, "float: $name"); |
| 555 |
|
| 556 |
my $css = $f->as_css_rgb; |
| 557 |
my $f2 = Imager::Color::Float->new($css); |
| 558 |
is_fcolor4($f2, $r, $g, $b, $a, "via css: $name ($css)"); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
{ |
| 563 |
# we make promises for as_css_rgb(), test those |
| 564 |
# if represntable as bytes, we return that |
| 565 |
is(Imager::Color->new(255, 127, 20)->as_float->as_css_rgb, |
| 566 |
"rgb(255, 127, 20)", "float as_css_rgb: representable as bytes"); |
| 567 |
is(Imager::Color::Float->new(1.0, 0.7654, 1/3)->as_css_rgb, |
| 568 |
"rgb(100% 76.54% 33.33%)", "float as_css_rgb: not representable as bytes"); |
| 569 |
is(Imager::Color->new(255, 127, 20, 128)->as_float->as_css_rgb, |
| 570 |
"rgba(255, 127, 20, 0.502)", "float as_css_rgb: representable as bytes with alpha"); |
| 571 |
is(Imager::Color::Float->new(1.0, 0.7654, 1/3, 0.5)->as_css_rgb, |
| 572 |
"rgba(100% 76.54% 33.33% / 0.5)", "float as_css_rgb: not representable as bytes with alpha"); |
| 573 |
} |
| 574 |
|
| 575 |
done_testing(); |
| 576 |
|
| 577 |
sub test_col { |
| 578 |
my ($c, $r, $g, $b, $a) = @_; |
| 579 |
unless ($c) { |
| 580 |
print "# $Imager::ERRSTR\n"; |
| 581 |
return 0; |
| 582 |
} |
| 583 |
my ($cr, $cg, $cb, $ca) = $c->rgba; |
| 584 |
return $r == $cr && $g == $cg && $b == $cb && $a == $ca; |
| 585 |
} |
| 586 |
|
| 587 |
sub color_close_enough { |
| 588 |
my ($name, $r, $g, $b, $c) = @_; |
| 589 |
|
| 590 |
my ($cr, $cg, $cb) = $c->rgba; |
| 591 |
ok(abs($cr-$r) <= 5 && abs($cg-$g) <= 5 && abs($cb-$b) <= 5, |
| 592 |
"$name - ($cr, $cg, $cb) <=> ($r, $g, $b)"); |
| 593 |
} |
| 594 |
|
| 595 |
sub color_close_enough_hsv { |
| 596 |
my ($name, $h, $s, $v, $c) = @_; |
| 597 |
|
| 598 |
my ($ch, $cs, $cv) = $c->rgba; |
| 599 |
if ($ch < 5 && $h > 250) { |
| 600 |
$ch += 255; |
| 601 |
} |
| 602 |
elsif ($ch > 250 && $h < 5) { |
| 603 |
$h += 255; |
| 604 |
} |
| 605 |
ok(abs($ch-$h) <= 5 && abs($cs-$s) <= 5 && abs($cv-$v) <= 5, |
| 606 |
"$name - ($ch, $cs, $cv) <=> ($h, $s, $v)"); |
| 607 |
} |
| 608 |
|