| 1 |
#!perl -w |
| 2 |
use strict; |
| 3 |
use Imager qw(:handy); |
| 4 |
use Scalar::Util qw(reftype); |
| 5 |
use Test::More; |
| 6 |
|
| 7 |
{ |
| 8 |
my $t = Imager::TrimColorList->new; |
| 9 |
ok($t, "make empty list"); |
| 10 |
is($t->count, 0, "it's empty"); |
| 11 |
is($t->get(0), undef, "nothing at [0]"); |
| 12 |
is_deeply([ $t->all ], [], "empty all()"); |
| 13 |
} |
| 14 |
|
| 15 |
{ |
| 16 |
my $cffeecc = NC(0xff, 0xee, 0xcc); |
| 17 |
my $cf0e0c0 = NC(0xF0, 0xE0, 0xC0); |
| 18 |
my $f_572 = NCF(0.5, 0.7, 0.2); |
| 19 |
my $f_683 = NCF(0.6, 0.8, 0.3); |
| 20 |
my @tests = |
| 21 |
( # name, input, expected |
| 22 |
[ "simple color", "#ffeecc", [ $cffeecc, $cffeecc ] ], |
| 23 |
[ "simple color object", $cffeecc, [ $cffeecc, $cffeecc ] ], |
| 24 |
[ "simple fcolor", $f_572, [ $f_572, $f_572 ] ], |
| 25 |
[ "single color in array", [ "#ffeecc" ], [ $cffeecc, $cffeecc ] ], |
| 26 |
[ "single color object in array", [ $cffeecc ], [ $cffeecc, $cffeecc ] ], |
| 27 |
[ "single fcolor", [ $f_572 ], [ $f_572, $f_572 ] ], |
| 28 |
[ "simple range", [ "#f0e0c0", "#ffeecc" ], [ $cf0e0c0, $cffeecc ] ], |
| 29 |
[ "simple fcolor range", [ $f_572, $f_683 ], [ $f_572, $f_683 ] ], |
| 30 |
[ "color with error amount", [ "#808080", 0.01 ], [ NC(0x7E, 0x7E, 0x7E), NC(0x82, 0x82, 0x82) ] ], |
| 31 |
[ |
| 32 |
"fcolor with error amount", |
| 33 |
[ NCF(0.4, 0.5, 0.6), 0.01 ], |
| 34 |
[ NCF(0.39, 0.49, 0.59), NCF(0.41, 0.51, 0.61) ], |
| 35 |
], |
| 36 |
); |
| 37 |
for my $test (@tests) { |
| 38 |
my ($name, $input, $expect) = @$test; |
| 39 |
my $t = Imager::TrimColorList->new; |
| 40 |
ok($t->add($input), "$name: add") |
| 41 |
or diag "$name: " . Imager->errstr; |
| 42 |
is($t->count, 1, "$name: and it shows in count"); |
| 43 |
my $e = $t->get(0); |
| 44 |
is_entry($e, $expect, $name); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
done_testing(); |
| 49 |
|
| 50 |
# is() doesn't really work for the objects we find in the trim color list |
| 51 |
sub is_entry { |
| 52 |
my ($testme, $expected, $name) = @_; |
| 53 |
|
| 54 |
# entries are always an arrayref of 2 elements |
| 55 |
if (!ref($testme) || reftype($testme) ne "ARRAY" || @$testme != 2) { |
| 56 |
fail("$name: not a 2 element arrayref"); |
| 57 |
} |
| 58 |
my $testme_text = _format_entry($testme); |
| 59 |
my $expected_text = _format_entry($expected); |
| 60 |
note "testme: ".join(", ", @$testme_text); |
| 61 |
note "expect: ".join(", ", @$expected_text); |
| 62 |
is_deeply($testme_text, $expected_text, $name); |
| 63 |
} |
| 64 |
|
| 65 |
sub _format_entry { |
| 66 |
my ($e) = @_; |
| 67 |
|
| 68 |
return [ |
| 69 |
_format_color($e->[0]), |
| 70 |
_format_color($e->[1]) |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
sub _format_color { |
| 75 |
my ($c) = @_; |
| 76 |
|
| 77 |
# trim color doesn't care about alpha |
| 78 |
if ($c->isa("Imager::Color")) { |
| 79 |
return sprintf("NC(%d,%d,%d)", $c->red, $c->green, $c->blue); |
| 80 |
} |
| 81 |
elsif ($c->isa("Imager::Color::Float")) { |
| 82 |
return sprintf("NCF(%.4g,%.4g,%.4g)", $c->red, $c->green, $c->blue); |
| 83 |
} |
| 84 |
die "Unexpected color $c"; |
| 85 |
} |