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 <mapnik/config.hpp>
00029 #include <boost/format.hpp>
00030 #include <sstream>
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 bool operator!=(Color const& other) const
00108 {
00109 return abgr_ != other.abgr_;
00110 }
00111
00112 inline std::string to_string() const
00113 {
00114 std::stringstream ss;
00115 ss << "rgb ("
00116 << red() << ","
00117 << green() << ","
00118 << blue() << ","
00119 << alpha() << ")";
00120 return ss.str();
00121 }
00122
00123 inline std::string to_hex_string() const
00124 {
00125 return (boost::format("#%1$02x%2$02x%3$02x")
00126 % red()
00127 % green()
00128 % blue() ).str();
00129 }
00130 };
00131 }
00132
00133 #endif //COLOR_HPP