VTK-m  2.0
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 
13 #include <vtkm/rendering/vtkm_rendering_export.h>
14 
15 #include <iostream>
16 #include <vtkm/Types.h>
17 namespace vtkm
18 {
19 namespace rendering
20 {
21 
28 class Color
29 {
30 public:
32 
35  : Components(0, 0, 0, 1)
36  {
37  }
38 
41  : Components(r_, g_, b_, a_)
42  {
43  }
44 
46  Color(const vtkm::Vec4f_32& components)
47  : Components(components)
48  {
49  }
50 
53  {
54  // Note that though GetComponentAsByte below
55  // multiplies by 256, we're dividing by 255. here.
56  // This is, believe it or not, still correct.
57  // That's partly because we always round down in
58  // that method. For example, if we set the float
59  // here using byte(1), /255 gives us .00392, which
60  // *256 gives us 1.0035, which is then rounded back
61  // down to byte(1) below. Or, if we set the float
62  // here using byte(254), /255 gives us .99608, which
63  // *256 gives us 254.996, which is then rounded
64  // back down to 254 below. So it actually reverses
65  // correctly, even though the multiplier and
66  // divider don't match between these two methods.
67  //
68  // Of course, converting in GetComponentAsByte from
69  // 1.0 gives 256, so we need to still clamp to 255
70  // anyway. Again, this is not a problem, because it
71  // doesn't really extend the range of floating point
72  // values which map to 255.
73  Components[i] = static_cast<vtkm::Float32>(v) / 255.f;
74  // clamp?
75  if (Components[i] < 0)
76  Components[i] = 0;
77  if (Components[i] > 1)
78  Components[i] = 1;
79  }
80 
83  {
84  // We need this to match what OpenGL/Mesa do.
85  // Why? Well, we need to set glClearColor
86  // using floats, but the frame buffer comes
87  // back as bytes (and is internally such) in
88  // most cases. In one example -- parallel
89  // compositing -- we need the byte values
90  // returned from here to match the byte values
91  // returned in the frame buffer. Though
92  // a quick source code inspection of Mesa
93  // led me to believe I should do *255., in
94  // fact this led to a mismatch. *256. was
95  // actually closer. (And arguably more correct
96  // if you think the byte value 255 should share
97  // approximately the same range in the float [0,1]
98  // space as the other byte values.) Note in the
99  // inverse method above, though, we still use 255;
100  // see SetComponentFromByte for an explanation of
101  // why that is correct, if non-obvious.
102 
103  int tv = vtkm::Int32(Components[i] * 256.f);
104  // Converting even from valid values (i.e 1.0)
105  // can give a result outside the range (i.e. 256),
106  // but we have to clamp anyway.
107  return vtkm::UInt8((tv < 0) ? 0 : (tv > 255) ? 255 : tv);
108  }
109 
112  {
113  r = GetComponentAsByte(0);
114  g = GetComponentAsByte(1);
115  b = GetComponentAsByte(2);
116  a = GetComponentAsByte(3);
117  }
118 
120  vtkm::Float64 RawBrightness() { return (Components[0] + Components[1] + Components[2]) / 3.; }
121 
122  VTKM_CONT
123  friend std::ostream& operator<<(std::ostream& out, const Color& c)
124  {
125  out << "[" << c.Components[0] << "," << c.Components[1] << "," << c.Components[2] << ","
126  << c.Components[3] << "]";
127  return out;
128  }
129 
130  static VTKM_RENDERING_EXPORT Color white, black;
131  static VTKM_RENDERING_EXPORT Color red, green, blue;
132  static VTKM_RENDERING_EXPORT Color cyan, magenta, yellow;
133  static VTKM_RENDERING_EXPORT Color gray10, gray20, gray30, gray40, gray50, gray60, gray70, gray80,
135 };
136 }
137 } //namespace vtkm::rendering
138 #endif //vtk_m_rendering_Color_h
vtkm::rendering::Color::Color
VTKM_EXEC_CONT Color()
Definition: Color.h:34
vtkm::rendering::Color
It's a color!
Definition: Color.h:28
vtkm::rendering::Color::green
static VTKM_RENDERING_EXPORT Color green
Definition: Color.h:131
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::rendering::Color::black
static VTKM_RENDERING_EXPORT Color black
Definition: Color.h:130
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::rendering::Color::SetComponentFromByte
VTKM_EXEC_CONT void SetComponentFromByte(vtkm::Int32 i, vtkm::UInt8 v)
Definition: Color.h:52
vtkm::rendering::Color::gray30
static VTKM_RENDERING_EXPORT Color gray30
Definition: Color.h:133
vtkm::rendering::Color::gray60
static VTKM_RENDERING_EXPORT Color gray60
Definition: Color.h:133
vtkm::rendering::Color::magenta
static VTKM_RENDERING_EXPORT Color magenta
Definition: Color.h:132
vtkm::rendering::Color::operator<<
VTKM_CONT friend std::ostream & operator<<(std::ostream &out, const Color &c)
Definition: Color.h:123
vtkm::rendering::Color::Color
VTKM_EXEC_CONT Color(vtkm::Float32 r_, vtkm::Float32 g_, vtkm::Float32 b_, vtkm::Float32 a_=1.f)
Definition: Color.h:40
vtkm::rendering::Color::yellow
static VTKM_RENDERING_EXPORT Color yellow
Definition: Color.h:132
vtkm::rendering::Color::red
static VTKM_RENDERING_EXPORT Color red
Definition: Color.h:131
vtkm::rendering::Color::gray10
static VTKM_RENDERING_EXPORT Color gray10
Definition: Color.h:133
vtkm::rendering::Color::gray20
static VTKM_RENDERING_EXPORT Color gray20
Definition: Color.h:133
vtkm::rendering::Color::white
static VTKM_RENDERING_EXPORT Color white
Definition: Color.h:130
vtkm::rendering::Color::gray90
static VTKM_RENDERING_EXPORT Color gray90
Definition: Color.h:134
vtkm::rendering::Color::GetComponentAsByte
VTKM_EXEC_CONT vtkm::UInt8 GetComponentAsByte(int i)
Definition: Color.h:82
vtkm::rendering::Color::Components
vtkm::Vec4f_32 Components
Definition: Color.h:31
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::rendering::Color::blue
static VTKM_RENDERING_EXPORT Color blue
Definition: Color.h:131
vtkm::rendering::Color::Color
VTKM_EXEC_CONT Color(const vtkm::Vec4f_32 &components)
Definition: Color.h:46
vtkm::Vec< vtkm::Float32, 4 >
vtkm::rendering::Color::gray40
static VTKM_RENDERING_EXPORT Color gray40
Definition: Color.h:133
vtkm::rendering::Color::GetRGBA
VTKM_EXEC_CONT void GetRGBA(vtkm::UInt8 &r, vtkm::UInt8 &g, vtkm::UInt8 &b, vtkm::UInt8 &a)
Definition: Color.h:111
vtkm::rendering::Color::cyan
static VTKM_RENDERING_EXPORT Color cyan
Definition: Color.h:132
vtkm::rendering::Color::RawBrightness
VTKM_EXEC_CONT vtkm::Float64 RawBrightness()
Definition: Color.h:120
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::rendering::Color::gray50
static VTKM_RENDERING_EXPORT Color gray50
Definition: Color.h:133
vtkm::rendering::Color::gray80
static VTKM_RENDERING_EXPORT Color gray80
Definition: Color.h:133
vtkm::rendering::Color::gray70
static VTKM_RENDERING_EXPORT Color gray70
Definition: Color.h:133