2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
#ifndef COLOR_H
|
|
|
|
#define COLOR_H
|
|
|
|
|
2012-01-05 02:28:29 +01:00
|
|
|
#include "../util/types.h"
|
|
|
|
#include "../util/tools.h"
|
2011-08-28 19:49:15 +02:00
|
|
|
#include "../const.h"
|
2011-08-28 15:17:58 +02:00
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
class Color
|
|
|
|
{
|
|
|
|
public:
|
2012-03-19 22:56:07 +01:00
|
|
|
Color() : m_r(1.0f), m_g(1.0f), m_b(1.0f), m_a(1.0f) { }
|
|
|
|
Color(uint32 rgba) { setRGBA(rgba); }
|
|
|
|
Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : m_r(r/255.0f), m_g(g/255.0f), m_b(b/255.0f), m_a(a/255.0f) { }
|
2012-04-07 01:12:46 +02:00
|
|
|
Color(int r, int g, int b, int a = 0xFF) : m_r(r/255.0f), m_g(g/255.0f), m_b(b/255.0f), m_a(a/255.0f) { }
|
|
|
|
Color(float r, float g, float b, float a = 1.0f) : m_r(r), m_g(g), m_b(b), m_a(a) { }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
uint8 a() const { return m_a*255.0f; }
|
|
|
|
uint8 b() const { return m_b*255.0f; }
|
|
|
|
uint8 g() const { return m_g*255.0f; }
|
|
|
|
uint8 r() const { return m_r*255.0f; }
|
2011-08-28 15:17:58 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
float aF() const { return m_a; }
|
|
|
|
float bF() const { return m_b; }
|
|
|
|
float gF() const { return m_g; }
|
|
|
|
float rF() const { return m_r; }
|
2012-01-24 19:39:16 +01:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
uint32 rgba() const { return uint32(a() | b() << 8 | g() << 16 | r() << 24); }
|
2011-08-28 15:17:58 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
void setRed(int r) { m_r = uint8(r)/255.0f; }
|
|
|
|
void setGreen(int g) { m_g = uint8(g)/255.0f; }
|
|
|
|
void setBlue(int b) { m_b = uint8(b)/255.0f; }
|
|
|
|
void setAlpha(int a) { m_a = uint8(a)/255.0f; }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
void setRed(float r) { m_r = r; }
|
|
|
|
void setGreen(float g) { m_g = g; }
|
|
|
|
void setBlue(float b) { m_b = b; }
|
|
|
|
void setAlpha(float a) { m_a = a; }
|
2011-08-19 15:23:35 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { m_r = r/255.0f; m_g = g/255.0f; m_b = b/255.0f; m_a = a/255.0f; }
|
|
|
|
void setRGBA(uint32 rgba) { setRGBA((rgba >> 0) & 0xff, (rgba >> 8) & 0xff, (rgba >> 16) & 0xff, (rgba >> 24) & 0xff); }
|
2011-08-19 15:23:35 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
Color& operator=(uint32_t rgba) { setRGBA(rgba); return *this; }
|
|
|
|
bool operator==(uint32_t rgba) const { return this->rgba() == rgba; }
|
2011-08-14 04:09:11 +02:00
|
|
|
|
2012-03-19 22:56:07 +01:00
|
|
|
Color& operator=(const Color& other) { m_r = other.m_r; m_g = other.m_g; m_b = other.m_b; m_a = other.m_a; return *this; }
|
|
|
|
bool operator==(const Color& other) const { return other.m_r == m_r && other.m_g == m_g && other.m_b == m_b && other.m_a == m_a; }
|
|
|
|
bool operator!=(const Color& other) const { return other.m_r != m_r || other.m_g != m_g || other.m_b != m_b || other.m_a != m_a; }
|
2010-11-25 00:36:15 +01:00
|
|
|
|
2011-12-26 12:53:16 +01:00
|
|
|
static Color from8bit(int color) {
|
|
|
|
if(color >= 216 || color <= 0)
|
|
|
|
return Color(0, 0, 0);
|
|
|
|
|
|
|
|
int r = int(color / 36) % 6 * 51;
|
|
|
|
int g = int(color / 6) % 6 * 51;
|
|
|
|
int b = color % 6 * 51;
|
|
|
|
return Color(r, g, b);
|
|
|
|
}
|
|
|
|
|
2012-03-20 16:17:10 +01:00
|
|
|
static const Color alpha;
|
|
|
|
static const Color white;
|
|
|
|
static const Color black;
|
|
|
|
static const Color red;
|
|
|
|
static const Color darkRed;
|
|
|
|
static const Color green;
|
|
|
|
static const Color darkGreen;
|
|
|
|
static const Color blue;
|
|
|
|
static const Color darkBlue;
|
|
|
|
static const Color pink;
|
|
|
|
static const Color darkPink;
|
|
|
|
static const Color yellow;
|
|
|
|
static const Color darkYellow;
|
|
|
|
static const Color teal;
|
|
|
|
static const Color darkTeal;
|
|
|
|
static const Color gray;
|
|
|
|
static const Color darkGray;
|
|
|
|
static const Color lightGray;
|
|
|
|
static const Color orange;
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
private:
|
2012-03-19 22:56:07 +01:00
|
|
|
float m_r;
|
|
|
|
float m_g;
|
|
|
|
float m_b;
|
|
|
|
float m_a;
|
2010-11-25 00:36:15 +01:00
|
|
|
};
|
|
|
|
|
2011-04-10 22:40:44 +02:00
|
|
|
inline std::ostream& operator<<(std::ostream& out, const Color& color)
|
|
|
|
{
|
2011-08-15 21:15:49 +02:00
|
|
|
using namespace std;
|
|
|
|
out << "#" << hex << setfill('0')
|
|
|
|
<< setw(2) << (int)color.r()
|
|
|
|
<< setw(2) << (int)color.g()
|
|
|
|
<< setw(2) << (int)color.b()
|
|
|
|
<< setw(2) << (int)color.a();
|
|
|
|
out << dec << setfill(' ');
|
2011-04-10 22:40:44 +02:00
|
|
|
return out;
|
2011-08-14 19:45:25 +02:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
|
|
|
|
inline std::istream& operator>>(std::istream& in, Color& color)
|
|
|
|
{
|
2011-08-15 21:15:49 +02:00
|
|
|
using namespace std;
|
2011-08-28 19:49:15 +02:00
|
|
|
std::string tmp;
|
2011-08-15 21:15:49 +02:00
|
|
|
|
|
|
|
if(in.get() == '#') {
|
2011-08-20 22:30:41 +02:00
|
|
|
in >> tmp;
|
|
|
|
|
|
|
|
if(tmp.length() == 6 || tmp.length() == 8) {
|
2011-08-28 18:02:26 +02:00
|
|
|
color.setRed((uint8)Fw::hex2dec(tmp.substr(0, 2)));
|
|
|
|
color.setGreen((uint8)Fw::hex2dec(tmp.substr(2, 2)));
|
|
|
|
color.setBlue((uint8)Fw::hex2dec(tmp.substr(4, 2)));
|
2011-08-20 22:30:41 +02:00
|
|
|
if(tmp.length() == 8)
|
2011-08-28 18:02:26 +02:00
|
|
|
color.setAlpha((uint8)Fw::hex2dec(tmp.substr(6, 2)));
|
2011-08-20 22:30:41 +02:00
|
|
|
else
|
|
|
|
color.setAlpha(255);
|
|
|
|
} else
|
|
|
|
in.seekg(-tmp.length()-1, ios_base::cur);
|
2011-08-28 19:49:15 +02:00
|
|
|
} else {
|
|
|
|
in.unget();
|
|
|
|
in >> tmp;
|
|
|
|
|
|
|
|
if(tmp == "alpha") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::alpha;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "black") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::black;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "white") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::white;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "red") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::red;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkRed") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkRed;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "green") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::green;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkGreen") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkGreen;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "blue") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::blue;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkBlue") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkBlue;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "pink") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::pink;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkPink") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkPink;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "yellow") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::yellow;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkYellow") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkYellow;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "teal") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::teal;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkTeal") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkTeal;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "gray") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::gray;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "darkGray") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::darkGray;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else if(tmp == "lightGray") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::lightGray;
|
2011-12-29 18:45:59 +01:00
|
|
|
} else if(tmp == "orange") {
|
2012-03-20 16:17:10 +01:00
|
|
|
color = Color::orange;
|
2011-08-28 19:49:15 +02:00
|
|
|
} else {
|
|
|
|
in.seekg(-tmp.length(), ios_base::cur);
|
|
|
|
}
|
2011-08-15 21:15:49 +02:00
|
|
|
}
|
2011-08-14 04:09:11 +02:00
|
|
|
return in;
|
2011-04-10 22:40:44 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#endif
|