nomlib
Loading...
Searching...
No Matches
Color4.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_MATH_COLOR4_HPP
30#define NOMLIB_MATH_COLOR4_HPP
31
32#include <algorithm>
33#include <vector>
34
35#include "nomlib/config.hpp"
36
37// FIXME: The following declaration is necessary in order to avoid a very
38// nasty compiling conflict that can happen under Windows anytime the
39// windef.h header file is included (commonly from windows.h), due to min and
40// max macros being declared there. This is why macros are evil.
41//
42// http://support.microsoft.com/kb/143208
43// http://stackoverflow.com/questions/5004858/stdmin-gives-error
44#if defined( NOM_PLATFORM_WINDOWS )
45 #undef min
46 #undef max
47#endif
48
49namespace nom {
50
52const std::string COLOR_DELIMITER = ", ";
53
58template <typename T>
59struct Color4
60{
62 Color4 ( void ) :
63 r ( -1 ),
64 g ( -1 ),
65 b ( -1 ),
66 a ( Color4<T>::ALPHA_OPAQUE )
67 {
68 //NOM_LOG_TRACE(NOM);
69 }
70
72 ~Color4 ( void )
73 {
74 //NOM_LOG_TRACE(NOM);
75 }
76
82 template <typename U>
83 Color4( const Color4<U>& rhs ) :
84 r{ static_cast<T> ( rhs.r ) },
85 g{ static_cast<T> ( rhs.g ) },
86 b{ static_cast<T> ( rhs.b ) },
87 a{ static_cast<T> ( rhs.a ) }
88 {
89 //NOM_LOG_TRACE(NOM);
90 }
91
97 explicit Color4( const Color4<float>& rhs ) :
98 r{ static_cast<float> ( rhs.r ) },
99 g{ static_cast<float> ( rhs.g ) },
100 b{ static_cast<float> ( rhs.b ) },
101 a{ static_cast<float> ( rhs.a ) }
102 {
103 //NOM_LOG_TRACE(NOM);
104 }
105
107 Color4 ( T r, T g, T b ) :
108 r ( r ),
109 g ( g ),
110 b ( b ),
111 a ( Color4<T>::ALPHA_OPAQUE )
112 {
113 //NOM_LOG_TRACE(NOM);
114 }
115
117 Color4 ( T red, T green, T blue, T alpha ) :
118 r ( red ),
119 g ( green ),
120 b ( blue ),
121 a ( alpha )
122 {
123 //NOM_LOG_TRACE(NOM);
124 }
125
127 static const T ALPHA_TRANSPARENT;
128
130 static const T ALPHA_OPAQUE;
131
135 static const Color4 null;
136
141 static const Color4 Transparent;
142 static const Color4 Black;
143 static const Color4 White;
144 static const Color4 Red;
145 static const Color4 Green;
146 static const Color4 Blue;
147 static const Color4 Yellow;
148 static const Color4 Magenta;
149 static const Color4 Cyan;
150
152 static const Color4 Silver;
153 static const Color4 Purple;
154 static const Color4 Orange;
155 static const Color4 LightGray;
156 static const Color4 Gray;
157 static const Color4 SkyBlue;
158
159 // static const Color4 ButtonGradient;
160
162 T r;
163
165 T g;
166
168 T b;
169
171 T a;
172};
173
177template <typename T>
178inline Color4<T> difference_color_blend( const Color4<T>& lhs, const Color4<T>& rhs )
179{
180 Color4<T> c;
181 c.r = abs( lhs.r - rhs.r );
182 c.g = abs( lhs.g - rhs.g );
183 c.b = abs( lhs.b - rhs.b );
184
185 return c;
186}
187
197template <typename T>
198inline std::ostream& operator << ( std::ostream& os, const Color4<T>& color )
199{
200 os
201 << static_cast<sint> ( color.r )
202 << COLOR_DELIMITER
203 << static_cast<sint> ( color.g )
204 << COLOR_DELIMITER
205 << static_cast<sint> ( color.b )
206 << COLOR_DELIMITER
207 << static_cast<sint> ( color.a );
208
209 return os;
210}
211
218inline std::ostream& operator <<( std::ostream& os, const Color4<float>& color )
219{
220 os
221 << static_cast<float> ( color.r )
222 << COLOR_DELIMITER
223 << static_cast<float> ( color.g )
224 << COLOR_DELIMITER
225 << static_cast<float> ( color.b )
226 << COLOR_DELIMITER
227 << static_cast<float> ( color.a );
228
229 return os;
230}
231
233template <typename T>
234inline bool operator == ( const Color4<T>& left, const Color4<T>& right )
235{
236 return ( left.r == right.r ) &&
237 ( left.g == right.g ) &&
238 ( left.b == right.b ) &&
239 ( left.a == right.a );
240}
241
243template <typename T>
244inline bool operator != ( const Color4<T>& left, const Color4<T>& right )
245{
246 return ! ( left == right );
247}
248
250template <typename T>
251inline Color4<T> operator + ( const Color4<T>& left, const Color4<T>& right )
252{
253 return Color4<T> ( static_cast<T> ( std::min ( left.r + right.r, 255 ) ),
254 static_cast<T> ( std::min ( left.g + right.g, 255 ) ),
255 static_cast<T> ( std::min ( left.b + right.b, 255 ) ),
256 static_cast<T> ( std::min ( left.a + right.a, 255 ) )
257 );
258}
259
261inline Color4<float> operator +( const Color4<float>& lhs, const Color4<float>& rhs )
262{
263 return Color4<float> (
264 std::min( ( lhs.r + rhs.r / 255 ), 1.0f ),
265 std::min( ( lhs.g + rhs.g / 255 ), 1.0f ),
266 std::min( ( lhs.b + rhs.b / 255 ), 1.0f ),
267 std::min( ( lhs.a + rhs.a / 255 ), 1.0f )
268 );
269}
270
272template <typename T>
273inline Color4<T> operator ++ ( Color4<T>& left )
274{
275 return Color4<T> ( static_cast<T> ( left.r-- ),
276 static_cast<T> ( left.g-- ),
277 static_cast<T> ( left.b-- ),
278 static_cast<T> ( left.a-- )
279 );
280}
281
282template <typename T>
284inline Color4<T> operator - ( const Color4<T>& left, const Color4<T>& right )
285{
286 return Color4<T> (
287 static_cast<T> ( std::min( left.r - right.r, 255 ) ),
288 static_cast<T> ( std::min( left.g - right.g, 255 ) ),
289 static_cast<T> ( std::min( left.b - right.b, 255 ) ),
290 static_cast<T> ( std::min( left.a - right.a, 255 ) )
291 );
292}
293
295inline Color4<float> operator -( const Color4<float>& lhs, const Color4<float>& rhs )
296{
297 return Color4<float> (
298 std::min( ( lhs.r - rhs.r ) / 255, 1.0f ),
299 std::min( ( lhs.g - rhs.g ) / 255, 1.0f ),
300 std::min( ( lhs.b - rhs.b ) / 255, 1.0f ),
301 std::min( ( lhs.a - rhs.a ) / 255, 1.0f )
302 );
303}
304
306template <typename T>
307inline Color4<T> operator -- ( Color4<T>& left )
308{
309 return Color4<T> ( static_cast<T> ( left.r-- ),
310 static_cast<T> ( left.g-- ),
311 static_cast<T> ( left.b-- ),
312 static_cast<T> ( left.a-- )
313 );
314}
315
317template <typename T>
318inline Color4<T> operator * ( const Color4<T>& left, const Color4<T>& right )
319{
320 return Color4<T> (
321 static_cast<T> ( left.r * right.r / 255 ),
322 static_cast<T> ( left.g * right.g / 255 ),
323 static_cast<T> ( left.b * right.b / 255 ),
324 static_cast<T> ( left.a * right.a / 255 )
325 );
326}
327
328template <typename T>
329inline Color4<T>& operator += ( Color4<T>& left, const Color4<T>& right )
330{
331 return left = left + right;
332}
333
334template <typename T>
335inline Color4<T>& operator -= ( Color4<T>& left, const Color4<T>& right )
336{
337 return left = left - right;
338}
339
340template <typename T>
341inline Color4<T>& operator *= ( Color4<T>& left, const Color4<T>& right )
342{
343 return left = left * right;
344}
345
350template <typename T>
351inline bool operator <(const Color4<T> lhs, const Color4<T>& rhs)
352{
353 return (lhs.r < rhs.r) && (lhs.r < rhs.r) &&
354 (lhs.g < rhs.g) && (lhs.g < rhs.g) &&
355 (lhs.b < rhs.b) && (lhs.b < rhs.b) &&
356 (lhs.a < rhs.a) && (lhs.a < rhs.a);
357}
358
363template <typename T>
364inline bool operator >(const Color4<T>& lhs, const Color4<T>& rhs)
365{
366 return (rhs.r < lhs.r) && (rhs.r < lhs.r) &&
367 (rhs.g < lhs.g) && (rhs.g < lhs.g) &&
368 (rhs.b < lhs.b) && (rhs.b < lhs.b) &&
369 (rhs.a < lhs.a) && (rhs.a < lhs.a);
370}
371
376template <typename T>
377inline bool operator <=(const Color4<T>& lhs, const Color4<T>& rhs)
378{
379 return (lhs.r <= rhs.r) && (lhs.r <= rhs.r) &&
380 (lhs.g <= rhs.g) && (lhs.g <= rhs.g) &&
381 (lhs.b <= rhs.b) && (lhs.b <= rhs.b) &&
382 (lhs.a <= rhs.a) && (lhs.a <= rhs.a);
383}
384
389template <typename T>
390inline bool operator >=(const Color4<T>& lhs, const Color4<T>& rhs)
391{
392 return (rhs.r <= lhs.r) && (rhs.r <= lhs.r) &&
393 (rhs.g <= lhs.g) && (rhs.g <= lhs.g) &&
394 (rhs.b <= lhs.b) && (rhs.b <= lhs.b) &&
395 (rhs.a <= lhs.a) && (rhs.a <= lhs.a);
396}
397
406template <typename T>
407inline Color4<T> operator /(const Color4<T>& lhs, const Color4<T>& rhs)
408{
409 return Color4<T>( lhs.r / rhs.r,
410 lhs.g / rhs.g,
411 lhs.b / rhs.b,
412 lhs.a / rhs.a
413 );
414}
415
425template <typename T>
426inline Color4<T>& operator /=(Color4<T>& lhs, Color4<T>& rhs)
427{
428 lhs.r /= rhs.r;
429 lhs.g /= rhs.g;
430 lhs.b /= rhs.b;
431 lhs.a /= rhs.a;
432
433 return lhs;
434}
435
437typedef Color4<int16> Color4i;
438
440typedef Color4<float> Color4f;
441
447typedef Color4<uint8> Color4u;
448
450typedef std::vector<Color4i> Color4iColors;
451
453typedef std::vector<Color4f> Color4fColors;
454
456typedef std::vector<Color4u> Color4uColors;
457
458Color4i
459make_color_from_hex_string(const std::string& hex_encoding);
460
461Color4i
462make_color_from_string(const std::string& color);
463
464} // namespace nom
465
466#endif // include guard defined
RGBA color container.
Definition Color4.hpp:60
static const int16 ALPHA_OPAQUE
Definition Color4.hpp:130
static const int16 ALPHA_TRANSPARENT
Definition Color4.hpp:127
Color4(void)
Default constructor; initialize values to Color<T>::null.
Definition Color4.hpp:62
static const Color4 Transparent
Definition Color4.hpp:141
static const Color4 Silver
Definition Color4.hpp:152
static const Color4 null
Definition Color4.hpp:135
Color4(T red, T green, T blue, T alpha)
Constructor variant for setting a color using RGBA values.
Definition Color4.hpp:117
Color4(const Color4< float > &rhs)
Copy constructor.
Definition Color4.hpp:97
~Color4(void)
Destructor.
Definition Color4.hpp:72
Color4(T r, T g, T b)
Constructor variant for setting a color using RGB values.
Definition Color4.hpp:107
Color4(const Color4< U > &rhs)
Copy constructor.
Definition Color4.hpp:83