1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: TestColorTransferFunctionStringArray.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 #include <vtkColorTransferFunction.h>
16 #include <vtkMath.h>
17 #include <vtkNew.h>
18 #include <vtkSmartPointer.h>
19
TestColorSpace()20 bool TestColorSpace()
21 {
22 vtkNew<vtkColorTransferFunction> ctf;
23 ctf->AddRGBPoint(0.0, 1.0, 0.0, 0.0);
24 ctf->AddRGBPoint(1.0, 0.0, 0.0, 1.0);
25 const unsigned char* rgba = ctf->MapValue(0.5);
26 if (rgba[0] != 128 || rgba[1] != 0 || rgba[2] != 128)
27 {
28 cerr << "ERROR: ColorSpace == VTK_CTF_RGB failed!" << endl;
29 return false;
30 }
31
32 ctf->SetColorSpaceToLabCIEDE2000();
33 rgba = ctf->MapValue(0.5);
34 if (rgba[0] != 196 || rgba[1] != 16 || rgba[2] != 123)
35 {
36 cerr << "ERROR: ColorSpace == VTK_CTF_LAB_CIEDE2000 failed!" << endl;
37 return false;
38 }
39
40 ctf->SetColorSpaceToStep();
41 rgba = ctf->MapValue(0.5);
42 if (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 255)
43 {
44 cerr << "ERROR: ColorSpace == VTK_CTF_STEP failed!" << endl;
45 return false;
46 }
47 return true;
48 }
49
TestColorTransferFunction(int vtkNotUsed (argc),char * vtkNotUsed (argv)[])50 int TestColorTransferFunction(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
51 {
52 vtkNew<vtkColorTransferFunction> ctf;
53
54 // Test for crash when getting table with no points
55 ctf->RemoveAllPoints();
56
57 // Range should be [0, 0]. Honest to goodness 0, not just very close to 0.
58 double range[2];
59 ctf->GetRange(range);
60
61 if (range[0] != 0.0 || range[1] != 0.0)
62 {
63 std::cerr << "After RemoveAllPoints() is called, range should be [0, 0]. "
64 << "It was [" << range[0] << ", " << range[1] << "].\n";
65 return EXIT_FAILURE;
66 }
67
68 double table[256 * 3];
69 ctf->GetTable(0.0, 1.0, 256, table);
70
71 // Table should be all black.
72 for (int i = 0; i < 3 * 256; ++i)
73 {
74 if (table[i] != 0.0)
75 {
76 std::cerr << "Table should have all zeros.\n";
77 return EXIT_FAILURE;
78 }
79 }
80
81 ctf->SetNanColorRGBA(1., 1., 1., 0.5);
82 ctf->GetTable(vtkMath::Nan(), 1., 256, table);
83 // Table should be all white (Nan color).
84 for (int i = 0; i < 3 * 256; ++i)
85 {
86 if (table[i] != 1.0)
87 {
88 std::cerr << "Table should have all ones.\n";
89 return EXIT_FAILURE;
90 }
91 }
92
93 ctf->AddRGBPoint(0.0, 0.0, 0.0, 0.0);
94 ctf->AddRGBPoint(1.0, 1.0, 1.0, 1.0);
95 double color[4] = { -1., -1., -1., -1. };
96 ctf->GetIndexedColor(0, color);
97 for (int i = 0; i < 3; ++i)
98 {
99 if (color[i] != 0.0)
100 {
101 std::cerr << "Color should have all zeros.\n";
102 return EXIT_FAILURE;
103 }
104 }
105 if (color[3] != 1.0)
106 {
107 std::cerr << "Opacity should be 1.\n";
108 return EXIT_FAILURE;
109 }
110
111 ctf->GetIndexedColor(-1, color);
112 for (int i = 0; i < 3; ++i)
113 {
114 if (color[i] != 1.0)
115 {
116 std::cerr << "Nan Color should have all ones.\n";
117 return EXIT_FAILURE;
118 }
119 }
120 if (color[3] != 0.5)
121 {
122 std::cerr << "Nan Color opacity should be 0.5.\n";
123 return EXIT_FAILURE;
124 }
125
126 if (!TestColorSpace())
127 {
128 return EXIT_FAILURE;
129 }
130
131 return EXIT_SUCCESS;
132 }
133