pixel.cpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#include <claw/pixel.hpp>
#include <claw/types.hpp>
#include <stdexcept>
#include <limits>
#include <climits>
#include <sstream>
namespace claw
{
namespace graphic
{
rgba_pixel transparent_pixel( 0, 0, 0, 0 );
rgba_pixel black_pixel
( 0, 0, 0, std::numeric_limits<rgba_pixel::component_type>::max() );
rgba_pixel white_pixel
( std::numeric_limits<rgba_pixel::component_type>::max(),
std::numeric_limits<rgba_pixel::component_type>::max(),
std::numeric_limits<rgba_pixel::component_type>::max(),
std::numeric_limits<rgba_pixel::component_type>::max() );
rgba_pixel blue_pixel
( 0, 0, std::numeric_limits<rgba_pixel::component_type>::max(),
std::numeric_limits<rgba_pixel::component_type>::max() );
rgba_pixel green_pixel
( 0, std::numeric_limits<rgba_pixel::component_type>::max(), 0,
std::numeric_limits<rgba_pixel::component_type>::max() );
rgba_pixel red_pixel
( std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
std::numeric_limits<rgba_pixel::component_type>::max() );
rgba_pixel yellow_pixel
( std::numeric_limits<rgba_pixel::component_type>::max(),
std::numeric_limits<rgba_pixel::component_type>::max(), 0,
std::numeric_limits<rgba_pixel::component_type>::max() );
rgba_pixel magenta_pixel
( std::numeric_limits<rgba_pixel::component_type>::max(), 0,
std::numeric_limits<rgba_pixel::component_type>::max(),
std::numeric_limits<rgba_pixel::component_type>::max() );
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() );
} // namespace graphic
} // namespace claw
/*----------------------------------------------------------------------------*/
claw::graphic::rgb_pixel::rgb_pixel()
{
} // rgb_pixel::rgb_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgb_pixel::rgb_pixel
( component_type r, component_type g, component_type b )
{
components.red = r;
components.green = g;
components.blue = b;
} // rgb_pixel::rgb_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgb_pixel::rgb_pixel( const rgba_pixel& p )
{
components.red = p.components.red;
components.green = p.components.green;
components.blue = p.components.blue;
} // rgb_pixel::rgb_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgb_pixel::rgb_pixel( const std::string& c )
{
std::istringstream iss(c);
u_int_32 color;
if ( c[0] == '#' )
iss.ignore(1);
if ( !(iss >> std::hex >> color) )
throw std::invalid_argument(c);
components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
components.green = (color & 0x00FF00) >> CHAR_BIT;
components.blue = color & 0x0000FF;
} // rgb_pixel::rgb_pixel()
/*----------------------------------------------------------------------------*/
bool claw::graphic::rgb_pixel::operator==(const rgb_pixel& that) const
{
return (components.red == that.components.red)
&& (components.green == that.components.green)
&& (components.blue == that.components.blue);
} // rgb_pixel::operator==()
/*----------------------------------------------------------------------------*/
bool claw::graphic::rgb_pixel::operator==(const rgba_pixel& that) const
{
return *this == rgb_pixel(that);
} // rgb_pixel::operator==()
/*----------------------------------------------------------------------------*/
bool claw::graphic::rgb_pixel::operator!=(const rgb_pixel& that) const
{
return !(*this == that);
} // rgb_pixel::operator!=()
/*----------------------------------------------------------------------------*/
bool claw::graphic::rgb_pixel::operator!=(const rgba_pixel& that) const
{
return !(*this == that);
} // rgb_pixel::operator!=()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel::rgba_pixel()
{
} // rgba_pixel::rgba_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel::rgba_pixel( const rgb_pixel& that )
{
components.red = that.components.red;
components.green = that.components.green;
components.blue = that.components.blue;
components.alpha = 255;
} // rgba_pixel::rgba_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel::rgba_pixel
( component_type r, component_type g, component_type b, component_type a )
{
components.red = r;
components.green = g;
components.blue = b;
components.alpha = a;
} // rgba_pixel::rgba_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel::rgba_pixel( const std::string& c )
{
std::istringstream iss(c);
u_int_32 color;
bool has_alpha;
if ( c[0] == '#' )
{
iss.ignore(1);
has_alpha = c.length() > 7;
}
else
has_alpha = c.length() > 6;
if ( !((iss >> std::hex >> color) && (iss.rdbuf()->in_avail() == 0)) )
throw std::invalid_argument(c);
if ( has_alpha )
components.alpha = (color & 0xFF000000) >> (CHAR_BIT * 3);
else
components.alpha = std::numeric_limits<component_type>::max();
components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
components.green = (color & 0x00FF00) >> CHAR_BIT;
components.blue = color & 0x0000FF;
} // rgba_pixel::rgba_pixel()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel&
claw::graphic::rgba_pixel::operator=( const rgb_pixel& that )
{
components.red = that.components.red;
components.green = that.components.green;
components.blue = that.components.blue;
components.alpha = 255;
return *this;
} // rgba_pixel::operator=()
/*----------------------------------------------------------------------------*/
bool claw::graphic::rgba_pixel::operator==( const rgba_pixel& that ) const
{
return pixel == that.pixel;
} // rgba_pixel::operator==()
/*----------------------------------------------------------------------------*/
bool claw::graphic::rgba_pixel::operator!=( const rgba_pixel& that ) const
{
return pixel != that.pixel;
} // rgba_pixel::operator!=()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel::component_type
claw::graphic::rgba_pixel::luminosity() const
{
return ((unsigned int)components.red * 183
+ (unsigned int)components.green * 54
+ (unsigned int)components.blue * 18
) / 256;
} // rgba_pixel::luminosity()
claw::graphic::green_pixel
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
claw::u_int_32
unsigned_integer_of_size< 32 >::type u_int_32
An unsigned integer on 32 bits.
Definition: types.hpp:141
claw::graphic::cyan_pixel
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
claw::graphic::transparent_pixel
rgba_pixel transparent_pixel(0, 0, 0, 0)
A transparent color.
Definition: pixel.hpp:133
claw::graphic::rgba_pixel::red
component_type red
Red component.
Definition: pixel.hpp:93
types.hpp
Some classes for the raw manipulation of the base types.
claw::graphic::rgba_pixel::components
struct claw::graphic::rgba_pixel::@15::@17 components
Component by component representation.
claw::graphic::rgba_pixel
RGBA pixel.
Definition: pixel.hpp:79
claw::graphic::rgb_pixel::operator==
bool operator==(const rgb_pixel &that) const
Compare to a pixel.
Definition: pixel.cpp:146
claw::graphic::black_pixel
rgba_pixel black_pixel(0, 0, 0, std::numeric_limits< rgba_pixel::component_type >::max())
The black color.
Definition: pixel.hpp:136
claw::graphic::rgba_pixel::pixel
unsigned int pixel
Compressed representation.
Definition: pixel.hpp:87
claw::graphic::rgb_pixel::rgb_pixel
rgb_pixel()
Default constructor.
Definition: pixel.cpp:88
claw::graphic::rgba_pixel::operator=
rgba_pixel & operator=(const rgb_pixel &that)
Assignement operator.
Definition: pixel.cpp:265
claw::graphic::rgb_pixel::blue
component_type blue
Blue component.
Definition: pixel.hpp:59
claw::graphic::rgb_pixel::components
struct claw::graphic::rgb_pixel::@14 components
Component by component representation.
claw::graphic::rgba_pixel::operator!=
bool operator!=(const rgba_pixel &that) const
Tell if two pixels are different.
Definition: pixel.cpp:290
claw::graphic::rgb_pixel::red
component_type red
Red component.
Definition: pixel.hpp:53
claw::graphic::yellow_pixel
rgba_pixel yellow_pixel
The yellow color.
claw::graphic::rgba_pixel::luminosity
component_type luminosity() const
Get the luminosity of the pixel.
Definition: pixel.cpp:306
claw::graphic::magenta_pixel
rgba_pixel magenta_pixel
The magenta color.
claw::graphic::red_pixel
rgba_pixel red_pixel
The red color.
claw::graphic::rgb_pixel::operator!=
bool operator!=(const rgb_pixel &that) const
Compare to a pixel.
Definition: pixel.cpp:168
claw::graphic::rgba_pixel::component_type
unsigned char component_type
The type of the components of the color.
Definition: pixel.hpp:82
claw::graphic::rgba_pixel::operator==
bool operator==(const rgba_pixel &that) const
Tell if two pixels are equal.
Definition: pixel.cpp:280
claw::graphic::rgb_pixel
RGB pixel.
Definition: pixel.hpp:44
claw::graphic::rgba_pixel::rgba_pixel
rgba_pixel()
Default constructor.
Definition: pixel.cpp:190
pixel.hpp
Representation of a pixel in image processing.
claw::graphic::rgb_pixel::component_type
unsigned char component_type
The type of the components of the color.
Definition: pixel.hpp:47
claw::graphic::white_pixel
rgba_pixel white_pixel
The white color.
claw
This is the main namespace.
Definition: algorithm.hpp:33
claw::graphic::rgb_pixel::green
component_type green
Green component.
Definition: pixel.hpp:56
claw::graphic::blue_pixel
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