2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
//$Id: color.hpp 39 2005-04-10 20:39:53Z pavlenko $
|
|
|
|
|
|
|
|
#ifndef COLOR_HPP
|
|
|
|
#define COLOR_HPP
|
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
#include <boost/format.hpp>
|
2006-03-22 16:52:29 +01:00
|
|
|
#include "config.hpp"
|
2006-02-27 22:57:08 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
2006-03-22 16:52:29 +01:00
|
|
|
class MAPNIK_DECL Color
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
private:
|
2006-09-15 09:56:35 +02:00
|
|
|
unsigned int abgr_;
|
2005-06-14 17:06:59 +02:00
|
|
|
public:
|
2006-05-24 17:13:51 +02:00
|
|
|
Color()
|
2006-09-15 09:56:35 +02:00
|
|
|
:abgr_(0xffffffff) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
Color(int red,int green,int blue,int alpha=0xff)
|
2006-09-17 21:45:01 +02:00
|
|
|
: abgr_((alpha&0xff) << 24 |
|
|
|
|
(blue&0xff) << 16 |
|
|
|
|
(green&0xff) << 8 |
|
|
|
|
red&0xff) {}
|
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
explicit Color(int rgba)
|
2006-09-15 09:56:35 +02:00
|
|
|
: abgr_(rgba) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
Color(const Color& rhs)
|
2006-09-15 09:56:35 +02:00
|
|
|
: abgr_(rhs.abgr_) {}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
Color& operator=(const Color& rhs)
|
|
|
|
{
|
|
|
|
if (this==&rhs) return *this;
|
2006-09-15 09:56:35 +02:00
|
|
|
abgr_=rhs.abgr_;
|
2006-05-24 17:13:51 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2006-09-15 09:56:35 +02:00
|
|
|
inline unsigned int red() const
|
2006-05-24 17:13:51 +02:00
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
return abgr_&0xff;
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
|
|
|
inline unsigned int green() const
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
return (abgr_>>8)&0xff;
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
2006-09-15 09:56:35 +02:00
|
|
|
inline unsigned int blue() const
|
|
|
|
{
|
|
|
|
return (abgr_>>16)&0xff;
|
|
|
|
}
|
|
|
|
inline unsigned int alpha() const
|
2006-05-24 17:13:51 +02:00
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
return (abgr_>>24)&0xff;
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
2005-09-08 15:20:37 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
inline void set_red(unsigned int r)
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
abgr_ = (abgr_ & 0xffffff00) | (r&0xff);
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
|
|
|
inline void set_green(unsigned int g)
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
abgr_ = (abgr_ & 0xffff00ff) | ((g&0xff) << 8);
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
|
|
|
inline void set_blue(unsigned int b)
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
abgr_ = (abgr_ & 0xff00ffff) | ((b&0xff) << 16);
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
2006-09-15 09:56:35 +02:00
|
|
|
inline void set_alpha(unsigned int a)
|
2006-05-24 17:13:51 +02:00
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
abgr_ = (abgr_ & 0x00ffffff | (a&0xff) << 24);
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
2006-09-15 09:56:35 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
inline unsigned int rgba() const
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
return abgr_;
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
|
|
|
inline void set_bgr(unsigned bgr)
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
abgr_ = (abgr_ & 0xff000000) | (bgr & 0xffffff);
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
|
|
|
inline bool operator==(Color const& other) const
|
|
|
|
{
|
2006-09-15 09:56:35 +02:00
|
|
|
return abgr_ == other.abgr_;
|
2006-05-24 17:13:51 +02:00
|
|
|
}
|
2006-09-15 10:19:03 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
inline std::string to_string() const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2006-09-15 10:19:03 +02:00
|
|
|
ss << "rgb ("
|
|
|
|
<< red() << ","
|
|
|
|
<< green() << ","
|
|
|
|
<< blue() << ","
|
|
|
|
<< alpha() << ")";
|
2006-05-24 17:13:51 +02:00
|
|
|
return ss.str();
|
|
|
|
}
|
2006-09-15 10:19:03 +02:00
|
|
|
|
2006-05-24 17:13:51 +02:00
|
|
|
inline std::string to_hex_string() const
|
2006-04-05 10:27:45 +02:00
|
|
|
{
|
2006-05-24 17:13:51 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << boost::format("#%1$02x%2$02x%3$02x") % red() % green() % blue();
|
|
|
|
return ss.str();
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //COLOR_HPP
|