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 
38 namespace Pt {
39 
40 namespace Gfx {
41 
42 class ColorF;
43 
46 class Color
47 {
48  public:
49  Color()
50  : _value(0)
51  { }
52 
53  Color(const Color& color) = default;
54 
55  explicit Color(uint32_t value)
56  : _value(value)
57  { }
58 
59  explicit Color(const uint8_t* base)
60  : _value()
61  {
62  std::memcpy( &_value, base, sizeof(uint32_t) );
63  }
64 
66  : _value( (uint32_t(255) << 24) | (uint32_t(r) << 16) | (uint32_t(g) << 8) | uint32_t(b) )
67  { }
68 
70  : _value( (uint32_t(a) << 24) | (uint32_t(r) << 16) | (uint32_t(g) << 8) | uint32_t(b) )
71  { }
72 
73  explicit Color(const ColorF& c);
74 
75  Color& operator=(const Color& color) = default;
76 
77  Color& operator=(const ColorF& c);
78 
79  Color& operator=(uint32_t value)
80  {
81  _value = value;
82  return *this;
83  }
84 
85  Pt::uint8_t alpha() const
86  {
87  return _value >> 24;
88  }
89 
90  void setAlpha(Pt::uint8_t a)
91  {
92  _value = (_value & 0x00FFFFFF) | (uint32_t(a) << 24);
93  }
94 
95  Pt::uint8_t red() const
96  {
97  return (_value & 0x00FF0000) >> 16;
98  }
99 
100  void setRed(Pt::uint8_t r)
101  {
102  _value = (_value & 0xFF00FFFF) | (uint32_t(r) << 16);
103  }
104 
105  Pt::uint8_t green() const
106  {
107  return (_value & 0x0000FF00) >> 8;
108  }
109 
110  void setGreen(Pt::uint8_t g)
111  {
112  _value = (_value & 0xFFFF00FF) | (uint32_t(g) << 8);
113  }
114 
115  Pt::uint8_t blue() const
116  {
117  return _value & 0x000000FF;
118  }
119 
120  void setBlue(Pt::uint8_t b)
121  {
122  _value = (_value & 0xFFFFFF00) | uint32_t(b);
123  }
124 
125  const uint32_t& value() const
126  {
127  return _value;
128  }
129 
130  bool operator==(const Color& other) const
131  { return _value == other._value; }
132 
133  bool operator!=(const Color& other) const
134  { return _value != other._value; }
135 
136  private:
137  Pt::uint32_t _value;
138 };
139 
142 class ColorF
143 {
144  public:
145  ColorF()
146  : _a(65535)
147  , _r(0)
148  , _g(0)
149  , _b(0)
150  { }
151 
152  ColorF(const ColorF&) = default;
153 
154  explicit ColorF(const Color& c);
155 
156  ColorF& operator=(const ColorF&) = default;
157 
158  ColorF& operator=(const Color& c);
159 
161  : _a(a)
162  , _r(r)
163  , _g(g)
164  , _b(b)
165  { }
166 
168  : _a(65535)
169  , _r(r)
170  , _g(g)
171  , _b(b)
172  { }
173 
174  Pt::uint16_t alpha() const
175  {
176  return _a;
177  }
178 
179  void setAlpha( Pt::uint16_t c)
180  {
181  _a = c;
182  }
183 
184  Pt::uint16_t red() const
185  {
186  return _r;
187  }
188 
189  void setRed( Pt::uint16_t c)
190  {
191  _r = c;
192  }
193 
194  Pt::uint16_t green() const
195  {
196  return _g;
197  }
198 
199  void setGreen( Pt::uint16_t c)
200  {
201  _g = c;
202  }
203 
204  Pt::uint16_t blue() const
205  {
206  return _b;
207  }
208 
209  void setBlue( Pt::uint16_t c)
210  {
211  _b = c;
212  }
213 
214  ColorF toGray() const
215  {
216  const Pt::uint32_t rf = 77;
217  const Pt::uint32_t gf = 128;
218  const Pt::uint32_t bf = 51;
219 
220  const Pt::uint32_t v = (_r * rf +
221  _g * gf +
222  _b * bf) >> 8;
223 
224  const Pt::uint16_t s = static_cast<Pt::uint16_t>(v);
225 
226  return ColorF(_a, s, s, s);
227  }
228 
229  static ColorF fromRgb8(Pt::uint8_t r, Pt::uint8_t g,
230  Pt::uint8_t b, Pt::uint8_t a = 255)
231  {
232  return ColorF(a * 257, r * 257, g * 257, b * 257);
233  }
234 
235  private:
236  Pt::uint16_t _a;
237  Pt::uint16_t _r;
238  Pt::uint16_t _g;
239  Pt::uint16_t _b;
240 };
241 
242 
243 inline Color::Color(const ColorF& c)
244 : _value( (Pt::uint32_t(c.alpha() >> 8) << 24) |
245  (Pt::uint32_t(c.red() >> 8) << 16) |
246  (Pt::uint32_t(c.green() >> 8) << 8) |
247  Pt::uint32_t(c.blue() >> 8) )
248 {
249 }
250 
251 
252 inline Color& Color::operator=(const ColorF& c)
253 {
254  _value = (Pt::uint32_t(c.alpha() >> 8) << 24) |
255  (Pt::uint32_t(c.red() >> 8) << 16) |
256  (Pt::uint32_t(c.green() >> 8) << 8) |
257  Pt::uint32_t(c.blue() >> 8);
258  return *this;
259 }
260 
261 
262 inline ColorF::ColorF(const Color& c)
263 : _a(c.alpha() * 257)
264 , _r(c.red() * 257)
265 , _g(c.green() * 257)
266 , _b(c.blue() * 257)
267 {
268 }
269 
270 inline ColorF& ColorF::operator=(const Color& c)
271 {
272  _a = c.alpha() * 257;
273  _r = c.red() * 257;
274  _g = c.green() * 257;
275  _b = c.blue() * 257;
276  return *this;
277 }
278 
279 } // namespace
280 
281 } // namespace
282 
283 #endif
Core module.
Definition: Allocator.h:33
Definition: Color.h:143
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Api-Types.h:20
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Api-Types.h:48
Standard color type.
Definition: Color.h:47
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Api-Types.h:34