Claw  1.7.3
code/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   {
00046     rgba_pixel transparent_pixel( 0, 0, 0, 0 );
00047 
00048     rgba_pixel black_pixel
00049     ( 0, 0, 0, std::numeric_limits<rgba_pixel::component_type>::max() );
00050     rgba_pixel white_pixel
00051     ( std::numeric_limits<rgba_pixel::component_type>::max(),
00052       std::numeric_limits<rgba_pixel::component_type>::max(),
00053       std::numeric_limits<rgba_pixel::component_type>::max(),
00054       std::numeric_limits<rgba_pixel::component_type>::max() );
00055 
00056     rgba_pixel blue_pixel
00057     ( 0, 0, std::numeric_limits<rgba_pixel::component_type>::max(),
00058       std::numeric_limits<rgba_pixel::component_type>::max() );
00059     rgba_pixel green_pixel
00060     ( 0, std::numeric_limits<rgba_pixel::component_type>::max(), 0,
00061       std::numeric_limits<rgba_pixel::component_type>::max() );
00062     rgba_pixel red_pixel
00063     ( std::numeric_limits<rgba_pixel::component_type>::max(), 0, 0,
00064       std::numeric_limits<rgba_pixel::component_type>::max() );
00065 
00066     rgba_pixel yellow_pixel
00067     ( std::numeric_limits<rgba_pixel::component_type>::max(),
00068       std::numeric_limits<rgba_pixel::component_type>::max(), 0,
00069       std::numeric_limits<rgba_pixel::component_type>::max() );
00070     rgba_pixel magenta_pixel
00071     ( std::numeric_limits<rgba_pixel::component_type>::max(), 0,
00072       std::numeric_limits<rgba_pixel::component_type>::max(),
00073       std::numeric_limits<rgba_pixel::component_type>::max() );
00074     rgba_pixel cyan_pixel
00075     ( 0, std::numeric_limits<rgba_pixel::component_type>::max(),
00076       std::numeric_limits<rgba_pixel::component_type>::max(),
00077       std::numeric_limits<rgba_pixel::component_type>::max() );
00078 
00081   } // namespace graphic
00082 } // namespace claw
00083 
00084 /*----------------------------------------------------------------------------*/
00088 claw::graphic::rgb_pixel::rgb_pixel()
00089 {
00090 
00091 } // rgb_pixel::rgb_pixel()
00092 
00093 /*----------------------------------------------------------------------------*/
00100 claw::graphic::rgb_pixel::rgb_pixel
00101 ( component_type r, component_type g, component_type b )
00102 { 
00103   components.red = r;
00104   components.green = g;
00105   components.blue = b;
00106 } // rgb_pixel::rgb_pixel()
00107 
00108 /*----------------------------------------------------------------------------*/
00113 claw::graphic::rgb_pixel::rgb_pixel( const rgba_pixel& p )
00114 { 
00115   components.red = p.components.red;
00116   components.green = p.components.green;
00117   components.blue = p.components.blue;
00118 } // rgb_pixel::rgb_pixel()
00119 
00120 /*----------------------------------------------------------------------------*/
00125 claw::graphic::rgb_pixel::rgb_pixel( const std::string& c )
00126 {
00127   std::istringstream iss(c);
00128   u_int_32 color;
00129 
00130   if ( c[0] == '#' )
00131     iss.ignore(1);
00132 
00133   if ( !(iss >> std::hex >> color) )
00134     throw std::invalid_argument(c);
00135 
00136   components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
00137   components.green = (color & 0x00FF00) >> CHAR_BIT;
00138   components.blue = color & 0x0000FF;
00139 } // rgb_pixel::rgb_pixel()
00140 
00141 /*----------------------------------------------------------------------------*/
00146 bool claw::graphic::rgb_pixel::operator==(const rgb_pixel& that) const
00147 { 
00148   return (components.red == that.components.red)
00149     && (components.green == that.components.green)
00150     && (components.blue == that.components.blue);
00151 } // rgb_pixel::operator==()
00152 
00153 /*----------------------------------------------------------------------------*/
00158 bool claw::graphic::rgb_pixel::operator==(const rgba_pixel& that) const
00159 { 
00160   return *this == rgb_pixel(that);
00161 } // rgb_pixel::operator==()
00162 
00163 /*----------------------------------------------------------------------------*/
00168 bool claw::graphic::rgb_pixel::operator!=(const rgb_pixel& that) const
00169 { 
00170   return !(*this == that);
00171 } // rgb_pixel::operator!=()
00172 
00173 /*----------------------------------------------------------------------------*/
00178 bool claw::graphic::rgb_pixel::operator!=(const rgba_pixel& that) const
00179 { 
00180   return !(*this == that);
00181 } // rgb_pixel::operator!=()
00182 
00183 
00184 
00185 
00186 /*----------------------------------------------------------------------------*/
00190 claw::graphic::rgba_pixel::rgba_pixel()
00191 {
00192 
00193 } // rgba_pixel::rgba_pixel()
00194 
00195 /*----------------------------------------------------------------------------*/
00201 claw::graphic::rgba_pixel::rgba_pixel( const rgb_pixel& that )
00202 {
00203   components.red = that.components.red;
00204   components.green = that.components.green;
00205   components.blue = that.components.blue;
00206   components.alpha = 255;
00207 } // rgba_pixel::rgba_pixel()
00208 
00209 /*----------------------------------------------------------------------------*/
00217 claw::graphic::rgba_pixel::rgba_pixel
00218 ( component_type r, component_type g, component_type b, component_type a )
00219 { 
00220   components.red = r;
00221   components.green = g;
00222   components.blue = b;
00223   components.alpha = a;
00224 } // rgba_pixel::rgba_pixel()
00225 
00226 /*----------------------------------------------------------------------------*/
00231 claw::graphic::rgba_pixel::rgba_pixel( const std::string& c )
00232 {
00233   std::istringstream iss(c);
00234   u_int_32 color;
00235   bool has_alpha;
00236 
00237   if ( c[0] == '#' )
00238     {
00239       iss.ignore(1);
00240       has_alpha = c.length() > 7;
00241     }
00242   else
00243     has_alpha = c.length() > 6;
00244 
00245   if ( !((iss >> std::hex >> color) && (iss.rdbuf()->in_avail() == 0)) )
00246     throw std::invalid_argument(c);
00247 
00248   if ( has_alpha )
00249     components.alpha = (color & 0xFF000000) >> (CHAR_BIT * 3);
00250   else
00251     components.alpha = std::numeric_limits<component_type>::max();
00252 
00253   components.red = (color & 0xFF0000) >> (CHAR_BIT * 2);
00254   components.green = (color & 0x00FF00) >> CHAR_BIT;
00255   components.blue = color & 0x0000FF;
00256 } // rgba_pixel::rgba_pixel()
00257 
00258 /*----------------------------------------------------------------------------*/
00264 claw::graphic::rgba_pixel&
00265 claw::graphic::rgba_pixel::operator=( const rgb_pixel& that )
00266 {
00267   components.red = that.components.red;
00268   components.green = that.components.green;
00269   components.blue = that.components.blue;
00270   components.alpha = 255;
00271 
00272   return *this;
00273 } // rgba_pixel::operator=()
00274 
00275 /*----------------------------------------------------------------------------*/
00280 bool claw::graphic::rgba_pixel::operator==( const rgba_pixel& that ) const
00281 {
00282   return pixel == that.pixel;
00283 } // rgba_pixel::operator==()
00284 
00285 /*----------------------------------------------------------------------------*/
00290 bool claw::graphic::rgba_pixel::operator!=( const rgba_pixel& that ) const
00291 {
00292   return pixel != that.pixel;
00293 } // rgba_pixel::operator!=()
00294 
00295 /*----------------------------------------------------------------------------*/
00305 claw::graphic::rgba_pixel::component_type
00306 claw::graphic::rgba_pixel::luminosity() const
00307 {
00308   return ((unsigned int)components.red * 183 
00309           + (unsigned int)components.green * 54 
00310           + (unsigned int)components.blue * 18
00311           ) / 256;
00312 } // rgba_pixel::luminosity()