libcvd
rgba.h
1 #ifndef CVD_RGBA_H
2 #define CVD_RGBA_H
3 
4 #include <iostream>
5 
6 namespace CVD
7 {
8 
10 // CVD::Rgba
11 // Template class to represent red, green, blue and alpha components
12 //
16 template <typename T>
17 class Rgba
18 {
19  public:
20  Rgba() = default;
21  Rgba(const Rgba&) = default;
22  Rgba& operator=(const Rgba&) = default;
23 
29  inline explicit Rgba(T r, T g, T b, T a)
30  : red(r)
31  , green(g)
32  , blue(b)
33  , alpha(a)
34  {
35  }
36  template <class S>
37  inline explicit Rgba(const Rgba<S>& rgba)
38  : red(static_cast<T>(rgba.red))
39  , green(static_cast<T>(rgba.green))
40  , blue(static_cast<T>(rgba.blue))
41  , alpha(static_cast<T>(rgba.alpha))
42  {
43  }
44 
45  T red;
46  T green;
47  T blue;
48  T alpha;
49 
52  template <typename T2>
54  {
55  red = static_cast<T>(c.red);
56  green = static_cast<T>(c.green);
57  blue = static_cast<T>(c.blue);
58  alpha = static_cast<T>(c.alpha);
59  return *this;
60  }
61 
64  bool operator==(const Rgba<T>& c) const
65  {
66  return red == c.red && green == c.green && blue == c.blue && alpha == c.alpha;
67  }
68 
71  bool operator!=(const Rgba<T>& c) const
72  {
73  return red != c.red || green != c.green || blue != c.blue || alpha != c.alpha;
74  }
75 
76  // T to_grey() const {return 0.3*red + 0.6*green + 0.1*blue;}
77 };
78 
83 template <typename T>
84 std::ostream& operator<<(std::ostream& os, const Rgba<T>& x)
85 {
86  return os << "(" << x.red << "," << x.green << ","
87  << x.blue << "," << x.alpha << ")";
88 }
89 
94 inline std::ostream& operator<<(std::ostream& os, const Rgba<unsigned char>& x)
95 {
96  return os << "(" << static_cast<unsigned int>(x.red) << ","
97  << static_cast<unsigned int>(x.green) << ","
98  << static_cast<unsigned int>(x.blue) << ","
99  << static_cast<unsigned int>(x.alpha) << ")";
100 }
101 
102 } // end namespace
103 #endif
T blue
The blue component.
Definition: rgba.h:47
All classes and functions are within the CVD namespace.
Definition: argb.h:6
T alpha
The alpha component.
Definition: rgba.h:48
bool operator!=(const Rgba< T > &c) const
Logical not-equals operator.
Definition: rgba.h:71
Rgba< T > & operator=(const Rgba< T2 > &c)
Assignment operator between two different storage types, using the standard casts as necessary...
Definition: rgba.h:53
A colour consisting of red, green, blue and alpha components.
Definition: rgba.h:17
bool operator==(const Rgba< T > &c) const
Logical equals operator.
Definition: rgba.h:64
T green
The green component.
Definition: rgba.h:46
Rgba(T r, T g, T b, T a)
Constructs a colour as specified.
Definition: rgba.h:29
T red
The red component.
Definition: rgba.h:45