Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Color.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Color.h"
22 
23 using namespace HFCore;
24 
25 #include "debug/CrtDbgNew.h"
26 
28  mRed(0),
29  mGreen(0),
30  mBlue(0),
31  mAlpha(255)
32 {
33 }
34 
36  mRed(r),
37  mGreen(g),
38  mBlue(b),
39  mAlpha(0xFF)
40 {
41 }
42 
44  mRed(r),
45  mGreen(g),
46  mBlue(b),
47  mAlpha(a)
48 {
49 }
50 
52  mRed((int32_hf)r),
53  mGreen((int32_hf)g),
54  mBlue((int32_hf)b),
55  mAlpha(0xFF)
56 {
57 }
58 
60  mRed((int32_hf)r),
61  mGreen((int32_hf)g),
62  mBlue((int32_hf)b),
63  mAlpha((int32_hf)a)
64 {
65 }
66 
67 Color::Color(const glm::vec3 &color) :
68  mRed((int32_hf)color.r),
69  mGreen((int32_hf)color.g),
70  mBlue((int32_hf)color.b),
71  mAlpha(0xFF)
72 {
73 }
74 
75 Color::Color(const glm::vec4 &color) :
76  mRed((int32_hf)color.r),
77  mGreen((int32_hf)color.g),
78  mBlue((int32_hf)color.b),
79  mAlpha((int32_hf)color.a)
80 {
81 }
82 
83 Color::Color(const HFRGBA &color) :
84  mRed(color.r),
85  mGreen(color.g),
86  mBlue(color.b),
87  mAlpha(color.a)
88 {
89 }
90 
92 {
93  return mRed;
94 }
95 
97 {
98  return mGreen;
99 }
100 
102 {
103  return mBlue;
104 }
105 
107 {
108  return mAlpha;
109 }
110 
112 {
113  return (fp32_hf)mRed;
114 }
115 
117 {
118  return (fp32_hf)mGreen;
119 }
120 
122 {
123  return (fp32_hf)mBlue;
124 }
125 
127 {
128  return (fp32_hf)mAlpha;
129 }
130 
132 {
133 #if defined(HF_PLATFORM_WINDOWS_D3D9)
134  return toUint32_LEBGRA();
135 #else
136  return toUint32_LERGBA();
137 #endif
138 }
139 
141 {
142  static int32_hf junk = 0;
143 
144  switch (idx)
145  {
146  case 0:
147  return mRed;
148  case 1:
149  return mGreen;
150  case 2:
151  return mBlue;
152  case 3:
153  return mAlpha;
154  default:
155  return junk;
156  }
157 }
158 
160 {
161  switch (idx)
162  {
163  case 0:
164  return mRed;
165  case 1:
166  return mGreen;
167  case 2:
168  return mBlue;
169  case 3:
170  return mAlpha;
171  default:
172  return 0;
173  }
174 }
175 
177 {
178  HFRGBA c = toRGBA();
179 
180  uint32_hf color = (c.a << 24) | (c.b << 16) | (c.g << 8) | (c.r);
181  return color;
182 }
183 
185 {
186  HFRGBA c = toRGBA();
187 
188  uint32_hf color = (c.a << 24) | (c.r << 16) | (c.g << 8) | (c.b);
189  return color;
190 }
191 
193 {
194  HFRGBA rgba;
195  rgba.r = (mRed & 0xFF);
196  rgba.g = (mGreen & 0xFF);
197  rgba.b = (mBlue & 0xFF);
198  rgba.a = (mAlpha & 0xFF);
199 
200  return rgba;
201 }
202 
203 glm::vec3 Color::toVec3() const
204 {
205  return glm::vec3(getRedf(), getGreenf(), getBluef());
206 }
207 
208 glm::vec4 Color::toVec4() const
209 {
210  return glm::vec4(getRedf(), getGreenf(), getBluef(), getAlphaf());
211 }
212 
214 {
215  HFRGBA c;
216  c.r = (color & 0xFF);
217  c.g = ((color >> 8) & 0xFF);
218  c.b = ((color >> 16) & 0xFF);
219  c.a = ((color >> 24) & 0xFF);
220  return Color(c);
221 }
222 
224 {
225  HFRGBA c;
226  c.r = ((color >> 16) & 0xFF);
227  c.g = ((color >> 8) & 0xFF);
228  c.b = (color & 0xFF);
229  c.a = ((color >> 24) & 0xFF);
230  return Color(c);
231 }
232 
233 Color Color::fromVec3(const glm::vec3 &color)
234 {
235  return Color(color.r, color.g, color.b);
236 }
237 
238 Color Color::fromVec4(const glm::vec4 &color)
239 {
240  return Color(color.r, color.g, color.b, color.a);
241 }
242 
244 {
245  return Color(r, g, b);
246 }
247 
249 {
250  return Color(r, g, b, a);
251 }
252 
254 {
255  return Color(r, g, b);
256 }
257 
259 {
260  return Color(r, g, b, a);
261 }
262 
263 bool Color::operator==(const Color &c)
264 {
265  return
266  (mRed == c.mRed) &&
267  (mGreen == c.mGreen) &&
268  (mBlue == c.mBlue) &&
269  (mAlpha == c.mAlpha);
270 }
271 
272 bool Color::operator!=(const Color &c)
273 {
274  return
275  (mRed != c.mRed) ||
276  (mGreen != c.mGreen) ||
277  (mBlue != c.mBlue) ||
278  (mAlpha != c.mAlpha);
279 }
280 
282 {
283  return Color(0, 0, 0, 255);
284 }
285 
287 {
288  return Color(0, 0, 255, 255);
289 }
290 
292 {
293  return Color(165, 42, 42, 255);
294 }
295 
297 {
298  return Color(0, 255, 255, 255);
299 }
300 
302 {
303  return Color(0, 128, 0, 255);
304 }
305 
307 {
308  return Color(128, 128, 128, 255);
309 }
310 
312 {
313  return Color(255, 165, 0, 255);
314 }
315 
317 {
318  return Color(255, 192, 203, 255);
319 }
320 
322 {
323  return Color(128, 0, 128, 255);
324 }
325 
327 {
328  return Color(255, 0, 0, 255);
329 }
330 
332 {
333  return Color(255, 255, 255, 255);
334 }
335 
337 {
338  return Color(255, 255, 0, 255);
339 }
fp32_hf getRedf() const
Get red color component as fp32_hf.
Definition: Color.cpp:111
unsigned int uint32_hf
A type definition for unsigned int.
Definition: HFDataTypes.h:354
int32_hf mAlpha
Alpha color component.
Definition: Color.h:559
uint8_hf b
blue color component in the range 0-255
Definition: Color.h:61
fp32_hf getAlphaf() const
Get alpha color component as fp32_hf.
Definition: Color.cpp:126
int32_hf mBlue
Blue color component.
Definition: Color.h:554
fp32_hf getGreenf() const
Get green color component as fp32_hf.
Definition: Color.cpp:116
static Color fromUint32_LEBGRA(uint32_hf color)
Unpack uint32_hf Little Endian BGRA and return as Color.
Definition: Color.cpp:223
static Color getColorYellow()
Get opaque Yellow Color.
Definition: Color.cpp:336
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
static Color getColorCyan()
Get opaque Cyan Color.
Definition: Color.cpp:296
uint8_hf g
green color component in the range 0-255
Definition: Color.h:60
static Color getColorBlack()
Get opaque Black Color.
Definition: Color.cpp:281
static Color getColorPurple()
Get opaque Purple Color.
Definition: Color.cpp:321
bool operator!=(const Color &c)
Overload of binary operator !=.
Definition: Color.cpp:272
static Color fromVec3(const glm::vec3 &color)
Get Color from vector.
Definition: Color.cpp:233
static Color getColorRed()
Get opaque Red Color.
Definition: Color.cpp:326
uint32_hf toUint32_LEBGRA() const
Get color packed in uint32_hf Little Endian BGRA format.
Definition: Color.cpp:184
static Color getColorBlue()
Get opaque Blue Color.
Definition: Color.cpp:286
glm::vec4 toVec4() const
Get Vector holding RGBA color components.
Definition: Color.cpp:208
Struct holding individual rgba components.
Definition: Color.h:57
int32_hf getRed() const
Get red color component as int32_hf.
Definition: Color.cpp:91
static Color getColorGreen()
Get opaque Green Color.
Definition: Color.cpp:301
static Color fromFloatRGB(fp32_hf r, fp32_hf g, fp32_hf b)
Get Color from RGB color components.
Definition: Color.cpp:253
static Color fromIntRGBA(int32_hf r, int32_hf g, int32_hf b, int32_hf a)
Get Color from RGBA color components.
Definition: Color.cpp:248
Color()
Constructor which inits to Color(0, 0, 0, 255) which is opaque Black.
Definition: Color.cpp:27
bool operator==(const Color &c)
Overload of binary operator ==.
Definition: Color.cpp:263
Interface for RGBA Color.
Definition: Color.h:51
fp32_hf getBluef() const
Get blue color component as fp32_hf.
Definition: Color.cpp:121
int32_hf & operator[](int32_hf idx)
Overload of binary &operator [].
Definition: Color.cpp:140
int32_hf getAlpha() const
Get alpha color component as int32_hf.
Definition: Color.cpp:106
static Color getColorGrey()
Get opaque Grey Color.
Definition: Color.cpp:306
HFRGBA toRGBA() const
Retrieve Color::HFRGBA struct filled with uint8_hf r, g, b, a color components.
Definition: Color.cpp:192
static Color fromFloatRGBA(fp32_hf r, fp32_hf g, fp32_hf b, fp32_hf a)
Get Color from RGBA color components.
Definition: Color.cpp:258
uint32_hf getVertexColorLE() const
Get Vertex color Little Endian packed format (GL: RGBA D3D9: BGRA or also referred to as ARGB)...
Definition: Color.cpp:131
Interface for RGBA Color.
Definition: Actor.h:34
int32_hf mGreen
Green color component.
Definition: Color.h:549
static Color getColorWhite()
Get opaque White Color.
Definition: Color.cpp:331
static Color getColorBrown()
Get opaque Brown Color.
Definition: Color.cpp:291
static Color fromIntRGB(int32_hf r, int32_hf g, int32_hf b)
Get Color from RGB color components.
Definition: Color.cpp:243
glm::vec3 toVec3() const
Get Vector holding RGB color components.
Definition: Color.cpp:203
uint8_hf a
alpha color component in the range 0-255
Definition: Color.h:62
int32_hf mRed
Red color component.
Definition: Color.h:544
static Color fromUint32_LERGBA(uint32_hf color)
Unpack uint32_hf Little Endian RGBA and return as Color.
Definition: Color.cpp:213
static Color getColorPink()
Get opaque Pink Color.
Definition: Color.cpp:316
static Color getColorOrange()
Get opaque Orange Color.
Definition: Color.cpp:311
uint32_hf toUint32_LERGBA() const
Get color packed in uint32_hf Little Endian RGBA format.
Definition: Color.cpp:176
static Color fromVec4(const glm::vec4 &color)
Get Color from vector.
Definition: Color.cpp:238
uint8_hf r
red color component in the range 0-255
Definition: Color.h:59
int32_hf getBlue() const
Get blue color component as int32_hf.
Definition: Color.cpp:101
float fp32_hf
A type definition for float.
Definition: HFDataTypes.h:359
int32_hf getGreen() const
Get green color component as int32_hf.
Definition: Color.cpp:96