itom
Loading...
Searching...
No Matches
color.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom and its software development toolkit (SDK).
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 In addition, as a special exception, the Institut für Technische
15 Optik (ITO) gives you certain additional rights.
16 These rights are described in the ITO LGPL Exception version 1.0,
17 which can be found in the file LGPL_EXCEPTION.txt in this package.
18
19 itom is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22 General Public Licence for more details.
23
24 You should have received a copy of the GNU Library General Public License
25 along with itom. If not, see <http://www.gnu.org/licenses/>.
26*********************************************************************** */
27
28#ifndef COLOR_H
29#define COLOR_H
30
31#include "typeDefs.h"
32
33namespace ito
34{
35
37
46 class Rgba32 : public RgbaBase32
47 {
48 public:
49
51 {
52 RGBA_B = 0,
53 RGBA_G = 1,
54 RGBA_R = 2,
55 RGBA_A = 3,
56 RGBA_Y = 4,
57 RGBA_RGB = 5
58 };
59
61 {
62 //memset(m_value, 0, 4*sizeof(ito::uint8));
63 }
64
65 static Rgba32 zeros()
66 {
67 Rgba32 temp;
68 temp.rgba = 0;
69 return temp;
70 }
71
72 static Rgba32 black()
73 {
74 Rgba32 temp(255, 0,0,0);
75 return temp;
76 }
77
79
82 static Rgba32 fromUnsignedLong(const uint32 val)
83 {
84 Rgba32 temp;
85 memcpy(temp.u8ptr(), &val, 4*sizeof(ito::uint8));
86 return temp;
87 }
88
89 Rgba32(const uint8 &a, const uint8 &r, const uint8 &g, const uint8 &b)
90 {
91 this->b = b;
92 this->g = g;
93 this->r = r;
94 this->a = a;
95 }
96
98
101 explicit Rgba32(const uint8 gray)
102 {
103 a = 0xFF;
104 r = gray;
105 b = gray;
106 g = gray;
107 }
108
109 Rgba32(const Rgba32 &rhs)
110 {
111 rgba = rhs.rgba;
112 };
113
115 {
116 b = static_cast<uint8>(std::min<int16>(b + rhs.b, 255));
117 g = static_cast<uint8>(std::min<int16>(g + rhs.g, 255));
118 r = static_cast<uint8>(std::min<int16>(r + rhs.r, 255));
119 a = static_cast<uint8>(std::min<int16>(a + rhs.a, 255));
120 return *this;
121 }
122
124 {
125 rgba = rhs.rgba;
126 return *this;
127 }
128
129 Rgba32& operator =(const uint32 &rhs)
130 {
131 rgba = rhs;
132 return *this;
133 }
134
136 {
137 b = static_cast<uint8>(std::max<int16>(b - rhs.b, 0));
138 g = static_cast<uint8>(std::max<int16>(g - rhs.g, 0));
139 r = static_cast<uint8>(std::max<int16>(r - rhs.r, 0));
140 a = static_cast<uint8>(std::max<int16>(a - rhs.a, 0));
141 return *this;
142 }
143
145
149 {
150 b = static_cast<uint8>(b * rhs.b / 255);
151 g = static_cast<uint8>(g * rhs.g / 255);
152 r = static_cast<uint8>(r * rhs.r / 255);
153 a = static_cast<uint8>(a * rhs.a / 255);
154 return *this;
155 }
156
158
162 Rgba32& operator *=(const ito::float32 &grayFactor)
163 {
164 if (grayFactor < 0.0)
165 {
166 throw std::runtime_error("Multiplication factor must be >= 0.0");
167 }
168 unsigned int t = static_cast<unsigned int>(b * grayFactor);
169 b = static_cast<uint8>(t <= 255 ? t : 255);
170 t = static_cast<unsigned int>(g * grayFactor);
171 g = static_cast<uint8>(t <= 255 ? t : 255);
172 t = static_cast<unsigned int>(r * grayFactor);
173 r = static_cast<uint8>(t <= 255 ? t : 255);
174 return *this;
175 }
176
178 {
179 if(rhs.b == 0 || rhs.g == 0 || rhs.r == 0 || rhs.a == 0)
180 {
181 throw std::runtime_error("Division by zero not allowed for rgba32-values");
182 }
183 b = static_cast<uint8>(std::min<int16>((b * (ito::int16)255) / rhs.b, 255));
184 g = static_cast<uint8>(std::min<int16>((g * (ito::int16)255) / rhs.g, 255));
185 r = static_cast<uint8>(std::min<int16>((r * (ito::int16)255) / rhs.r, 255));
186 a = static_cast<uint8>(std::min<int16>((a * (ito::int16)255) / rhs.a, 255));
187 return *this;
188 }
189
190 Rgba32 operator +(const Rgba32 &second) const
191 {
192 Rgba32 first(*this);
193 first += second;
194 return first;
195 }
196
197 Rgba32 operator -(const Rgba32 &second) const
198 {
199 Rgba32 first(*this);
200 first -= second;
201 return first;
202 }
203
204 Rgba32 operator *(const Rgba32 &second) const
205 {
206 Rgba32 first(*this);
207 first *= second;
208 return first;
209 }
210
211 Rgba32 operator *(const ito::float32 &second) const
212 {
213 Rgba32 first(*this);
214 first *= second;
215 return first;
216 }
217
218 Rgba32 operator /(const Rgba32 &second) const
219 {
220 Rgba32 first(*this);
221 first /= second;
222 return first;
223 }
224
225 bool operator ==(const Rgba32 &rhs) const
226 {
227 return (b == rhs.b) && (g == rhs.g) && (r == rhs.r && (a == rhs.a));
228 }
229
230 bool operator !=(const Rgba32 &rhs) const
231 {
232 return (b != rhs.b) || (g != rhs.g) || (r != rhs.r || (a != rhs.a));
233 }
234
235 inline float32 gray() const
236 {
237 return static_cast<float32>(0.299 * r + 0.587 * g + 0.114 * b);
238 }
239
240 uint8& alpha() {return a;};
241 uint8& red() {return r;};
242 uint8& green() {return g;};
243 uint8& blue() {return b;};
245 uint8 alpha() const {return a;};
246 uint8 red() const {return r;};
247 uint8 green() const {return g;};
248 uint8 blue() const {return b;};
250 uint32& argb() {return rgba;};
251 uint32 argb() const {return rgba;};
253 uint32* u32ptr() {return (&rgba);}
254 uint8* u8ptr() {return ((uint8*)(&rgba));}
255 };
256
257
258 template<uint8 _COLOR> class RGBChannel : public Rgba32
259 {
260 public:
262 {
263 //memset(m_value, 0, 4*sizeof(ito::uint8));
264 };
265
266 explicit RGBChannel(const uint8 gray)
267 {
268 r = g = b = 0;
269 a = 0xFF;
270 items[_COLOR] = gray;
271 }
272
273 static RGBChannel<_COLOR> zeros()
274 {
276 temp.rgba = 0;
277 return temp;
278 }
279
280 static RGBChannel<_COLOR> black()
281 {
283 temp.rgba = 0;
284 temp.a = 0xFF;
285 return temp;
286 }
287
289 {
290 rgba = 0;
291 a = 0xFF;
292 items[_COLOR] = rhs.items[_COLOR];
293 }
294
296 {
297 items[_COLOR] = static_cast<uint8>(std::min<int16>(items[_COLOR] + rhs.items[_COLOR], 255));
298 return *this;
299 }
300
302 {
303 items[_COLOR] = rhs.items[_COLOR];
304 return *this;
305 }
306
307 RGBChannel& operator =(const uint32 &rhs)
308 {
309 items[_COLOR] = ((unsigned char*)&rhs)[_COLOR];
310 return *this;
311 }
312
314 {
315 items[_COLOR] = static_cast<uint8>(std::max<int16>(items[_COLOR] - rhs.items[_COLOR], 0));
316 return *this;
317 }
318
320 {
321 items[_COLOR] = static_cast<uint8>(items[_COLOR] * rhs.items[_COLOR] / 255);
322 return *this;
323 }
324
326 {
327 if(rhs.items[_COLOR] == 0)
328 {
329 throw std::runtime_error("Division by zero not allowed for rgba32-values");
330 }
331 items[_COLOR] = static_cast<uint8>(std::min<int16>((items[_COLOR] * (ito::int16)255)/ rhs.items[_COLOR], 255));
332 return *this;
333 }
334
335 RGBChannel operator +(const RGBChannel &second) const
336 {
337 RGBChannel<_COLOR> first(*this);
338 first += second;
339 return first;
340 }
341
342 RGBChannel operator -(const RGBChannel &second) const
343 {
344 RGBChannel<_COLOR> first(*this);
345 first -= second;
346 return first;
347 }
348
349 RGBChannel operator *(const RGBChannel &second) const
350 {
351 RGBChannel<_COLOR> first(*this);
352 first *= second;
353 return first;
354 }
355
356 RGBChannel operator /(const RGBChannel &second) const
357 {
358 RGBChannel<_COLOR> first(*this);
359 first /= second;
360 return first;
361 }
362
363 bool operator ==(const RGBChannel &rhs) const
364 {
365 return items[_COLOR] == rhs.items[_COLOR];
366 }
367
368 bool operator !=(const RGBChannel &rhs) const
369 {
370 return items[_COLOR] != rhs.items[_COLOR];
371 }
372
373 bool operator <(const RGBChannel &rhs) const
374 {
375 return (items[_COLOR] < rhs.items[_COLOR]);
376 }
377
378 bool operator >(const RGBChannel &rhs) const
379 {
380 return (items[_COLOR] < rhs.items[_COLOR]);
381 }
382
383 inline float32 gray() const {return static_cast<float32> (items[_COLOR]);};
385 uint8& value() {return items[_COLOR];};
386 uint8 value() const {return items[_COLOR];};
387 };
388
389 typedef RGBChannel<Rgba32::RGBA_A> AlphaChannel;
390 typedef RGBChannel<Rgba32::RGBA_R> RedChannel;
391 typedef RGBChannel<Rgba32::RGBA_G> GreenChannel;
392 typedef RGBChannel<Rgba32::RGBA_B> BlueChannel;
394
395} //end namespace ito
396
397#endif //COLOR_H
Definition color.h:259
RGBChannel & operator/=(const RGBChannel &rhs)
Definition color.h:325
bool operator<(const RGBChannel &rhs) const
Definition color.h:373
RGBChannel & operator-=(const RGBChannel &rhs)
Definition color.h:313
RGBChannel & operator*=(const RGBChannel &rhs)
Definition color.h:319
bool operator>(const RGBChannel &rhs) const
Definition color.h:378
RGBChannel()
Definition color.h:261
uint8 & value()
Definition color.h:385
bool operator==(const RGBChannel &rhs) const
Definition color.h:363
RGBChannel operator/(const RGBChannel &second) const
Definition color.h:356
RGBChannel operator+(const RGBChannel &second) const
Definition color.h:335
RGBChannel operator-(const RGBChannel &second) const
Definition color.h:342
RGBChannel & operator+=(const RGBChannel &rhs)
Definition color.h:295
RGBChannel & operator=(const RGBChannel &rhs)
Definition color.h:301
RGBChannel(const RGBChannel &rhs)
Definition color.h:288
uint8 value() const
Definition color.h:386
RGBChannel(const uint8 gray)
Definition color.h:266
bool operator!=(const RGBChannel &rhs) const
Definition color.h:368
RGBChannel operator*(const RGBChannel &second) const
Definition color.h:349
This class implements basic functionality for color handling in itom. \detail This class implements A...
Definition color.h:47
uint8 blue() const
Definition color.h:248
Rgba32 operator-(const Rgba32 &second) const
Definition color.h:197
uint32 & argb()
Definition color.h:250
bool operator==(const Rgba32 &rhs) const
Definition color.h:225
Rgba32(const Rgba32 &rhs)
Definition color.h:109
Rgba32(const uint8 &a, const uint8 &r, const uint8 &g, const uint8 &b)
Definition color.h:89
Rgba32 & operator*=(const Rgba32 &rhs)
Multiplication by another Rgba32 value.
Definition color.h:148
uint8 alpha() const
Definition color.h:245
RGBSelectionFlags
Definition color.h:51
@ RGBA_B
blue
Definition color.h:52
@ RGBA_R
red
Definition color.h:54
@ RGBA_Y
gray
Definition color.h:56
@ RGBA_G
green
Definition color.h:53
@ RGBA_A
alpha
Definition color.h:55
Rgba32 operator*(const Rgba32 &second) const
Definition color.h:204
float32 gray() const
Definition color.h:235
static Rgba32 fromUnsignedLong(const uint32 val)
static constructor to create Rgba32 from uint32 containing the values argb
Definition color.h:82
Rgba32 & operator-=(const Rgba32 &rhs)
Definition color.h:135
Rgba32 & operator=(const Rgba32 &rhs)
Definition color.h:123
uint8 & red()
Definition color.h:241
uint8 red() const
Definition color.h:246
uint32 argb() const
Definition color.h:251
Rgba32 & operator/=(const Rgba32 &rhs)
Definition color.h:177
Rgba32 & operator+=(const Rgba32 &rhs)
Definition color.h:114
bool operator!=(const Rgba32 &rhs) const
Definition color.h:230
Rgba32 operator+(const Rgba32 &second) const
Definition color.h:190
Rgba32 operator/(const Rgba32 &second) const
Definition color.h:218
uint32 * u32ptr()
Definition color.h:253
Rgba32()
Definition color.h:60
uint8 & blue()
Definition color.h:243
uint8 & green()
Definition color.h:242
Rgba32(const uint8 gray)
Constructor which will set color channels to gray uint8 and alpha to 255.
Definition color.h:101
uint8 green() const
Definition color.h:247
Definition typeDefs.h:143
Definition apiFunctionsGraph.cpp:40