Claw  1.7.3
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@gamned.org
24 */
30 #include <claw/pixel.hpp>
31 
32 #include <claw/types.hpp>
33 
34 #include <stdexcept>
35 #include <limits>
36 #include <climits>
37 #include <sstream>
38 
39 namespace claw
40 {
41  namespace graphic
42  {
46  rgba_pixel transparent_pixel( 0, 0, 0, 0 );
47 
48  rgba_pixel black_pixel
49  ( 0, 0, 0, std::numeric_limits<rgba_pixel::component_type>::max() );
50  rgba_pixel white_pixel
51  ( std::numeric_limits<rgba_pixel::component_type>::max(),
52  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 
56  rgba_pixel blue_pixel
57  ( 0, 0, std::numeric_limits<rgba_pixel::component_type>::max(),
58  std::numeric_limits<rgba_pixel::component_type>::max() );
59  rgba_pixel green_pixel
60  ( 0, std::numeric_limits<rgba_pixel::component_type>::max(), 0,
61  std::numeric_limits<rgba_pixel::component_type>::max() );
62  rgba_pixel red_pixel
63  ( std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
64  std::numeric_limits<rgba_pixel::component_type>::max() );
65 
66  rgba_pixel yellow_pixel
67  ( std::numeric_limits<rgba_pixel::component_type>::max(),
68  std::numeric_limits<rgba_pixel::component_type>::max(), 0,
69  std::numeric_limits<rgba_pixel::component_type>::max() );
70  rgba_pixel magenta_pixel
71  ( std::numeric_limits<rgba_pixel::component_type>::max(), 0,
72  std::numeric_limits<rgba_pixel::component_type>::max(),
73  std::numeric_limits<rgba_pixel::component_type>::max() );
74  rgba_pixel cyan_pixel
75  ( 0, std::numeric_limits<rgba_pixel::component_type>::max(),
76  std::numeric_limits<rgba_pixel::component_type>::max(),
77  std::numeric_limits<rgba_pixel::component_type>::max() );
78 
81  } // namespace graphic
82 } // namespace claw
83 
84 /*----------------------------------------------------------------------------*/
89 {
90 
91 } // rgb_pixel::rgb_pixel()
92 
93 /*----------------------------------------------------------------------------*/
102 {
103  components.red = r;
104  components.green = g;
105  components.blue = b;
106 } // rgb_pixel::rgb_pixel()
107 
108 /*----------------------------------------------------------------------------*/
114 {
115  components.red = p.components.red;
116  components.green = p.components.green;
117  components.blue = p.components.blue;
118 } // rgb_pixel::rgb_pixel()
119 
120 /*----------------------------------------------------------------------------*/
125 claw::graphic::rgb_pixel::rgb_pixel( const std::string& c )
126 {
127  std::istringstream iss(c);
128  u_int_32 color;
129 
130  if ( c[0] == '#' )
131  iss.ignore(1);
132 
133  if ( !(iss >> std::hex >> color) )
134  throw std::invalid_argument(c);
135 
136  components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
137  components.green = (color & 0x00FF00) >> CHAR_BIT;
138  components.blue = color & 0x0000FF;
139 } // rgb_pixel::rgb_pixel()
140 
141 /*----------------------------------------------------------------------------*/
147 {
148  return (components.red == that.components.red)
149  && (components.green == that.components.green)
150  && (components.blue == that.components.blue);
151 } // rgb_pixel::operator==()
152 
153 /*----------------------------------------------------------------------------*/
159 {
160  return *this == rgb_pixel(that);
161 } // rgb_pixel::operator==()
162 
163 /*----------------------------------------------------------------------------*/
169 {
170  return !(*this == that);
171 } // rgb_pixel::operator!=()
172 
173 /*----------------------------------------------------------------------------*/
179 {
180  return !(*this == that);
181 } // rgb_pixel::operator!=()
182 
183 
184 
185 
186 /*----------------------------------------------------------------------------*/
191 {
192 
193 } // rgba_pixel::rgba_pixel()
194 
195 /*----------------------------------------------------------------------------*/
202 {
203  components.red = that.components.red;
204  components.green = that.components.green;
205  components.blue = that.components.blue;
206  components.alpha = 255;
207 } // rgba_pixel::rgba_pixel()
208 
209 /*----------------------------------------------------------------------------*/
219 {
220  components.red = r;
221  components.green = g;
222  components.blue = b;
223  components.alpha = a;
224 } // rgba_pixel::rgba_pixel()
225 
226 /*----------------------------------------------------------------------------*/
232 {
233  std::istringstream iss(c);
234  u_int_32 color;
235  bool has_alpha;
236 
237  if ( c[0] == '#' )
238  {
239  iss.ignore(1);
240  has_alpha = c.length() > 7;
241  }
242  else
243  has_alpha = c.length() > 6;
244 
245  if ( !((iss >> std::hex >> color) && (iss.rdbuf()->in_avail() == 0)) )
246  throw std::invalid_argument(c);
247 
248  if ( has_alpha )
249  components.alpha = (color & 0xFF000000) >> (CHAR_BIT * 3);
250  else
251  components.alpha = std::numeric_limits<component_type>::max();
252 
253  components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
254  components.green = (color & 0x00FF00) >> CHAR_BIT;
255  components.blue = color & 0x0000FF;
256 } // rgba_pixel::rgba_pixel()
257 
258 /*----------------------------------------------------------------------------*/
266 {
267  components.red = that.components.red;
268  components.green = that.components.green;
269  components.blue = that.components.blue;
270  components.alpha = 255;
271 
272  return *this;
273 } // rgba_pixel::operator=()
274 
275 /*----------------------------------------------------------------------------*/
281 {
282  return pixel == that.pixel;
283 } // rgba_pixel::operator==()
284 
285 /*----------------------------------------------------------------------------*/
291 {
292  return pixel != that.pixel;
293 } // rgba_pixel::operator!=()
294 
295 /*----------------------------------------------------------------------------*/
307 {
308  return ((unsigned int)components.red * 183
309  + (unsigned int)components.green * 54
310  + (unsigned int)components.blue * 18
311  ) / 256;
312 } // rgba_pixel::luminosity()