File: 200-trimcolorlist.t

package info (click to toggle)
libimager-perl 1.019%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,824 kB
  • sloc: perl: 32,886; ansic: 28,193; makefile: 52; cpp: 4
file content (85 lines) | stat: -rw-r--r-- 2,569 bytes parent folder | download | duplicates (3)
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
#!perl -w
use strict;
use Imager qw(:handy);
use Scalar::Util qw(reftype);
use Test::More;

{
  my $t = Imager::TrimColorList->new;
  ok($t, "make empty list");
  is($t->count, 0, "it's empty");
  is($t->get(0), undef, "nothing at [0]");
  is_deeply([ $t->all ], [], "empty all()");
}

{
  my $cffeecc = NC(0xff, 0xee, 0xcc);
  my $cf0e0c0 = NC(0xF0, 0xE0, 0xC0);
  my $f_572 = NCF(0.5, 0.7, 0.2);
  my $f_683 = NCF(0.6, 0.8, 0.3);
  my @tests =
    ( # name, input, expected
      [ "simple color", "#ffeecc", [ $cffeecc, $cffeecc ] ],
      [ "simple color object", $cffeecc, [ $cffeecc, $cffeecc ] ],
      [ "simple fcolor", $f_572, [ $f_572, $f_572 ] ],
      [ "single color in array", [ "#ffeecc" ], [ $cffeecc, $cffeecc ] ],
      [ "single color object in array", [ $cffeecc ], [ $cffeecc, $cffeecc ] ],
      [ "single fcolor", [ $f_572 ], [ $f_572, $f_572 ] ],
      [ "simple range", [ "#f0e0c0", "#ffeecc" ], [ $cf0e0c0, $cffeecc ] ],
      [ "simple fcolor range", [ $f_572, $f_683 ], [ $f_572, $f_683 ] ],
      [ "color with error amount", [ "#808080", 0.01 ], [ NC(0x7E, 0x7E, 0x7E), NC(0x82, 0x82, 0x82) ] ],
      [
	"fcolor with error amount",
	[ NCF(0.4, 0.5, 0.6), 0.01 ],
	[ NCF(0.39, 0.49, 0.59), NCF(0.41, 0.51, 0.61) ],
       ],
     );
  for my $test (@tests) {
    my ($name, $input, $expect) = @$test;
    my $t = Imager::TrimColorList->new;
    ok($t->add($input), "$name: add")
      or diag "$name: " . Imager->errstr;
    is($t->count, 1, "$name: and it shows in count");
    my $e = $t->get(0);
    is_entry($e, $expect, $name);
  }
}

done_testing();

# is() doesn't really work for the objects we find in the trim color list
sub is_entry {
  my ($testme, $expected, $name) = @_;

  # entries are always an arrayref of 2 elements
  if (!ref($testme) || reftype($testme) ne "ARRAY" || @$testme != 2) {
    fail("$name: not a 2 element arrayref");
  }
  my $testme_text = _format_entry($testme);
  my $expected_text = _format_entry($expected);
  note "testme: ".join(", ", @$testme_text);
  note "expect: ".join(", ", @$expected_text);
  is_deeply($testme_text, $expected_text, $name);
}

sub _format_entry {
  my ($e) = @_;

  return [
    _format_color($e->[0]),
    _format_color($e->[1])
    ];
}

sub _format_color {
  my ($c) = @_;

  # trim color doesn't care about alpha
  if ($c->isa("Imager::Color")) {
    return sprintf("NC(%d,%d,%d)", $c->red, $c->green, $c->blue);
  }
  elsif ($c->isa("Imager::Color::Float")) {
    return sprintf("NCF(%.4g,%.4g,%.4g)", $c->red, $c->green, $c->blue);
  }
  die "Unexpected color $c";
}