VTK-m  2.2
Color.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_rendering_Color_h
11 #define vtk_m_rendering_Color_h
12 
14 
15 #include <iostream>
16 #include <vtkm/Types.h>
17 namespace vtkm
18 {
19 namespace rendering
20 {
21 
29 class Color
30 {
31 public:
33 
37  : Components(0, 0, 0, 1)
38  {
39  }
40 
46  : Components(r_, g_, b_, a_)
47  {
48  }
49 
54  Color(const vtkm::Vec4f_32& components)
55  : Components(components)
56  {
57  }
58 
64  {
65  // Note that though GetComponentAsByte below
66  // multiplies by 256, we're dividing by 255. here.
67  // This is, believe it or not, still correct.
68  // That's partly because we always round down in
69  // that method. For example, if we set the float
70  // here using byte(1), /255 gives us .00392, which
71  // *256 gives us 1.0035, which is then rounded back
72  // down to byte(1) below. Or, if we set the float
73  // here using byte(254), /255 gives us .99608, which
74  // *256 gives us 254.996, which is then rounded
75  // back down to 254 below. So it actually reverses
76  // correctly, even though the multiplier and
77  // divider don't match between these two methods.
78  //
79  // Of course, converting in GetComponentAsByte from
80  // 1.0 gives 256, so we need to still clamp to 255
81  // anyway. Again, this is not a problem, because it
82  // doesn't really extend the range of floating point
83  // values which map to 255.
84  Components[i] = static_cast<vtkm::Float32>(v) / 255.f;
85  // clamp?
86  if (Components[i] < 0)
87  Components[i] = 0;
88  if (Components[i] > 1)
89  Components[i] = 1;
90  }
91 
94  {
95  // We need this to match what OpenGL/Mesa do.
96  // Why? Well, we need to set glClearColor
97  // using floats, but the frame buffer comes
98  // back as bytes (and is internally such) in
99  // most cases. In one example -- parallel
100  // compositing -- we need the byte values
101  // returned from here to match the byte values
102  // returned in the frame buffer. Though
103  // a quick source code inspection of Mesa
104  // led me to believe I should do *255., in
105  // fact this led to a mismatch. *256. was
106  // actually closer. (And arguably more correct
107  // if you think the byte value 255 should share
108  // approximately the same range in the float [0,1]
109  // space as the other byte values.) Note in the
110  // inverse method above, though, we still use 255;
111  // see SetComponentFromByte for an explanation of
112  // why that is correct, if non-obvious.
113 
114  int tv = vtkm::Int32(Components[i] * 256.f);
115  // Converting even from valid values (i.e 1.0)
116  // can give a result outside the range (i.e. 256),
117  // but we have to clamp anyway.
118  return vtkm::UInt8((tv < 0) ? 0 : (tv > 255) ? 255 : tv);
119  }
120 
123  {
124  r = GetComponentAsByte(0);
125  g = GetComponentAsByte(1);
126  b = GetComponentAsByte(2);
127  a = GetComponentAsByte(3);
128  }
129 
131  vtkm::Float64 RawBrightness() { return (Components[0] + Components[1] + Components[2]) / 3.; }
132 
133  VTKM_CONT
134  friend std::ostream& operator<<(std::ostream& out, const Color& c)
135  {
136  out << "[" << c.Components[0] << "," << c.Components[1] << "," << c.Components[2] << ","
137  << c.Components[3] << "]";
138  return out;
139  }
140 
146 };
147 }
148 } //namespace vtkm::rendering
149 #endif //vtk_m_rendering_Color_h
vtkm::rendering::Color::GetComponentAsByte
vtkm::UInt8 GetComponentAsByte(int i)
Definition: Color.h:93
vtkm::rendering::Color::gray80
static Color gray80
Definition: Color.h:144
vtkm::rendering::Color
Representation of a color.
Definition: Color.h:29
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_RENDERING_EXPORT
#define VTKM_RENDERING_EXPORT
Definition: vtkm_rendering_export.h:44
vtkm::rendering::Color::gray60
static Color gray60
Definition: Color.h:144
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::rendering::Color::gray40
static Color gray40
Definition: Color.h:144
vtkm::rendering::Color::gray20
static Color gray20
Definition: Color.h:144
vtkm::rendering::Color::gray90
static Color gray90
Definition: Color.h:145
vtkm::rendering::Color::cyan
static Color cyan
Definition: Color.h:143
vtkm::rendering::Color::Color
Color(vtkm::Float32 r_, vtkm::Float32 g_, vtkm::Float32 b_, vtkm::Float32 a_=1.f)
Create a color with specified RGBA values.
Definition: Color.h:45
vtkm::rendering::Color::gray50
static Color gray50
Definition: Color.h:144
vtkm::rendering::Color::gray30
static Color gray30
Definition: Color.h:144
vtkm_rendering_export.h
vtkm::rendering::Color::gray70
static Color gray70
Definition: Color.h:144
vtkm::rendering::Color::white
static Color white
Definition: Color.h:141
vtkm::rendering::Color::Color
Color()
Create a black color.
Definition: Color.h:36
vtkm::rendering::Color::magenta
static Color magenta
Definition: Color.h:143
vtkm::rendering::Color::Components
vtkm::Vec4f_32 Components
Definition: Color.h:32
vtkm::rendering::Color::gray10
static Color gray10
Definition: Color.h:144
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:169
vtkm::rendering::Color::SetComponentFromByte
void SetComponentFromByte(vtkm::Int32 i, vtkm::UInt8 v)
Set the color value from 8 bit RGBA components.
Definition: Color.h:63
vtkm::rendering::Color::operator<<
friend std::ostream & operator<<(std::ostream &out, const Color &c)
Definition: Color.h:134
vtkm::Vec< vtkm::Float32, 4 >
vtkm::rendering::Color::yellow
static Color yellow
Definition: Color.h:143
vtkm::rendering::Color::blue
static Color blue
Definition: Color.h:142
vtkm::Float32
float Float32
Base type to use for 32-bit floating-point numbers.
Definition: Types.h:157
vtkm::Int32
int32_t Int32
Base type to use for 32-bit signed integer numbers.
Definition: Types.h:181
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
vtkm::rendering::Color::RawBrightness
vtkm::Float64 RawBrightness()
Definition: Color.h:131
vtkm::rendering::Color::green
static Color green
Definition: Color.h:142
vtkm::rendering::Color::red
static Color red
Definition: Color.h:142
vtkm::rendering::Color::Color
Color(const vtkm::Vec4f_32 &components)
Create a color with specified RGBA values.
Definition: Color.h:54
vtkm::rendering::Color::GetRGBA
void GetRGBA(vtkm::UInt8 &r, vtkm::UInt8 &g, vtkm::UInt8 &b, vtkm::UInt8 &a)
Definition: Color.h:122
vtkm::rendering::Color::black
static Color black
Definition: Color.h:141