Color.h
1/* Copyright (C) 2016 Marc Boris Duerner
2 Copyright (C) 2015 Laurentiu-Gheorghe Crisan
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 As a special exception, you may use this file as part of a free
10 software library without restriction. Specifically, if other files
11 instantiate templates or use macros or inline functions from this
12 file, or you compile this file and link it with other files to
13 produce an executable, this file does not by itself cause the
14 resulting executable to be covered by the GNU General Public
15 License. This exception does not however invalidate any other
16 reasons why the executable file might be covered by the GNU Library
17 General Public License.
18
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 MA 02110-1301 USA
28*/
29
30#ifndef PT_GFX_COLOR_H
31#define PT_GFX_COLOR_H
32
33#include <Pt/Gfx/Api.h>
34#include <Pt/Types.h>
35#include <cstddef>
36#include <cstring>
37
38namespace Pt {
39
40namespace Gfx {
41
42class ColorF;
43
64class Color
65{
66 public:
67 Color()
68 : _value(0)
69 { }
70
71 Color(const Color& color) = default;
72
73 explicit Color(uint32_t value)
74 : _value(value)
75 { }
76
77 explicit Color(const uint8_t* base)
78 : _value()
79 {
80 std::memcpy( &_value, base, sizeof(uint32_t) );
81 }
82
84 : _value( (uint32_t(255) << 24) | (uint32_t(r) << 16) | (uint32_t(g) << 8) | uint32_t(b) )
85 { }
86
88 : _value( (uint32_t(a) << 24) | (uint32_t(r) << 16) | (uint32_t(g) << 8) | uint32_t(b) )
89 { }
90
91 explicit Color(const ColorF& c);
92
93 Color& operator=(const Color& color) = default;
94
95 Color& operator=(const ColorF& c);
96
97 Color& operator=(uint32_t value)
98 {
99 _value = value;
100 return *this;
101 }
102
103 Pt::uint8_t alpha() const
104 {
105 return _value >> 24;
106 }
107
108 void setAlpha(Pt::uint8_t a)
109 {
110 _value = (_value & 0x00FFFFFF) | (uint32_t(a) << 24);
111 }
112
113 Pt::uint8_t red() const
114 {
115 return (_value & 0x00FF0000) >> 16;
116 }
117
118 void setRed(Pt::uint8_t r)
119 {
120 _value = (_value & 0xFF00FFFF) | (uint32_t(r) << 16);
121 }
122
123 Pt::uint8_t green() const
124 {
125 return (_value & 0x0000FF00) >> 8;
126 }
127
128 void setGreen(Pt::uint8_t g)
129 {
130 _value = (_value & 0xFFFF00FF) | (uint32_t(g) << 8);
131 }
132
133 Pt::uint8_t blue() const
134 {
135 return _value & 0x000000FF;
136 }
137
138 void setBlue(Pt::uint8_t b)
139 {
140 _value = (_value & 0xFFFFFF00) | uint32_t(b);
141 }
142
143 const uint32_t& value() const
144 {
145 return _value;
146 }
147
148 bool operator==(const Color& other) const
149 { return _value == other._value; }
150
151 bool operator!=(const Color& other) const
152 { return _value != other._value; }
153
154 private:
155 Pt::uint32_t _value;
156};
157
172class ColorF
173{
174 public:
175 ColorF()
176 : _r(0)
177 , _g(0)
178 , _b(0)
179 , _a(0)
180 { }
181
182 ColorF(const ColorF&) = default;
183
184 explicit ColorF(const Color& c);
185
186 ColorF& operator=(const ColorF&) = default;
187
188 ColorF& operator=(const Color& c);
189
190 ColorF(float a, float r, float g, float b)
191 : _r(r)
192 , _g(g)
193 , _b(b)
194 , _a(a)
195 { }
196
197 ColorF(float r, float g, float b)
198 : _r(r)
199 , _g(g)
200 , _b(b)
201 , _a(1.f)
202 { }
203
204 float alpha() const
205 {
206 return _a;
207 }
208
209 void setAlpha(float c)
210 {
211 _a = c;
212 }
213
214 float red() const
215 {
216 return _r;
217 }
218
219 void setRed(float c)
220 {
221 _r = c;
222 }
223
224 float green() const
225 {
226 return _g;
227 }
228
229 void setGreen(float c)
230 {
231 _g = c;
232 }
233
234 float blue() const
235 {
236 return _b;
237 }
238
239 void setBlue(float c)
240 {
241 _b = c;
242 }
243
244 ColorF toGray() const
245 {
246 const float s = 0.299f * _r + 0.587f * _g + 0.114f * _b;
247 return ColorF(_a, s, s, s);
248 }
249
252 static float toChannelF(Pt::uint8_t c)
253 {
254 return c * (1.f / 255.f);
255 }
256
261 static Pt::uint8_t toChannel8(float c)
262 {
263 if(c <= 0.f)
264 return 0;
265 if(c >= 1.f)
266 return 255;
267
268 return static_cast<Pt::uint8_t>(c * 255.f + 0.5f);
269 }
270
271 static ColorF fromRgb8(Pt::uint8_t r, Pt::uint8_t g,
272 Pt::uint8_t b, Pt::uint8_t a = 255)
273 {
274 return ColorF(toChannelF(a), toChannelF(r),
275 toChannelF(g), toChannelF(b));
276 }
277
278 private:
279 float _r;
280 float _g;
281 float _b;
282 float _a;
283};
284
285
286inline Color::Color(const ColorF& c)
287: _value( (Pt::uint32_t(ColorF::toChannel8(c.alpha())) << 24) |
288 (Pt::uint32_t(ColorF::toChannel8(c.red())) << 16) |
289 (Pt::uint32_t(ColorF::toChannel8(c.green())) << 8) |
290 Pt::uint32_t(ColorF::toChannel8(c.blue())) )
291{
292}
293
294
295inline Color& Color::operator=(const ColorF& c)
296{
297 _value = (Pt::uint32_t(ColorF::toChannel8(c.alpha())) << 24) |
298 (Pt::uint32_t(ColorF::toChannel8(c.red())) << 16) |
299 (Pt::uint32_t(ColorF::toChannel8(c.green())) << 8) |
301 return *this;
302}
303
304
305inline ColorF::ColorF(const Color& c)
306: _r(toChannelF(c.red()))
307, _g(toChannelF(c.green()))
308, _b(toChannelF(c.blue()))
309, _a(toChannelF(c.alpha()))
310{
311}
312
313
314inline ColorF& ColorF::operator=(const Color& c)
315{
316 _r = toChannelF(c.red());
317 _g = toChannelF(c.green());
318 _b = toChannelF(c.blue());
319 _a = toChannelF(c.alpha());
320 return *this;
321}
322
323} // namespace
324
325} // namespace
326
327#endif
Floating-point RGBA color.
Definition Color.h:173
static float toChannelF(Pt::uint8_t c)
Converts an 8-bit channel to the unit interval.
Definition Color.h:252
static Pt::uint8_t toChannel8(float c)
Converts a unit-interval channel to 8-bit.
Definition Color.h:261
8-bit ARGB color.
Definition Color.h:65
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33