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
39namespace 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/*----------------------------------------------------------------------------*/
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()
rgba_pixel cyan_pixel(0, std::numeric_limits< rgba_pixel::component_type >::max(), std::numeric_limits< rgba_pixel::component_type >::max(), std::numeric_limits< rgba_pixel::component_type >::max())
The cyan color.
Definition pixel.hpp:157
rgba_pixel blue_pixel(0, 0, std::numeric_limits< rgba_pixel::component_type >::max(), std::numeric_limits< rgba_pixel::component_type >::max())
The blue color.
Definition pixel.hpp:142
rgba_pixel black_pixel(0, 0, 0, std::numeric_limits< rgba_pixel::component_type >::max())
The black color.
Definition pixel.hpp:136
rgba_pixel red_pixel
The red color.
rgba_pixel green_pixel(0, std::numeric_limits< rgba_pixel::component_type >::max(), 0, std::numeric_limits< rgba_pixel::component_type >::max())
The green color.
Definition pixel.hpp:145
rgba_pixel white_pixel
The white color.
rgba_pixel transparent_pixel(0, 0, 0, 0)
A transparent color.
Definition pixel.hpp:133
rgba_pixel yellow_pixel
The yellow color.
rgba_pixel magenta_pixel
The magenta color.
This is the main namespace.
Definition algorithm.hpp:34
unsigned_integer_of_size< 32 >::type u_int_32
An unsigned integer on 32 bits.
Definition types.hpp:141
Representation of a pixel in image processing.
component_type red
Red component.
Definition pixel.hpp:53
component_type blue
Blue component.
Definition pixel.hpp:59
bool operator==(const rgb_pixel &that) const
Compare to a pixel.
Definition pixel.cpp:146
unsigned char component_type
The type of the components of the color.
Definition pixel.hpp:47
component_type green
Green component.
Definition pixel.hpp:56
bool operator!=(const rgb_pixel &that) const
Compare to a pixel.
Definition pixel.cpp:168
struct claw::graphic::rgb_pixel::@14 components
Component by component representation.
rgb_pixel()
Default constructor.
Definition pixel.cpp:88
unsigned char component_type
The type of the components of the color.
Definition pixel.hpp:82
rgba_pixel & operator=(const rgb_pixel &that)
Assignement operator.
Definition pixel.cpp:265
component_type red
Red component.
Definition pixel.hpp:93
component_type luminosity() const
Get the luminosity of the pixel.
Definition pixel.cpp:306
struct claw::graphic::rgba_pixel::@15::@17 components
Component by component representation.
bool operator!=(const rgba_pixel &that) const
Tell if two pixels are different.
Definition pixel.cpp:290
bool operator==(const rgba_pixel &that) const
Tell if two pixels are equal.
Definition pixel.cpp:280
component_type green
Green component.
Definition pixel.hpp:96
component_type blue
Blue component.
Definition pixel.hpp:99
unsigned int pixel
Compressed representation.
Definition pixel.hpp:87
rgba_pixel()
Default constructor.
Definition pixel.cpp:190
Some classes for the raw manipulation of the base types.