libcvd
rgb.h
1 // //
3 // rgb.h //
4 // //
5 // Contains definitions of Rgb template class //
6 // //
7 // derived from IPRS_* developed by Tom Drummond //
8 // //
10 #ifndef CVD_RGB_H
11 #define CVD_RGB_H
12 
13 #include <iostream>
14 
15 #include <cvd/byte.h>
16 
17 namespace CVD
18 {
19 
24 template <class T>
25 class Rgb
26 {
27  public:
28  Rgb() = default;
29  Rgb(const Rgb&) = default;
30  Rgb& operator=(const Rgb&) = default;
31 
36  inline Rgb(T r, T g, T b)
37  : red(r)
38  , green(g)
39  , blue(b)
40  {
41  }
42 
43  template <class S>
44  inline explicit Rgb(const Rgb<S>& rgb)
45  : red(static_cast<T>(rgb.red))
46  , green(static_cast<T>(rgb.green))
47  , blue(static_cast<T>(rgb.blue))
48  {
49  }
50 
51  T red;
52  T green;
53  T blue;
54 
57  inline bool operator==(const Rgb<T>& c) const
58  {
59  return red == c.red && green == c.green && blue == c.blue;
60  }
61 
64  inline bool operator!=(const Rgb<T>& c) const
65  {
66  return red != c.red || green != c.green || blue != c.blue;
67  }
68 
71  template <class T2>
72  inline Rgb<T>& operator=(const Rgb<T2>& c)
73  {
74  red = static_cast<T>(c.red);
75  green = static_cast<T>(c.green);
76  blue = static_cast<T>(c.blue);
77  return *this;
78  }
79 
80  // T to_grey() {return 0.3*red + 0.6*green + 0.1*blue;}
81 };
82 
87 template <class T>
88 std::ostream& operator<<(std::ostream& os, const Rgb<T>& x)
89 {
90  return os << "(" << x.red << "," << x.green << ","
91  << x.blue << ")";
92 }
93 
98 inline std::ostream& operator<<(std::ostream& os, const Rgb<char>& x)
99 {
100  return os << "(" << (int)(unsigned char)x.red << ","
101  << (int)(unsigned char)x.green << ","
102  << (int)(unsigned char)x.blue << ")";
103 }
104 
109 inline std::ostream& operator<<(std::ostream& os, const Rgb<byte>& x)
110 {
111  return os << "(" << static_cast<int>(x.red) << ","
112  << static_cast<int>(x.green) << ","
113  << static_cast<int>(x.blue) << ")";
114 }
115 
116 } // end namespace
117 #endif
T red
The red component.
Definition: rgb.h:51
T blue
The blue component.
Definition: rgb.h:53
A colour consisting of red, green and blue components.
Definition: rgb.h:25
All classes and functions are within the CVD namespace.
Definition: argb.h:6
Rgb< T > & operator=(const Rgb< T2 > &c)
Assignment operator between two different storage types, using the standard casts as necessary...
Definition: rgb.h:72
bool operator==(const Rgb< T > &c) const
Logical equals operator.
Definition: rgb.h:57
Rgb(T r, T g, T b)
Constructs a colour as specified.
Definition: rgb.h:36
T green
The green component.
Definition: rgb.h:52
bool operator!=(const Rgb< T > &c) const
Logical not-equals operator.
Definition: rgb.h:64