00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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