/home/andreas/src/svn/mapnik/include/mapnik/color.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * 
00003  * This file is part of Mapnik (c++ mapping toolkit)
00004  *
00005  * Copyright (C) 2006 Artem Pavlenko
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  *****************************************************************************/
00022 
00023 //$Id: color.hpp 39 2005-04-10 20:39:53Z pavlenko $
00024 
00025 #ifndef COLOR_HPP
00026 #define COLOR_HPP
00027 
00028 #include <sstream>
00029 #include <boost/format.hpp>
00030 #include <mapnik/config.hpp>
00031 
00032 namespace mapnik {
00033 
00034     class MAPNIK_DECL Color
00035     {
00036     private:
00037         unsigned int abgr_;
00038     public:
00039         Color()
00040             :abgr_(0xffffffff) {}
00041 
00042         Color(int red,int green,int blue,int alpha=0xff)
00043             : abgr_((alpha&0xff) << 24 | 
00044                     (blue&0xff) << 16  | 
00045                     (green&0xff) << 8  | 
00046                     red&0xff) {}
00047         
00048         explicit Color(int rgba)
00049             : abgr_(rgba) {}
00050 
00051         Color(const Color& rhs)
00052             : abgr_(rhs.abgr_) {}
00053 
00054         Color& operator=(const Color& rhs)
00055         {
00056             if (this==&rhs) return *this;
00057             abgr_=rhs.abgr_;
00058             return *this;
00059         }
00060         inline unsigned int red() const
00061         {
00062             return abgr_&0xff;
00063         }
00064         inline unsigned int green() const
00065         {
00066             return (abgr_>>8)&0xff;
00067         }
00068         inline unsigned int blue() const
00069         {
00070             return (abgr_>>16)&0xff;
00071         }
00072         inline unsigned int alpha() const
00073         {
00074             return (abgr_>>24)&0xff;
00075         }
00076         
00077         inline void set_red(unsigned int r)
00078         {
00079             abgr_ = (abgr_ & 0xffffff00) | (r&0xff);
00080         }
00081         inline void set_green(unsigned int g)
00082         {
00083             abgr_ = (abgr_ & 0xffff00ff) | ((g&0xff) << 8);
00084         }
00085         inline void set_blue(unsigned int b)
00086         {
00087             abgr_ = (abgr_ & 0xff00ffff) | ((b&0xff) << 16);
00088         }
00089         inline void set_alpha(unsigned int a)
00090         {
00091             abgr_ = (abgr_ & 0x00ffffff | (a&0xff) << 24);
00092         }
00093 
00094         inline unsigned int rgba() const
00095         {
00096             return abgr_;
00097         }
00098         inline void set_bgr(unsigned bgr)
00099         {
00100             abgr_ = (abgr_ & 0xff000000) | (bgr & 0xffffff);
00101         }
00102         inline bool operator==(Color const& other) const
00103         {
00104             return abgr_ == other.abgr_;
00105         }
00106         
00107         inline std::string to_string() const
00108         {
00109             std::stringstream ss;
00110             ss << "rgb (" 
00111                << red()   << ","  
00112                << green() << ","  
00113                << blue()  << ","
00114                << alpha() << ")";
00115             return ss.str();
00116         }
00117         
00118         inline std::string to_hex_string() const
00119         {
00120           return (boost::format("#%1$02x%2$02x%3$02x") 
00121                   % red() 
00122                   % green() 
00123                   % blue() ).str();
00124         }
00125     };    
00126 }
00127 
00128 #endif //COLOR_HPP

Generated on Thu Jul 19 17:59:25 2007 for Mapnik by  doxygen 1.4.7