Imagine++
Color.h
1 // ===========================================================================
2 // Imagine++ Libraries
3 // Copyright (C) Imagine
4 // For detailed information: http://imagine.enpc.fr/software
5 // ===========================================================================
6 
7 
8 namespace Imagine {
11 
18  typedef unsigned char octet;
21  typedef unsigned char byte;
22 
27  template <typename T>
28  class RGB : public FVector<T,3> {
29  private:
30  typedef FVector<T,3> Base;
31  public:
37  RGB() : Base() {}
44  explicit RGB(T v) : Base(v) {}
51  RGB(T r,T g,T b) : Base(r,g,b) {}
59  explicit RGB(const T t[3]): Base(t) {}
66  RGB(const Base& x) : Base(x) {}
74  template <typename T2> RGB(const FVector<T2,3>& x) : Base(x) {}
83  template <typename T2> RGB(const FArray<T2,4>& x) : Base(T(x[0]), T(x[1]), T(x[2])) {}
90  const T& r() const { return (*this)[0]; }
97  T& r() { return (*this)[0]; }
104  const T& g() const { return (*this)[1]; }
111  T& g() { return (*this)[1]; }
118  const T& b() const { return (*this)[2]; }
125  T& b() { return (*this)[2]; }
132  operator T () const { return T(0.3*r()+0.59*g()+0.11*b()); }
133  };
134 
139  template <typename T>
140  class RGBA : public FVector<T,4> {
141  private:
142  typedef FVector<T,4> Base;
143  public:
149  RGBA() : Base() {}
156  explicit RGBA(T x) : Base(x) { (*this)[3]=T(1); }
163  RGBA(T r,T g,T b,T a){
164  (*this)[0]=r;(*this)[1]=g;(*this)[2]=b;(*this)[3]=a;
165  }
173  RGBA(T r,T g, T b){
174  (*this)[0]=r;(*this)[1]=g;(*this)[2]=b;(*this)[3]=T(255);
175  }
176 
184  RGBA(const T t[4]): Base(t) {}
191  RGBA(const Base& x) : Base(x) {}
199  template <typename T2> RGBA(const FArray<T2,4>& x) : Base(x) {}
207  template <typename T2> RGBA(const FArray<T2,3>& x){
208  (*this)[0]=T(x[0]);
209  (*this)[1]=T(x[1]);
210  (*this)[2]=T(x[2]);
211  (*this)[3]=T(255);
212  }
219  const T& r() const { return (*this)[0]; }
226  T& r() { return (*this)[0]; }
233  const T& g() const { return (*this)[1]; }
240  T& g() { return (*this)[1]; }
247  const T& b() const { return (*this)[2]; }
254  T& b() { return (*this)[2]; }
261  const T& a() const { return (*this)[3]; }
268  T& a() { return (*this)[3]; }
275  operator T () const { return T(0.3*r()+0.59*g()+0.11*b()); }
276  };
283  typedef RGB<octet> Color;
289  const Color WHITE(255,255,255);
295  const Color BLACK(0,0,0);
301  const Color BLUE(0,0,255);
307  const Color RED(255,0,0);
313  const Color GREEN(0,255,0);
319  const Color YELLOW(255,255,0);
325  const Color CYAN(0,255,255);
331  const Color MAGENTA(255,0,255);
337  const Color ORANGE(255,128,0);
343  const Color PURPLE(128,0,255);
344 
346  inline std::ostream& operator<<(std::ostream& out, const Color& c) {
347  return out << (int)c[0] << ' ' << (int)c[1] << ' ' << (int)c[2];
348  }
362  const AlphaColor AWHITE=AlphaColor(255,255,255);
368  const AlphaColor ABLACK=AlphaColor(0,0,0);
374  const AlphaColor ABLUE=AlphaColor(0,0,255);
380  const AlphaColor ARED=AlphaColor(255,0,0);
386  const AlphaColor AGREEN=AlphaColor(0,255,0);
392  const AlphaColor AYELLOW=AlphaColor(255,255,0);
398  const AlphaColor ACYAN=AlphaColor(0,255,255);
404  const AlphaColor AMAGENTA=AlphaColor(255,0,255);
405 
413  template<typename T> inline FVector<double,3> RGB2YUV(const RGB<T>& rgb) {
414  double y=.299*double(rgb[0])+.587*double(rgb[1])+.114*double(rgb[2]);
415  return FVector<double,3>(y,.492*(double(rgb[2])-y),.877*(double(rgb[0])-y));
416  }
424  template<typename T> inline RGB<T> YUV2RGB(const FVector<double,3>& yuv) {
425  double r(yuv[2]/.877+yuv[0]);
426  double b(yuv[1]/.492+yuv[0]);
427  double g((yuv[0]-.299*r-.114*b)/.587);
428  return RGB<T>(T(r),T(g),T(b));
429  }
430 
431 
433 }
RGB(const T t[3])
Constructor from C array.
Definition: Color.h:59
Vector of fixed size.
Definition: FVector.h:17
RGBA(T r, T g, T b)
Constructor with r,g,b values Constructs a RGBA of type T.
Definition: Color.h:173
const Color BLUE(0, 0, 255)
Predefined color.
RGBA< octet > AlphaColor
RGBA alias.
Definition: Color.h:356
RGBA(const FArray< T2, 3 > &x)
Constructor from RGB.
Definition: Color.h:207
const T & b() const
Read BLUE alias.
Definition: Color.h:118
const T & b() const
Read BLUE alias.
Definition: Color.h:247
RGB< octet > Color
RGB alias.
Definition: Color.h:283
RGB(const FArray< T2, 4 > &x)
Constructor from RGBA.
Definition: Color.h:83
const Color MAGENTA(255, 0, 255)
Predefined color.
const AlphaColor AYELLOW
Predefined color.
Definition: Color.h:392
const T & g() const
Read GREEN alias.
Definition: Color.h:104
const T & r() const
Read RED alias.
Definition: Color.h:219
RGB(T r, T g, T b)
Constructor with r,g,b values Constructs a RGB of type T.
Definition: Color.h:51
RGBA(T r, T g, T b, T a)
Constructor with r,g,b,a values Constructs a RGBA of type T.
Definition: Color.h:163
const T & r() const
Read RED alias.
Definition: Color.h:90
const AlphaColor AWHITE
Predefined color.
Definition: Color.h:362
RGB< T > YUV2RGB(const FVector< double, 3 > &yuv)
YUV to RGB.
Definition: Color.h:424
RGBA(const T t[4])
Constructor from C array.
Definition: Color.h:184
T & b()
Write BLUE alias.
Definition: Color.h:254
RED GREEN BLUE Alpha color.
Definition: Color.h:140
RGB(const FVector< T2, 3 > &x)
Constructor from other value type Constructs from RGB with different type.
Definition: Color.h:74
T & r()
Write RED alias.
Definition: Color.h:97
T & g()
Write GREEN alias.
Definition: Color.h:111
const AlphaColor ABLUE
Predefined color.
Definition: Color.h:374
RGBA(T x)
Constructor with constant value.
Definition: Color.h:156
RGBA(const FArray< T2, 4 > &x)
Constructor from other value type Constructs from RGBA with different type.
Definition: Color.h:199
Array of fixed size.
Definition: FArray.h:17
const Color GREEN(0, 255, 0)
Predefined color.
const AlphaColor ABLACK
Predefined color.
Definition: Color.h:368
const Color YELLOW(255, 255, 0)
Predefined color.
const AlphaColor ARED
Predefined color.
Definition: Color.h:380
T & a()
Write alpha alias.
Definition: Color.h:268
T & b()
Write BLUE alias.
Definition: Color.h:125
unsigned char octet
0 to 255 integer type.
Definition: Color.h:18
RGBA()
Empty constructor.
Definition: Color.h:149
const AlphaColor ACYAN
Predefined color.
Definition: Color.h:398
T & r()
Write RED alias.
Definition: Color.h:226
RGB()
Empty constructor.
Definition: Color.h:37
const Color CYAN(0, 255, 255)
Predefined color.
RGBA(const Base &x)
Copy constructor.
Definition: Color.h:191
const T & g() const
Read GREEN alias.
Definition: Color.h:233
const Color RED(255, 0, 0)
Predefined color.
const Color WHITE(255, 255, 255)
Predefined color.
const AlphaColor AGREEN
Predefined color.
Definition: Color.h:386
const AlphaColor AMAGENTA
Predefined color.
Definition: Color.h:404
unsigned char byte
Definition: Color.h:21
RGB(const Base &x)
Copy constructor.
Definition: Color.h:66
std::ostream & operator<<(std::ostream &out, const Color &c)
Display color as three integral values.
Definition: Color.h:346
const T & x() const
Read alias.
Definition: FVector.h:104
const Color PURPLE(128, 0, 255)
Predefined color.
Imagine++ namespace.
Definition: Array.h:7
T & g()
Write GREEN alias.
Definition: Color.h:240
RED GREEN BLUE color.
Definition: Color.h:28
const T & a() const
Read alpha alias.
Definition: Color.h:261
const Color BLACK(0, 0, 0)
Predefined color.
const Color ORANGE(255, 128, 0)
Predefined color.
FVector< double, 3 > RGB2YUV(const RGB< T > &rgb)
RGB to YUV.
Definition: Color.h:413
RGB(T v)
Constructor with constant value.
Definition: Color.h:44