Color.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. 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 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON 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
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/math/Color.hpp"
30 
31 namespace nom {
32 
33 const Color Color::null ( -1, -1, -1, -1 );
34 
35 const Color Color::Black ( 0, 0, 0 );
36 const Color Color::White ( 255, 255, 255 );
37 const Color Color::Red ( 255, 0, 0 );
38 const Color Color::Green ( 0, 255, 0 );
39 const Color Color::Blue ( 0, 0, 255 );
40 const Color Color::Yellow ( 255, 255, 255 );
41 const Color Color::Magenta ( 255, 0, 255 );
42 const Color Color::Cyan ( 0, 255, 255 );
43 
44 const Color Color::LightGray ( 99, 99, 99 );
45 const Color Color::Gray ( 67, 67, 67 );
46 
47 const Color Color::NomPrimaryColorKey ( 110, 144, 190 );
48 const Color Color::NomSecondaryColorKey ( 223, 75, 255 );
49 
50 Color::Color ( void ) : red ( 0 ), green ( 0 ), blue ( 0 ),
51  alpha ( 255 )
52 {}
53 
54 Color::Color ( uint8 r, uint8 g, uint8 b ) : red ( r ),
55  green ( g ),
56  blue ( b ),
57  alpha ( 255 )
58 {}
59 
60 Color::Color ( uint8 r, uint8 g, uint8 b, uint8 a ): red ( r ),
61  green ( g ),
62  blue ( b ),
63  alpha ( a )
64 {}
65 
66 Color::Color ( const Color& color ) : red ( color.red ),
67  green ( color.green ),
68  blue ( color.blue ),
69  alpha ( color.alpha )
70 {}
71 
72 Color::~Color ( void ) {}
73 
74 bool Color::isNull ( void ) const
75 {
76  if ( *this == Color::null )
77  {
78  return true;
79  }
80 
81  return false;
82 }
83 
84 const Color& Color::get ( void ) const
85 {
86  return *this;
87 }
88 
89 Color& Color::operator = ( const Color& other )
90 {
91  this->red = other.red;
92  this->green = other.green;
93  this->blue = other.blue;
94  this->alpha = other.alpha;
95 
96  return *this;
97 }
98 
99 std::ostream& operator << ( std::ostream& os, const Color& color )
100 {
101  os << static_cast<int> ( color.red ) << color_delimiter
102  << static_cast<int> ( color.green) << color_delimiter
103  << static_cast<int> ( color.blue ) << color_delimiter
104  << static_cast<int> ( color.alpha );
105  return os;
106 }
107 
108 bool operator == ( const Color& left, const Color& right )
109 {
110  return (left.red == right.red ) &&
111  (left.green == right.green ) &&
112  (left.blue == right.blue ) &&
113  (left.alpha == right.alpha );
114 }
115 
116 bool operator != ( const Color& left, const Color& right )
117 {
118  return ! ( left == right );
119 }
120 
121 Color operator + ( const Color& left, const Color& right )
122 {
123  return Color ( static_cast<uint8> ( std::min ( left.red + right.red, 255 ) ),
124  static_cast<uint8> ( std::min ( left.green + right.green, 255 ) ),
125  static_cast<uint8> ( std::min ( left.blue + right.blue, 255 ) ),
126  static_cast<uint8> ( std::min ( left.alpha + right.alpha, 255 ) )
127  );
128 }
129 
130 Color operator ++ ( Color& left )
131 {
132  return Color ( static_cast<uint8> ( left.red-- ),
133  static_cast<uint8> ( left.green-- ),
134  static_cast<uint8> ( left.blue-- ),
135  static_cast<uint8> ( left.alpha-- )
136  );
137 }
138 
139 Color operator - ( const Color& left, const Color& right )
140 {
141  return Color ( static_cast<uint8> ( std::min ( left.red - right.red, 255 ) ),
142  static_cast<uint8> ( std::min ( left.green - right.green, 255 ) ),
143  static_cast<uint8> ( std::min ( left.blue - right.blue, 255 ) ),
144  static_cast<uint8> ( std::min ( left.alpha - right.alpha, 255 ) )
145  );
146 }
147 
148 Color operator -- ( Color& left )
149 {
150  return Color ( static_cast<uint8> ( left.red-- ),
151  static_cast<uint8> ( left.green-- ),
152  static_cast<uint8> ( left.blue-- ),
153  static_cast<uint8> ( left.alpha-- )
154  );
155 }
156 
157 Color operator * ( const Color& left, const Color& right )
158 {
159  return Color ( static_cast<uint8> ( left.red * right.red / 255 ),
160  static_cast<uint8> ( left.green * right.green / 255 ),
161  static_cast<uint8> ( left.blue * right.blue / 255 ),
162  static_cast<uint8> ( left.alpha * right.alpha / 255 )
163  );
164 }
165 
166 Color& operator += ( Color& left, const Color& right )
167 {
168  return left = left + right;
169 }
170 
171 Color& operator -= ( Color& left, const Color& right )
172 {
173  return left = left - right;
174 }
175 
176 Color& operator *= ( Color& left, const Color& right )
177 {
178  return left = left * right;
179 }
180 
181 
182 } // namespace nom
static const Color NomSecondaryColorKey
Light magenta color key.
Definition: Color.hpp:94
static const Color NomPrimaryColorKey
Sky blue color key.
Definition: Color.hpp:91
Color & operator=(const Color &other)
Copy assignment operator.
Definition: Color.cpp:89
static const Color Black
Predefined color constants for convenience sake.
Definition: Color.hpp:77
static const Color null
Convenience object that will always contain a value of -1.
Definition: Color.hpp:74
bool isNull(void) const
Definition: Color.cpp:74
Color(void)
Default constructor; sets the color to their respective defaults.
Definition: Color.cpp:50
static const Color LightGray
Additional predefined color constants.
Definition: Color.hpp:87
const Color & get(void) const
Convenience getter for obtaining a copy of this object.
Definition: Color.cpp:84
~Color(void)
Destructor.
Definition: Color.cpp:72
Utility class container for RGBA colors.
Definition: Color.hpp:43