Thea
ColorRgb8.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2013
12 //
13 //============================================================================
14 
15 /*
16  ORIGINAL HEADER
17 
18  @file Color3uint8.h
19 
20  @maintainer Morgan McGuire, http://graphics.cs.williams.edu
21 
22  @created 2003-04-07
23  @edited 2010-03-24
24 
25  Copyright 2000-2010, Morgan McGuire.
26  All rights reserved.
27 */
28 
29 #ifndef __Thea_ColorRgb8_hpp__
30 #define __Thea_ColorRgb8_hpp__
31 
32 #include "Common.hpp"
33 #include "ColorL.hpp"
34 #include "Math.hpp"
35 
36 namespace Thea {
37 
38 // Forward declarations
39 class ColorRgb;
40 class ColorRgba;
41 
47 class THEA_API ColorRgb8
48 {
49  private:
50  uint8 c[3];
51 
52  public:
53  typedef uint8 value_type;
54 
56  ColorRgb8() {}
57 
59  ColorRgb8(uint8 r_, uint8 g_, uint8 b_) { c[0] = r_; c[1] = g_; c[2] = b_; }
60 
62  explicit ColorRgb8(uint8 v) { c[0] = c[1] = c[2] = v; }
63 
65  explicit ColorRgb8(uint8 const * v) { c[0] = v[0]; c[1] = v[1]; c[2] = v[2]; }
66 
68  ColorRgb8(ColorRgba const & other);
69 
71  ColorRgb8(ColorRgb const & src);
72 
74  static ColorRgb8 fromARGB(uint32 argb);
75 
77  uint8 r() const { return c[0]; }
78 
80  uint8 & r() { return c[0]; }
81 
83  uint8 g() const { return c[1]; }
84 
86  uint8 & g() { return c[1]; }
87 
89  uint8 b() const { return c[2]; }
90 
92  uint8 & b() { return c[2]; }
93 
95  uint8 const * data() const { return c; }
96 
98  uint8 * data() { return c; }
99 
101  template <typename IntegerT> uint8 const & operator[](IntegerT channel) const
102  {
103  debugAssertM(channel >= 0 && channel <= 2, "ColorRgb8: Channel must be 0, 1 or 2");
104  return c[channel];
105  }
106 
108  template <typename IntegerT> uint8 & operator[](IntegerT channel)
109  {
110  debugAssertM(channel >= 0 && channel <= 2, "ColorRgb8: Channel must be 0, 1 or 2");
111  return c[channel];
112  }
113 
115  void set(uint8 r_, uint8 g_, uint8 b_)
116  {
117  c[0] = r_;
118  c[1] = g_;
119  c[2] = b_;
120  }
121 
123  void fill(uint8 v) { c[0] = c[1] = c[2] = v; }
124 
126  ColorRgb8 operator+(ColorRgb8 const & rhs) const
127  {
128  return ColorRgb8((uint8)std::min(255, (int)c[0] + (int)rhs.c[0]),
129  (uint8)std::min(255, (int)c[1] + (int)rhs.c[1]),
130  (uint8)std::min(255, (int)c[2] + (int)rhs.c[2]));
131  }
132 
134  ColorRgb8 operator-(ColorRgb8 const & rhs) const
135  {
136  return ColorRgb8((uint8)std::max(0, (int)c[0] - (int)rhs.c[0]),
137  (uint8)std::max(0, (int)c[1] - (int)rhs.c[1]),
138  (uint8)std::max(0, (int)c[2] - (int)rhs.c[2]));
139  }
140 
142  ColorRgb8 operator*(Real s) const
143  {
144  return ColorRgb8((uint8)Math::clamp((Real)Math::round(c[0] * s), (Real)0, (Real)255),
145  (uint8)Math::clamp((Real)Math::round(c[1] * s), (Real)0, (Real)255),
146  (uint8)Math::clamp((Real)Math::round(c[2] * s), (Real)0, (Real)255));
147  }
148 
150  ColorRgb8 operator/(Real s) const
151  {
152  return ColorRgb8((uint8)Math::clamp((Real)Math::round(c[0] / s), (Real)0, (Real)255),
153  (uint8)Math::clamp((Real)Math::round(c[1] / s), (Real)0, (Real)255),
154  (uint8)Math::clamp((Real)Math::round(c[2] / s), (Real)0, (Real)255));
155  }
156 
158  ColorRgb8 & operator+=(ColorRgb8 const & rhs)
159  {
160  *this = *this + rhs;
161  return *this;
162  }
163 
165  ColorRgb8 & operator-=(ColorRgb8 const & rhs)
166  {
167  *this = *this - rhs;
168  return *this;
169  }
170 
172  ColorRgb8 & operator*=(Real s)
173  {
174  *this = *this * s;
175  return *this;
176  }
177 
179  ColorRgb8 & operator/=(Real s)
180  {
181  *this = *this / s;
182  return *this;
183  }
184 
186  ColorRgb8 max(ColorRgb8 const & rhs) const
187  {
188  return ColorRgb8(std::max(c[0], rhs.c[0]), std::max(c[1], rhs.c[1]), std::max(c[2], rhs.c[2]));
189  }
190 
192  ColorRgb8 min(ColorRgb8 const & rhs) const
193  {
194  return ColorRgb8(std::min(c[0], rhs.c[0]), std::min(c[1], rhs.c[1]), std::min(c[2], rhs.c[2]));
195  }
196 
198  uint8 max() const
199  {
200  return std::max(std::max(c[0], c[1]), c[2]);
201  }
202 
204  uint8 min() const
205  {
206  return std::min(std::min(c[0], c[1]), c[2]);
207  }
208 
210  ColorRgb8 bgr() const
211  {
212  return ColorRgb8(c[2], c[1], c[0]);
213  }
214 
219  uint32 asUInt32() const
220  {
221  return (0xFF << 24) | ((uint32)c[0] << 16) | ((uint32)c[1] << 8) | (uint32)c[2];
222  }
223 
225  std::string toString() const;
226 
227 } THEA_END_PACKED_CLASS(1) // class ColorRgb8
228 
229 
230 inline ColorRgb8
231 operator*(Real s, ColorRgb8 const & c)
232 {
233  return c * s;
234 }
235 
237 inline ColorRgb8
238 operator*(ColorL & s, ColorRgb8 const & c)
239 {
240  return c * s.value();
241 }
242 
244 inline ColorRgb8
245 operator*(ColorRgb8 const & c, ColorL & s)
246 {
247  return c * s.value();
248 }
249 
250 } // namespace Thea
251 
252 #endif
ColorRgb8 & operator/=(Real s)
Divide by a scalar and assign.
Definition: ColorRgb8.hpp:179
ColorRgb8 operator-(ColorRgb8 const &rhs) const
Subtraction.
Definition: ColorRgb8.hpp:134
ColorRgb8 max(ColorRgb8 const &rhs) const
Get the per-component maximum of two colors.
Definition: ColorRgb8.hpp:186
ColorL operator*(Real lhs, ColorL const &rhs)
Multiply by a scalar.
Definition: ColorL.hpp:201
A color with three floating-point channels: red, green and blue, each in [0, 1].
Definition: ColorRgb.hpp:51
uint8 value_type
The numerical type of each channel.
Definition: ColorRgb8.hpp:53
std::string toString(char const *s)
Convert a C-string to a std::string object, returning an empty string if the input is a null pointer...
uint32 asUInt32() const
Get the color packed into a 32-bit unsigned integer.
Definition: ColorRgb8.hpp:219
ColorRgb8 bgr() const
Swap the red and blue channels.
Definition: ColorRgb8.hpp:210
uint8 g() const
The value of the green channel.
Definition: ColorRgb8.hpp:83
uint8 & g()
A reference to the green channel.
Definition: ColorRgb8.hpp:86
Root namespace for the Thea library.
ColorRgb8()
Default constructor.
Definition: ColorRgb8.hpp:56
uint8 min() const
Get the smallest component.
Definition: ColorRgb8.hpp:204
uint8 & r()
A reference to the red channel.
Definition: ColorRgb8.hpp:80
A color with three byte-sized channels: red, green and blue, each in [0, 255].
Definition: ColorRgb8.hpp:47
ColorRgb8 operator*(Real s) const
Multiplication by a scalar.
Definition: ColorRgb8.hpp:142
uint8 b() const
The value of the blue channel.
Definition: ColorRgb8.hpp:89
ColorRgb8 & operator+=(ColorRgb8 const &rhs)
Add and assign.
Definition: ColorRgb8.hpp:158
T clamp(T const &x, U const &lo, V const &hi)
Clamp a number to lie in the range [lo, hi] (inclusive).
Definition: Math.hpp:328
ColorRgb8 & operator*=(Real s)
Multiply by a scalar and assign.
Definition: ColorRgb8.hpp:172
uint8 r() const
The value of the red channel.
Definition: ColorRgb8.hpp:77
#define THEA_END_PACKED_CLASS(byte_align)
Mark end of class that uses tight alignment.
Definition: Platform.hpp:142
ColorRgb8 operator+(ColorRgb8 const &rhs) const
Addition.
Definition: ColorRgb8.hpp:126
uint8 * data()
Get the address of the array storing color channel values in RGB order.
Definition: ColorRgb8.hpp:98
uint8 & b()
A reference to the blue channel.
Definition: ColorRgb8.hpp:92
Monochrome luminance value in [0, 1], with automatic scaling by 255 when switching between integer (C...
Definition: ColorL.hpp:46
ColorRgb8 operator/(Real s) const
Division by a scalar.
Definition: ColorRgb8.hpp:150
ColorRgb8(uint8 const *v)
Construct a color from three components in an array.
Definition: ColorRgb8.hpp:65
uint8 & operator[](IntegerT channel)
Array-style channel access.
Definition: ColorRgb8.hpp:108
#define THEA_BEGIN_PACKED_CLASS(byte_align)
Use tight alignment for a class.
Definition: Platform.hpp:133
Real value() const
The value of the color.
Definition: ColorL.hpp:67
ColorRgb8(uint8 r_, uint8 g_, uint8 b_)
Construct from red, green and blue components.
Definition: ColorRgb8.hpp:59
ColorRgb8(uint8 v)
Initialize all channels to the same value.
Definition: ColorRgb8.hpp:62
A color with four floating-point channels: red, green and blue, each in [0, 1].
Definition: ColorRgba.hpp:52
float round(float x)
Round a real number to the nearest integer using the lrintf() routine.
Definition: Math.hpp:313
ColorRgb8 & operator-=(ColorRgb8 const &rhs)
Subtract and assign.
Definition: ColorRgb8.hpp:165
uint8 const & operator[](IntegerT channel) const
Array-style channel access.
Definition: ColorRgb8.hpp:101
ColorRgb8 min(ColorRgb8 const &rhs) const
Get the per-component minimum of two colors.
Definition: ColorRgb8.hpp:192
void fill(uint8 v)
Set all channels to the same value.
Definition: ColorRgb8.hpp:123
uint8 const * data() const
Get the address of the array storing color channel values in RGB order.
Definition: ColorRgb8.hpp:95
void debugAssertM(CondT const &test, MessageT const &msg)
Check if a test condition is true, and immediately abort the program with an error code if not...
Definition: Common.hpp:52
uint8 max() const
Get the largest component.
Definition: ColorRgb8.hpp:198