/src/ogre/OgreMain/include/OgreColourValue.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #ifndef _COLOURVALUE_H__ |
29 | | #define _COLOURVALUE_H__ |
30 | | |
31 | | #include "OgrePrerequisites.h" |
32 | | |
33 | | namespace Ogre { |
34 | | /** \addtogroup Core |
35 | | * @{ |
36 | | */ |
37 | | /** \addtogroup General |
38 | | * @{ |
39 | | */ |
40 | | |
41 | | typedef uint32 RGBA; |
42 | | typedef uint32 ARGB; |
43 | | typedef uint32 ABGR; |
44 | | typedef uint32 BGRA; |
45 | | |
46 | | /** Class representing colour. |
47 | | |
48 | | Colour is represented as 4 components, each of which is a |
49 | | floating-point value from 0.0 to 1.0. |
50 | | @par |
51 | | The 3 'normal' colour components are red, green and blue, a higher |
52 | | number indicating greater amounts of that component in the colour. |
53 | | The forth component is the 'alpha' value, which represents |
54 | | transparency. In this case, 0.0 is completely transparent and 1.0 is |
55 | | fully opaque. |
56 | | */ |
57 | | class _OgreExport ColourValue |
58 | | { |
59 | | public: |
60 | | static const ColourValue &ZERO; |
61 | | static const ColourValue &Black; |
62 | | static const ColourValue &White; |
63 | | static const ColourValue &Red; |
64 | | static const ColourValue &Green; |
65 | | static const ColourValue &Blue; |
66 | | |
67 | | explicit constexpr ColourValue( float red = 1.0f, |
68 | | float green = 1.0f, |
69 | | float blue = 1.0f, |
70 | 0 | float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha) |
71 | 0 | { } |
72 | | |
73 | 0 | explicit ColourValue(const uchar* byte) : r(byte[0]), g(byte[1]), b(byte[2]), a(byte[3]) |
74 | 0 | { |
75 | 0 | *this /= 255; |
76 | 0 | } |
77 | | |
78 | | bool operator==(const ColourValue& rhs) const |
79 | 0 | { |
80 | 0 | return (r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a); |
81 | 0 | } |
82 | 0 | bool operator!=(const ColourValue& rhs) const { return !(*this == rhs); } |
83 | | |
84 | | float r,g,b,a; |
85 | | |
86 | | /// @name conversions from/ to native-endian packed formats |
87 | | /// @{ |
88 | | |
89 | | /// value packed as #PF_R8G8B8A8 |
90 | | RGBA getAsRGBA(void) const; |
91 | | |
92 | | /// value packed as #PF_A8R8G8B8 |
93 | | ARGB getAsARGB(void) const; |
94 | | |
95 | | /// value packed as #PF_B8G8R8A8 |
96 | | BGRA getAsBGRA(void) const; |
97 | | |
98 | | /// value packed as #PF_A8B8G8R8 |
99 | | ABGR getAsABGR(void) const; |
100 | | |
101 | | /// value packed as #PF_BYTE_RGBA |
102 | | RGBA getAsBYTE() const |
103 | 0 | { |
104 | | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
105 | | return getAsRGBA(); |
106 | | #else |
107 | 0 | return getAsABGR(); |
108 | 0 | #endif |
109 | 0 | } |
110 | | |
111 | | /// Set value from #PF_R8G8B8A8 |
112 | | void setAsRGBA(RGBA val); |
113 | | |
114 | | /// Set value from #PF_A8R8G8B8 |
115 | | void setAsARGB(ARGB val); |
116 | | |
117 | | /// Set value from #PF_B8G8R8A8 |
118 | | void setAsBGRA(BGRA val); |
119 | | |
120 | | /// Set value from #PF_A8B8G8R8 |
121 | | void setAsABGR(ABGR val); |
122 | | /// @} |
123 | | |
124 | | /** Clamps colour value to the range [0, 1]. |
125 | | */ |
126 | | void saturate(void) |
127 | 0 | { |
128 | 0 | if (r < 0) |
129 | 0 | r = 0; |
130 | 0 | else if (r > 1) |
131 | 0 | r = 1; |
132 | |
|
133 | 0 | if (g < 0) |
134 | 0 | g = 0; |
135 | 0 | else if (g > 1) |
136 | 0 | g = 1; |
137 | |
|
138 | 0 | if (b < 0) |
139 | 0 | b = 0; |
140 | 0 | else if (b > 1) |
141 | 0 | b = 1; |
142 | |
|
143 | 0 | if (a < 0) |
144 | 0 | a = 0; |
145 | 0 | else if (a > 1) |
146 | 0 | a = 1; |
147 | 0 | } |
148 | | |
149 | | /** As saturate, except that this colour value is unaffected and |
150 | | the saturated colour value is returned as a copy. */ |
151 | | ColourValue saturateCopy(void) const |
152 | 0 | { |
153 | 0 | ColourValue ret = *this; |
154 | 0 | ret.saturate(); |
155 | 0 | return ret; |
156 | 0 | } |
157 | | |
158 | | /// Array accessor operator |
159 | | float operator [] ( const size_t i ) const |
160 | 0 | { |
161 | 0 | assert( i < 4 ); |
162 | 0 |
|
163 | 0 | return *(&r+i); |
164 | 0 | } |
165 | | |
166 | | /// Array accessor operator |
167 | | float& operator [] ( const size_t i ) |
168 | 0 | { |
169 | 0 | assert( i < 4 ); |
170 | 0 |
|
171 | 0 | return *(&r+i); |
172 | 0 | } |
173 | | |
174 | | /// Pointer accessor for direct copying |
175 | | float* ptr() |
176 | 0 | { |
177 | 0 | return &r; |
178 | 0 | } |
179 | | /// Pointer accessor for direct copying |
180 | | const float* ptr() const |
181 | 0 | { |
182 | 0 | return &r; |
183 | 0 | } |
184 | | |
185 | | |
186 | | // arithmetic operations |
187 | | ColourValue operator + ( const ColourValue& rkVector ) const |
188 | 0 | { |
189 | 0 | ColourValue kSum; |
190 | |
|
191 | 0 | kSum.r = r + rkVector.r; |
192 | 0 | kSum.g = g + rkVector.g; |
193 | 0 | kSum.b = b + rkVector.b; |
194 | 0 | kSum.a = a + rkVector.a; |
195 | |
|
196 | 0 | return kSum; |
197 | 0 | } |
198 | | |
199 | | ColourValue operator - ( const ColourValue& rkVector ) const |
200 | 0 | { |
201 | 0 | ColourValue kDiff; |
202 | 0 |
|
203 | 0 | kDiff.r = r - rkVector.r; |
204 | 0 | kDiff.g = g - rkVector.g; |
205 | 0 | kDiff.b = b - rkVector.b; |
206 | 0 | kDiff.a = a - rkVector.a; |
207 | 0 |
|
208 | 0 | return kDiff; |
209 | 0 | } |
210 | | |
211 | | ColourValue operator * (const float fScalar ) const |
212 | 0 | { |
213 | 0 | ColourValue kProd; |
214 | |
|
215 | 0 | kProd.r = fScalar*r; |
216 | 0 | kProd.g = fScalar*g; |
217 | 0 | kProd.b = fScalar*b; |
218 | 0 | kProd.a = fScalar*a; |
219 | |
|
220 | 0 | return kProd; |
221 | 0 | } |
222 | | |
223 | | ColourValue operator * ( const ColourValue& rhs) const |
224 | 0 | { |
225 | 0 | ColourValue kProd; |
226 | 0 |
|
227 | 0 | kProd.r = rhs.r * r; |
228 | 0 | kProd.g = rhs.g * g; |
229 | 0 | kProd.b = rhs.b * b; |
230 | 0 | kProd.a = rhs.a * a; |
231 | 0 |
|
232 | 0 | return kProd; |
233 | 0 | } |
234 | | |
235 | | ColourValue operator / ( const ColourValue& rhs) const |
236 | 0 | { |
237 | 0 | ColourValue kProd; |
238 | 0 |
|
239 | 0 | kProd.r = r / rhs.r; |
240 | 0 | kProd.g = g / rhs.g; |
241 | 0 | kProd.b = b / rhs.b; |
242 | 0 | kProd.a = a / rhs.a; |
243 | 0 |
|
244 | 0 | return kProd; |
245 | 0 | } |
246 | | |
247 | | ColourValue operator / (const float fScalar ) const |
248 | 0 | { |
249 | 0 | assert( fScalar != 0.0 ); |
250 | 0 |
|
251 | 0 | ColourValue kDiv; |
252 | 0 |
|
253 | 0 | float fInv = 1.0f / fScalar; |
254 | 0 | kDiv.r = r * fInv; |
255 | 0 | kDiv.g = g * fInv; |
256 | 0 | kDiv.b = b * fInv; |
257 | 0 | kDiv.a = a * fInv; |
258 | 0 |
|
259 | 0 | return kDiv; |
260 | 0 | } |
261 | | |
262 | | friend ColourValue operator * (const float fScalar, const ColourValue& rkVector ) |
263 | 0 | { |
264 | 0 | ColourValue kProd; |
265 | 0 |
|
266 | 0 | kProd.r = fScalar * rkVector.r; |
267 | 0 | kProd.g = fScalar * rkVector.g; |
268 | 0 | kProd.b = fScalar * rkVector.b; |
269 | 0 | kProd.a = fScalar * rkVector.a; |
270 | 0 |
|
271 | 0 | return kProd; |
272 | 0 | } |
273 | | |
274 | | // arithmetic updates |
275 | | ColourValue& operator += ( const ColourValue& rkVector ) |
276 | 0 | { |
277 | 0 | r += rkVector.r; |
278 | 0 | g += rkVector.g; |
279 | 0 | b += rkVector.b; |
280 | 0 | a += rkVector.a; |
281 | 0 |
|
282 | 0 | return *this; |
283 | 0 | } |
284 | | |
285 | | ColourValue& operator -= ( const ColourValue& rkVector ) |
286 | 0 | { |
287 | 0 | r -= rkVector.r; |
288 | 0 | g -= rkVector.g; |
289 | 0 | b -= rkVector.b; |
290 | 0 | a -= rkVector.a; |
291 | 0 |
|
292 | 0 | return *this; |
293 | 0 | } |
294 | | |
295 | | ColourValue& operator *= (const float fScalar ) |
296 | 0 | { |
297 | 0 | r *= fScalar; |
298 | 0 | g *= fScalar; |
299 | 0 | b *= fScalar; |
300 | 0 | a *= fScalar; |
301 | 0 | return *this; |
302 | 0 | } |
303 | | |
304 | | ColourValue& operator /= (const float fScalar ) |
305 | 0 | { |
306 | 0 | assert( fScalar != 0.0 ); |
307 | |
|
308 | 0 | float fInv = 1.0f / fScalar; |
309 | |
|
310 | 0 | r *= fInv; |
311 | 0 | g *= fInv; |
312 | 0 | b *= fInv; |
313 | 0 | a *= fInv; |
314 | |
|
315 | 0 | return *this; |
316 | 0 | } |
317 | | |
318 | | /** Set a colour value from Hue, Saturation and Brightness. |
319 | | @param hue Hue value, scaled to the [0,1] range as opposed to the 0-360 |
320 | | @param saturation Saturation level, [0,1] |
321 | | @param brightness Brightness level, [0,1] |
322 | | */ |
323 | | void setHSB(float hue, float saturation, float brightness); |
324 | | |
325 | | /** Convert the current colour to Hue, Saturation and Brightness values. |
326 | | @param hue Output hue value, scaled to the [0,1] range as opposed to the 0-360 |
327 | | @param saturation Output saturation level, [0,1] |
328 | | @param brightness Output brightness level, [0,1] |
329 | | */ |
330 | | void getHSB(float& hue, float& saturation, float& brightness) const; |
331 | | |
332 | | /// @deprecated |
333 | | OGRE_DEPRECATED void getHSB(float* hue, float* saturation, float* brightness) const |
334 | 0 | { |
335 | 0 | getHSB(*hue, *saturation, *brightness); |
336 | 0 | } |
337 | | |
338 | | /** Function for writing to a stream. |
339 | | */ |
340 | | inline friend std::ostream& operator << |
341 | | ( std::ostream& o, const ColourValue& c ) |
342 | 0 | { |
343 | 0 | o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")"; |
344 | 0 | return o; |
345 | 0 | } |
346 | | |
347 | | }; |
348 | | /** @} */ |
349 | | /** @} */ |
350 | | |
351 | | } // namespace |
352 | | |
353 | | #endif |