TDME2 1.9.121
Color4Base.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4
5#include <tdme/tdme.h>
7#include <tdme/math/Math.h>
8
9using std::array;
10
12
13/**
14 * Color 4 base definition class
15 * @author Andreas Drewke
16 * @version $Id$
17 */
19{
20protected:
21 array<float, 4> data;
22
23public:
24 /**
25 * Public constructor
26 */
27 inline Color4Base() {
28 data[0] = 0.0f;
29 data[1] = 0.0f;
30 data[2] = 0.0f;
31 data[3] = 1.0f;
32 }
33
34 /**
35 * Public constructor
36 * @param color color
37 */
38 inline Color4Base(const Color4Base& color) {
39 data = color.data;
40 }
41
42 /**
43 * Public constructor
44 * @param color color
45 */
46 inline Color4Base(const array<float, 4>& color) {
47 this->data = color;
48 }
49
50 /**
51 * Public constructor
52 * @param r red
53 * @param g green
54 * @param b blue
55 * @param a alpha
56 */
57 inline Color4Base(float r, float g, float b, float a) {
58 data[0] = r;
59 data[1] = g;
60 data[2] = b;
61 data[3] = a;
62 }
63
64 /**
65 * Set up color
66 * @param color color float array
67 */
68 inline void set(const array<float, 4>& color) {
69 this->data = color;
70 }
71
72 /**
73 * Set up color
74 * @param r red
75 * @param g green
76 * @param b blue
77 * @param a alpha
78 */
79 inline void set(float r, float g, float b, float a) {
80 data[0] = r;
81 data[1] = g;
82 data[2] = b;
83 data[3] = a;
84 }
85
86 /**
87 * Sets up this color with given color
88 * @param color color
89 */
90 inline void set(const Color4Base& color) {
91 data = color.data;
92 }
93
94 /**
95 * Add to color
96 * @param r red
97 * @param g green
98 * @param b blue
99 * @param a alpha
100 */
101 inline void add(float r, float g, float b, float a) {
102 data[0] += r;
103 data[1] += g;
104 data[2] += b;
105 data[3] += a;
106 }
107
108 /**
109 * @return red component
110 */
111 inline float getRed() const {
112 return data[0];
113 }
114
115 /**
116 * @param red red component
117 */
118 inline void setRed(float red) {
119 data[0] = red;
120 }
121
122 /**
123 * @return green component
124 */
125 inline float getGreen() const {
126 return data[1];
127 }
128
129 /**
130 * @param green green component
131 */
132 inline void setGreen(float green) {
133 data[1] = green;
134 }
135
136 /**
137 * @return blue component
138 */
139 inline float getBlue() const {
140 return data[2];
141 }
142
143 /**
144 * @param blue blue component
145 */
146 inline void setBlue(float blue) {
147 data[2] = blue;
148 }
149
150 /**
151 * @return alpha component
152 */
153 inline float getAlpha() const {
154 return data[3];
155 }
156
157 /**
158 * @param alpha alpha component
159 */
160 inline void setAlpha(float alpha) {
161 data[3] = alpha;
162 }
163
164 /**
165 * Scale this color
166 * @param scale scale
167 * @return this color
168 */
169 inline Color4Base& scale(float scale) {
170 data[0] *= scale;
171 data[1] *= scale;
172 data[2] *= scale;
173 data[3] *= scale;
174 return *this;
175 }
176
177 /**
178 * Scale this color
179 * @param scale scale
180 * @return this color
181 */
183 data[0] *= scale.data[0];
184 data[1] *= scale.data[1];
185 data[2] *= scale.data[2];
186 data[3] *= scale.data[3];
187 return *this;
188 }
189
190 /**
191 * Add color to this color
192 * @param color color
193 * @return this color
194 */
195 inline Color4Base& add(const Color4Base& color) {
196 data[0] += color.data[0];
197 data[1] += color.data[1];
198 data[2] += color.data[2];
199 data[3] += color.data[3];
200 return *this;
201 }
202
203 /**
204 * Subtracts color from this color
205 * @param color color
206 * @return this color
207 */
208 inline Color4Base& sub(const Color4Base& color) {
209 data[0] -= color.data[0];
210 data[1] -= color.data[1];
211 data[2] -= color.data[2];
212 data[3] -= color.data[3];
213 return *this;
214 }
215
216 /**
217 * @return rgba as float array
218 */
219 inline array<float, 4>& getArray() const {
220 return (array<float, 4>&)data;
221 }
222
223 /**
224 * Array access operator
225 * @param i index
226 * @return vector3 component
227 */
228 inline float& operator[](int i) {
229 return data[i];
230 }
231
232 /**
233 * Const array access operator
234 * @param i index
235 * @return vector3 component
236 */
237 inline const float& operator[](int i) const {
238 return data[i];
239 }
240
241 /**
242 * Compares this color with given color
243 * @return equality
244 */
245 inline bool equals(const Color4Base& c) const {
246 return (this == &c) || (
247 Math::abs(data[0] - c.data[0]) < Math::EPSILON &&
248 Math::abs(data[1] - c.data[1]) < Math::EPSILON &&
249 Math::abs(data[2] - c.data[2]) < Math::EPSILON &&
250 Math::abs(data[3] - c.data[3]) < Math::EPSILON
251 );
252 }
253
254};
Color 4 base definition class.
Definition: Color4Base.h:19
array< float, 4 > & getArray() const
Definition: Color4Base.h:219
void set(const array< float, 4 > &color)
Set up color.
Definition: Color4Base.h:68
void setGreen(float green)
Definition: Color4Base.h:132
Color4Base & sub(const Color4Base &color)
Subtracts color from this color.
Definition: Color4Base.h:208
Color4Base & add(const Color4Base &color)
Add color to this color.
Definition: Color4Base.h:195
void setAlpha(float alpha)
Definition: Color4Base.h:160
Color4Base & scale(const Color4Base &scale)
Scale this color.
Definition: Color4Base.h:182
Color4Base(const Color4Base &color)
Public constructor.
Definition: Color4Base.h:38
Color4Base(const array< float, 4 > &color)
Public constructor.
Definition: Color4Base.h:46
void add(float r, float g, float b, float a)
Add to color.
Definition: Color4Base.h:101
Color4Base(float r, float g, float b, float a)
Public constructor.
Definition: Color4Base.h:57
Color4Base()
Public constructor.
Definition: Color4Base.h:27
const float & operator[](int i) const
Const array access operator.
Definition: Color4Base.h:237
float & operator[](int i)
Array access operator.
Definition: Color4Base.h:228
void set(const Color4Base &color)
Sets up this color with given color.
Definition: Color4Base.h:90
bool equals(const Color4Base &c) const
Compares this color with given color.
Definition: Color4Base.h:245
Color4Base & scale(float scale)
Scale this color.
Definition: Color4Base.h:169
void set(float r, float g, float b, float a)
Set up color.
Definition: Color4Base.h:79
Standard math functions.
Definition: Math.h:21