Thea
ColorRgba8.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 Color4uint8.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_ColorRgba8_hpp__
30 #define __Thea_ColorRgba8_hpp__
31 
32 #include "Common.hpp"
33 #include "ColorL.hpp"
34 #include "ColorRgb8.hpp"
35 #include "Math.hpp"
36 
37 namespace Thea {
38 
39 // Forward declarations
40 class ColorRgba;
41 
47 class THEA_API ColorRgba8
48 {
49  private:
50  uint8 c[4];
51 
52  public:
53  typedef uint8 value_type;
54 
57 
59  ColorRgba8(uint8 r_, uint8 g_, uint8 b_, uint8 a_) { c[0] = r_; c[1] = g_; c[2] = b_; c[3] = a_; }
60 
62  ColorRgba8(ColorRgb8 const & rgb_, uint8 a_ = 255) { c[0] = rgb_.r(); c[1] = rgb_.g(); c[2] = rgb_.b(); c[3] = a_; }
63 
65  explicit ColorRgba8(uint8 const * v) { c[0] = v[0]; c[1] = v[1]; c[2] = v[2]; c[3] = v[3]; }
66 
68  ColorRgba8(ColorRgba const & src);
69 
71  static ColorRgba8 fromARGB(uint32 argb);
72 
74  uint8 r() const { return c[0]; }
75 
77  uint8 & r() { return c[0]; }
78 
80  uint8 g() const { return c[1]; }
81 
83  uint8 & g() { return c[1]; }
84 
86  uint8 b() const { return c[2]; }
87 
89  uint8 & b() { return c[2]; }
90 
92  uint8 a() const { return c[3]; }
93 
95  uint8 & a() { return c[3]; }
96 
98  ColorRgb8 rgb() const { return ColorRgb8(c[0], c[1], c[2]); }
99 
101  uint8 const * data() const { return c; }
102 
104  uint8 * data() { return c; }
105 
107  template <typename IntegerT> uint8 const & operator[](IntegerT channel) const
108  {
109  debugAssertM(channel >= 0 && channel <= 3, "ColorRgba8: Channel must be 0, 1, 2 or 3");
110  return c[channel];
111  }
112 
114  template <typename IntegerT> uint8 & operator[](IntegerT channel)
115  {
116  debugAssertM(channel >= 0 && channel <= 3, "ColorRgba8: Channel must be 0, 1, 2 or 3");
117  return c[channel];
118  }
119 
121  void set(uint8 r_, uint8 g_, uint8 b_, uint8 a_)
122  {
123  c[0] = r_;
124  c[1] = g_;
125  c[2] = b_;
126  c[3] = a_;
127  }
128 
130  void fill(uint8 v) { c[0] = c[1] = c[2] = c[3] = v; }
131 
133  ColorRgba8 operator+(ColorRgba8 const & rhs) const
134  {
135  return ColorRgba8((uint8)std::min(255, (int)c[0] + (int)rhs.c[0]),
136  (uint8)std::min(255, (int)c[1] + (int)rhs.c[1]),
137  (uint8)std::min(255, (int)c[2] + (int)rhs.c[2]),
138  (uint8)std::min(255, (int)c[3] + (int)rhs.c[3]));
139  }
140 
142  ColorRgba8 operator-(ColorRgba8 const & rhs) const
143  {
144  return ColorRgba8((uint8)std::max(0, (int)c[0] - (int)rhs.c[0]),
145  (uint8)std::max(0, (int)c[1] - (int)rhs.c[1]),
146  (uint8)std::max(0, (int)c[2] - (int)rhs.c[2]),
147  (uint8)std::max(0, (int)c[3] - (int)rhs.c[3]));
148  }
149 
151  ColorRgba8 operator*(Real s) const
152  {
153  return ColorRgba8((uint8)Math::clamp((Real)Math::round(c[0] * s), (Real)0, (Real)255),
154  (uint8)Math::clamp((Real)Math::round(c[1] * s), (Real)0, (Real)255),
155  (uint8)Math::clamp((Real)Math::round(c[2] * s), (Real)0, (Real)255),
156  (uint8)Math::clamp((Real)Math::round(c[3] * s), (Real)0, (Real)255));
157  }
158 
160  ColorRgba8 operator/(Real s) const
161  {
162  return ColorRgba8((uint8)Math::clamp((Real)Math::round(c[0] / s), (Real)0, (Real)255),
163  (uint8)Math::clamp((Real)Math::round(c[1] / s), (Real)0, (Real)255),
164  (uint8)Math::clamp((Real)Math::round(c[2] / s), (Real)0, (Real)255),
165  (uint8)Math::clamp((Real)Math::round(c[3] / s), (Real)0, (Real)255));
166  }
167 
169  ColorRgba8 & operator+=(ColorRgba8 const & rhs)
170  {
171  *this = *this + rhs;
172  return *this;
173  }
174 
176  ColorRgba8 & operator-=(ColorRgba8 const & rhs)
177  {
178  *this = *this - rhs;
179  return *this;
180  }
181 
183  ColorRgba8 & operator*=(Real s)
184  {
185  *this = *this * s;
186  return *this;
187  }
188 
190  ColorRgba8 & operator/=(Real s)
191  {
192  *this = *this / s;
193  return *this;
194  }
195 
197  ColorRgba8 bgra() const
198  {
199  return ColorRgba8(c[2], c[1], c[0], c[3]);
200  }
201 
206  uint32 asUInt32() const
207  {
208  return ((uint32)c[3] << 24) | ((uint32)c[0] << 16) | ((uint32)c[1] << 8) | (uint32)c[2];
209  }
210 
212  std::string toString() const;
213 
214 } THEA_END_PACKED_CLASS(1) // class ColorRgba8
215 
216 
217 inline ColorRgba8
218 operator*(Real s, ColorRgba8 const & c)
219 {
220  return c * s;
221 }
222 
224 inline ColorRgba8
225 operator*(ColorL & s, ColorRgba8 const & c)
226 {
227  return c * s.value();
228 }
229 
231 inline ColorRgba8
232 operator*(ColorRgba8 const & c, ColorL & s)
233 {
234  return c * s.value();
235 }
236 
237 } // namespace Thea
238 
239 #endif
ColorL operator*(Real lhs, ColorL const &rhs)
Multiply by a scalar.
Definition: ColorL.hpp:201
uint8 r() const
The value of the red channel.
Definition: ColorRgba8.hpp:74
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...
uint8 & b()
A reference to the blue channel.
Definition: ColorRgba8.hpp:89
uint8 g() const
The value of the green channel.
Definition: ColorRgb8.hpp:83
uint32 asUInt32() const
Get the color packed into a 32-bit unsigned integer.
Definition: ColorRgba8.hpp:206
ColorRgba8 operator-(ColorRgba8 const &rhs) const
Subtraction.
Definition: ColorRgba8.hpp:142
ColorRgba8 & operator/=(Real s)
Divide by a scalar and assign.
Definition: ColorRgba8.hpp:190
Root namespace for the Thea library.
ColorRgba8()
Default constructor.
Definition: ColorRgba8.hpp:56
ColorRgba8 operator/(Real s) const
Division by a scalar.
Definition: ColorRgba8.hpp:160
uint8 & operator[](IntegerT channel)
Array-style channel access.
Definition: ColorRgba8.hpp:114
ColorRgba8(uint8 r_, uint8 g_, uint8 b_, uint8 a_)
Construct from red, green, blue and alpha components.
Definition: ColorRgba8.hpp:59
A color with three byte-sized channels: red, green and blue, each in [0, 255].
Definition: ColorRgb8.hpp:47
A color with four byte-sized channels: red, green, blue and alpha, each in [0, 255].
Definition: ColorRgba8.hpp:47
uint8 & r()
A reference to the red channel.
Definition: ColorRgba8.hpp:77
uint8 * data()
Get the address of the array storing color channel values in RGBA order.
Definition: ColorRgba8.hpp:104
uint8 b() const
The value of the blue channel.
Definition: ColorRgb8.hpp:89
uint8 b() const
The value of the blue channel.
Definition: ColorRgba8.hpp:86
ColorRgba8 operator*(Real s) const
Multiplication by a scalar.
Definition: ColorRgba8.hpp:151
uint8 value_type
The numerical type of each channel.
Definition: ColorRgba8.hpp:53
ColorRgba8 & operator+=(ColorRgba8 const &rhs)
Add and assign.
Definition: ColorRgba8.hpp:169
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
ColorRgba8 operator+(ColorRgba8 const &rhs) const
Addition.
Definition: ColorRgba8.hpp:133
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
uint8 & a()
A reference to the alpha channel.
Definition: ColorRgba8.hpp:95
uint8 const * data() const
Get the address of the array storing color channel values in RGBA order.
Definition: ColorRgba8.hpp:101
Monochrome luminance value in [0, 1], with automatic scaling by 255 when switching between integer (C...
Definition: ColorL.hpp:46
ColorRgba8(uint8 const *v)
Construct a color from four components in an array.
Definition: ColorRgba8.hpp:65
uint8 & g()
A reference to the green channel.
Definition: ColorRgba8.hpp:83
#define THEA_BEGIN_PACKED_CLASS(byte_align)
Use tight alignment for a class.
Definition: Platform.hpp:133
ColorRgb8 rgb() const
Get the red, green and blue channels as a ColorRgb8.
Definition: ColorRgba8.hpp:98
Real value() const
The value of the color.
Definition: ColorL.hpp:67
ColorRgba8 bgra() const
Swap the red and blue channels.
Definition: ColorRgba8.hpp:197
uint8 g() const
The value of the green channel.
Definition: ColorRgba8.hpp:80
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
uint8 const & operator[](IntegerT channel) const
Array-style channel access.
Definition: ColorRgba8.hpp:107
ColorRgba8 & operator*=(Real s)
Multiply by a scalar and assign.
Definition: ColorRgba8.hpp:183
uint8 a() const
The value of the alpha channel.
Definition: ColorRgba8.hpp:92
ColorRgba8(ColorRgb8 const &rgb_, uint8 a_=255)
Construct from an RGB color and an alpha component.
Definition: ColorRgba8.hpp:62
void fill(uint8 v)
Set all channels to the same value.
Definition: ColorRgba8.hpp:130
ColorRgba8 & operator-=(ColorRgba8 const &rhs)
Subtract and assign.
Definition: ColorRgba8.hpp:176
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