Thea
ColorRgb.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 ColorRgb.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 2001-06-02
27  @edited 2009-04-28
28 
29  Copyright 2000-2009, Morgan McGuire.
30  All rights reserved.
31 */
32 
33 #ifndef __Thea_ColorRgb_hpp__
34 #define __Thea_ColorRgb_hpp__
35 
36 #include "Common.hpp"
37 #include "ColorL.hpp"
38 #include "Math.hpp"
39 #include "MatVec.hpp"
40 
41 namespace Thea {
42 
43 // Forward declarations
44 class ColorRgb8;
45 class ColorRgba;
46 
51 class THEA_API ColorRgb
52 {
53  private:
54  Real c[3];
55 
56  public:
57  typedef Real value_type;
58 
60  ColorRgb() {}
61 
63  ColorRgb(Real r_, Real g_, Real b_) { c[0] = r_; c[1] = g_; c[2] = b_; }
64 
66  explicit ColorRgb(Real v) { c[0] = c[1] = c[2] = v; }
67 
69  explicit ColorRgb(Vector3 const & v) { c[0] = v[0]; c[1] = v[1]; c[2] = v[2]; }
70 
72  explicit ColorRgb(Real const * v) { c[0] = v[0]; c[1] = v[1]; c[2] = v[2]; }
73 
75  ColorRgb(ColorRgba const & other);
76 
78  ColorRgb(ColorRgb8 const & src);
79 
81  static ColorRgb fromARGB(uint32 argb);
82 
84  static ColorRgb random();
85 
87  static ColorRgb const & wheelRandom();
88 
94  static ColorRgb ansiMap(int i);
95 
102  static ColorRgb pastelMap(int i);
103 
105  Real r() const { return c[0]; }
106 
108  Real & r() { return c[0]; }
109 
111  Real g() const { return c[1]; }
112 
114  Real & g() { return c[1]; }
115 
117  Real b() const { return c[2]; }
118 
120  Real & b() { return c[2]; }
121 
123  Real const * data() const { return c; }
124 
126  Real * data() { return c; }
127 
129  template <typename IntegerT> Real const & operator[](IntegerT channel) const
130  {
131  debugAssertM(channel >= 0 && channel <= 2, "ColorRgb: Channel must be 0, 1 or 2");
132  return c[channel];
133  }
134 
136  template <typename IntegerT> Real & operator[](IntegerT channel)
137  {
138  debugAssertM(channel >= 0 && channel <= 2, "ColorRgb: Channel must be 0, 1 or 2");
139  return c[channel];
140  }
141 
143  void set(Real r_, Real g_, Real b_)
144  {
145  c[0] = r_;
146  c[1] = g_;
147  c[2] = b_;
148  }
149 
151  void fill(Real v) { c[0] = c[1] = c[2] = v; }
152 
154  ColorRgb operator+(ColorRgb const & rhs) const
155  {
156  return ColorRgb(c[0] + rhs.c[0], c[1] + rhs.c[1], c[2] + rhs.c[2]);
157  }
158 
160  ColorRgb operator-(ColorRgb const & rhs) const
161  {
162  return ColorRgb(c[0] - rhs.c[0], c[1] - rhs.c[1], c[2] - rhs.c[2]);
163  }
164 
166  ColorRgb operator*(Real s) const
167  {
168  return ColorRgb(c[0] * s, c[1] * s, c[2] * s);
169  }
170 
172  ColorRgb operator*(ColorRgb const & rhs) const
173  {
174  return ColorRgb(c[0] * rhs.c[0], c[1] * rhs.c[1], c[2] * rhs.c[2]);
175  }
176 
178  ColorRgb operator/(Real s) const
179  {
180  return ColorRgb(c[0] / s, c[1] / s, c[2] / s);
181  }
182 
184  ColorRgb operator/(ColorRgb const & rhs) const
185  {
186  return ColorRgb(c[0] / rhs.c[0], c[1] / rhs.c[1], c[2] / rhs.c[2]);
187  }
188 
191  {
192  return ColorRgb(-c[0], -c[1], -c[2]);
193  }
194 
196  ColorRgb & operator+=(ColorRgb const & rhs)
197  {
198  c[0] += rhs.c[0];
199  c[1] += rhs.c[1];
200  c[2] += rhs.c[2];
201  return *this;
202  }
203 
205  ColorRgb & operator-=(ColorRgb const & rhs)
206  {
207  c[0] -= rhs.c[0];
208  c[1] -= rhs.c[1];
209  c[2] -= rhs.c[2];
210  return *this;
211  }
212 
214  ColorRgb & operator*=(ColorRgb const & rhs)
215  {
216  c[0] *= rhs.c[0];
217  c[1] *= rhs.c[1];
218  c[2] *= rhs.c[2];
219  return *this;
220  }
221 
223  ColorRgb & operator/=(ColorRgb const & rhs)
224  {
225  c[0] /= rhs.c[0];
226  c[1] /= rhs.c[1];
227  c[2] /= rhs.c[2];
228  return *this;
229  }
230 
233  {
234  c[0] *= s;
235  c[1] *= s;
236  c[2] *= s;
237  return *this;
238  }
239 
242  {
243  c[0] /= s;
244  c[1] /= s;
245  c[2] /= s;
246  return *this;
247  }
248 
250  Real squaredNorm() const { return c[0] * c[0] + c[1] * c[1] + c[2] * c[2]; }
251 
253  Real norm() const { return std::sqrt(squaredNorm()); }
254 
256  void normalize()
257  {
258  *this = normalized();
259  }
260 
263  {
264  Real len = norm();
265  if (std::abs(len) < 32 * std::numeric_limits<Real>::min())
266  return ColorRgb(0, 0, 0);
267  else
268  return *this / len;
269  }
270 
272  bool fuzzyEq(ColorRgb const & other) const
273  {
274  return Math::fuzzyEq((*this - other).squaredNorm(), (Real)0);
275  }
276 
278  bool fuzzyNe(ColorRgb const & other) const
279  {
280  return Math::fuzzyNe((*this - other).squaredNorm(), (Real)0);
281  }
282 
284  ColorRgb pow(ColorRgb const & rhs) const
285  {
286  return ColorRgb(std::pow(c[0], rhs.c[0]), std::pow(c[1], rhs.c[1]), std::pow(c[2], rhs.c[2]));
287  }
288 
290  ColorRgb pow(Real s) const
291  {
292  return ColorRgb(std::pow(c[0], s), std::pow(c[1], s), std::pow(c[2], s));
293  }
294 
296  ColorRgb max(ColorRgb const & rhs) const
297  {
298  return ColorRgb(std::max(c[0], rhs.c[0]), std::max(c[1], rhs.c[1]), std::max(c[2], rhs.c[2]));
299  }
300 
302  ColorRgb min(ColorRgb const & rhs) const
303  {
304  return ColorRgb(std::min(c[0], rhs.c[0]), std::min(c[1], rhs.c[1]), std::min(c[2], rhs.c[2]));
305  }
306 
308  Real max() const
309  {
310  return std::max(std::max(c[0], c[1]), c[2]);
311  }
312 
314  Real min() const
315  {
316  return std::min(std::min(c[0], c[1]), c[2]);
317  }
318 
320  ColorRgb bgr() const
321  {
322  return ColorRgb(c[2], c[1], c[0]);
323  }
324 
326  std::string toString() const;
327 
336  static ColorRgb fromHSV(Vector3 const & hsv);
337 
343  Vector3 toHSV() const;
344 
346  static ColorRgb jetColorMap(Real val);
347 
353  static ColorRgb rainbowColorMap(Real hue);
354 
355  // Named colors
356  static ColorRgb const & red();
357  static ColorRgb const & green();
358  static ColorRgb const & blue();
359  static ColorRgb const & purple();
360  static ColorRgb const & cyan();
361  static ColorRgb const & yellow();
362  static ColorRgb const & brown();
363  static ColorRgb const & orange();
364  static ColorRgb const & black();
365  static ColorRgb const & gray();
366  static ColorRgb const & white();
367  static ColorRgb const & zero();
368 
369 }; // class ColorRgb
370 
372 inline ColorRgb
373 operator*(Real s, ColorRgb const & c)
374 {
375  return c * s;
376 }
377 
379 inline ColorRgb
380 operator*(ColorL & s, ColorRgb const & c)
381 {
382  return c * s.value();
383 }
384 
386 inline ColorRgb
387 operator*(ColorRgb const & c, ColorL & s)
388 {
389  return c * s.value();
390 }
391 
392 } // namespace Thea
393 
394 #endif
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
Real g() const
The value of the green channel.
Definition: ColorRgb.hpp:111
ColorRgb operator/(ColorRgb const &rhs) const
Component-wise division by another color.
Definition: ColorRgb.hpp:184
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...
Real const & operator[](IntegerT channel) const
Array-style channel access.
Definition: ColorRgb.hpp:129
ColorRgb normalized() const
Get a unit magnitude color by dividing by the magnitude.
Definition: ColorRgb.hpp:262
Root namespace for the Thea library.
ColorRgb(Vector3 const &v)
Construct a color from a 3-vector.
Definition: ColorRgb.hpp:69
ColorRgb min(ColorRgb const &rhs) const
Get the per-component minimum of two colors.
Definition: ColorRgb.hpp:302
ColorRgb operator+(ColorRgb const &rhs) const
Addition.
Definition: ColorRgb.hpp:154
ColorRgb max(ColorRgb const &rhs) const
Get the per-component maximum of two colors.
Definition: ColorRgb.hpp:296
Real & r()
A reference to the red channel.
Definition: ColorRgb.hpp:108
Real squaredNorm() const
Get the square of the magnitude of the color.
Definition: ColorRgb.hpp:250
ColorRgb & operator-=(ColorRgb const &rhs)
Subtract and assign.
Definition: ColorRgb.hpp:205
ColorRgb & operator+=(ColorRgb const &rhs)
Add and assign.
Definition: ColorRgb.hpp:196
bool fuzzyNe(ColorRgb const &other) const
Check if two colors are not approximately equal.
Definition: ColorRgb.hpp:278
A color with three byte-sized channels: red, green and blue, each in [0, 255].
Definition: ColorRgb8.hpp:47
Real b() const
The value of the blue channel.
Definition: ColorRgb.hpp:117
ColorRgb & operator/=(Real s)
Divide by a scalar and assign.
Definition: ColorRgb.hpp:241
ColorRgb pow(ColorRgb const &rhs) const
Raise the components to powers specified as another color.
Definition: ColorRgb.hpp:284
ColorRgb()
Default constructor.
Definition: ColorRgb.hpp:60
Real value_type
The numerical type of each channel.
Definition: ColorRgb.hpp:57
ColorRgb(Real v)
Initialize all channels to the same value.
Definition: ColorRgb.hpp:66
bool fuzzyEq(ColorRgb const &other) const
Check if two colors are approximately equal.
Definition: ColorRgb.hpp:272
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
ColorRgb(Real const *v)
Construct a color from three components in an array.
Definition: ColorRgb.hpp:72
ColorRgb(Real r_, Real g_, Real b_)
Construct from red, green and blue components.
Definition: ColorRgb.hpp:63
ColorRgb pow(Real s) const
Raise the components to a power.
Definition: ColorRgb.hpp:290
Monochrome luminance value in [0, 1], with automatic scaling by 255 when switching between integer (C...
Definition: ColorL.hpp:46
Real const * data() const
Get the address of the array storing color channel values in RGB order.
Definition: ColorRgb.hpp:123
ColorRgb bgr() const
Swap the red and blue channels.
Definition: ColorRgb.hpp:320
ColorRgb & operator*=(Real s)
Multiply by a scalar and assign.
Definition: ColorRgb.hpp:232
Real min() const
Get the smallest component.
Definition: ColorRgb.hpp:314
ColorRgb operator/(Real s) const
Division by a scalar.
Definition: ColorRgb.hpp:178
Real value() const
The value of the color.
Definition: ColorL.hpp:67
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
ColorRgb operator*(ColorRgb const &rhs) const
Component-wise multiplication by another color.
Definition: ColorRgb.hpp:172
A color with four floating-point channels: red, green and blue, each in [0, 1].
Definition: ColorRgba.hpp:52
Real * data()
Get the address of the array storing color channel values in RGB order.
Definition: ColorRgb.hpp:126
ColorRgb & operator/=(ColorRgb const &rhs)
Divide component-wise and assign.
Definition: ColorRgb.hpp:223
ColorRgb & operator*=(ColorRgb const &rhs)
Multiply component-wise and assign.
Definition: ColorRgb.hpp:214
ColorRgb operator-(ColorRgb const &rhs) const
Subtraction.
Definition: ColorRgb.hpp:160
Real norm() const
Get the magnitude of the color.
Definition: ColorRgb.hpp:253
Real & g()
A reference to the green channel.
Definition: ColorRgb.hpp:114
Real max() const
Get the largest component.
Definition: ColorRgb.hpp:308
ColorRgb operator*(Real s) const
Multiplication by a scalar.
Definition: ColorRgb.hpp:166
Real & b()
A reference to the blue channel.
Definition: ColorRgb.hpp:120
void fill(Real v)
Set all channels to the same value.
Definition: ColorRgb.hpp:151
Real & operator[](IntegerT channel)
Array-style channel access.
Definition: ColorRgb.hpp:136
Real r() const
The value of the red channel.
Definition: ColorRgb.hpp:105
ColorRgb operator-() const
Negation of the color.
Definition: ColorRgb.hpp:190
void normalize()
Scale the color to unit magnitude.
Definition: ColorRgb.hpp:256
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