Otclient  14/8/2020
color.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef COLOR_H
24 #define COLOR_H
25 
26 #include "../stdext/types.h"
27 #include "../stdext/cast.h"
28 #include "../stdext/string.h"
29 #include "../const.h"
30 #include <iomanip>
31 
32 class Color
33 {
34 public:
35  Color() : m_r(1.0f), m_g(1.0f), m_b(1.0f), m_a(1.0f) { }
37  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) { }
38  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) { }
39  Color(float r, float g, float b, float a = 1.0f) : m_r(r), m_g(g), m_b(b), m_a(a) { }
40  Color(const std::string& coltext);
41 
42  Color(const Color &color) = default;
43 
44  uint8 a() const { return m_a*255.0f; }
45  uint8 b() const { return m_b*255.0f; }
46  uint8 g() const { return m_g*255.0f; }
47  uint8 r() const { return m_r*255.0f; }
48 
49  float aF() const { return m_a; }
50  float bF() const { return m_b; }
51  float gF() const { return m_g; }
52  float rF() const { return m_r; }
53 
54  uint32 rgba() const { return uint32(a() | b() << 8 | g() << 16 | r() << 24); }
55 
56  void setRed(int r) { m_r = uint8(r)/255.0f; }
57  void setGreen(int g) { m_g = uint8(g)/255.0f; }
58  void setBlue(int b) { m_b = uint8(b)/255.0f; }
59  void setAlpha(int a) { m_a = uint8(a)/255.0f; }
60 
61  void setRed(float r) { m_r = r; }
62  void setGreen(float g) { m_g = g; }
63  void setBlue(float b) { m_b = b; }
64  void setAlpha(float a) { m_a = a; }
65 
66  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; }
67  void setRGBA(uint32 rgba) { setRGBA((rgba >> 0) & 0xff, (rgba >> 8) & 0xff, (rgba >> 16) & 0xff, (rgba >> 24) & 0xff); }
68 
69  Color operator+(const Color& other) const { return Color(m_r + other.m_r, m_g + other.m_g, m_b + other.m_b, m_a + other.m_a); }
70  Color operator-(const Color& other) const { return Color(m_r - other.m_r, m_g - other.m_g, m_b - other.m_b, m_a - other.m_a); }
71 
72  Color operator*(float v) const { return Color(m_r*v, m_g*v, m_b*v, m_a*v); }
73  Color operator/(float v) const { return Color(m_r/v, m_g/v, m_b/v, m_a/v); }
74 
75  Color& operator=(uint32_t rgba) { setRGBA(rgba); return *this; }
76  bool operator==(uint32_t rgba) const { return this->rgba() == rgba; }
77 
78  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; }
79  bool operator==(const Color& other) const { return other.rgba() == rgba(); }
80  bool operator!=(const Color& other) const { return other.rgba() != rgba(); }
81 
82  static uint8 to8bit(const Color& color) {
83  uint8 c = 0;
84  c += (color.r() / 51) * 36;
85  c += (color.g() / 51) * 6;
86  c += (color.b() / 51);
87  return c;
88  }
89 
90  static Color from8bit(int color) {
91  if(color >= 216 || color <= 0)
92  return Color(0, 0, 0);
93 
94  int r = int(color / 36) % 6 * 51;
95  int g = int(color / 6) % 6 * 51;
96  int b = color % 6 * 51;
97  return Color(r, g, b);
98  }
99 
100  static const Color alpha;
101  static const Color white;
102  static const Color black;
103  static const Color red;
104  static const Color darkRed;
105  static const Color green;
106  static const Color darkGreen;
107  static const Color blue;
108  static const Color darkBlue;
109  static const Color pink;
110  static const Color darkPink;
111  static const Color yellow;
112  static const Color darkYellow;
113  static const Color teal;
114  static const Color darkTeal;
115  static const Color gray;
116  static const Color darkGray;
117  static const Color lightGray;
118  static const Color orange;
119 
120 private:
121  float m_r;
122  float m_g;
123  float m_b;
124  float m_a;
125 };
126 
127 inline std::ostream& operator<<(std::ostream& out, const Color& color)
128 {
129  using namespace std;
130  out << "#" << hex << setfill('0')
131  << setw(2) << (int)color.r()
132  << setw(2) << (int)color.g()
133  << setw(2) << (int)color.b()
134  << setw(2) << (int)color.a();
135  out << dec << setfill(' ');
136  return out;
137 }
138 
139 inline std::istream& operator>>(std::istream& in, Color& color)
140 {
141  using namespace std;
142  std::string tmp;
143 
144  if(in.get() == '#') {
145  in >> tmp;
146 
147  if(tmp.length() == 6 || tmp.length() == 8) {
148  color.setRed((uint8)stdext::hex_to_dec(tmp.substr(0, 2)));
149  color.setGreen((uint8)stdext::hex_to_dec(tmp.substr(2, 2)));
150  color.setBlue((uint8)stdext::hex_to_dec(tmp.substr(4, 2)));
151  if(tmp.length() == 8)
152  color.setAlpha((uint8)stdext::hex_to_dec(tmp.substr(6, 2)));
153  else
154  color.setAlpha(255);
155  } else
156  in.seekg(-(std::istream::streampos)tmp.length()-1, ios_base::cur);
157  } else {
158  in.unget();
159  in >> tmp;
160 
161  if(tmp == "alpha") {
162  color = Color::alpha;
163  } else if(tmp == "black") {
164  color = Color::black;
165  } else if(tmp == "white") {
166  color = Color::white;
167  } else if(tmp == "red") {
168  color = Color::red;
169  } else if(tmp == "darkRed") {
170  color = Color::darkRed;
171  } else if(tmp == "green") {
172  color = Color::green;
173  } else if(tmp == "darkGreen") {
174  color = Color::darkGreen;
175  } else if(tmp == "blue") {
176  color = Color::blue;
177  } else if(tmp == "darkBlue") {
178  color = Color::darkBlue;
179  } else if(tmp == "pink") {
180  color = Color::pink;
181  } else if(tmp == "darkPink") {
182  color = Color::darkPink;
183  } else if(tmp == "yellow") {
184  color = Color::yellow;
185  } else if(tmp == "darkYellow") {
186  color = Color::darkYellow;
187  } else if(tmp == "teal") {
188  color = Color::teal;
189  } else if(tmp == "darkTeal") {
190  color = Color::darkTeal;
191  } else if(tmp == "gray") {
192  color = Color::gray;
193  } else if(tmp == "darkGray") {
194  color = Color::darkGray;
195  } else if(tmp == "lightGray") {
196  color = Color::lightGray;
197  } else if(tmp == "orange") {
198  color = Color::orange;
199  } else {
200  in.seekg(-tmp.length(), ios_base::cur);
201  }
202  }
203  return in;
204 }
205 
206 #endif
Color::g
uint8 g() const
Definition: color.h:46
Color::setAlpha
void setAlpha(int a)
Definition: color.h:59
Color::setGreen
void setGreen(float g)
Definition: color.h:62
Color
Definition: color.h:32
Color::setBlue
void setBlue(float b)
Definition: color.h:63
Color::lightGray
static const Color lightGray
Definition: color.h:117
Color::operator=
Color & operator=(uint32_t rgba)
Definition: color.h:75
Color::operator!=
bool operator!=(const Color &other) const
Definition: color.h:80
Color::setRGBA
void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a=0xFF)
Definition: color.h:66
Color::gray
static const Color gray
Definition: color.h:115
Color::operator+
Color operator+(const Color &other) const
Definition: color.h:69
Color::rF
float rF() const
Definition: color.h:52
uint32
uint32_t uint32
Definition: types.h:35
Color::setRed
void setRed(float r)
Definition: color.h:61
Color::gF
float gF() const
Definition: color.h:51
Color::setRGBA
void setRGBA(uint32 rgba)
Definition: color.h:67
operator>>
std::istream & operator>>(std::istream &in, Color &color)
Definition: color.h:139
Color::Color
Color(float r, float g, float b, float a=1.0f)
Definition: color.h:39
Color::setRed
void setRed(int r)
Definition: color.h:56
Color::setBlue
void setBlue(int b)
Definition: color.h:58
Color::a
uint8 a() const
Definition: color.h:44
Color::darkRed
static const Color darkRed
Definition: color.h:104
Color::green
static const Color green
Definition: color.h:105
Color::orange
static const Color orange
Definition: color.h:118
Color::setGreen
void setGreen(int g)
Definition: color.h:57
Color::darkPink
static const Color darkPink
Definition: color.h:110
Color::darkGreen
static const Color darkGreen
Definition: color.h:106
Color::white
static const Color white
Definition: color.h:101
Color::Color
Color(uint32 rgba)
Definition: color.h:36
Color::darkBlue
static const Color darkBlue
Definition: color.h:108
Color::Color
Color(uint8 r, uint8 g, uint8 b, uint8 a=0xFF)
Definition: color.h:37
Color::Color
Color()
Definition: color.h:35
Color::darkGray
static const Color darkGray
Definition: color.h:116
stdext::hex_to_dec
uint64_t hex_to_dec(const std::string &str)
Definition: string.cpp:67
Color::blue
static const Color blue
Definition: color.h:107
Color::aF
float aF() const
Definition: color.h:49
Color::Color
Color(int r, int g, int b, int a=0xFF)
Definition: color.h:38
Color::black
static const Color black
Definition: color.h:102
Color::setAlpha
void setAlpha(float a)
Definition: color.h:64
Color::operator/
Color operator/(float v) const
Definition: color.h:73
Color::alpha
static const Color alpha
Definition: color.h:100
Color::bF
float bF() const
Definition: color.h:50
std
Definition: packed_vector.h:157
Color::r
uint8 r() const
Definition: color.h:47
Color::darkYellow
static const Color darkYellow
Definition: color.h:112
Color::red
static const Color red
Definition: color.h:103
Color::pink
static const Color pink
Definition: color.h:109
Color::darkTeal
static const Color darkTeal
Definition: color.h:114
Color::teal
static const Color teal
Definition: color.h:113
Color::operator==
bool operator==(const Color &other) const
Definition: color.h:79
Color::from8bit
static Color from8bit(int color)
Definition: color.h:90
Color::to8bit
static uint8 to8bit(const Color &color)
Definition: color.h:82
Color::operator*
Color operator*(float v) const
Definition: color.h:72
Color::rgba
uint32 rgba() const
Definition: color.h:54
Color::b
uint8 b() const
Definition: color.h:45
uint8
uint8_t uint8
Definition: types.h:37
Color::operator==
bool operator==(uint32_t rgba) const
Definition: color.h:76
operator<<
std::ostream & operator<<(std::ostream &out, const Color &color)
Definition: color.h:127
Color::yellow
static const Color yellow
Definition: color.h:111
Color::operator=
Color & operator=(const Color &other)
Definition: color.h:78
Color::operator-
Color operator-(const Color &other) const
Definition: color.h:70