1// This may look like C code, but it is really -*- C++ -*-
2//
3// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
4//
5// Test Magick::Color classes
6//
7
8#include <Magick++.h>
9#include <string>
10#include <iostream>
11
12using namespace std;
13
14using namespace Magick;
15
16int main( int /*argc*/, char **argv)
17{
18
19  // Initialize ImageMagick install location for Windows
20  InitializeMagick(*argv);
21
22  int failures=0;
23
24  try {
25
26    //
27    // Verify conversion from named colors as well as ColorRGB constructor
28    //
29
30    {
31      struct colorStr
32      {
33	const char* color;
34	double red;
35	double green;
36	double blue;
37      };
38
39      // Convert ratios from rgb.txt via value/255
40      struct colorStr colorMap [] =
41      {
42	{ "red", 1,0,0 },
43	{ "green", 0,0.5019607843137256,0 },
44	{ "blue", 0,0,1 },
45	{ "black", 0,0,0 },
46	{ "white", 1,1,1 },
47	{ "cyan", 0,1,1 },
48	{ "magenta", 1,0,1 },
49	{ "yellow", 1,1,0 },
50	{ NULL, 0,0,0 }
51      };
52
53      for ( int i = 0; colorMap[i].color != NULL; i++ )
54	{
55	  {
56	    Color color( colorMap[i].color );
57	    ColorRGB colorMatch( colorMap[i].red,
58				 colorMap[i].green,
59				 colorMap[i].blue );
60	    if ( color != colorMatch )
61	      {
62		++failures;
63		cout << "Line: " << __LINE__ << " Color(\""
64		     << colorMap[i].color << "\") is "
65		     << string(color)
66		     << " rather than "
67		     << string(colorMatch)
68		     << endl;
69                // printf ("Green: %10.16f\n", color.green());
70	      }
71	  }
72	}
73    }
74
75    // Test conversion to/from X11-style color specifications
76    {
77      const char * colorStrings[] =
78      {
79	"#ABC",
80	"#AABBCC",
81	"#AAAABBBBCCCC",
82	NULL
83      };
84
85#if MAGICKCORE_QUANTUM_DEPTH == 8
86      string expectedString = "#AABBCC";
87#elif MAGICKCORE_QUANTUM_DEPTH == 16
88      string expectedString = "#AAAABBBBCCCC";
89#elif MAGICKCORE_QUANTUM_DEPTH == 32
90      string expectedString = "#AAAAAAAABBBBBBBBCCCCCCCC";
91#elif MAGICKCORE_QUANTUM_DEPTH == 64
92      string expectedString = "#AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC";
93#else
94# error Quantum depth not supported!
95#endif
96
97      for ( int i = 0; colorStrings[i] != NULL; ++i )
98	{
99	  if ( string(Color(colorStrings[i])) != expectedString )
100	    {
101	      ++failures;
102	      cout << "Line: " << __LINE__
103		   << " Conversion from " << colorStrings[i]
104		   << " is "
105		   << string(Color(colorStrings[i])) << " rather than "
106		   << expectedString
107		   << endl;
108	    }
109	}
110    }
111
112    // Test ColorGray
113    {
114      double resolution = 1.0/QuantumRange;
115      if ( resolution < 0.0000001 )
116        resolution = 0.0000001;
117      double max_error = resolution + MagickEpsilon;
118
119      for( double value = 0; value < 1.0 + MagickEpsilon; value += resolution )
120        {
121          ColorGray gray(value);
122          if ( gray.shade() < value - max_error || gray.shade() > value + max_error )
123            {
124              ++failures;
125              cout << "Line: " << __LINE__
126                   << " shade is "
127                   << gray.shade()
128                   << " rather than nominal "
129                   << value
130                   << endl;
131            }
132        }
133    }
134
135  }
136  catch( Exception &error_ )
137    {
138      cout << "Caught exception: " << error_.what() << endl;
139      return 1;
140    }
141  catch( exception &error_ )
142    {
143      cout << "Caught exception: " << error_.what() << endl;
144      return 1;
145    }
146
147  if ( failures )
148    {
149      cout << failures << " failures" << endl;
150      return 1;
151    }
152
153  return 0;
154}
155