Debian Codegraph @

cvd/rgb.h

raw symbols SCIP
1 //////////////////////////////////////////////////////////////////////////
2 // //
3 // rgb.h //
4 // //
5 // Contains definitions of Rgb template class //
6 // //
7 // derived from IPRS_* developed by Tom Drummond //
8 // //
9 //////////////////////////////////////////////////////////////////////////
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
20 /// A colour consisting of red, green and blue components.
21 /// Often used to store 24-bit colour information, i.e. <code>CVD::Rgb<CVD::byte></code>
22 /// @param T The datatype of each component
23 /// @ingroup gImage
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
32 /// Constructs a colour as specified
33 /// @param r The red component
34 /// @param g The green component
35 /// @param b The blue component
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; ///< The red component
52 T green; ///< The green component
53 T blue; ///< The blue component
54
55 /// Logical equals operator. Returns true if each component is the same.
56 /// @param c Rgb to compare with
57 inline bool operator==(const Rgb<T>& c) const
58 {
59 return red == c.red && green == c.green && blue == c.blue;
60 }
61
62 /// Logical not-equals operator. Returns true unless each component is the same.
63 /// @param c Rgb to compare with
64 inline bool operator!=(const Rgb<T>& c) const
65 {
66 return red != c.red || green != c.green || blue != c.blue;
67 }
68
69 /// Assignment operator between two different storage types, using the standard casts as necessary
70 /// @param c The colour to copy from
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
83 /// Write the colour to a stream in the format "(red,green,blue)"
84 /// @param os The stream
85 /// @param x The colour object
86 /// @relates Rgb
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
94 /// Read a colour from a stream, interpreting three numbers as <code>char</code>s
95 /// @param os The stream
96 /// @param x The colour object
97 /// @relates Rgb
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
105 /// Read a colour from a stream, interpreting three numbers as <code>byte</code>s
106 /// @param os The stream
107 /// @param x The colour object
108 /// @relates Rgb
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