Thea
ColorRgba.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 Color4.h
19 
20  Color class
21 
22  @maintainer Morgan McGuire, http://graphics.cs.williams.edu
23  @cite Portions based on Dave Eberly's Magic Software Library
24  at <A HREF="http://www.magic-software.com">http://www.magic-software.com</A>
25 
26  @created 2002-06-25
27  @edited 2009-11-15
28 
29  Copyright 2000-2009, Morgan McGuire.
30  All rights reserved.
31 */
32 
33 #ifndef __Thea_ColorRgba_hpp__
34 #define __Thea_ColorRgba_hpp__
35 
36 #include "Common.hpp"
37 #include "ColorL.hpp"
38 #include "ColorRgb.hpp"
39 #include "Math.hpp"
40 #include "MatVec.hpp"
41 
42 namespace Thea {
43 
44 // Forward declarations
45 class ColorRgb8;
46 class ColorRgba8;
47 
52 class THEA_API ColorRgba
53 {
54  private:
55  Real c[4];
56 
57  public:
58  typedef Real value_type;
59 
61  ColorRgba() {}
62 
64  ColorRgba(Real r_, Real g_, Real b_, Real a_ = 1) { c[0] = r_; c[1] = g_; c[2] = b_; c[3] = a_; }
65 
67  ColorRgba(ColorRgb const & rgb_, Real a_ = 1) { c[0] = rgb_.r(); c[1] = rgb_.g(); c[2] = rgb_.b(); c[3] = a_; }
68 
70  explicit ColorRgba(Vector4 const & v) { c[0] = v[0]; c[1] = v[1]; c[2] = v[2]; c[3] = v[3]; }
71 
73  explicit ColorRgba(Real const * v) { c[0] = v[0]; c[1] = v[1]; c[2] = v[2]; c[3] = v[3]; }
74 
76  ColorRgba(ColorRgba8 const & src);
77 
79  ColorRgba(ColorRgb8 const & src);
80 
82  static ColorRgba fromARGB(uint32 argb);
83 
85  Real r() const { return c[0]; }
86 
88  Real & r() { return c[0]; }
89 
91  Real g() const { return c[1]; }
92 
94  Real & g() { return c[1]; }
95 
97  Real b() const { return c[2]; }
98 
100  Real & b() { return c[2]; }
101 
103  Real a() const { return c[3]; }
104 
106  Real & a() { return c[3]; }
107 
109  ColorRgb rgb() const { return ColorRgb(c[0], c[1], c[2]); }
110 
112  Real const * data() const { return c; }
113 
115  Real * data() { return c; }
116 
118  template <typename IntegerT> Real const & operator[](IntegerT channel) const
119  {
120  debugAssertM(channel >= 0 && channel <= 3, "ColorRgba: Channel must be 0, 1, 2 or 3");
121  return c[channel];
122  }
123 
125  template <typename IntegerT> Real & operator[](IntegerT channel)
126  {
127  debugAssertM(channel >= 0 && channel <= 3, "ColorRgba: Channel must be 0, 1, 2 or 3");
128  return c[channel];
129  }
130 
132  void set(Real r_, Real g_, Real b_, Real a_)
133  {
134  c[0] = r_;
135  c[1] = g_;
136  c[2] = b_;
137  c[3] = a_;
138  }
139 
141  void fill(Real v) { c[0] = c[1] = c[2] = c[3] = v; }
142 
144  ColorRgba operator+(ColorRgba const & rhs) const
145  {
146  return ColorRgba(c[0] + rhs.c[0], c[1] + rhs.c[1], c[2] + rhs.c[2], c[3] + rhs.c[3]);
147  }
148 
150  ColorRgba operator-(ColorRgba const & rhs) const
151  {
152  return ColorRgba(c[0] - rhs.c[0], c[1] - rhs.c[1], c[2] - rhs.c[2], c[3] - rhs.c[3]);
153  }
154 
156  ColorRgba operator*(Real s) const
157  {
158  return ColorRgba(c[0] * s, c[1] * s, c[2] * s, c[3] * s);
159  }
160 
162  ColorRgba operator*(ColorRgba const & rhs) const
163  {
164  return ColorRgba(c[0] * rhs.c[0], c[1] * rhs.c[1], c[2] * rhs.c[2], c[3] * rhs.c[3]);
165  }
166 
168  ColorRgba operator/(Real s) const
169  {
170  return ColorRgba(c[0] / s, c[1] / s, c[2] / s, c[3] / s);
171  }
172 
174  ColorRgba operator/(ColorRgba const & rhs) const
175  {
176  return ColorRgba(c[0] / rhs.c[0], c[1] / rhs.c[1], c[2] / rhs.c[2], c[3] / rhs.c[3]);
177  }
178 
181  {
182  return ColorRgba(-c[0], -c[1], -c[2], -c[3]);
183  }
184 
187  {
188  c[0] += rhs.c[0];
189  c[1] += rhs.c[1];
190  c[2] += rhs.c[2];
191  c[3] += rhs.c[3];
192  return *this;
193  }
194 
197  {
198  c[0] -= rhs.c[0];
199  c[1] -= rhs.c[1];
200  c[2] -= rhs.c[2];
201  c[3] -= rhs.c[3];
202  return *this;
203  }
204 
207  {
208  c[0] *= rhs.c[0];
209  c[1] *= rhs.c[1];
210  c[2] *= rhs.c[2];
211  c[3] *= rhs.c[3];
212  return *this;
213  }
214 
217  {
218  c[0] /= rhs.c[0];
219  c[1] /= rhs.c[1];
220  c[2] /= rhs.c[2];
221  c[3] /= rhs.c[3];
222  return *this;
223  }
224 
227  {
228  c[0] *= s;
229  c[1] *= s;
230  c[2] *= s;
231  c[3] *= s;
232  return *this;
233  }
234 
237  {
238  c[0] /= s;
239  c[1] /= s;
240  c[2] /= s;
241  c[3] /= s;
242  return *this;
243  }
244 
246  bool fuzzyEq(ColorRgba const & other) const
247  {
248  return Math::fuzzyEq((*this - other).squaredNorm(), (Real)0);
249  }
250 
252  bool fuzzyNe(ColorRgba const & other) const
253  {
254  return Math::fuzzyNe((*this - other).squaredNorm(), (Real)0);
255  }
256 
258  ColorRgba bgra() const
259  {
260  return ColorRgba(c[2], c[1], c[0], c[3]);
261  }
262 
264  std::string toString() const;
265 
266  // Named colors
267  static ColorRgba const & red();
268  static ColorRgba const & green();
269  static ColorRgba const & blue();
270  static ColorRgba const & purple();
271  static ColorRgba const & cyan();
272  static ColorRgba const & yellow();
273  static ColorRgba const & brown();
274  static ColorRgba const & orange();
275  static ColorRgba const & black();
276  static ColorRgba const & gray();
277  static ColorRgba const & white();
278  static ColorRgba const & zero();
279 
280  private:
282  Real squaredNorm() const { return c[0] * c[0] + c[1] * c[1] + c[2] * c[2] + c[3] * c[3]; }
283 
284 }; // class ColorRgba
285 
287 inline ColorRgba
288 operator*(Real s, ColorRgba const & c)
289 {
290  return c * s;
291 }
292 
294 inline ColorRgba
295 operator*(ColorL & s, ColorRgba const & c)
296 {
297  return c * s.value();
298 }
299 
301 inline ColorRgba
302 operator*(ColorRgba const & c, ColorL & s)
303 {
304  return c * s.value();
305 }
306 
307 } // namespace Thea
308 
309 #endif
ColorRgba(Real r_, Real g_, Real b_, Real a_=1)
Construct from red, green, blue and alpha components.
Definition: ColorRgba.hpp:64
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
ColorRgba bgra() const
Swap the red and blue channels.
Definition: ColorRgba.hpp:258
Real g() const
The value of the green channel.
Definition: ColorRgb.hpp:111
Real b() const
The value of the blue channel.
Definition: ColorRgba.hpp:97
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...
ColorRgba & operator*=(ColorRgba const &rhs)
Multiply component-wise and assign.
Definition: ColorRgba.hpp:206
Root namespace for the Thea library.
Real * data()
Get the address of the array storing color channel values in RGBA order.
Definition: ColorRgba.hpp:115
ColorRgba(ColorRgb const &rgb_, Real a_=1)
Construct from an RGB color and an alpha component.
Definition: ColorRgba.hpp:67
Real & r()
A reference to the red channel.
Definition: ColorRgba.hpp:88
ColorRgba & operator+=(ColorRgba const &rhs)
Add and assign.
Definition: ColorRgba.hpp:186
Real g() const
The value of the green channel.
Definition: ColorRgba.hpp:91
ColorRgba & operator/=(Real s)
Divide by a scalar and assign.
Definition: ColorRgba.hpp:236
A color with three byte-sized channels: red, green and blue, each in [0, 255].
Definition: ColorRgb8.hpp:47
ColorRgb rgb() const
Get the red, green and blue channels as a ColorRgb.
Definition: ColorRgba.hpp:109
ColorRgba(Real const *v)
Construct a color from four components in an array.
Definition: ColorRgba.hpp:73
Real const & operator[](IntegerT channel) const
Array-style channel access.
Definition: ColorRgba.hpp:118
A color with four byte-sized channels: red, green, blue and alpha, each in [0, 255].
Definition: ColorRgba8.hpp:47
ColorRgba operator+(ColorRgba const &rhs) const
Addition.
Definition: ColorRgba.hpp:144
Real b() const
The value of the blue channel.
Definition: ColorRgb.hpp:117
ColorRgba operator-() const
Negation of the color.
Definition: ColorRgba.hpp:180
Real a() const
The value of the alpha channel.
Definition: ColorRgba.hpp:103
ColorRgba & operator/=(ColorRgba const &rhs)
Divide component-wise and assign.
Definition: ColorRgba.hpp:216
ColorRgba()
Default constructor.
Definition: ColorRgba.hpp:61
bool fuzzyEq(T const &a, T const &b, T const &tol)
Check if two numbers are approximately equal, with a given tolerance.
Definition: Math.hpp:210
ColorRgba operator/(ColorRgba const &rhs) const
Component-wise division by another color.
Definition: ColorRgba.hpp:174
Real & a()
A reference to the alpha channel.
Definition: ColorRgba.hpp:106
ColorRgba operator/(Real s) const
Division by a scalar.
Definition: ColorRgba.hpp:168
ColorRgba & operator-=(ColorRgba const &rhs)
Subtract and assign.
Definition: ColorRgba.hpp:196
Monochrome luminance value in [0, 1], with automatic scaling by 255 when switching between integer (C...
Definition: ColorL.hpp:46
Real r() const
The value of the red channel.
Definition: ColorRgba.hpp:85
Real & g()
A reference to the green channel.
Definition: ColorRgba.hpp:94
Real value() const
The value of the color.
Definition: ColorL.hpp:67
void fill(Real v)
Set all channels to the same value.
Definition: ColorRgba.hpp:141
bool fuzzyNe(T const &a, T const &b, T const &tol)
Check if two numbers are not approximately equal, with a given tolerance.
Definition: Math.hpp:216
bool fuzzyNe(ColorRgba const &other) const
Check if two colors are not approximately equal.
Definition: ColorRgba.hpp:252
Real const * data() const
Get the address of the array storing color channel values in RGBA order.
Definition: ColorRgba.hpp:112
Real & b()
A reference to the blue channel.
Definition: ColorRgba.hpp:100
A color with four floating-point channels: red, green and blue, each in [0, 1].
Definition: ColorRgba.hpp:52
ColorRgba & operator*=(Real s)
Multiply by a scalar and assign.
Definition: ColorRgba.hpp:226
ColorRgba operator-(ColorRgba const &rhs) const
Subtraction.
Definition: ColorRgba.hpp:150
ColorRgba operator*(Real s) const
Multiplication by a scalar.
Definition: ColorRgba.hpp:156
bool fuzzyEq(ColorRgba const &other) const
Check if two colors are approximately equal.
Definition: ColorRgba.hpp:246
Real value_type
The numerical type of each channel.
Definition: ColorRgba.hpp:58
ColorRgba operator*(ColorRgba const &rhs) const
Component-wise multiplication by another color.
Definition: ColorRgba.hpp:162
Real & operator[](IntegerT channel)
Array-style channel access.
Definition: ColorRgba.hpp:125
Real r() const
The value of the red channel.
Definition: ColorRgb.hpp:105
ColorRgba(Vector4 const &v)
Construct a color from a 4-vector.
Definition: ColorRgba.hpp:70
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