implemented to_hex_color() method that returns

#RRGGBB color representation.
This commit is contained in:
Artem Pavlenko 2006-05-24 15:13:51 +00:00
parent b73ca350fb
commit ba2514398a

View file

@ -25,6 +25,7 @@
#ifndef COLOR_HPP
#define COLOR_HPP
#include <boost/format.hpp>
#include "config.hpp"
#include <sstream>
@ -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();
}
};
}