claw 1.9.0
 
Loading...
Searching...
No Matches
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*/
31
32#include <claw/types.hpp>
33
34#include <climits>
35#include <limits>
36#include <sstream>
37#include <stdexcept>
38
39namespace claw
40{
41 namespace graphic
42 {
47
49 black_pixel(0, 0, 0,
50 std::numeric_limits<rgba_pixel::component_type>::max());
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
58 blue_pixel(0, 0,
59 std::numeric_limits<rgba_pixel::component_type>::max(),
60 std::numeric_limits<rgba_pixel::component_type>::max());
62 green_pixel(0, std::numeric_limits<rgba_pixel::component_type>::max(),
63 0, std::numeric_limits<rgba_pixel::component_type>::max());
65 red_pixel(std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
66 std::numeric_limits<rgba_pixel::component_type>::max());
67
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());
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());
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
83
84 }
85}
86
92
106
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
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 cyan_pixel
The cyan color.
rgba_pixel green_pixel
The green color.
rgba_pixel transparent_pixel
A transparent color.
rgba_pixel red_pixel
The red color.
rgba_pixel white_pixel
The white color.
rgba_pixel black_pixel
The black color.
rgba_pixel yellow_pixel
The yellow color.
rgba_pixel magenta_pixel
The magenta color.
rgba_pixel blue_pixel
The blue color.
Everything about image structures and processing.
Definition claw.hpp:58
This is the main namespace.
unsigned_integer_of_size< 32 >::type u_int_32
An unsigned integer on 32 bits.
Definition types.hpp:138
Representation of a pixel in image processing.
struct claw::graphic::rgb_pixel::@217001364276256254254300335037210026331301104326 components
Component by component representation.
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:142
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:162
rgb_pixel()
Default constructor.
Definition pixel.cpp:90
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:248
component_type red
Red component.
Definition pixel.hpp:93
component_type luminosity() const
Get the luminosity of the pixel.
Definition pixel.cpp:286
struct claw::graphic::rgba_pixel::@243011111317154221214371326113376113353054140240::@062221254366262347154034240363361326201063204322 components
Component by component representation.
bool operator!=(const rgba_pixel &that) const
Tell if two pixels are different.
Definition pixel.cpp:271
bool operator==(const rgba_pixel &that) const
Tell if two pixels are equal.
Definition pixel.cpp:262
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:179
Some classes for the raw manipulation of the base types.