Claw 1.7.0
pixel.cpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00030 #include <claw/pixel.hpp>
00031 
00032 #include <claw/types.hpp>
00033 
00034 #include <stdexcept>
00035 #include <limits>
00036 #include <climits>
00037 #include <sstream>
00038 
00039 namespace claw
00040 {
00041   namespace graphic
00042   {
00043     rgba_pixel transparent_pixel( 0, 0, 0, 0 );
00044 
00045     rgba_pixel black_pixel
00046     ( 0, 0, 0, std::numeric_limits<rgba_pixel::component_type>::max() );
00047     rgba_pixel white_pixel
00048     ( std::numeric_limits<rgba_pixel::component_type>::max(),
00049       std::numeric_limits<rgba_pixel::component_type>::max(),
00050       std::numeric_limits<rgba_pixel::component_type>::max(),
00051       std::numeric_limits<rgba_pixel::component_type>::max() );
00052 
00053     rgba_pixel blue_pixel
00054     ( 0, 0, std::numeric_limits<rgba_pixel::component_type>::max(),
00055       std::numeric_limits<rgba_pixel::component_type>::max() );
00056     rgba_pixel green_pixel
00057     ( 0, std::numeric_limits<rgba_pixel::component_type>::max(), 0,
00058       std::numeric_limits<rgba_pixel::component_type>::max() );
00059     rgba_pixel red_pixel
00060     ( std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
00061       std::numeric_limits<rgba_pixel::component_type>::max() );
00062 
00063     rgba_pixel yellow_pixel
00064     ( std::numeric_limits<rgba_pixel::component_type>::max(),
00065       std::numeric_limits<rgba_pixel::component_type>::max(), 0,
00066       std::numeric_limits<rgba_pixel::component_type>::max() );
00067     rgba_pixel magenta_pixel
00068     ( std::numeric_limits<rgba_pixel::component_type>::max(), 0,
00069       std::numeric_limits<rgba_pixel::component_type>::max(),
00070       std::numeric_limits<rgba_pixel::component_type>::max() );
00071     rgba_pixel cyan_pixel
00072     ( 0, std::numeric_limits<rgba_pixel::component_type>::max(),
00073       std::numeric_limits<rgba_pixel::component_type>::max(),
00074       std::numeric_limits<rgba_pixel::component_type>::max() );
00075 
00076   } // namespace graphic
00077 } // namespace claw
00078 
00079 /*----------------------------------------------------------------------------*/
00083 claw::graphic::rgb_pixel::rgb_pixel()
00084 {
00085 
00086 } // rgb_pixel::rgb_pixel()
00087 
00088 /*----------------------------------------------------------------------------*/
00095 claw::graphic::rgb_pixel::rgb_pixel
00096 ( component_type r, component_type g, component_type b )
00097 { 
00098   components.red = r;
00099   components.green = g;
00100   components.blue = b;
00101 } // rgb_pixel::rgb_pixel()
00102 
00103 /*----------------------------------------------------------------------------*/
00108 claw::graphic::rgb_pixel::rgb_pixel( const rgba_pixel& p )
00109 { 
00110   components.red = p.components.red;
00111   components.green = p.components.green;
00112   components.blue = p.components.blue;
00113 } // rgb_pixel::rgb_pixel()
00114 
00115 /*----------------------------------------------------------------------------*/
00120 claw::graphic::rgb_pixel::rgb_pixel( const std::string& c )
00121 {
00122   std::istringstream iss(c);
00123   u_int_32 color;
00124 
00125   if ( c[0] == '#' )
00126     iss.ignore(1);
00127 
00128   if ( !(iss >> std::hex >> color) )
00129     throw std::invalid_argument(c);
00130 
00131   components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
00132   components.green = (color & 0x00FF00) >> CHAR_BIT;
00133   components.blue = color & 0x0000FF;
00134 } // rgb_pixel::rgb_pixel()
00135 
00136 /*----------------------------------------------------------------------------*/
00141 bool claw::graphic::rgb_pixel::operator==(const rgb_pixel& that) const
00142 { 
00143   return (components.red == that.components.red)
00144     && (components.green == that.components.green)
00145     && (components.blue == that.components.blue);
00146 } // rgb_pixel::operator==()
00147 
00148 /*----------------------------------------------------------------------------*/
00153 bool claw::graphic::rgb_pixel::operator==(const rgba_pixel& that) const
00154 { 
00155   return *this == rgb_pixel(that);
00156 } // rgb_pixel::operator==()
00157 
00158 /*----------------------------------------------------------------------------*/
00163 bool claw::graphic::rgb_pixel::operator!=(const rgb_pixel& that) const
00164 { 
00165   return !(*this == that);
00166 } // rgb_pixel::operator!=()
00167 
00168 /*----------------------------------------------------------------------------*/
00173 bool claw::graphic::rgb_pixel::operator!=(const rgba_pixel& that) const
00174 { 
00175   return !(*this == that);
00176 } // rgb_pixel::operator!=()
00177 
00178 
00179 
00180 
00181 /*----------------------------------------------------------------------------*/
00185 claw::graphic::rgba_pixel::rgba_pixel()
00186 {
00187 
00188 } // rgba_pixel::rgba_pixel()
00189 
00190 /*----------------------------------------------------------------------------*/
00196 claw::graphic::rgba_pixel::rgba_pixel( const rgb_pixel& that )
00197 {
00198   components.red = that.components.red;
00199   components.green = that.components.green;
00200   components.blue = that.components.blue;
00201   components.alpha = 255;
00202 } // rgba_pixel::rgba_pixel()
00203 
00204 /*----------------------------------------------------------------------------*/
00212 claw::graphic::rgba_pixel::rgba_pixel
00213 ( component_type r, component_type g, component_type b, component_type a )
00214 { 
00215   components.red = r;
00216   components.green = g;
00217   components.blue = b;
00218   components.alpha = a;
00219 } // rgba_pixel::rgba_pixel()
00220 
00221 /*----------------------------------------------------------------------------*/
00226 claw::graphic::rgba_pixel::rgba_pixel( const std::string& c )
00227 {
00228   std::istringstream iss(c);
00229   u_int_32 color;
00230   bool has_alpha;
00231 
00232   if ( c[0] == '#' )
00233     {
00234       iss.ignore(1);
00235       has_alpha = c.length() > 7;
00236     }
00237   else
00238     has_alpha = c.length() > 6;
00239 
00240   if ( !((iss >> std::hex >> color) && (iss.rdbuf()->in_avail() == 0)) )
00241     throw std::invalid_argument(c);
00242 
00243   if ( has_alpha )
00244     components.alpha = (color & 0xFF000000) >> (CHAR_BIT * 3);
00245   else
00246     components.alpha = std::numeric_limits<component_type>::max();
00247 
00248   components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
00249   components.green = (color & 0x00FF00) >> CHAR_BIT;
00250   components.blue = color & 0x0000FF;
00251 } // rgba_pixel::rgba_pixel()
00252 
00253 /*----------------------------------------------------------------------------*/
00259 claw::graphic::rgba_pixel&
00260 claw::graphic::rgba_pixel::operator=( const rgb_pixel& that )
00261 {
00262   components.red = that.components.red;
00263   components.green = that.components.green;
00264   components.blue = that.components.blue;
00265   components.alpha = 255;
00266 
00267   return *this;
00268 } // rgba_pixel::operator=()
00269 
00270 /*----------------------------------------------------------------------------*/
00275 bool claw::graphic::rgba_pixel::operator==( const rgba_pixel& that ) const
00276 {
00277   return pixel == that.pixel;
00278 } // rgba_pixel::operator==()
00279 
00280 /*----------------------------------------------------------------------------*/
00285 bool claw::graphic::rgba_pixel::operator!=( const rgba_pixel& that ) const
00286 {
00287   return pixel != that.pixel;
00288 } // rgba_pixel::operator!=()
00289 
00290 /*----------------------------------------------------------------------------*/
00300 claw::graphic::rgba_pixel::component_type
00301 claw::graphic::rgba_pixel::luminosity() const
00302 {
00303   return ((unsigned int)components.red * 183 
00304           + (unsigned int)components.green * 54 
00305           + (unsigned int)components.blue * 18
00306           ) / 256;
00307 } // rgba_pixel::luminosity()