1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestCategoricalColors.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #include "vtkLookupTable.h"
17 #include "vtkColorSeries.h"
18 #include "vtkDoubleArray.h"
19 #include "vtkUnsignedCharArray.h"
20 
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <string>
25 #include <map>
26 
27 #include <cstdio> // For EXIT_SUCCESS
28 
29 //-----------------------------------------------------------------------------
30 //! Get a hexadecimal string of the RGBA colors.
RGBAToHexString(const unsigned char * rgba)31 std::string RGBAToHexString(const unsigned char *rgba)
32 {
33   std::ostringstream os;
34   for (int i = 0; i < 4; ++i)
35   {
36     os << std::setw(2) << std::setfill('0')
37     << std::hex << static_cast<int>(rgba[i]);
38   }
39   return os.str();
40 }
41 
TestCategoricalColors(int vtkNotUsed (argc),char * vtkNotUsed (argv)[])42 int TestCategoricalColors(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
43 {
44   bool res = true;
45   // Create the LUT and add some annotations.
46   vtkLookupTable* lut = vtkLookupTable::New();
47   lut->SetAnnotation(0., "Zero");
48   lut->SetAnnotation(.5, "Half");
49   lut->SetAnnotation(1., "Ichi");
50   lut->SetAnnotation(1., "One");
51   lut->SetAnnotation(2., "Ni");
52   lut->SetAnnotation(2., "Two");
53   lut->SetAnnotation(3, "San");
54   lut->SetAnnotation(4, "Floor");
55   lut->SetAnnotation(5, "Hive");
56   lut->SetAnnotation(6, "Licks");
57   lut->SetAnnotation(7, "Leaven");
58   lut->SetAnnotation(9, "Kyuu");
59   lut->RemoveAnnotation(2.);
60 
61   vtkColorSeries* palettes = vtkColorSeries::New();
62 #if 0
63   vtkIdType numSchemes = palettes->GetNumberOfColorSchemes();
64   for (vtkIdType i = 0; i < numSchemes; ++ i)
65   {
66     palettes->SetColorScheme(i);
67     std::cout << i << ": " << palettes->GetColorSchemeName() << std::endl;
68   }
69 #endif
70   palettes->SetColorSchemeByName("Brewer Qualitative Accent");
71   palettes->BuildLookupTable(lut);
72 
73   std::map<double, std::string> expectedColors;
74   expectedColors[0] = "0x7fc97fff";
75   expectedColors[9] = "0x7fc97fff";
76   expectedColors[1] = "0xfdc086ff";
77   expectedColors[2] = "0x800000ff";
78   expectedColors[3] = "0xffff99ff";
79   expectedColors[0.5] = "0xbeaed4ff";
80   expectedColors[-999] = "0x800000ff"; // NaN
81 
82   const unsigned char* rgba = lut->MapValue(0.);
83   std::string v = "0x" + RGBAToHexString(rgba);
84   if (expectedColors[0] != v)
85   {
86     std::cout
87       << "Fail for "
88       << std::setw(3) << std::left
89       << 0 << ": got: "
90       << v
91       << " expected: "
92       << expectedColors[0]
93       << std::endl;
94     res &= false;
95   }
96   rgba = lut->MapValue(3.);
97   v = "0x" + RGBAToHexString(rgba);
98   if (expectedColors[3] != v)
99   {
100     std::cout
101       << "Fail for "
102       << std::setw(3) << std::left
103       << 3 << ": got: "
104       << v
105       << " expected: "
106       << expectedColors[3]
107       << std::endl;
108     res &= false;
109   }
110 
111   vtkDoubleArray* data = vtkDoubleArray::New();
112   data->InsertNextValue(0.);
113   data->InsertNextValue(9.);
114   data->InsertNextValue(1.);
115   data->InsertNextValue(2.);
116   data->InsertNextValue(3.);
117   data->InsertNextValue(.5);
118 
119 
120   vtkUnsignedCharArray* color = lut->MapScalars(data, VTK_RGBA, 0);
121   unsigned char* cval;
122   for (vtkIdType i = 0; i < color->GetNumberOfTuples(); ++ i)
123   {
124     cval = color->GetPointer(i * 4);
125     v = "0x" + RGBAToHexString(cval);
126     if (expectedColors[data->GetTuple1(i)] != v)
127     {
128       std::cout
129         << "Fail for "
130         << std::setw(3) << std::left
131         << data->GetTuple1(i) << ": got: "
132         << v
133         << " expected: "
134         << expectedColors[data->GetTuple1(i)]
135         << std::endl;
136       res &= false;
137     }
138   }
139   cval = lut->GetNanColorAsUnsignedChars();
140   v = "0x" + RGBAToHexString(cval);
141   if (expectedColors[-999] != v)
142   {
143     std::cout
144       << "Fail for "
145       << "NaN: got: "
146       << v
147       << " expected: "
148       << expectedColors[-999]
149       << std::endl;
150     res &= false;
151   }
152 
153   color->Delete();
154   data->Delete();
155   lut->Delete();
156   palettes->Delete();
157 
158   return (res) ? EXIT_SUCCESS : EXIT_FAILURE;
159 }
160