1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkColor.h
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 /**
17  * @class   vtkColor
18  * @brief   templated type for storage of colors.
19  *
20  *
21  * This class is a templated data type for storing and manipulating fixed size
22  * colors. It derives from the vtkVector templated data structure.
23  */
24 
25 #ifndef vtkColor_h
26 #define vtkColor_h
27 
28 #include "vtkObject.h" // for legacy macros
29 #include "vtkTuple.h"
30 
31 // .NAME vtkColor3 - templated base type for storage of 3 component colors.
32 //
33 template <typename T>
34 class vtkColor3 : public vtkTuple<T, 3>
35 {
36 public:
37   vtkColor3() = default;
38 
vtkColor3(const T & scalar)39   explicit vtkColor3(const T& scalar)
40     : vtkTuple<T, 3>(scalar)
41   {
42   }
43 
vtkColor3(const T * init)44   explicit vtkColor3(const T* init)
45     : vtkTuple<T, 3>(init)
46   {
47   }
48 
vtkColor3(const T & red,const T & green,const T & blue)49   vtkColor3(const T& red, const T& green, const T& blue)
50   {
51     this->Data[0] = red;
52     this->Data[1] = green;
53     this->Data[2] = blue;
54   }
55 
56   ///@{
57   /**
58    * Set the red, green and blue components of the color.
59    */
Set(const T & red,const T & green,const T & blue)60   void Set(const T& red, const T& green, const T& blue)
61   {
62     this->Data[0] = red;
63     this->Data[1] = green;
64     this->Data[2] = blue;
65   }
66   ///@}
67 
68   /**
69    * Set the red component of the color, i.e. element 0.
70    */
SetRed(const T & red)71   void SetRed(const T& red) { this->Data[0] = red; }
72 
73   /**
74    * Get the red component of the color, i.e. element 0.
75    */
GetRed()76   const T& GetRed() const { return this->Data[0]; }
77 
78   /**
79    * Set the green component of the color, i.e. element 1.
80    */
SetGreen(const T & green)81   void SetGreen(const T& green) { this->Data[1] = green; }
82 
83   /**
84    * Get the green component of the color, i.e. element 1.
85    */
GetGreen()86   const T& GetGreen() const { return this->Data[1]; }
87 
88   /**
89    * Set the blue component of the color, i.e. element 2.
90    */
SetBlue(const T & blue)91   void SetBlue(const T& blue) { this->Data[2] = blue; }
92 
93   /**
94    * Get the blue component of the color, i.e. element 2.
95    */
GetBlue()96   const T& GetBlue() const { return this->Data[2]; }
97 };
98 
99 // .NAME vtkColor4 - templated base type for storage of 4 component colors.
100 //
101 template <typename T>
102 class vtkColor4 : public vtkTuple<T, 4>
103 {
104 public:
105   vtkColor4() = default;
106 
vtkColor4(const T & scalar)107   explicit vtkColor4(const T& scalar)
108     : vtkTuple<T, 4>(scalar)
109   {
110   }
111 
vtkColor4(const T * init)112   explicit vtkColor4(const T* init)
113     : vtkTuple<T, 4>(init)
114   {
115   }
116 
vtkColor4(const T & red,const T & green,const T & blue,const T & alpha)117   vtkColor4(const T& red, const T& green, const T& blue, const T& alpha)
118   {
119     this->Data[0] = red;
120     this->Data[1] = green;
121     this->Data[2] = blue;
122     this->Data[3] = alpha;
123   }
124 
125   ///@{
126   /**
127    * Set the red, green and blue components of the color.
128    */
Set(const T & red,const T & green,const T & blue)129   void Set(const T& red, const T& green, const T& blue)
130   {
131     this->Data[0] = red;
132     this->Data[1] = green;
133     this->Data[2] = blue;
134   }
135   ///@}
136 
137   ///@{
138   /**
139    * Set the red, green, blue and alpha components of the color.
140    */
Set(const T & red,const T & green,const T & blue,const T & alpha)141   void Set(const T& red, const T& green, const T& blue, const T& alpha)
142   {
143     this->Data[0] = red;
144     this->Data[1] = green;
145     this->Data[2] = blue;
146     this->Data[3] = alpha;
147   }
148   ///@}
149 
150   /**
151    * Set the red component of the color, i.e. element 0.
152    */
SetRed(const T & red)153   void SetRed(const T& red) { this->Data[0] = red; }
154 
155   /**
156    * Get the red component of the color, i.e. element 0.
157    */
GetRed()158   const T& GetRed() const { return this->Data[0]; }
159 
160   /**
161    * Set the green component of the color, i.e. element 1.
162    */
SetGreen(const T & green)163   void SetGreen(const T& green) { this->Data[1] = green; }
164 
165   /**
166    * Get the green component of the color, i.e. element 1.
167    */
GetGreen()168   const T& GetGreen() const { return this->Data[1]; }
169 
170   /**
171    * Set the blue component of the color, i.e. element 2.
172    */
SetBlue(const T & blue)173   void SetBlue(const T& blue) { this->Data[2] = blue; }
174 
175   /**
176    * Get the blue component of the color, i.e. element 2.
177    */
GetBlue()178   const T& GetBlue() const { return this->Data[2]; }
179 
180   /**
181    * Set the alpha component of the color, i.e. element 3.
182    */
SetAlpha(const T & alpha)183   void SetAlpha(const T& alpha) { this->Data[3] = alpha; }
184 
185   /**
186    * Get the alpha component of the color, i.e. element 3.
187    */
GetAlpha()188   const T& GetAlpha() const { return this->Data[3]; }
189 };
190 
191 /**
192  * Some derived classes for the different colors commonly used.
193  */
194 class vtkColor3ub : public vtkColor3<unsigned char>
195 {
196 public:
197   vtkColor3ub() = default;
vtkColor3ub(unsigned char scalar)198   explicit vtkColor3ub(unsigned char scalar)
199     : vtkColor3<unsigned char>(scalar)
200   {
201   }
vtkColor3ub(const unsigned char * init)202   explicit vtkColor3ub(const unsigned char* init)
203     : vtkColor3<unsigned char>(init)
204   {
205   }
206 
207   ///@{
208   /**
209    * Construct a color from a hexadecimal representation such as 0x0000FF (blue).
210    */
vtkColor3ub(int hexSigned)211   explicit vtkColor3ub(int hexSigned)
212   {
213     unsigned int hex = static_cast<unsigned int>(hexSigned);
214     this->Data[2] = hex & 0xff;
215     hex >>= 8;
216     this->Data[1] = hex & 0xff;
217     hex >>= 8;
218     this->Data[0] = hex & 0xff;
219   }
220   ///@}
221 
vtkColor3ub(unsigned char r,unsigned char g,unsigned char b)222   vtkColor3ub(unsigned char r, unsigned char g, unsigned char b)
223     : vtkColor3<unsigned char>(r, g, b)
224   {
225   }
226 };
227 
228 class vtkColor3f : public vtkColor3<float>
229 {
230 public:
231   vtkColor3f() = default;
vtkColor3f(float scalar)232   explicit vtkColor3f(float scalar)
233     : vtkColor3<float>(scalar)
234   {
235   }
vtkColor3f(const float * init)236   explicit vtkColor3f(const float* init)
237     : vtkColor3<float>(init)
238   {
239   }
vtkColor3f(float r,float g,float b)240   vtkColor3f(float r, float g, float b)
241     : vtkColor3<float>(r, g, b)
242   {
243   }
244 };
245 
246 class vtkColor3d : public vtkColor3<double>
247 {
248 public:
249   vtkColor3d() = default;
vtkColor3d(double scalar)250   explicit vtkColor3d(double scalar)
251     : vtkColor3<double>(scalar)
252   {
253   }
vtkColor3d(const double * init)254   explicit vtkColor3d(const double* init)
255     : vtkColor3<double>(init)
256   {
257   }
vtkColor3d(double r,double g,double b)258   vtkColor3d(double r, double g, double b)
259     : vtkColor3<double>(r, g, b)
260   {
261   }
262 };
263 
264 class vtkColor4ub : public vtkColor4<unsigned char>
265 {
266 public:
267   vtkColor4ub() = default;
vtkColor4ub(unsigned char scalar)268   explicit vtkColor4ub(unsigned char scalar)
269     : vtkColor4<unsigned char>(scalar)
270   {
271   }
vtkColor4ub(const unsigned char * init)272   explicit vtkColor4ub(const unsigned char* init)
273     : vtkColor4<unsigned char>(init)
274   {
275   }
276 
277   ///@{
278   /**
279    * Construct a color from a hexadecimal representation such as 0x0000FFAA
280    * (opaque blue).
281    */
vtkColor4ub(int hexSigned)282   explicit vtkColor4ub(int hexSigned)
283   {
284     unsigned int hex = static_cast<unsigned int>(hexSigned);
285     this->Data[3] = hex & 0xff;
286     hex >>= 8;
287     this->Data[2] = hex & 0xff;
288     hex >>= 8;
289     this->Data[1] = hex & 0xff;
290     hex >>= 8;
291     this->Data[0] = hex & 0xff;
292   }
293   ///@}
294 
295   vtkColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255)
296     : vtkColor4<unsigned char>(r, g, b, a)
297   {
298   }
vtkColor4ub(const vtkColor3ub & c)299   vtkColor4ub(const vtkColor3ub& c)
300     : vtkColor4<unsigned char>(c[0], c[1], c[2], 255)
301   {
302   }
303 };
304 
305 class vtkColor4f : public vtkColor4<float>
306 {
307 public:
308   vtkColor4f() = default;
vtkColor4f(float scalar)309   explicit vtkColor4f(float scalar)
310     : vtkColor4<float>(scalar)
311   {
312   }
vtkColor4f(const float * init)313   explicit vtkColor4f(const float* init)
314     : vtkColor4<float>(init)
315   {
316   }
317   vtkColor4f(float r, float g, float b, float a = 1.0)
318     : vtkColor4<float>(r, g, b, a)
319   {
320   }
321 };
322 
323 class vtkColor4d : public vtkColor4<double>
324 {
325 public:
326   vtkColor4d() = default;
vtkColor4d(double scalar)327   explicit vtkColor4d(double scalar)
328     : vtkColor4<double>(scalar)
329   {
330   }
vtkColor4d(const double * init)331   explicit vtkColor4d(const double* init)
332     : vtkColor4<double>(init)
333   {
334   }
335   vtkColor4d(double r, double g, double b, double a = 1.0)
336     : vtkColor4<double>(r, g, b, a)
337   {
338   }
339 };
340 
341 #endif // vtkColor_h
342 // VTK-HeaderTest-Exclude: vtkColor.h
343