]> git.imager.perl.org - imager.git/blob - t/t15color.t
d7f316c1188309a0c1c4b2ad2f39616b8d6af3f7
[imager.git] / t / t15color.t
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
3
4 ######################### We start with some black magic to print on failure.
5
6 # Change 1..1 below to 1..last_test_to_print .
7 # (It may become useful if the test is moved to ./t subdirectory.)
8
9 BEGIN { $| = 1; print "1..22\n"; }
10 END {print "not ok 1\n" unless $loaded;}
11 use Imager;
12 $loaded = 1;
13 print "ok 1\n";
14
15 init_log("testout/t15color.log",1);
16
17 my $c1 = Imager::Color->new(100, 150, 200, 250);
18 print test_col($c1, 100, 150, 200, 250) ? "ok 2\n" : "not ok 2\n";
19 my $c2 = Imager::Color->new(100, 150, 200);
20 print test_col($c2, 100, 150, 200, 255) ? "ok 3\n" : "not ok 3\n";
21 my $c3 = Imager::Color->new("#6496C8");
22 print test_col($c3, 100, 150, 200, 255) ? "ok 4\n" : "not ok 4\n";
23 # crashes in Imager-0.38pre8 and earlier
24 my @foo;
25 for (1..1000) {
26   push(@foo, Imager::Color->new("#FFFFFF"));
27 }
28 my $fail;
29 for (@foo) {
30   Imager::Color::set_internal($_, 128, 128, 128, 128) == $_ or ++$fail;
31   Imager::Color::set_internal($_, 128, 128, 128, 128) == $_ or ++$fail;
32   test_col($_, 128, 128, 128, 128) or ++$fail;
33 }
34 $fail and print "not ";
35 print "ok 5\n";
36
37 # test the new OO methods
38 color_ok(6, 100, 150, 200, 255, Imager::Color->new(r=>100, g=>150, b=>200));
39 color_ok(7, 101, 151, 201, 255, 
40          Imager::Color->new(red=>101, green=>151, blue=>201));
41 color_ok(8, 102, 255, 255, 255, Imager::Color->new(grey=>102));
42 color_ok(9, 103, 255, 255, 255, Imager::Color->new(gray=>103));
43 if (-e '/usr/lib/X11/rgb.txt') {
44   color_ok(10, 0, 0, 255, 255, Imager::Color->new(xname=>'blue'));
45 }
46 else {
47   print "ok 10 # skip - no X rgb.txt found\n";
48 }
49 color_ok(11, 255, 250, 250, 255, 
50          Imager::Color->new(gimp=>'snow', palette=>'testimg/test_gimp_pal'));
51 color_ok(12, 255, 255, 255, 255, Imager::Color->new(h=>0, 's'=>0, 'v'=>1.0));
52 color_ok(13, 255, 0, 0, 255, Imager::Color->new(h=>0, 's'=>1, v=>1));
53 color_ok(14, 128, 129, 130, 255, Imager::Color->new(web=>'#808182'));
54 color_ok(15, 0x11, 0x22, 0x33, 255, Imager::Color->new(web=>'#123'));
55 color_ok(16, 255, 150, 121, 255, Imager::Color->new(rgb=>[ 255, 150, 121 ]));
56 color_ok(17, 255, 150, 121, 128, 
57          Imager::Color->new(rgba=>[ 255, 150, 121, 128 ]));
58 color_ok(18, 255, 0, 0, 255, Imager::Color->new(hsv=>[ 0, 1, 1 ]));
59 color_ok(19, 129, 130, 131, 134, 
60          Imager::Color->new(channel0=>129, channel1=>130, channel2=>131,
61                             channel3=>134));
62 color_ok(20, 129, 130, 131, 134, 
63          Imager::Color->new(c0=>129, c1=>130, c2=>131, c3=>134));
64 color_ok(21, 200, 201, 203, 204, 
65          Imager::Color->new(channels=>[ 200, 201, 203, 204 ]));
66 color_ok(22, 255, 250, 250, 255, 
67          Imager::Color->new(name=>'snow', palette=>'testimg/test_gimp_pal'));
68 sub test_col {
69   my ($c, $r, $g, $b, $a) = @_;
70   unless ($c) {
71     print "# $Imager::ERRSTR\n";
72     return 0;
73   }
74   my ($cr, $cg, $cb, $ca) = $c->rgba;
75   return $r == $cr && $g == $cg && $b == $cb && $a == $ca;
76 }
77
78 sub color_ok {
79   my ($test_num, $r, $g, $b, $a, $c) = @_;
80
81   if (test_col($c, $r, $g, $b, $a)) {
82     print "ok $test_num\n";
83   }
84   else {
85     print "not ok $test_num\n"
86   }
87 }
88