Pioneer
Loading...
Searching...
No Matches
Color.h
Go to the documentation of this file.
1// Copyright © 2008-2023 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
9struct lua_State;
10
11struct 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
61namespace {
62 static const float s_inv255 = 1.0f / 255.0f;
63#define INV255(n) (Uint8(float(n) * s_inv255))
64} // namespace
65
66struct 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 constexpr Color4ub(const Color4ub &c, const Uint8 a) :
95 r(c.r),
96 g(c.g),
97 b(c.b),
98 a(a) {}
99
100 operator unsigned char *() { return &r; }
101 operator const unsigned char *() const { return &r; }
102 Color4ub operator+(const Color4ub &c) const { return Color4ub(c.r + r, c.g + g, c.b + b, c.a + a); }
103 Color4ub &operator*=(const float f)
104 {
105 r = Uint8(r * f);
106 g = Uint8(g * f);
107 b = Uint8(b * f);
108 a = Uint8(a * f);
109 return *this;
110 }
112 {
113 r *= INV255(c.r);
114 g *= INV255(c.g);
115 b *= INV255(c.b);
116 a *= INV255(c.a);
117 return *this;
118 }
119 Color4ub operator*(const float f) const { return Color4ub(Uint8(f * r), Uint8(f * g), Uint8(f * b), Uint8(f * a)); }
120 Color4ub operator*(const Color4ub &c) const { return Color4ub(INV255(c.r) * r, INV255(c.g) * g, INV255(c.b) * b, INV255(c.a) * a); }
121 Color4ub operator/(const float f) const { return Color4ub(Uint8(r / f), Uint8(g / f), Uint8(b / f), Uint8(a / f)); }
122
123 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)); }
124 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)); }
125
126 Color4f ToColor4f() const { return Color4f(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); }
127
128 void ToLuaTable(lua_State *l);
129 static Color4ub FromLuaTable(lua_State *l, int idx);
130
131 Uint8 GetLuminance() const;
132 Color4ub Shade(float factor)
133 {
134 Color4ub out = *this;
135 out.r = static_cast<Uint8>(r * (1.0f - factor));
136 out.g = static_cast<Uint8>(g * (1.0f - factor));
137 out.b = static_cast<Uint8>(b * (1.0f - factor));
138 return out;
139 }
140 Color4ub Tint(float factor)
141 {
142 Color4ub out = *this;
143 out.r = static_cast<Uint8>(r + (255.0f - r) * factor);
144 out.g = static_cast<Uint8>(g + (255.0f - g) * factor);
145 out.b = static_cast<Uint8>(b + (255.0f - b) * factor);
146 return out;
147 }
148 Color4ub Opacity(float factor)
149 {
150 Color4ub out = *this;
151 out.a = static_cast<Uint8>(factor <= 1.0 ? factor * 255 : uint8_t(factor));
152 return out;
153 }
154
155 static const Color4ub BLACK;
156 static const Color4ub WHITE;
157 static const Color4ub RED;
158 static const Color4ub GREEN;
159 static const Color4ub BLUE;
160 static const Color4ub YELLOW;
161 static const Color4ub GRAY;
162 static const Color4ub STEELBLUE;
163 static const Color4ub BLANK;
164 static const Color4ub PINK;
165};
166
167struct Color3ub {
168 Uint8 r, g, b;
169 constexpr Color3ub() :
170 r(0),
171 g(0),
172 b(0) {}
173 constexpr Color3ub(Uint8 v_) :
174 r(v_),
175 g(v_),
176 b(v_) {}
177 constexpr Color3ub(Uint8 r_, Uint8 g_, Uint8 b_) :
178 r(r_),
179 g(g_),
180 b(b_) {}
181 constexpr Color3ub(const Color4f &c) :
182 r(Uint8(c.r * 255.f)),
183 g(Uint8(c.g * 255.f)),
184 b(Uint8(c.b * 255.f)) {}
185
186 operator unsigned char *() { return &r; }
187 operator const unsigned char *() const { return &r; }
189 {
190 r *= INV255(c.r);
191 g *= INV255(c.g);
192 b *= INV255(c.b);
193 return *this;
194 }
195 Color3ub operator+(const Color3ub &c) const { return Color3ub(c.r + r, c.g + g, c.b + b); }
196 Color3ub operator*(const float f) const { return Color3ub(Uint8(f * r), Uint8(f * g), Uint8(f * b)); }
197 Color3ub operator*(const Color3ub &c) const { return Color3ub(INV255(c.r) * r, INV255(c.g) * g, INV255(c.b) * b); }
198 Color3ub operator/(const float f) const { return Color3ub(Uint8(r / f), Uint8(g / f), Uint8(b / f)); }
199
200 Color4f ToColor4f() const { return Color4f(r / 255.0f, g / 255.0f, b / 255.0f); }
201
202 static const Color3ub BLACK;
203 static const Color3ub WHITE;
204 static const Color3ub RED;
205 static const Color3ub GREEN;
206 static const Color3ub BLUE;
207 static const Color3ub YELLOW;
208 static const Color3ub STEELBLUE;
209 static const Color3ub BLANK;
210};
211
213
214#endif /* _COLOR_H */
Color4ub Color
Definition Color.h:212
#define INV255(n)
Definition Color.h:63
Definition Color.h:167
static const Color3ub BLACK
Definition Color.h:202
static const Color3ub BLUE
Definition Color.h:206
Color3ub & operator*=(const Color3ub &c)
Definition Color.h:188
Color4f ToColor4f() const
Definition Color.h:200
constexpr Color3ub(const Color4f &c)
Definition Color.h:181
Color3ub operator*(const float f) const
Definition Color.h:196
static const Color3ub WHITE
Definition Color.h:203
static const Color3ub STEELBLUE
Definition Color.h:208
Uint8 r
Definition Color.h:168
Uint8 g
Definition Color.h:168
static const Color3ub GREEN
Definition Color.h:205
Color3ub operator+(const Color3ub &c) const
Definition Color.h:195
static const Color3ub YELLOW
Definition Color.h:207
static const Color3ub BLANK
Definition Color.h:209
Color3ub operator*(const Color3ub &c) const
Definition Color.h:197
constexpr Color3ub()
Definition Color.h:169
Color3ub operator/(const float f) const
Definition Color.h:198
static const Color3ub RED
Definition Color.h:204
constexpr Color3ub(Uint8 v_)
Definition Color.h:173
Uint8 b
Definition Color.h:168
constexpr Color3ub(Uint8 r_, Uint8 g_, Uint8 b_)
Definition Color.h:177
Definition Color.h:11
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
Color4f & operator*=(const float v)
Definition Color.h:35
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
Color4ub operator/(const float f) const
Definition Color.h:121
Color4ub Opacity(float factor)
Definition Color.h:148
Color4ub & operator*=(const float f)
Definition Color.h:103
Color4ub operator*(const Color4ub &c) const
Definition Color.h:120
static const Color4ub WHITE
Definition Color.h:156
Color4ub Shade(float factor)
Definition Color.h:132
static const Color4ub YELLOW
Definition Color.h:160
static const Color4ub GRAY
Definition Color.h:161
Color4ub Tint(float factor)
Definition Color.h:140
Color4ub & operator*=(const Color4ub &c)
Definition Color.h:111
friend bool operator!=(const Color4ub &aIn, const Color4ub &bIn)
Definition Color.h:124
constexpr Color4ub()
Definition Color.h:69
Color4f ToColor4f() const
Definition Color.h:126
static const Color4ub RED
Definition Color.h:157
static const Color4ub STEELBLUE
Definition Color.h:162
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:155
static const Color4ub GREEN
Definition Color.h:158
friend bool operator==(const Color4ub &aIn, const Color4ub &bIn)
Definition Color.h:123
static const Color4ub BLUE
Definition Color.h:159
Color4ub operator*(const float f) const
Definition Color.h:119
static const Color4ub BLANK
Definition Color.h:163
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
constexpr Color4ub(const Color4ub &c, const Uint8 a)
Definition Color.h:94
static const Color4ub PINK
Definition Color.h:164
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:102
constexpr Color4ub(Uint8 r_, Uint8 g_, Uint8 b_)
Definition Color.h:74
constexpr Color4ub(const Uint32 rgba)
Definition Color.h:89