Rev Author Line No. Line
178 kaklik 1 #ifndef RGBA_H
2 #define RGBA_H
3  
4 #include <cmath>
5 #include "colour_sensitivity.h"
6  
7 namespace mimas {
8  
9 template <typename T>
10 class rgba
11 {
12 protected:
13 T b,g,r,a;
14  
15 public:
16  
17 rgba(void)
18 {
19 r=g=b=a=T();
20 }
21  
22 template< class U >
23 rgba(const rgba< U > &rhs)
24 {
25 r = (T)rhs.getRed();
26 g = (T)rhs.getGreen();
27 b = (T)rhs.getBlue();
28 a = (T)rhs.getAlpha();
29 }
30  
31 rgba( T _r, T _g, T _b, T _a = T() ):
32 b(_b), g(_g), r(_r), a(_a)
33 {
34 }
35  
36 template< typename U >
37 explicit rgba(const U &val)
38 {
39 r=(T)val;
40 g=(T)val;
41 b=(T)val;
42 a=T();
43 }
44  
45 //inline functions to avoid overhead of the call,
46 //especially used for convert_trait
47 inline T getRed(void) const
48 {
49 return r;
50 }
51  
52 inline T getGreen(void) const
53 {
54 return g;
55 }
56  
57 inline T getBlue(void) const
58 {
59 return b;
60 }
61  
62 inline T getAlpha(void) const
63 {
64 return a;
65 }
66  
67  
68 void setRed(T val)
69 {
70 r=val;
71 }
72  
73 void setGreen(T val)
74 {
75 g=val;
76 }
77  
78 void setBlue(T val)
79 {
80 b=val;
81 }
82  
83 void setAlpha(T val)
84 {
85 a=val;
86 }
87  
88 void set(T val)
89 {
90 r=g=b=a=val;
91 }
92  
93  
94 //mixes the 2 colors according to the ammount of transparency
95 void overlay(T r_,T g_,T b_,T a_)
96 {
97 //assumption that alpha take a value between 0 and 255
98 //should be corrected according to the type T
99 r = ((255 - a_)* r + a_ * r_)/255;
100 g = ((255 - a_)* g + a_ * g_)/255;
101 b = ((255 - a_)* b + a_ * b_)/255;
102 }
103  
104 //overload casting to double
105 operator double(void) const
106 {
107 return
108 SENSITIVITY_RED * r + SENSITIVITY_GREEN * g + SENSITIVITY_BLUE * b;
109 }
110  
111 operator float(void) const
112 {
113 return
114 SENSITIVITY_RED * r + SENSITIVITY_GREEN * g + SENSITIVITY_BLUE * b;
115 }
116  
117 operator short int(void) const
118 {
119 const int
120 rwgt = (int)( SENSITIVITY_RED * 65536 ),
121 gwgt = (int)( SENSITIVITY_GREEN * 65536 ),
122 bwgt = (int)( SENSITIVITY_BLUE * 65536 );
123 return (short int)( ( rwgt * r + gwgt * g + bwgt * b ) >> 16 );
124 }
125  
126 operator int(void) const
127 {
128 return (int)operator double();
129 }
130  
131 //overload casting to unsigned char
132 operator unsigned char(void) const
133 {
134 const int
135 rwgt = (int)( SENSITIVITY_RED * 65536 ),
136 gwgt = (int)( SENSITIVITY_GREEN * 65536 ),
137 bwgt = (int)( SENSITIVITY_BLUE * 65536 );
138 return (unsigned char)( ( rwgt * r + gwgt * g + bwgt * b ) >> 16 );
139 }
140  
141 /// Equality of two pixels
142 bool operator==(const rgba<T> &rhs)
143 {
144 return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
145 }
146  
147 /// Check for unequal pixels.
148 bool operator!=(const rgba<T> &rhs)
149 {
150 return !(r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a);
151 }
152  
153 rgba< T > operator+( const rgba< T > &o ) const
154 {
155 return rgba< T >( r + o.r, g + o.g, b + o.b );
156 }
157  
158 rgba< T > operator-( const rgba< T > &o ) const
159 {
160 return rgba< T >( r - o.r, g - o.g, b - o.b );
161 }
162  
163 rgba< T > &operator+=( const rgba< T > &o )
164 {
165 r += o.r; g += o.g; b += o.b;
166 return *this;
167 }
168  
169 rgba< T > &operator-=( const rgba< T > &o )
170 {
171 r -= o.r; g -= o.g; b -= o.b;
172 return *this;
173 }
174  
175 double getHue(void) const;///< returns number in the range 0-360
176 double getSaturation(void) const; ///< returns saturation between 0 and 1.0
177  
178 };
179  
180 }
181  
182 #include "rgba.tcc"
183  
184 #endif
185