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  private:
131  Pt::uint32_t _value;
132 };
133 
136 class ColorF
137 {
138  public:
139  ColorF()
140  : _a(65535)
141  , _r(0)
142  , _g(0)
143  , _b(0)
144  { }
145 
146  ColorF(const ColorF&) = default;
147 
148  explicit ColorF(const Color& c);
149 
150  ColorF& operator=(const ColorF&) = default;
151 
152  ColorF& operator=(const Color& c);
153 
155  : _a(a)
156  , _r(r)
157  , _g(g)
158  , _b(b)
159  { }
160 
162  : _a(65535)
163  , _r(r)
164  , _g(g)
165  , _b(b)
166  { }
167 
168  Pt::uint16_t alpha() const
169  {
170  return _a;
171  }
172 
173  void setAlpha( Pt::uint16_t c)
174  {
175  _a = c;
176  }
177 
178  Pt::uint16_t red() const
179  {
180  return _r;
181  }
182 
183  void setRed( Pt::uint16_t c)
184  {
185  _r = c;
186  }
187 
188  Pt::uint16_t green() const
189  {
190  return _g;
191  }
192 
193  void setGreen( Pt::uint16_t c)
194  {
195  _g = c;
196  }
197 
198  Pt::uint16_t blue() const
199  {
200  return _b;
201  }
202 
203  void setBlue( Pt::uint16_t c)
204  {
205  _b = c;
206  }
207 
208  ColorF toGray() const
209  {
210  const Pt::uint32_t rf = 77;
211  const Pt::uint32_t gf = 128;
212  const Pt::uint32_t bf = 51;
213 
214  const Pt::uint32_t v = (_r * rf +
215  _g * gf +
216  _b * bf) >> 8;
217 
218  const Pt::uint16_t s = static_cast<Pt::uint16_t>(v);
219 
220  return ColorF(_a, s, s, s);
221  }
222 
223  static ColorF fromRgb8(Pt::uint8_t r, Pt::uint8_t g,
224  Pt::uint8_t b, Pt::uint8_t a = 255)
225  {
226  return ColorF(a * 257, r * 257, g * 257, b * 257);
227  }
228 
229  private:
230  Pt::uint16_t _a;
231  Pt::uint16_t _r;
232  Pt::uint16_t _g;
233  Pt::uint16_t _b;
234 };
235 
236 
237 inline Color::Color(const ColorF& c)
238 : _value( (Pt::uint32_t(c.alpha() >> 8) << 24) |
239  (Pt::uint32_t(c.red() >> 8) << 16) |
240  (Pt::uint32_t(c.green() >> 8) << 8) |
241  Pt::uint32_t(c.blue() >> 8) )
242 {
243 }
244 
245 
246 inline Color& Color::operator=(const ColorF& c)
247 {
248  _value = (Pt::uint32_t(c.alpha() >> 8) << 24) |
249  (Pt::uint32_t(c.red() >> 8) << 16) |
250  (Pt::uint32_t(c.green() >> 8) << 8) |
251  Pt::uint32_t(c.blue() >> 8);
252  return *this;
253 }
254 
255 
256 inline ColorF::ColorF(const Color& c)
257 : _a(c.alpha() * 257)
258 , _r(c.red() * 257)
259 , _g(c.green() * 257)
260 , _b(c.blue() * 257)
261 {
262 }
263 
264 inline ColorF& ColorF::operator=(const Color& c)
265 {
266  _a = c.alpha() * 257;
267  _r = c.red() * 257;
268  _g = c.green() * 257;
269  _b = c.blue() * 257;
270  return *this;
271 }
272 
273 } // namespace
274 
275 } // namespace
276 
277 #endif
Core module.
Definition: Allocator.h:33
Definition: Color.h:137
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