Imagine++
Color.h
1// ===========================================================================
2// Imagine++ Libraries
3// Copyright (C) Imagine
4// For detailed information: http://imagine.enpc.fr/software
5// ===========================================================================
6
14 typedef unsigned char byte;
15
16namespace Imagine {
19
20
25 template <typename T>
26 class RGB : public FVector<T,3> {
27 private:
28 typedef FVector<T,3> Base;
29 public:
35 RGB() : Base() {}
42 explicit RGB(T v) : Base(v) {}
49 RGB(T r,T g,T b) : Base(r,g,b) {}
57 explicit RGB(const T t[3]): Base(t) {}
64 RGB(const Base& x) : Base(x) {}
72 template <typename T2> RGB(const FVector<T2,3>& x) : Base(x) {}
81 template <typename T2> RGB(const FArray<T2,4>& x) : Base(T(x[0]), T(x[1]), T(x[2])) {}
88 const T& r() const { return (*this)[0]; }
95 T& r() { return (*this)[0]; }
102 const T& g() const { return (*this)[1]; }
109 T& g() { return (*this)[1]; }
116 const T& b() const { return (*this)[2]; }
123 T& b() { return (*this)[2]; }
130 operator T () const { return T(0.3*r()+0.59*g()+0.11*b()); }
131 };
132
137 template <typename T>
138 class RGBA : public FVector<T,4> {
139 private:
140 typedef FVector<T,4> Base;
141 public:
147 RGBA() : Base() {}
154 explicit RGBA(T x) : Base(x) { (*this)[3]=T(1); }
161 RGBA(T r,T g,T b,T a){
162 (*this)[0]=r;(*this)[1]=g;(*this)[2]=b;(*this)[3]=a;
163 }
171 RGBA(T r,T g, T b){
172 (*this)[0]=r;(*this)[1]=g;(*this)[2]=b;(*this)[3]=T(255);
173 }
174
182 RGBA(const T t[4]): Base(t) {}
189 RGBA(const Base& x) : Base(x) {}
197 template <typename T2> RGBA(const FArray<T2,4>& x) : Base(x) {}
205 template <typename T2> RGBA(const FArray<T2,3>& x){
206 (*this)[0]=T(x[0]);
207 (*this)[1]=T(x[1]);
208 (*this)[2]=T(x[2]);
209 (*this)[3]=T(255);
210 }
217 const T& r() const { return (*this)[0]; }
224 T& r() { return (*this)[0]; }
231 const T& g() const { return (*this)[1]; }
238 T& g() { return (*this)[1]; }
245 const T& b() const { return (*this)[2]; }
252 T& b() { return (*this)[2]; }
259 const T& a() const { return (*this)[3]; }
266 T& a() { return (*this)[3]; }
273 operator T () const { return T(0.3*r()+0.59*g()+0.11*b()); }
274 };
287 const Color WHITE=Color(255,255,255);
293 const Color BLACK=Color(0,0,0);
299 const Color BLUE=Color(0,0,255);
305 const Color RED=Color(255,0,0);
311 const Color GREEN=Color(0,255,0);
317 const Color YELLOW=Color(255,255,0);
323 const Color CYAN=Color(0,255,255);
329 const Color MAGENTA=Color(255,0,255);
335 const Color ORANGE=Color(255,128,0);
341 const Color PURPLE=Color(128,0,255);
342
344 inline std::ostream& operator<<(std::ostream& out, const Color& c) {
345 return out << (int)c[0] << ' ' << (int)c[1] << ' ' << (int)c[2];
346 }
360 const AlphaColor AWHITE=AlphaColor(255,255,255);
378 const AlphaColor ARED=AlphaColor(255,0,0);
390 const AlphaColor AYELLOW=AlphaColor(255,255,0);
396 const AlphaColor ACYAN=AlphaColor(0,255,255);
403
411 template<typename T> inline FVector<double,3> RGB2YUV(const RGB<T>& rgb) {
412 double y=.299*double(rgb[0])+.587*double(rgb[1])+.114*double(rgb[2]);
413 return FVector<double,3>(y,.492*(double(rgb[2])-y),.877*(double(rgb[0])-y));
414 }
422 template<typename T> inline RGB<T> YUV2RGB(const FVector<double,3>& yuv) {
423 double r(yuv[2]/.877+yuv[0]);
424 double b(yuv[1]/.492+yuv[0]);
425 double g((yuv[0]-.299*r-.114*b)/.587);
426 return RGB<T>(T(r),T(g),T(b));
427 }
428
429
431}
Array of fixed size.
Definition: FArray.h:17
Vector of fixed size.
Definition: FVector.h:17
const T & x() const
Read alias.
Definition: FVector.h:104
RED GREEN BLUE Alpha color.
Definition: Color.h:138
RGBA(T r, T g, T b)
Constructor with r,g,b values Constructs a RGBA of type T.
Definition: Color.h:171
T & g()
Write GREEN alias.
Definition: Color.h:238
const T & r() const
Read RED alias.
Definition: Color.h:217
const T & b() const
Read BLUE alias.
Definition: Color.h:245
T & a()
Write alpha alias.
Definition: Color.h:266
T & r()
Write RED alias.
Definition: Color.h:224
const T & g() const
Read GREEN alias.
Definition: Color.h:231
RGBA(const Base &x)
Copy constructor.
Definition: Color.h:189
RGBA(T x)
Constructor with constant value.
Definition: Color.h:154
RGBA(const T t[4])
Constructor from C array.
Definition: Color.h:182
T & b()
Write BLUE alias.
Definition: Color.h:252
RGBA(const FArray< T2, 4 > &x)
Constructor from other value type Constructs from RGBA with different type.
Definition: Color.h:197
RGBA()
Empty constructor.
Definition: Color.h:147
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:161
const T & a() const
Read alpha alias.
Definition: Color.h:259
RGBA(const FArray< T2, 3 > &x)
Constructor from RGB.
Definition: Color.h:205
RED GREEN BLUE color.
Definition: Color.h:26
const T & g() const
Read GREEN alias.
Definition: Color.h:102
T & b()
Write BLUE alias.
Definition: Color.h:123
T & r()
Write RED alias.
Definition: Color.h:95
RGB(T v)
Constructor with constant value.
Definition: Color.h:42
RGB(const Base &x)
Copy constructor.
Definition: Color.h:64
const T & b() const
Read BLUE alias.
Definition: Color.h:116
RGB(const T t[3])
Constructor from C array.
Definition: Color.h:57
T & g()
Write GREEN alias.
Definition: Color.h:109
RGB()
Empty constructor.
Definition: Color.h:35
const T & r() const
Read RED alias.
Definition: Color.h:88
RGB(const FArray< T2, 4 > &x)
Constructor from RGBA.
Definition: Color.h:81
RGB(const FVector< T2, 3 > &x)
Constructor from other value type Constructs from RGB with different type.
Definition: Color.h:72
RGB(T r, T g, T b)
Constructor with r,g,b values Constructs a RGB of type T.
Definition: Color.h:49
const AlphaColor ABLUE
Predefined color.
Definition: Color.h:372
const Color PURPLE
Predefined color.
Definition: Color.h:341
const Color CYAN
Predefined color.
Definition: Color.h:323
const AlphaColor AYELLOW
Predefined color.
Definition: Color.h:390
const Color BLACK
Predefined color.
Definition: Color.h:293
const AlphaColor ARED
Predefined color.
Definition: Color.h:378
const Color YELLOW
Predefined color.
Definition: Color.h:317
const AlphaColor ABLACK
Predefined color.
Definition: Color.h:366
const Color WHITE
Predefined color.
Definition: Color.h:287
const AlphaColor AMAGENTA
Predefined color.
Definition: Color.h:402
const AlphaColor ACYAN
Predefined color.
Definition: Color.h:396
const AlphaColor AGREEN
Predefined color.
Definition: Color.h:384
std::ostream & operator<<(std::ostream &out, const Color &c)
Display color as three integral values.
Definition: Color.h:344
RGB< T > YUV2RGB(const FVector< double, 3 > &yuv)
YUV to RGB.
Definition: Color.h:422
RGBA< byte > AlphaColor
RGBA<byte> alias.
Definition: Color.h:354
FVector< double, 3 > RGB2YUV(const RGB< T > &rgb)
RGB to YUV.
Definition: Color.h:411
const Color ORANGE
Predefined color.
Definition: Color.h:335
const AlphaColor AWHITE
Predefined color.
Definition: Color.h:360
const Color BLUE
Predefined color.
Definition: Color.h:299
RGB< byte > Color
RGB<byte> alias.
Definition: Color.h:281
const Color MAGENTA
Predefined color.
Definition: Color.h:329
const Color RED
Predefined color.
Definition: Color.h:305
const Color GREEN
Predefined color.
Definition: Color.h:311
Imagine++ namespace.