Pioneer
Color.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _COLOR_H
5 #define _COLOR_H
6 
7 #include <SDL_stdinc.h>
8 
9 struct lua_State;
10 
11 struct Color4f {
12  float r, g, b, a;
13  constexpr Color4f() :
14  r(0.f),
15  g(0.f),
16  b(0.f),
17  a(1.f) {}
18  constexpr Color4f(float v_) :
19  r(v_),
20  g(v_),
21  b(v_),
22  a(v_) {}
23  constexpr Color4f(float r_, float g_, float b_) :
24  r(r_),
25  g(g_),
26  b(b_),
27  a(1.f) {}
28  constexpr Color4f(float r_, float g_, float b_, float a_) :
29  r(r_),
30  g(g_),
31  b(b_),
32  a(a_) {}
33  operator float *() { return &r; }
34  operator const float *() const { return &r; }
35  Color4f &operator*=(const float v)
36  {
37  r *= v;
38  g *= v;
39  b *= v;
40  a *= v;
41  return *this;
42  }
43  friend Color4f operator*(const Color4f &c, const float v) { return Color4f(c.r * v, c.g * v, c.b * v, c.a * v); }
44 
45  void ToLuaTable(lua_State *l);
46  static Color4f FromLuaTable(lua_State *l, int idx);
47 
48  float GetLuminance() const;
49 
50  static const Color4f BLACK;
51  static const Color4f WHITE;
52  static const Color4f RED;
53  static const Color4f GREEN;
54  static const Color4f BLUE;
55  static const Color4f YELLOW;
56  static const Color4f GRAY;
57  static const Color4f STEELBLUE;
58  static const Color4f BLANK;
59 };
60 
61 namespace {
62  static const float s_inv255 = 1.0f / 255.0f;
63 #define INV255(n) (Uint8(float(n) * s_inv255))
64 } // namespace
65 
66 struct Color4ub {
67 
68  Uint8 r, g, b, a;
69  constexpr Color4ub() :
70  r(0),
71  g(0),
72  b(0),
73  a(255) {}
74  constexpr Color4ub(Uint8 r_, Uint8 g_, Uint8 b_) :
75  r(r_),
76  g(g_),
77  b(b_),
78  a(255) {}
79  constexpr Color4ub(Uint8 r_, Uint8 g_, Uint8 b_, Uint8 a_) :
80  r(r_),
81  g(g_),
82  b(b_),
83  a(a_) {}
84  constexpr Color4ub(const Color4f &c) :
85  r(Uint8(c.r * 255.f)),
86  g(Uint8(c.g * 255.f)),
87  b(Uint8(c.b * 255.f)),
88  a(Uint8(c.a * 255.f)) {}
89  constexpr Color4ub(const Uint32 rgba) :
90  r((rgba >> 24) & 0xff),
91  g((rgba >> 16) & 0xff),
92  b((rgba >> 8) & 0xff),
93  a(rgba & 0xff) {}
94 
95  operator unsigned char *() { return &r; }
96  operator const unsigned char *() const { return &r; }
97  Color4ub operator+(const Color4ub &c) const { return Color4ub(c.r + r, c.g + g, c.b + b, c.a + a); }
98  Color4ub &operator*=(const float f)
99  {
100  r = Uint8(r * f);
101  g = Uint8(g * f);
102  b = Uint8(b * f);
103  a = Uint8(a * f);
104  return *this;
105  }
107  {
108  r *= INV255(c.r);
109  g *= INV255(c.g);
110  b *= INV255(c.b);
111  a *= INV255(c.a);
112  return *this;
113  }
114  Color4ub operator*(const float f) const { return Color4ub(Uint8(f * r), Uint8(f * g), Uint8(f * b), Uint8(f * a)); }
115  Color4ub operator*(const Color4ub &c) const { return Color4ub(INV255(c.r) * r, INV255(c.g) * g, INV255(c.b) * b, INV255(c.a) * a); }
116  Color4ub operator/(const float f) const { return Color4ub(Uint8(r / f), Uint8(g / f), Uint8(b / f), Uint8(a / f)); }
117 
118  friend bool operator==(const Color4ub &aIn, const Color4ub &bIn) { return ((aIn.r == bIn.r) && (aIn.g == bIn.g) && (aIn.b == bIn.b) && (aIn.a == bIn.a)); }
119  friend bool operator!=(const Color4ub &aIn, const Color4ub &bIn) { return ((aIn.r != bIn.r) || (aIn.g != bIn.g) || (aIn.b != bIn.b) || (aIn.a != bIn.a)); }
120 
121  Color4f ToColor4f() const { return Color4f(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); }
122 
123  void ToLuaTable(lua_State *l);
124  static Color4ub FromLuaTable(lua_State *l, int idx);
125 
126  Uint8 GetLuminance() const;
127  void Shade(float factor)
128  {
129  r = static_cast<Uint8>(r * (1.0f - factor));
130  g = static_cast<Uint8>(g * (1.0f - factor));
131  b = static_cast<Uint8>(b * (1.0f - factor));
132  }
133  void Tint(float factor)
134  {
135  r = static_cast<Uint8>(r + (255.0f - r) * factor);
136  g = static_cast<Uint8>(g + (255.0f - g) * factor);
137  b = static_cast<Uint8>(b + (255.0f - b) * factor);
138  }
139 
140  static const Color4ub BLACK;
141  static const Color4ub WHITE;
142  static const Color4ub RED;
143  static const Color4ub GREEN;
144  static const Color4ub BLUE;
145  static const Color4ub YELLOW;
146  static const Color4ub GRAY;
147  static const Color4ub STEELBLUE;
148  static const Color4ub BLANK;
149  static const Color4ub PINK;
150 };
151 
152 struct Color3ub {
153  Uint8 r, g, b;
154  constexpr Color3ub() :
155  r(0),
156  g(0),
157  b(0) {}
158  constexpr Color3ub(Uint8 v_) :
159  r(v_),
160  g(v_),
161  b(v_) {}
162  constexpr Color3ub(Uint8 r_, Uint8 g_, Uint8 b_) :
163  r(r_),
164  g(g_),
165  b(b_) {}
166  constexpr Color3ub(const Color4f &c) :
167  r(Uint8(c.r * 255.f)),
168  g(Uint8(c.g * 255.f)),
169  b(Uint8(c.b * 255.f)) {}
170 
171  operator unsigned char *() { return &r; }
172  operator const unsigned char *() const { return &r; }
174  {
175  r *= INV255(c.r);
176  g *= INV255(c.g);
177  b *= INV255(c.b);
178  return *this;
179  }
180  Color3ub operator+(const Color3ub &c) const { return Color3ub(c.r + r, c.g + g, c.b + b); }
181  Color3ub operator*(const float f) const { return Color3ub(Uint8(f * r), Uint8(f * g), Uint8(f * b)); }
182  Color3ub operator*(const Color3ub &c) const { return Color3ub(INV255(c.r) * r, INV255(c.g) * g, INV255(c.b) * b); }
183  Color3ub operator/(const float f) const { return Color3ub(Uint8(r / f), Uint8(g / f), Uint8(b / f)); }
184 
185  Color4f ToColor4f() const { return Color4f(r / 255.0f, g / 255.0f, b / 255.0f); }
186 
187  static const Color3ub BLACK;
188  static const Color3ub WHITE;
189  static const Color3ub RED;
190  static const Color3ub GREEN;
191  static const Color3ub BLUE;
192  static const Color3ub YELLOW;
193  static const Color3ub STEELBLUE;
194  static const Color3ub BLANK;
195 };
196 
197 typedef Color4ub Color;
198 
199 #endif /* _COLOR_H */
Color4ub Color
Definition: Color.h:197
#define INV255(n)
Definition: Color.h:63
Definition: Color.h:152
static const Color3ub BLACK
Definition: Color.h:187
static const Color3ub BLUE
Definition: Color.h:191
Color4f ToColor4f() const
Definition: Color.h:185
constexpr Color3ub(const Color4f &c)
Definition: Color.h:166
Color3ub operator*(const float f) const
Definition: Color.h:181
static const Color3ub WHITE
Definition: Color.h:188
static const Color3ub STEELBLUE
Definition: Color.h:193
Uint8 r
Definition: Color.h:153
Uint8 g
Definition: Color.h:153
static const Color3ub GREEN
Definition: Color.h:190
Color3ub & operator*=(const Color3ub &c)
Definition: Color.h:173
Color3ub operator+(const Color3ub &c) const
Definition: Color.h:180
static const Color3ub YELLOW
Definition: Color.h:192
static const Color3ub BLANK
Definition: Color.h:194
Color3ub operator*(const Color3ub &c) const
Definition: Color.h:182
constexpr Color3ub()
Definition: Color.h:154
Color3ub operator/(const float f) const
Definition: Color.h:183
static const Color3ub RED
Definition: Color.h:189
constexpr Color3ub(Uint8 v_)
Definition: Color.h:158
Uint8 b
Definition: Color.h:153
constexpr Color3ub(Uint8 r_, Uint8 g_, Uint8 b_)
Definition: Color.h:162
Definition: Color.h:11
Color4f & operator*=(const float v)
Definition: Color.h:35
void ToLuaTable(lua_State *l)
Definition: Color.cpp:42
static const Color4f WHITE
Definition: Color.h:51
float GetLuminance() const
Definition: Color.cpp:37
float b
Definition: Color.h:12
float a
Definition: Color.h:12
static const Color4f RED
Definition: Color.h:52
static Color4f FromLuaTable(lua_State *l, int idx)
Definition: Color.cpp:61
static const Color4f YELLOW
Definition: Color.h:55
static const Color4f BLANK
Definition: Color.h:58
float r
Definition: Color.h:12
constexpr Color4f(float r_, float g_, float b_, float a_)
Definition: Color.h:28
float g
Definition: Color.h:12
friend Color4f operator*(const Color4f &c, const float v)
Definition: Color.h:43
constexpr Color4f(float r_, float g_, float b_)
Definition: Color.h:23
static const Color4f STEELBLUE
Definition: Color.h:57
constexpr Color4f()
Definition: Color.h:13
constexpr Color4f(float v_)
Definition: Color.h:18
static const Color4f GREEN
Definition: Color.h:53
static const Color4f BLACK
Definition: Color.h:50
static const Color4f BLUE
Definition: Color.h:54
static const Color4f GRAY
Definition: Color.h:56
Definition: Color.h:66
void Shade(float factor)
Definition: Color.h:127
Color4ub operator/(const float f) const
Definition: Color.h:116
Color4ub operator*(const Color4ub &c) const
Definition: Color.h:115
static const Color4ub WHITE
Definition: Color.h:141
static const Color4ub YELLOW
Definition: Color.h:145
static const Color4ub GRAY
Definition: Color.h:146
friend bool operator!=(const Color4ub &aIn, const Color4ub &bIn)
Definition: Color.h:119
void Tint(float factor)
Definition: Color.h:133
constexpr Color4ub()
Definition: Color.h:69
Color4ub & operator*=(const float f)
Definition: Color.h:98
Color4f ToColor4f() const
Definition: Color.h:121
static const Color4ub RED
Definition: Color.h:142
static const Color4ub STEELBLUE
Definition: Color.h:147
Uint8 a
Definition: Color.h:68
constexpr Color4ub(Uint8 r_, Uint8 g_, Uint8 b_, Uint8 a_)
Definition: Color.h:79
static const Color4ub BLACK
Definition: Color.h:140
static const Color4ub GREEN
Definition: Color.h:143
friend bool operator==(const Color4ub &aIn, const Color4ub &bIn)
Definition: Color.h:118
static const Color4ub BLUE
Definition: Color.h:144
Color4ub operator*(const float f) const
Definition: Color.h:114
static const Color4ub BLANK
Definition: Color.h:148
Color4ub & operator*=(const Color4ub &c)
Definition: Color.h:106
static Color4ub FromLuaTable(lua_State *l, int idx)
Definition: Color.cpp:88
Uint8 GetLuminance() const
Definition: Color.cpp:106
void ToLuaTable(lua_State *l)
Definition: Color.cpp:79
Uint8 b
Definition: Color.h:68
static const Color4ub PINK
Definition: Color.h:149
Uint8 g
Definition: Color.h:68
constexpr Color4ub(const Color4f &c)
Definition: Color.h:84
Uint8 r
Definition: Color.h:68
Color4ub operator+(const Color4ub &c) const
Definition: Color.h:97
constexpr Color4ub(Uint8 r_, Uint8 g_, Uint8 b_)
Definition: Color.h:74
constexpr Color4ub(const Uint32 rgba)
Definition: Color.h:89