Claw  1.7.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@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  {
43  rgba_pixel transparent_pixel( 0, 0, 0, 0 );
44 
45  rgba_pixel black_pixel
46  ( 0, 0, 0, std::numeric_limits<rgba_pixel::component_type>::max() );
47  rgba_pixel white_pixel
48  ( std::numeric_limits<rgba_pixel::component_type>::max(),
49  std::numeric_limits<rgba_pixel::component_type>::max(),
50  std::numeric_limits<rgba_pixel::component_type>::max(),
51  std::numeric_limits<rgba_pixel::component_type>::max() );
52 
53  rgba_pixel blue_pixel
54  ( 0, 0, std::numeric_limits<rgba_pixel::component_type>::max(),
55  std::numeric_limits<rgba_pixel::component_type>::max() );
56  rgba_pixel green_pixel
57  ( 0, std::numeric_limits<rgba_pixel::component_type>::max(), 0,
58  std::numeric_limits<rgba_pixel::component_type>::max() );
59  rgba_pixel red_pixel
60  ( std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
61  std::numeric_limits<rgba_pixel::component_type>::max() );
62 
63  rgba_pixel yellow_pixel
64  ( std::numeric_limits<rgba_pixel::component_type>::max(),
65  std::numeric_limits<rgba_pixel::component_type>::max(), 0,
66  std::numeric_limits<rgba_pixel::component_type>::max() );
67  rgba_pixel magenta_pixel
68  ( std::numeric_limits<rgba_pixel::component_type>::max(), 0,
69  std::numeric_limits<rgba_pixel::component_type>::max(),
70  std::numeric_limits<rgba_pixel::component_type>::max() );
71  rgba_pixel cyan_pixel
72  ( 0, std::numeric_limits<rgba_pixel::component_type>::max(),
73  std::numeric_limits<rgba_pixel::component_type>::max(),
74  std::numeric_limits<rgba_pixel::component_type>::max() );
75 
76  } // namespace graphic
77 } // namespace claw
78 
79 /*----------------------------------------------------------------------------*/
84 {
85 
86 } // rgb_pixel::rgb_pixel()
87 
88 /*----------------------------------------------------------------------------*/
96 ( component_type r, component_type g, component_type b )
97 {
98  components.red = r;
99  components.green = g;
100  components.blue = b;
101 } // rgb_pixel::rgb_pixel()
102 
103 /*----------------------------------------------------------------------------*/
109 {
110  components.red = p.components.red;
111  components.green = p.components.green;
112  components.blue = p.components.blue;
113 } // rgb_pixel::rgb_pixel()
114 
115 /*----------------------------------------------------------------------------*/
120 claw::graphic::rgb_pixel::rgb_pixel( const std::string& c )
121 {
122  std::istringstream iss(c);
123  u_int_32 color;
124 
125  if ( c[0] == '#' )
126  iss.ignore(1);
127 
128  if ( !(iss >> std::hex >> color) )
129  throw std::invalid_argument(c);
130 
131  components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
132  components.green = (color & 0x00FF00) >> CHAR_BIT;
133  components.blue = color & 0x0000FF;
134 } // rgb_pixel::rgb_pixel()
135 
136 /*----------------------------------------------------------------------------*/
142 {
143  return (components.red == that.components.red)
144  && (components.green == that.components.green)
145  && (components.blue == that.components.blue);
146 } // rgb_pixel::operator==()
147 
148 /*----------------------------------------------------------------------------*/
154 {
155  return *this == rgb_pixel(that);
156 } // rgb_pixel::operator==()
157 
158 /*----------------------------------------------------------------------------*/
164 {
165  return !(*this == that);
166 } // rgb_pixel::operator!=()
167 
168 /*----------------------------------------------------------------------------*/
174 {
175  return !(*this == that);
176 } // rgb_pixel::operator!=()
177 
178 
179 
180 
181 /*----------------------------------------------------------------------------*/
186 {
187 
188 } // rgba_pixel::rgba_pixel()
189 
190 /*----------------------------------------------------------------------------*/
197 {
198  components.red = that.components.red;
199  components.green = that.components.green;
200  components.blue = that.components.blue;
201  components.alpha = 255;
202 } // rgba_pixel::rgba_pixel()
203 
204 /*----------------------------------------------------------------------------*/
213 ( component_type r, component_type g, component_type b, component_type a )
214 {
215  components.red = r;
216  components.green = g;
217  components.blue = b;
218  components.alpha = a;
219 } // rgba_pixel::rgba_pixel()
220 
221 /*----------------------------------------------------------------------------*/
227 {
228  std::istringstream iss(c);
229  u_int_32 color;
230  bool has_alpha;
231 
232  if ( c[0] == '#' )
233  {
234  iss.ignore(1);
235  has_alpha = c.length() > 7;
236  }
237  else
238  has_alpha = c.length() > 6;
239 
240  if ( !((iss >> std::hex >> color) && (iss.rdbuf()->in_avail() == 0)) )
241  throw std::invalid_argument(c);
242 
243  if ( has_alpha )
244  components.alpha = (color & 0xFF000000) >> (CHAR_BIT * 3);
245  else
246  components.alpha = std::numeric_limits<component_type>::max();
247 
248  components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
249  components.green = (color & 0x00FF00) >> CHAR_BIT;
250  components.blue = color & 0x0000FF;
251 } // rgba_pixel::rgba_pixel()
252 
253 /*----------------------------------------------------------------------------*/
261 {
262  components.red = that.components.red;
263  components.green = that.components.green;
264  components.blue = that.components.blue;
265  components.alpha = 255;
266 
267  return *this;
268 } // rgba_pixel::operator=()
269 
270 /*----------------------------------------------------------------------------*/
276 {
277  return pixel == that.pixel;
278 } // rgba_pixel::operator==()
279 
280 /*----------------------------------------------------------------------------*/
286 {
287  return pixel != that.pixel;
288 } // rgba_pixel::operator!=()
289 
290 /*----------------------------------------------------------------------------*/
300 claw::graphic::rgba_pixel::component_type
302 {
303  return ((unsigned int)components.red * 183
304  + (unsigned int)components.green * 54
305  + (unsigned int)components.blue * 18
306  ) / 256;
307 } // rgba_pixel::luminosity()