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 "export.hpp"
36#include "nomlib/config.hpp"
37
38// FIXME: The following declaration is necessary in order to avoid a very
39// nasty compiling conflict that can happen under Windows anytime the
40// windef.h header file is included (commonly from windows.h), due to min and
41// max macros being declared there. This is why macros are evil.
42//
43// http://support.microsoft.com/kb/143208
44// http://stackoverflow.com/questions/5004858/stdmin-gives-error
45#if defined( NOM_PLATFORM_WINDOWS )
46 #undef min
47 #undef max
48#endif
49
50namespace nom {
51
53const std::string COLOR_DELIMITER = ", ";
54
59template <typename T>
60struct NOM_EXPORT Color4
61{
63 Color4 ( void ) :
64 r ( -1 ),
65 g ( -1 ),
66 b ( -1 ),
67 a ( Color4<T>::ALPHA_OPAQUE )
68 {
69 //NOM_LOG_TRACE(NOM);
70 }
71
73 ~Color4 ( void )
74 {
75 //NOM_LOG_TRACE(NOM);
76 }
77
83 template <typename U>
84 Color4( const Color4<U>& rhs ) :
85 r{ static_cast<T> ( rhs.r ) },
86 g{ static_cast<T> ( rhs.g ) },
87 b{ static_cast<T> ( rhs.b ) },
88 a{ static_cast<T> ( rhs.a ) }
89 {
90 //NOM_LOG_TRACE(NOM);
91 }
92
98 explicit Color4( const Color4<float>& rhs ) :
99 r{ static_cast<float> ( rhs.r ) },
100 g{ static_cast<float> ( rhs.g ) },
101 b{ static_cast<float> ( rhs.b ) },
102 a{ static_cast<float> ( rhs.a ) }
103 {
104 //NOM_LOG_TRACE(NOM);
105 }
106
108 Color4 ( T r, T g, T b ) :
109 r ( r ),
110 g ( g ),
111 b ( b ),
112 a ( Color4<T>::ALPHA_OPAQUE )
113 {
114 //NOM_LOG_TRACE(NOM);
115 }
116
118 Color4 ( T red, T green, T blue, T alpha ) :
119 r ( red ),
120 g ( green ),
121 b ( blue ),
122 a ( alpha )
123 {
124 //NOM_LOG_TRACE(NOM);
125 }
126
128 NOM_EXPORT static const T ALPHA_TRANSPARENT;
129
131 NOM_EXPORT static const T ALPHA_OPAQUE;
132
136 NOM_EXPORT static const Color4 null;
137
142 NOM_EXPORT static const Color4 Transparent;
143 NOM_EXPORT static const Color4 Black;
144 NOM_EXPORT static const Color4 White;
145 NOM_EXPORT static const Color4 Red;
146 NOM_EXPORT static const Color4 Green;
147 NOM_EXPORT static const Color4 Blue;
148 NOM_EXPORT static const Color4 Yellow;
149 NOM_EXPORT static const Color4 Magenta;
150 NOM_EXPORT static const Color4 Cyan;
151
153 NOM_EXPORT static const Color4 Silver;
154 NOM_EXPORT static const Color4 Purple;
155 NOM_EXPORT static const Color4 Orange;
156 NOM_EXPORT static const Color4 LightGray;
157 NOM_EXPORT static const Color4 Gray;
158 NOM_EXPORT static const Color4 SkyBlue;
159
160 // static const Color4 ButtonGradient;
161
163 NOM_EXPORT T r;
164
166 NOM_EXPORT T g;
167
169 NOM_EXPORT T b;
170
172 NOM_EXPORT T a;
173};
174
178template <typename T>
179NOM_EXPORT inline
180Color4<T> difference_color_blend( const Color4<T>& lhs, const Color4<T>& rhs )
181{
182 Color4<T> c;
183 c.r = abs( lhs.r - rhs.r );
184 c.g = abs( lhs.g - rhs.g );
185 c.b = abs( lhs.b - rhs.b );
186
187 return c;
188}
189
199template <typename T>
200NOM_EXPORT inline
201std::ostream& operator << ( std::ostream& os, const Color4<T>& color )
202{
203 os
204 << static_cast<sint> ( color.r )
205 << COLOR_DELIMITER
206 << static_cast<sint> ( color.g )
207 << COLOR_DELIMITER
208 << static_cast<sint> ( color.b )
209 << COLOR_DELIMITER
210 << static_cast<sint> ( color.a );
211
212 return os;
213}
214
221NOM_EXPORT inline
222std::ostream& operator <<( std::ostream& os, const Color4<float>& color )
223{
224 os
225 << static_cast<float> ( color.r )
226 << COLOR_DELIMITER
227 << static_cast<float> ( color.g )
228 << COLOR_DELIMITER
229 << static_cast<float> ( color.b )
230 << COLOR_DELIMITER
231 << static_cast<float> ( color.a );
232
233 return os;
234}
235
237template <typename T>
238NOM_EXPORT inline
239bool operator == ( const Color4<T>& left, const Color4<T>& right )
240{
241 return ( left.r == right.r ) &&
242 ( left.g == right.g ) &&
243 ( left.b == right.b ) &&
244 ( left.a == right.a );
245}
246
248template <typename T>
249NOM_EXPORT inline
250bool operator != ( const Color4<T>& left, const Color4<T>& right )
251{
252 return ! ( left == right );
253}
254
256template <typename T>
257NOM_EXPORT inline
258Color4<T> operator + ( const Color4<T>& left, const Color4<T>& right )
259{
260 return Color4<T> ( static_cast<T> ( std::min ( left.r + right.r, 255 ) ),
261 static_cast<T> ( std::min ( left.g + right.g, 255 ) ),
262 static_cast<T> ( std::min ( left.b + right.b, 255 ) ),
263 static_cast<T> ( std::min ( left.a + right.a, 255 ) )
264 );
265}
266
268NOM_EXPORT inline
269Color4<float> operator +( const Color4<float>& lhs, const Color4<float>& rhs )
270{
271 return Color4<float> (
272 std::min( ( lhs.r + rhs.r / 255 ), 1.0f ),
273 std::min( ( lhs.g + rhs.g / 255 ), 1.0f ),
274 std::min( ( lhs.b + rhs.b / 255 ), 1.0f ),
275 std::min( ( lhs.a + rhs.a / 255 ), 1.0f )
276 );
277}
278
280template <typename T>
281NOM_EXPORT inline Color4<T> operator ++ ( Color4<T>& left )
282{
283 return Color4<T> ( static_cast<T> ( left.r-- ),
284 static_cast<T> ( left.g-- ),
285 static_cast<T> ( left.b-- ),
286 static_cast<T> ( left.a-- )
287 );
288}
289
290template <typename T>
292NOM_EXPORT inline
293Color4<T> operator - ( const Color4<T>& left, const Color4<T>& right )
294{
295 return Color4<T> (
296 static_cast<T> ( std::min( left.r - right.r, 255 ) ),
297 static_cast<T> ( std::min( left.g - right.g, 255 ) ),
298 static_cast<T> ( std::min( left.b - right.b, 255 ) ),
299 static_cast<T> ( std::min( left.a - right.a, 255 ) )
300 );
301}
302
304NOM_EXPORT inline
305Color4<float> operator -( const Color4<float>& lhs, const Color4<float>& rhs )
306{
307 return Color4<float> (
308 std::min( ( lhs.r - rhs.r ) / 255, 1.0f ),
309 std::min( ( lhs.g - rhs.g ) / 255, 1.0f ),
310 std::min( ( lhs.b - rhs.b ) / 255, 1.0f ),
311 std::min( ( lhs.a - rhs.a ) / 255, 1.0f )
312 );
313}
314
316template <typename T>
317NOM_EXPORT inline Color4<T> operator -- ( Color4<T>& left )
318{
319 return Color4<T> ( static_cast<T> ( left.r-- ),
320 static_cast<T> ( left.g-- ),
321 static_cast<T> ( left.b-- ),
322 static_cast<T> ( left.a-- )
323 );
324}
325
327template <typename T>
328NOM_EXPORT inline
329Color4<T> operator * ( const Color4<T>& left, const Color4<T>& right )
330{
331 return Color4<T> (
332 static_cast<T> ( left.r * right.r / 255 ),
333 static_cast<T> ( left.g * right.g / 255 ),
334 static_cast<T> ( left.b * right.b / 255 ),
335 static_cast<T> ( left.a * right.a / 255 )
336 );
337}
338
339template <typename T>
340NOM_EXPORT inline
341Color4<T>& operator += ( Color4<T>& left, const Color4<T>& right )
342{
343 return left = left + right;
344}
345
346template <typename T>
347NOM_EXPORT inline
348Color4<T>& operator -= ( Color4<T>& left, const Color4<T>& right )
349{
350 return left = left - right;
351}
352
353template <typename T>
354NOM_EXPORT inline
355Color4<T>& operator *= ( Color4<T>& left, const Color4<T>& right )
356{
357 return left = left * right;
358}
359
364template <typename T>
365NOM_EXPORT inline
366bool operator <(const Color4<T> lhs, const Color4<T>& rhs)
367{
368 return (lhs.r < rhs.r) && (lhs.r < rhs.r) &&
369 (lhs.g < rhs.g) && (lhs.g < rhs.g) &&
370 (lhs.b < rhs.b) && (lhs.b < rhs.b) &&
371 (lhs.a < rhs.a) && (lhs.a < rhs.a);
372}
373
378template <typename T>
379NOM_EXPORT inline
380bool operator >(const Color4<T>& lhs, const Color4<T>& rhs)
381{
382 return (rhs.r < lhs.r) && (rhs.r < lhs.r) &&
383 (rhs.g < lhs.g) && (rhs.g < lhs.g) &&
384 (rhs.b < lhs.b) && (rhs.b < lhs.b) &&
385 (rhs.a < lhs.a) && (rhs.a < lhs.a);
386}
387
392template <typename T>
393NOM_EXPORT inline
394bool operator <=(const Color4<T>& lhs, const Color4<T>& rhs)
395{
396 return (lhs.r <= rhs.r) && (lhs.r <= rhs.r) &&
397 (lhs.g <= rhs.g) && (lhs.g <= rhs.g) &&
398 (lhs.b <= rhs.b) && (lhs.b <= rhs.b) &&
399 (lhs.a <= rhs.a) && (lhs.a <= rhs.a);
400}
401
406template <typename T>
407NOM_EXPORT inline
408bool operator >=(const Color4<T>& lhs, const Color4<T>& rhs)
409{
410 return (rhs.r <= lhs.r) && (rhs.r <= lhs.r) &&
411 (rhs.g <= lhs.g) && (rhs.g <= lhs.g) &&
412 (rhs.b <= lhs.b) && (rhs.b <= lhs.b) &&
413 (rhs.a <= lhs.a) && (rhs.a <= lhs.a);
414}
415
424template <typename T>
425NOM_EXPORT inline
426Color4<T> operator /(const Color4<T>& lhs, const Color4<T>& rhs)
427{
428 return Color4<T>( lhs.r / rhs.r,
429 lhs.g / rhs.g,
430 lhs.b / rhs.b,
431 lhs.a / rhs.a
432 );
433}
434
444template <typename T>
445NOM_EXPORT
446inline Color4<T>& operator /=(Color4<T>& lhs, Color4<T>& rhs)
447{
448 lhs.r /= rhs.r;
449 lhs.g /= rhs.g;
450 lhs.b /= rhs.b;
451 lhs.a /= rhs.a;
452
453 return lhs;
454}
455
457typedef Color4<int16> Color4i;
458
460typedef Color4<float> Color4f;
461
467typedef Color4<uint8> Color4u;
468
470typedef std::vector<Color4i> Color4iColors;
471
473typedef std::vector<Color4f> Color4fColors;
474
476typedef std::vector<Color4u> Color4uColors;
477
478template <> NOM_EXPORT const uint8 Color4u::ALPHA_TRANSPARENT;
479template <> NOM_EXPORT const uint8 Color4u::ALPHA_OPAQUE;
480template <> NOM_EXPORT const int16 Color4i::ALPHA_TRANSPARENT;
481template <> NOM_EXPORT const int16 Color4i::ALPHA_OPAQUE;
482template <> NOM_EXPORT const float Color4f::ALPHA_TRANSPARENT;
483template <> NOM_EXPORT const float Color4f::ALPHA_OPAQUE;
484template <> NOM_EXPORT const Color4i Color4i::null;
486template <> NOM_EXPORT const Color4f Color4f::null;
487template <> NOM_EXPORT const Color4i Color4i::Transparent;
488template <> NOM_EXPORT const Color4i Color4i::Black;
489template <> NOM_EXPORT const Color4i Color4i::White;
490template <> NOM_EXPORT const Color4i Color4i::Red;
491template <> NOM_EXPORT const Color4i Color4i::Green;
492template <> NOM_EXPORT const Color4i Color4i::Blue;
493template <> NOM_EXPORT const Color4i Color4i::Yellow;
494template <> NOM_EXPORT const Color4i Color4i::Magenta;
495template <> NOM_EXPORT const Color4i Color4i::Cyan;
496template <> NOM_EXPORT const Color4i Color4i::Silver;
497template <> NOM_EXPORT const Color4i Color4i::Purple;
498template <> NOM_EXPORT const Color4i Color4i::Orange;
499template <> NOM_EXPORT const Color4i Color4i::LightGray;
500template <> NOM_EXPORT const Color4i Color4i::Gray;
501template <> NOM_EXPORT const Color4i Color4i::SkyBlue;
502
503template <> NOM_EXPORT const Color4f Color4f::Transparent;
504template <> NOM_EXPORT const Color4f Color4f::Black;
505template <> NOM_EXPORT const Color4f Color4f::White;
506template <> NOM_EXPORT const Color4f Color4f::Red;
507template <> NOM_EXPORT const Color4f Color4f::Green;
508template <> NOM_EXPORT const Color4f Color4f::Blue;
509template <> NOM_EXPORT const Color4f Color4f::Yellow;
510template <> NOM_EXPORT const Color4f Color4f::Magenta;
511template <> NOM_EXPORT const Color4f Color4f::Cyan;
512
513NOM_EXPORT Color4i
514make_color_from_hex_string(const std::string& hex_encoding);
515
516NOM_EXPORT Color4i
517make_color_from_string(const std::string& color);
518
519} // namespace nom
520
521#endif // include guard defined
RGBA color container.
Definition Color4.hpp:61
NOM_EXPORT int16 a
Definition Color4.hpp:172
static NOM_EXPORT const Color4 null
Definition Color4.hpp:136
NOM_EXPORT int16 g
Definition Color4.hpp:166
Color4(void)
Default constructor; initialize values to Color<T>::null.
Definition Color4.hpp:63
static NOM_EXPORT const Color4 Silver
Definition Color4.hpp:153
NOM_EXPORT int16 r
Definition Color4.hpp:163
static NOM_EXPORT const int16 ALPHA_TRANSPARENT
Definition Color4.hpp:128
Color4(T red, T green, T blue, T alpha)
Constructor variant for setting a color using RGBA values.
Definition Color4.hpp:118
static NOM_EXPORT const int16 ALPHA_OPAQUE
Definition Color4.hpp:131
NOM_EXPORT int16 b
Definition Color4.hpp:169
static NOM_EXPORT const Color4 Transparent
Definition Color4.hpp:142
Color4(const Color4< float > &rhs)
Copy constructor.
Definition Color4.hpp:98
~Color4(void)
Destructor.
Definition Color4.hpp:73
Color4(T r, T g, T b)
Constructor variant for setting a color using RGB values.
Definition Color4.hpp:108
Color4(const Color4< U > &rhs)
Copy constructor.
Definition Color4.hpp:84