Debian Codegraph @

cvd/rgba.h

raw symbols SCIP
1 #ifndef CVD_RGBA_H
2 #define CVD_RGBA_H
3
4 #include <iostream>
5
6 namespace CVD
7 {
8
9 //////////////////////////////
10 // CVD::Rgba
11 // Template class to represent red, green, blue and alpha components
12 //
13 /// A colour consisting of red, green, blue and alpha components
14 /// @param T The datatype of each component
15 /// @ingroup gImage
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
24 /// Constructs a colour as specified
25 /// @param r The red component
26 /// @param g The green component
27 /// @param b The blue component
28 /// @param a The alpha component
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; ///< The red component
46 T green; ///< The green component
47 T blue; ///< The blue component
48 T alpha; ///< The alpha component
49
50 /// Assignment operator between two different storage types, using the standard casts as necessary
51 /// @param c The colour to copy from
52 template <typename T2>
53 Rgba<T>& operator=(const Rgba<T2>& c)
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
62 /// Logical equals operator. Returns true if each component is the same.
63 /// @param c Rgba to compare with
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
69 /// Logical not-equals operator. Returns true unless each component is the same.
70 /// @param c Rgba to compare with
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
79 /// Write the colour to a stream in the format "(red,green,blue,alpha)"
80 /// @param os The stream
81 /// @param x The colour object
82 /// @relates Rgba
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
90 /// Write the colour to a stream in the format "(red,green,blue,alpha)"
91 /// @param os The stream
92 /// @param x The colour object
93 /// @relates Rgba
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