From ba2514398ae5302a98d0beadd00b03ecdcaa5fb5 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Wed, 24 May 2006 15:13:51 +0000 Subject: [PATCH] implemented to_hex_color() method that returns #RRGGBB color representation. --- include/color.hpp | 139 ++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 66 deletions(-) diff --git a/include/color.hpp b/include/color.hpp index dd52f048a..1d4578432 100644 --- a/include/color.hpp +++ b/include/color.hpp @@ -25,6 +25,7 @@ #ifndef COLOR_HPP #define COLOR_HPP +#include #include "config.hpp" #include @@ -33,78 +34,84 @@ namespace mapnik { class MAPNIK_DECL Color { private: - unsigned int rgba_; + unsigned int rgba_; public: - Color() - :rgba_(0xffffffff) {} + Color() + :rgba_(0xffffffff) {} - Color(int red,int green,int blue,int alpha=0xff) - : rgba_((alpha&0xff) << 24 | (blue&0xff) << 16 | (green&0xff) << 8 | red&0xff) {} + Color(int red,int green,int blue,int alpha=0xff) + : rgba_((alpha&0xff) << 24 | (blue&0xff) << 16 | (green&0xff) << 8 | red&0xff) {} - explicit Color(int rgba) - : rgba_(rgba) {} + explicit Color(int rgba) + : rgba_(rgba) {} - Color(const Color& rhs) - : rgba_(rhs.rgba_) {} + Color(const Color& rhs) + : rgba_(rhs.rgba_) {} - Color& operator=(const Color& rhs) - { - if (this==&rhs) return *this; - rgba_=rhs.rgba_; - return *this; - } - inline unsigned int blue() const - { - return (rgba_>>16)&0xff; - } - inline unsigned int green() const - { - return (rgba_>>8)&0xff; - } - inline unsigned int red() const - { - return rgba_&0xff; - } - - inline void set_red(unsigned int r) - { - rgba_ = (rgba_ & 0xffffff00) | (r&0xff); - } - - inline void set_green(unsigned int g) - { - rgba_ = (rgba_ & 0xffff00ff) | ((g&0xff) << 8); - } - - inline void set_blue(unsigned int b) - { - rgba_ = (rgba_ & 0xff00ffff) | ((b&0xff) << 16); - } - - inline unsigned int alpha() const - { - return (rgba_>>24)&0xff; - } - - inline unsigned int rgba() const - { - return rgba_; - } - inline void set_bgr(unsigned bgr) - { - rgba_ = (rgba_ & 0xff000000) | (bgr & 0xffffff); - } - inline bool operator==(Color const& other) const - { - return rgba_ == other.rgba_; - } - - inline std::string to_string() const + Color& operator=(const Color& rhs) { - std::stringstream ss; - ss << "rgb (" << red() << "," << green() << "," << blue() <<")"; - return ss.str(); - } + if (this==&rhs) return *this; + rgba_=rhs.rgba_; + return *this; + } + inline unsigned int blue() const + { + return (rgba_>>16)&0xff; + } + inline unsigned int green() const + { + return (rgba_>>8)&0xff; + } + inline unsigned int red() const + { + return rgba_&0xff; + } + + inline void set_red(unsigned int r) + { + rgba_ = (rgba_ & 0xffffff00) | (r&0xff); + } + + inline void set_green(unsigned int g) + { + rgba_ = (rgba_ & 0xffff00ff) | ((g&0xff) << 8); + } + + inline void set_blue(unsigned int b) + { + rgba_ = (rgba_ & 0xff00ffff) | ((b&0xff) << 16); + } + + inline unsigned int alpha() const + { + return (rgba_>>24)&0xff; + } + + inline unsigned int rgba() const + { + return rgba_; + } + inline void set_bgr(unsigned bgr) + { + rgba_ = (rgba_ & 0xff000000) | (bgr & 0xffffff); + } + inline bool operator==(Color const& other) const + { + return rgba_ == other.rgba_; + } + + inline std::string to_string() const + { + std::stringstream ss; + ss << "rgb (" << red() << "," << green() << "," << blue() <<")"; + return ss.str(); + } + inline std::string to_hex_string() const + { + std::stringstream ss; + ss << boost::format("#%1$02x%2$02x%3$02x") % red() % green() % blue(); + return ss.str(); + } }; }