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, MA
27  02110-1301 USA
28 */
29 
30 #ifndef PT_GFX_COLOR_H
31 #define PT_GFX_COLOR_H
32 
33 #include <Pt/Types.h>
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/String.h>
36 
37 
38 namespace Pt {
39 namespace Gfx {
40 
41 
42 class Color
43 {
44  public:
45  Color()
46  : _a(65535)
47  , _r(0)
48  , _g(0)
49  , _b(0)
50  { }
51 
53  : _a(a)
54  , _r(r)
55  , _g(g)
56  , _b(b)
57  { }
58 
60  : _a(65535)
61  , _r(r)
62  , _g(g)
63  , _b(b)
64  { }
65 
66  Pt::uint16_t alpha() const
67  {
68  return _a;
69  }
70 
71  Pt::uint16_t red() const
72  {
73  return _r;
74  }
75 
76  Pt::uint16_t green() const
77  {
78  return _g;
79  }
80 
81  Pt::uint16_t blue() const
82  {
83  return _b;
84  }
85 
86  void setAlpha( Pt::uint16_t c)
87  {
88  _a = c;
89  }
90 
91  void setRed( Pt::uint16_t c)
92  {
93  _r = c;
94  }
95 
96  void setGreen( Pt::uint16_t c)
97  {
98  _g = c;
99  }
100 
101  void setBlue( Pt::uint16_t c)
102  {
103  _b = c;
104  }
105 
106  Color toGray() const
107  {
108  const Pt::uint32_t rf = 77;
109  const Pt::uint32_t gf = 128;
110  const Pt::uint32_t bf = 51;
111 
112  const Pt::uint32_t v = (_r * rf +
113  _g * gf +
114  _b * bf) >> 8;
115 
116  const Pt::uint16_t s = static_cast<Pt::uint16_t>(v);
117 
118  return Color(_a, s, s, s);
119  }
120 
121  static Color fromRgb8(Pt::uint8_t r, Pt::uint8_t g,
122  Pt::uint8_t b, Pt::uint8_t a = 255)
123  {
124  return Color(a * 257, r * 257, g * 257, b * 257);
125  }
126 
127  bool operator==(const Color& c) const
128  {
129  return _a == c._a && _r == c._r && _g == c._g && _b == c._b;
130  }
131 
132  private:
133  Pt::uint16_t _a;
134  Pt::uint16_t _r;
135  Pt::uint16_t _g;
136  Pt::uint16_t _b;
137 };
138 
139 
140 } // namespace
141 } // namespace
142 
143 #endif
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18