VTK  9.7.20260710
vtkColor.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
31
32#ifndef vtkColor_h
33#define vtkColor_h
34
35#include "vtkObject.h" // for legacy macros
36#include "vtkTuple.h"
37
38#include <algorithm> // for std::clamp
39#include <cmath> // for std::round
40
41// Forward declarations for conversion methods.
42VTK_ABI_NAMESPACE_BEGIN
43class vtkColor3ub;
44class vtkColor3f;
45class vtkColor3d;
46class vtkColor4ub;
47class vtkColor4f;
48class vtkColor4d;
49
50// .NAME vtkColor3 - templated base type for storage of 3 component colors.
51//
52template <typename T>
53class vtkColor3 : public vtkTuple<T, 3>
54{
55public:
56 vtkColor3() = default;
57
58 explicit vtkColor3(const T& scalar)
59 : vtkTuple<T, 3>(scalar)
60 {
61 }
62
63 explicit vtkColor3(const T* init)
64 : vtkTuple<T, 3>(init)
65 {
66 }
67
68 vtkColor3(const T& red, const T& green, const T& blue)
69 {
70 this->Data[0] = red;
71 this->Data[1] = green;
72 this->Data[2] = blue;
73 }
74
76
79 void Set(const T& red, const T& green, const T& blue)
80 {
81 this->Data[0] = red;
82 this->Data[1] = green;
83 this->Data[2] = blue;
84 }
85
86
90 void SetRed(const T& red) { this->Data[0] = red; }
91
95 const T& GetRed() const { return this->Data[0]; }
96
100 void SetGreen(const T& green) { this->Data[1] = green; }
101
105 const T& GetGreen() const { return this->Data[1]; }
106
110 void SetBlue(const T& blue) { this->Data[2] = blue; }
111
115 const T& GetBlue() const { return this->Data[2]; }
116};
117
118// .NAME vtkColor4 - templated base type for storage of 4 component colors.
119//
120template <typename T>
121class vtkColor4 : public vtkTuple<T, 4>
122{
123public:
124 vtkColor4() = default;
125
126 explicit vtkColor4(const T& scalar)
127 : vtkTuple<T, 4>(scalar)
128 {
129 }
130
131 explicit vtkColor4(const T* init)
132 : vtkTuple<T, 4>(init)
133 {
134 }
135
136 vtkColor4(const T& red, const T& green, const T& blue, const T& alpha)
137 {
138 this->Data[0] = red;
139 this->Data[1] = green;
140 this->Data[2] = blue;
141 this->Data[3] = alpha;
142 }
143
145
148 void Set(const T& red, const T& green, const T& blue)
149 {
150 this->Data[0] = red;
151 this->Data[1] = green;
152 this->Data[2] = blue;
153 }
154
155
157
160 void Set(const T& red, const T& green, const T& blue, const T& alpha)
161 {
162 this->Data[0] = red;
163 this->Data[1] = green;
164 this->Data[2] = blue;
165 this->Data[3] = alpha;
166 }
167
168
172 void SetRed(const T& red) { this->Data[0] = red; }
173
177 const T& GetRed() const { return this->Data[0]; }
178
182 void SetGreen(const T& green) { this->Data[1] = green; }
183
187 const T& GetGreen() const { return this->Data[1]; }
188
192 void SetBlue(const T& blue) { this->Data[2] = blue; }
193
197 const T& GetBlue() const { return this->Data[2]; }
198
202 void SetAlpha(const T& alpha) { this->Data[3] = alpha; }
203
207 const T& GetAlpha() const { return this->Data[3]; }
208};
209
213class vtkColor3ub : public vtkColor3<unsigned char>
214{
215public:
216 vtkColor3ub() = default;
217 explicit vtkColor3ub(unsigned char scalar)
218 : vtkColor3<unsigned char>(scalar)
219 {
220 }
221 explicit vtkColor3ub(const unsigned char* init)
222 : vtkColor3<unsigned char>(init)
223 {
224 }
225
227
230 explicit vtkColor3ub(int hexSigned)
231 {
232 unsigned int hex = static_cast<unsigned int>(hexSigned);
233 this->Data[2] = hex & 0xff;
234 hex >>= 8;
235 this->Data[1] = hex & 0xff;
236 hex >>= 8;
237 this->Data[0] = hex & 0xff;
238 }
239
240
241 vtkColor3ub(unsigned char r, unsigned char g, unsigned char b)
242 : vtkColor3<unsigned char>(r, g, b)
243 {
244 }
245
249 inline vtkColor3f ToFloat() const;
250
254 inline vtkColor3d ToDouble() const;
255};
256
257class vtkColor3f : public vtkColor3<float>
258{
259public:
260 vtkColor3f() = default;
261 explicit vtkColor3f(float scalar)
262 : vtkColor3<float>(scalar)
263 {
264 }
265 explicit vtkColor3f(const float* init)
266 : vtkColor3<float>(init)
267 {
268 }
269 vtkColor3f(float r, float g, float b)
270 : vtkColor3<float>(r, g, b)
271 {
272 }
273
277 inline vtkColor3ub ToUnsignedChar() const;
278
282 inline vtkColor3d ToDouble() const;
283};
284
285class vtkColor3d : public vtkColor3<double>
286{
287public:
288 vtkColor3d() = default;
289 explicit vtkColor3d(double scalar)
290 : vtkColor3<double>(scalar)
291 {
292 }
293 explicit vtkColor3d(const double* init)
294 : vtkColor3<double>(init)
295 {
296 }
297 vtkColor3d(double r, double g, double b)
298 : vtkColor3<double>(r, g, b)
299 {
300 }
301
305 inline vtkColor3ub ToUnsignedChar() const;
306
310 inline vtkColor3f ToFloat() const;
311};
312
313class vtkColor4ub : public vtkColor4<unsigned char>
314{
315public:
316 vtkColor4ub() = default;
317 explicit vtkColor4ub(unsigned char scalar)
318 : vtkColor4<unsigned char>(scalar)
319 {
320 }
321 explicit vtkColor4ub(const unsigned char* init)
322 : vtkColor4<unsigned char>(init)
323 {
324 }
325
327
331 explicit vtkColor4ub(int hexSigned)
332 {
333 unsigned int hex = static_cast<unsigned int>(hexSigned);
334 this->Data[3] = hex & 0xff;
335 hex >>= 8;
336 this->Data[2] = hex & 0xff;
337 hex >>= 8;
338 this->Data[1] = hex & 0xff;
339 hex >>= 8;
340 this->Data[0] = hex & 0xff;
341 }
342
343
344 vtkColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255)
345 : vtkColor4<unsigned char>(r, g, b, a)
346 {
347 }
349 : vtkColor4<unsigned char>(c[0], c[1], c[2], 255)
350 {
351 }
352
356 inline vtkColor4f ToFloat() const;
357
361 inline vtkColor4d ToDouble() const;
362};
363
364class vtkColor4f : public vtkColor4<float>
365{
366public:
367 vtkColor4f() = default;
368 explicit vtkColor4f(float scalar)
369 : vtkColor4<float>(scalar)
370 {
371 }
372 explicit vtkColor4f(const float* init)
373 : vtkColor4<float>(init)
374 {
375 }
376 vtkColor4f(float r, float g, float b, float a = 1.0)
377 : vtkColor4<float>(r, g, b, a)
378 {
379 }
380
384 inline vtkColor4ub ToUnsignedChar() const;
385
389 inline vtkColor4d ToDouble() const;
390};
391
392class vtkColor4d : public vtkColor4<double>
393{
394public:
395 vtkColor4d() = default;
396 explicit vtkColor4d(double scalar)
397 : vtkColor4<double>(scalar)
398 {
399 }
400 explicit vtkColor4d(const double* init)
401 : vtkColor4<double>(init)
402 {
403 }
404 vtkColor4d(double r, double g, double b, double a = 1.0)
405 : vtkColor4<double>(r, g, b, a)
406 {
407 }
408
412 inline vtkColor4ub ToUnsignedChar() const;
413
417 inline vtkColor4f ToFloat() const;
418};
419
420// ---------- vtkColor3ub conversions ----------
421
423{
424 return vtkColor3f(this->Data[0] / 255.0f, this->Data[1] / 255.0f, this->Data[2] / 255.0f);
425}
426
428{
429 return vtkColor3d(this->Data[0] / 255.0, this->Data[1] / 255.0, this->Data[2] / 255.0);
430}
431
432// ---------- vtkColor3f conversions ----------
433
435{
436 return vtkColor3ub(
437 static_cast<unsigned char>(std::round(std::clamp(this->Data[0] * 255.0f, 0.0f, 255.0f))),
438 static_cast<unsigned char>(std::round(std::clamp(this->Data[1] * 255.0f, 0.0f, 255.0f))),
439 static_cast<unsigned char>(std::round(std::clamp(this->Data[2] * 255.0f, 0.0f, 255.0f))));
440}
441
443{
444 return vtkColor3d(static_cast<double>(this->Data[0]), static_cast<double>(this->Data[1]),
445 static_cast<double>(this->Data[2]));
446}
447
448// ---------- vtkColor3d conversions ----------
449
451{
452 return vtkColor3ub(
453 static_cast<unsigned char>(std::round(std::clamp(this->Data[0] * 255.0, 0.0, 255.0))),
454 static_cast<unsigned char>(std::round(std::clamp(this->Data[1] * 255.0, 0.0, 255.0))),
455 static_cast<unsigned char>(std::round(std::clamp(this->Data[2] * 255.0, 0.0, 255.0))));
456}
457
459{
460 return vtkColor3f(static_cast<float>(this->Data[0]), static_cast<float>(this->Data[1]),
461 static_cast<float>(this->Data[2]));
462}
463
464// ---------- vtkColor4ub conversions ----------
465
467{
468 return vtkColor4f(
469 this->Data[0] / 255.0f, this->Data[1] / 255.0f, this->Data[2] / 255.0f, this->Data[3] / 255.0f);
470}
471
473{
474 return vtkColor4d(
475 this->Data[0] / 255.0, this->Data[1] / 255.0, this->Data[2] / 255.0, this->Data[3] / 255.0);
476}
477
478// ---------- vtkColor4f conversions ----------
479
481{
482 return vtkColor4ub(
483 static_cast<unsigned char>(std::round(std::clamp(this->Data[0] * 255.0f, 0.0f, 255.0f))),
484 static_cast<unsigned char>(std::round(std::clamp(this->Data[1] * 255.0f, 0.0f, 255.0f))),
485 static_cast<unsigned char>(std::round(std::clamp(this->Data[2] * 255.0f, 0.0f, 255.0f))),
486 static_cast<unsigned char>(std::round(std::clamp(this->Data[3] * 255.0f, 0.0f, 255.0f))));
487}
488
490{
491 return vtkColor4d(static_cast<double>(this->Data[0]), static_cast<double>(this->Data[1]),
492 static_cast<double>(this->Data[2]), static_cast<double>(this->Data[3]));
493}
494
495// ---------- vtkColor4d conversions ----------
496
498{
499 return vtkColor4ub(
500 static_cast<unsigned char>(std::round(std::clamp(this->Data[0] * 255.0, 0.0, 255.0))),
501 static_cast<unsigned char>(std::round(std::clamp(this->Data[1] * 255.0, 0.0, 255.0))),
502 static_cast<unsigned char>(std::round(std::clamp(this->Data[2] * 255.0, 0.0, 255.0))),
503 static_cast<unsigned char>(std::round(std::clamp(this->Data[3] * 255.0, 0.0, 255.0))));
504}
505
507{
508 return vtkColor4f(static_cast<float>(this->Data[0]), static_cast<float>(this->Data[1]),
509 static_cast<float>(this->Data[2]), static_cast<float>(this->Data[3]));
510}
511
512VTK_ABI_NAMESPACE_END
513#endif // vtkColor_h
514// VTK-HeaderTest-Exclude: vtkColor.h
vtkColor3(const T &scalar)
Definition vtkColor.h:58
void SetGreen(const T &green)
Set the green component of the color, i.e.
Definition vtkColor.h:100
vtkColor3()=default
void Set(const T &red, const T &green, const T &blue)
Set the red, green and blue components of the color.
Definition vtkColor.h:79
const T & GetBlue() const
Get the blue component of the color, i.e.
Definition vtkColor.h:115
void SetRed(const T &red)
Set the red component of the color, i.e.
Definition vtkColor.h:90
const T & GetRed() const
Get the red component of the color, i.e.
Definition vtkColor.h:95
vtkColor3(const T &red, const T &green, const T &blue)
Definition vtkColor.h:68
vtkColor3(const T *init)
Definition vtkColor.h:63
const T & GetGreen() const
Get the green component of the color, i.e.
Definition vtkColor.h:105
void SetBlue(const T &blue)
Set the blue component of the color, i.e.
Definition vtkColor.h:110
vtkColor3d(const double *init)
Definition vtkColor.h:293
vtkColor3d(double scalar)
Definition vtkColor.h:289
vtkColor3d()=default
vtkColor3f ToFloat() const
Convert to vtkColor3f with a simple static_cast.
Definition vtkColor.h:458
vtkColor3d(double r, double g, double b)
Definition vtkColor.h:297
vtkColor3ub ToUnsignedChar() const
Convert to vtkColor3ub by scaling from [0.0,1.0] to [0,255].
Definition vtkColor.h:450
vtkColor3f()=default
vtkColor3f(float r, float g, float b)
Definition vtkColor.h:269
vtkColor3f(float scalar)
Definition vtkColor.h:261
vtkColor3d ToDouble() const
Convert to vtkColor3d with a simple static_cast.
Definition vtkColor.h:442
vtkColor3f(const float *init)
Definition vtkColor.h:265
vtkColor3ub ToUnsignedChar() const
Convert to vtkColor3ub by scaling from [0.0,1.0] to [0,255].
Definition vtkColor.h:434
Some derived classes for the different colors commonly used.
Definition vtkColor.h:214
vtkColor3ub(unsigned char r, unsigned char g, unsigned char b)
Definition vtkColor.h:241
vtkColor3ub(unsigned char scalar)
Definition vtkColor.h:217
vtkColor3ub()=default
vtkColor3d ToDouble() const
Convert to vtkColor3d by scaling from [0,255] to [0.0,1.0].
Definition vtkColor.h:427
vtkColor3f ToFloat() const
Convert to vtkColor3f by scaling from [0,255] to [0.0,1.0].
Definition vtkColor.h:422
vtkColor3ub(int hexSigned)
Construct a color from a hexadecimal representation such as 0x0000FF (blue).
Definition vtkColor.h:230
vtkColor3ub(const unsigned char *init)
Definition vtkColor.h:221
vtkColor4()=default
const T & GetAlpha() const
Get the alpha component of the color, i.e.
Definition vtkColor.h:207
void SetAlpha(const T &alpha)
Set the alpha component of the color, i.e.
Definition vtkColor.h:202
vtkColor4(const T &scalar)
Definition vtkColor.h:126
void Set(const T &red, const T &green, const T &blue, const T &alpha)
Set the red, green, blue and alpha components of the color.
Definition vtkColor.h:160
vtkColor4(const T &red, const T &green, const T &blue, const T &alpha)
Definition vtkColor.h:136
void SetRed(const T &red)
Set the red component of the color, i.e.
Definition vtkColor.h:172
vtkColor4(const T *init)
Definition vtkColor.h:131
const T & GetBlue() const
Get the blue component of the color, i.e.
Definition vtkColor.h:197
void SetBlue(const T &blue)
Set the blue component of the color, i.e.
Definition vtkColor.h:192
const T & GetGreen() const
Get the green component of the color, i.e.
Definition vtkColor.h:187
void SetGreen(const T &green)
Set the green component of the color, i.e.
Definition vtkColor.h:182
void Set(const T &red, const T &green, const T &blue)
Set the red, green and blue components of the color.
Definition vtkColor.h:148
const T & GetRed() const
Get the red component of the color, i.e.
Definition vtkColor.h:177
vtkColor4d(double r, double g, double b, double a=1.0)
Definition vtkColor.h:404
vtkColor4f ToFloat() const
Convert to vtkColor4f with a simple static_cast.
Definition vtkColor.h:506
vtkColor4d(const double *init)
Definition vtkColor.h:400
vtkColor4d()=default
vtkColor4d(double scalar)
Definition vtkColor.h:396
vtkColor4ub ToUnsignedChar() const
Convert to vtkColor4ub by scaling from [0.0,1.0] to [0,255].
Definition vtkColor.h:497
vtkColor4f(float r, float g, float b, float a=1.0)
Definition vtkColor.h:376
vtkColor4f(float scalar)
Definition vtkColor.h:368
vtkColor4ub ToUnsignedChar() const
Convert to vtkColor4ub by scaling from [0.0,1.0] to [0,255].
Definition vtkColor.h:480
vtkColor4f(const float *init)
Definition vtkColor.h:372
vtkColor4f()=default
vtkColor4d ToDouble() const
Convert to vtkColor4d with a simple static_cast.
Definition vtkColor.h:489
vtkColor4d ToDouble() const
Convert to vtkColor4d by scaling from [0,255] to [0.0,1.0].
Definition vtkColor.h:472
vtkColor4ub(int hexSigned)
Construct a color from a hexadecimal representation such as 0x0000FFAA (opaque blue).
Definition vtkColor.h:331
vtkColor4f ToFloat() const
Convert to vtkColor4f by scaling from [0,255] to [0.0,1.0].
Definition vtkColor.h:466
vtkColor4ub(const vtkColor3ub &c)
Definition vtkColor.h:348
vtkColor4ub(unsigned char scalar)
Definition vtkColor.h:317
vtkColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition vtkColor.h:344
vtkColor4ub()=default
vtkColor4ub(const unsigned char *init)
Definition vtkColor.h:321
vtkTuple()=default