claw  1.9.0
pixel.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@stuff-o-matic.com
24 */
30 #include <claw/graphic/pixel.hpp>
31 
32 #include <claw/types.hpp>
33 
34 #include <climits>
35 #include <limits>
36 #include <sstream>
37 #include <stdexcept>
38 
39 namespace claw
40 {
41  namespace graphic
42  {
46  rgba_pixel transparent_pixel(0, 0, 0, 0);
47 
48  rgba_pixel
49  black_pixel(0, 0, 0,
50  std::numeric_limits<rgba_pixel::component_type>::max());
51  rgba_pixel
52  white_pixel(std::numeric_limits<rgba_pixel::component_type>::max(),
53  std::numeric_limits<rgba_pixel::component_type>::max(),
54  std::numeric_limits<rgba_pixel::component_type>::max(),
55  std::numeric_limits<rgba_pixel::component_type>::max());
56 
57  rgba_pixel
58  blue_pixel(0, 0,
59  std::numeric_limits<rgba_pixel::component_type>::max(),
60  std::numeric_limits<rgba_pixel::component_type>::max());
61  rgba_pixel
62  green_pixel(0, std::numeric_limits<rgba_pixel::component_type>::max(),
63  0, std::numeric_limits<rgba_pixel::component_type>::max());
64  rgba_pixel
65  red_pixel(std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
66  std::numeric_limits<rgba_pixel::component_type>::max());
67 
68  rgba_pixel
69  yellow_pixel(std::numeric_limits<rgba_pixel::component_type>::max(),
70  std::numeric_limits<rgba_pixel::component_type>::max(), 0,
71  std::numeric_limits<rgba_pixel::component_type>::max());
72  rgba_pixel
73  magenta_pixel(std::numeric_limits<rgba_pixel::component_type>::max(),
74  0,
75  std::numeric_limits<rgba_pixel::component_type>::max(),
76  std::numeric_limits<rgba_pixel::component_type>::max());
77  rgba_pixel
78  cyan_pixel(0, std::numeric_limits<rgba_pixel::component_type>::max(),
79  std::numeric_limits<rgba_pixel::component_type>::max(),
80  std::numeric_limits<rgba_pixel::component_type>::max());
81 
84  }
85 }
86 
91 {}
92 
100  component_type b)
101 {
102  components.red = r;
103  components.green = g;
104  components.blue = b;
105 }
106 
112 {
113  components.red = p.components.red;
114  components.green = p.components.green;
115  components.blue = p.components.blue;
116 }
117 
123 {
124  std::istringstream iss(c);
125  u_int_32 color;
126 
127  if(c[0] == '#')
128  iss.ignore(1);
129 
130  if(!(iss >> std::hex >> color))
131  throw std::invalid_argument(c);
132 
133  components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
134  components.green = (color & 0x00FF00) >> CHAR_BIT;
135  components.blue = color & 0x0000FF;
136 }
137 
143 {
144  return (components.red == that.components.red)
145  && (components.green == that.components.green)
146  && (components.blue == that.components.blue);
147 }
148 
154 {
155  return *this == rgb_pixel(that);
156 }
157 
163 {
164  return !(*this == that);
165 }
166 
172 {
173  return !(*this == that);
174 }
175 
180 {}
181 
188 {
189  components.red = that.components.red;
190  components.green = that.components.green;
191  components.blue = that.components.blue;
192  components.alpha = 255;
193 }
194 
204 {
205  components.red = r;
206  components.green = g;
207  components.blue = b;
208  components.alpha = a;
209 }
210 
216 {
217  std::istringstream iss(c);
218  u_int_32 color;
219  bool has_alpha;
220 
221  if(c[0] == '#')
222  {
223  iss.ignore(1);
224  has_alpha = c.length() > 7;
225  }
226  else
227  has_alpha = c.length() > 6;
228 
229  if(!((iss >> std::hex >> color) && (iss.rdbuf()->in_avail() == 0)))
230  throw std::invalid_argument(c);
231 
232  if(has_alpha)
233  components.alpha = (color & 0xFF000000) >> (CHAR_BIT * 3);
234  else
235  components.alpha = std::numeric_limits<component_type>::max();
236 
237  components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
238  components.green = (color & 0x00FF00) >> CHAR_BIT;
239  components.blue = color & 0x0000FF;
240 }
241 
249 {
250  components.red = that.components.red;
251  components.green = that.components.green;
252  components.blue = that.components.blue;
253  components.alpha = 255;
254 
255  return *this;
256 }
257 
263 {
264  return pixel == that.pixel;
265 }
266 
272 {
273  return pixel != that.pixel;
274 }
275 
287 {
288  return ((unsigned int)components.red * 183
289  + (unsigned int)components.green * 54
290  + (unsigned int)components.blue * 18)
291  / 256;
292 }
rgba_pixel black_pixel
The black color.
unsigned_integer_of_size< 32 >::type u_int_32
An unsigned integer on 32 bits.
Definition: types.hpp:138
component_type red
Red component.
Definition: pixel.hpp:93
Some classes for the raw manipulation of the base types.
struct claw::graphic::rgba_pixel::@15::@17 components
Component by component representation.
bool operator==(const rgb_pixel &that) const
Compare to a pixel.
Definition: pixel.cpp:142
rgba_pixel blue_pixel
The blue color.
unsigned int pixel
Compressed representation.
Definition: pixel.hpp:87
rgb_pixel()
Default constructor.
Definition: pixel.cpp:90
rgba_pixel & operator=(const rgb_pixel &that)
Assignement operator.
Definition: pixel.cpp:248
component_type blue
Blue component.
Definition: pixel.hpp:59
struct claw::graphic::rgb_pixel::@14 components
Component by component representation.
bool operator!=(const rgba_pixel &that) const
Tell if two pixels are different.
Definition: pixel.cpp:271
component_type red
Red component.
Definition: pixel.hpp:53
rgba_pixel yellow_pixel
The yellow color.
component_type luminosity() const
Get the luminosity of the pixel.
Definition: pixel.cpp:286
rgba_pixel green_pixel
The green color.
rgba_pixel magenta_pixel
The magenta color.
rgba_pixel red_pixel
The red color.
bool operator!=(const rgb_pixel &that) const
Compare to a pixel.
Definition: pixel.cpp:162
unsigned char component_type
The type of the components of the color.
Definition: pixel.hpp:82
bool operator==(const rgba_pixel &that) const
Tell if two pixels are equal.
Definition: pixel.cpp:262
rgba_pixel()
Default constructor.
Definition: pixel.cpp:179
Representation of a pixel in image processing.
rgba_pixel transparent_pixel
A transparent color.
unsigned char component_type
The type of the components of the color.
Definition: pixel.hpp:47
rgba_pixel white_pixel
The white color.
rgba_pixel cyan_pixel
The cyan color.
This is the main namespace.
Definition: application.hpp:49
component_type green
Green component.
Definition: pixel.hpp:56