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