Skip to content

Commit e2dc957

Browse files
committed
Moved Colour constants into the Colour definitions
1 parent b99c01a commit e2dc957

File tree

5 files changed

+183
-51
lines changed

5 files changed

+183
-51
lines changed

engine/utils/Colour.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@
66
// https://opensource.org/licenses/Zlib
77
//
88

9+
#include "Colour.h"
10+
911
namespace Siege
10-
{} // namespace Siege
12+
{
13+
Colour<uint8_t>::Colour(const FColour& other) :
14+
r {static_cast<uint8_t>(other.r)},
15+
g {static_cast<uint8_t>(other.g)},
16+
b {static_cast<uint8_t>(other.b)},
17+
a {static_cast<uint8_t>(other.a)}
18+
{}
19+
} // namespace Siege

engine/utils/Colour.h

Lines changed: 167 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,83 +19,198 @@ namespace Siege
1919
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
2020
struct Colour;
2121

22-
typedef Colour<float> FColour;
23-
typedef Colour<uint8_t> IColour;
22+
typedef struct Colour<float> FColour;
23+
typedef struct Colour<uint8_t> IColour;
2424

25-
template<typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type*>
26-
struct Colour
25+
/**
26+
* An 8-bit unsigned integer based Colour struct. Every colour channel in this struct ranges between
27+
* 0 and 255, with 255 being the brightest colour channel
28+
*/
29+
template<>
30+
struct Colour<uint8_t>
2731
{
32+
// Static constants
33+
34+
static const IColour White;
35+
static const IColour Black;
36+
static const IColour Red;
37+
static const IColour Green;
38+
static const IColour Blue;
39+
static const IColour Pink;
40+
2841
// 'Structors
2942

30-
inline constexpr Colour(T red = 0, T green = 0, T blue = 0, T alpha = 0) :
43+
/**
44+
* The base constructor for the colour
45+
* @param red the red channel value
46+
* @param green the green channel value
47+
* @param blue the blue channel value
48+
* @param alpha the alpha channel value
49+
*/
50+
inline constexpr Colour(uint8_t red = 0,
51+
uint8_t green = 0,
52+
uint8_t blue = 0,
53+
uint8_t alpha = 0) :
3154
r {red},
3255
g {green},
3356
b {blue},
3457
a {alpha}
3558
{}
3659

37-
inline constexpr Colour(const FColour& other) :
38-
r {static_cast<T>(other.r)},
39-
g {static_cast<T>(other.g)},
40-
b {static_cast<T>(other.b)},
41-
a {static_cast<T>(other.a)}
42-
{}
60+
/**
61+
* A conversion from a float-based Colour struct.
62+
* @warn this does not convert the normalised values of an FColour into an IColour. Please be
63+
* careful with how you convert between these two. If you want a function which converts the
64+
* normalised value of the FColour to a 0 - 255 range, please use the ToIColour() method
65+
* @param other
66+
*/
67+
Colour(const FColour& other);
4368

69+
/**
70+
* A copy constructor for the IColour struct
71+
* @param other the IColour to copy over
72+
*/
4473
inline constexpr Colour(const IColour& other) :
45-
r {static_cast<T>(other.r)},
46-
g {static_cast<T>(other.g)},
47-
b {static_cast<T>(other.b)},
48-
a {static_cast<T>(other.a)}
74+
r {other.r},
75+
g {other.g},
76+
b {other.b},
77+
a {other.a}
4978
{}
5079

51-
inline constexpr bool operator==(const FColour& other) const
80+
/**
81+
* An equality operator
82+
* @param other the Colour to compare to
83+
* @return true of they are the same, false if they aren't
84+
*/
85+
inline constexpr bool operator==(const IColour& other) const
5286
{
5387
return r == other.r && g == other.g && b == other.b && a == other.a;
5488
}
5589

56-
inline constexpr bool operator==(const IColour& other) const
90+
/**
91+
* An in-equality operator
92+
* @param other the Colour to compare to
93+
* @return false of they are the same, true if they aren't
94+
*/
95+
inline constexpr bool operator!=(const IColour& other) const
5796
{
58-
return r == other.r && g == other.g && b == other.b && a == other.a;
97+
return r == other.r || g == other.g || b == other.b || a == other.a;
5998
}
6099

61-
inline constexpr bool operator!=(const FColour& other) const
100+
// Public members
101+
102+
uint8_t r {0};
103+
uint8_t g {0};
104+
uint8_t b {0};
105+
uint8_t a {0};
106+
};
107+
108+
inline constexpr IColour IColour::White {255, 255, 255, 255};
109+
inline constexpr IColour IColour::Black {0, 0, 0, 255};
110+
inline constexpr IColour IColour::Red {255, 0, 0, 255};
111+
inline constexpr IColour IColour::Green {0, 255, 0, 255};
112+
inline constexpr IColour IColour::Blue {0, 0, 255, 255};
113+
inline constexpr IColour IColour::Pink {255, 109, 194, 255};
114+
115+
template<>
116+
struct Colour<float>
117+
{
118+
// Static constants
119+
120+
static const FColour White;
121+
static const FColour Black;
122+
static const FColour Red;
123+
static const FColour Green;
124+
static const FColour Blue;
125+
static const FColour Pink;
126+
127+
/**
128+
* The base constructor for the colour
129+
* @param red the red channel value
130+
* @param green the green channel value
131+
* @param blue the blue channel value
132+
* @param alpha the alpha channel value
133+
*/
134+
inline constexpr Colour(float red = 0.f,
135+
float green = 0.f,
136+
float blue = 0.f,
137+
float alpha = 0.f) :
138+
r {red},
139+
g {green},
140+
b {blue},
141+
a {alpha}
142+
{}
143+
144+
/**
145+
* A copy constructor for the FColour struct
146+
* @param other the FColour to copy over
147+
*/
148+
inline constexpr Colour(const FColour& other) :
149+
r {other.r},
150+
g {other.g},
151+
b {other.b},
152+
a {other.a}
153+
{}
154+
155+
/**
156+
* A conversion from an 8-bit unsigned integer based Colour struct.
157+
* @warn this does not convert the normalised values of an FColour into an IColour. Please be
158+
* careful with how you convert between these two. If you want a function which converts the
159+
* normalised value of the FColour to a 0 - 255 range, please use the ToFColour() method
160+
* @param other
161+
*/
162+
inline constexpr Colour(const IColour& other) :
163+
r {static_cast<float>(other.r)},
164+
g {static_cast<float>(other.g)},
165+
b {static_cast<float>(other.b)},
166+
a {static_cast<float>(other.a)}
167+
{}
168+
169+
/**
170+
* An equality operator
171+
* @param other the Colour to compare to
172+
* @return true of they are the same, false if they aren't
173+
*/
174+
inline constexpr bool operator==(const FColour& other) const
62175
{
63-
return r == other.r || g == other.g || b == other.b || a == other.a;
176+
return r == other.r && g == other.g && b == other.b && a == other.a;
64177
}
65178

66-
inline constexpr bool operator!=(const IColour& other) const
179+
/**
180+
* An in-equality operator
181+
* @param other the Colour to compare to
182+
* @return false of they are the same, true if they aren't
183+
*/
184+
inline constexpr bool operator!=(const FColour& other) const
67185
{
68186
return r == other.r || g == other.g || b == other.b || a == other.a;
69187
}
70188

71-
// Public members
72-
73-
T r {0};
74-
T g {0};
75-
T b {0};
76-
T a {0};
189+
float r {0.f};
190+
float g {0.f};
191+
float b {0.f};
192+
float a {0.f};
77193
};
78194

79195
// FColour constants
80196

81-
inline constexpr FColour FWhite = {1.f, 1.f, 1.f, 1.f};
82-
inline constexpr FColour FBlack = {0.f, 0.f, 0.f, 1.f};
83-
inline constexpr FColour FRed = {1.f, 0.f, 0.f, 1.f};
84-
inline constexpr FColour FGreen = {0.f, 1.f, 0.f, 1.f};
85-
inline constexpr FColour FBlue = {0.f, 0.f, 1.f, 1.f};
86-
inline constexpr FColour FPink = {1.f, 0.427450980392, 0.760784313725, 1.f};
87-
88-
// IColour constants
89-
90-
inline constexpr IColour IWhite = {255, 255, 255, 255};
91-
inline constexpr IColour IBlack = {0, 0, 0, 255};
92-
inline constexpr IColour IRed = {255, 0, 0, 255};
93-
inline constexpr IColour IGreen = {0, 255, 0, 255};
94-
inline constexpr IColour IBlue = {0, 0, 255, 255};
95-
inline constexpr IColour IPink = {255, 109, 194, 255};
197+
inline constexpr FColour FColour::White = {1.f, 1.f, 1.f, 1.f};
198+
inline constexpr FColour FColour::Black = {0.f, 0.f, 0.f, 1.f};
199+
inline constexpr FColour FColour::Red = {1.f, 0.f, 0.f, 1.f};
200+
inline constexpr FColour FColour::Green = {0.f, 1.f, 0.f, 1.f};
201+
inline constexpr FColour FColour::Blue = {0.f, 0.f, 1.f, 1.f};
202+
inline constexpr FColour FColour::Pink = {1.f, 0.427450980392, 0.760784313725, 1.f};
96203

97204
// Conversion functions
98205

206+
/**
207+
* Converts an IColour to an FColour. This function is different to the standard constructors in
208+
* that it takes the 0 to 255 range of the IColour and normalises it between 0 and 1 (as a float).
209+
* This Method is useful for converting 8 bit Colours to float colours in an accurate manner. This
210+
* is the recommended approach for converting the two colours
211+
* @param other the IColour to compare
212+
* @return an FColour with the normalised values
213+
*/
99214
inline constexpr FColour ToFColour(const IColour& other)
100215
{
101216
return {(static_cast<float>(other.r) - 0.f) / (255.f - 0.f),
@@ -104,6 +219,14 @@ inline constexpr FColour ToFColour(const IColour& other)
104219
(static_cast<float>(other.a) - 0.f) / (255.f - 0.f)};
105220
}
106221

222+
/**
223+
* Converts an FColour to an IColour. This function is different to the standard constructors in
224+
* that it takes the 0 to 1 range of the FColour and expands it between 0 and 255 (as a uint8_t).
225+
* This Method is useful for converting 8 bit Colours to float colours in an accurate manner. This
226+
* is the recommended approach for converting the two colours
227+
* @param other the FColour to convert
228+
* @return an IColour with the expanded values
229+
*/
107230
inline constexpr FColour ToIColour(const FColour& other)
108231
{
109232
return {static_cast<float>(other.r * 255),
@@ -117,7 +240,7 @@ inline constexpr FColour ToIColour(const FColour& other)
117240
namespace std
118241
{
119242
template<>
120-
struct ::std::hash<Siege::FColour>
243+
struct hash<Siege::FColour>
121244
{
122245
size_t operator()(const Siege::FColour& colour) const
123246
{
@@ -128,7 +251,7 @@ struct ::std::hash<Siege::FColour>
128251
};
129252

130253
template<>
131-
struct ::std::hash<Siege::IColour>
254+
struct hash<Siege::IColour>
132255
{
133256
size_t operator()(const Siege::IColour& colour) const
134257
{

examples/game/src/tools/DevConsole.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ void DevConsole::OnDraw2D()
152152
Siege::Window* window = ServiceLocator::GetWindow();
153153

154154
// Draw the console to the screen
155-
Siege::Statics::Render().DrawRectangle2D(0, 0, window->GetWidth(), 40, Siege::IBlack);
156-
Siege::Statics::Render().DrawText2D("~ " + inputText, 10.f, 10.f, 20.f, Siege::IWhite);
155+
Siege::Statics::Render().DrawRectangle2D(0, 0, window->GetWidth(), 40, Siege::IColour::Black);
156+
Siege::Statics::Render().DrawText2D("~ " + inputText, 10.f, 10.f, 20.f, Siege::IColour::White);
157157
}
158158

159159
bool DevConsole::CheckEditorMode()

examples/game/src/tools/EditorController.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,19 @@ void EditorController::OnDraw2D()
189189
(int) screenPosition.x - Siege::Window::GetTextWidth(nameLabel, 20) / 2,
190190
(int) screenPosition.y,
191191
20,
192-
Siege::IPink);
192+
Siege::IColour::Pink);
193193
Siege::Statics::Render().DrawText2D(
194194
posLabel,
195195
(int) screenPosition.x - Siege::Window::GetTextWidth(posLabel, 18) / 2,
196196
(int) screenPosition.y + 20,
197197
18,
198-
currentMode == POSITION ? BRIGHT_PINK : Siege::IPink);
198+
currentMode == POSITION ? BRIGHT_PINK : Siege::IColour::Pink);
199199
Siege::Statics::Render().DrawText2D(
200200
rotLabel,
201201
(int) screenPosition.x - Siege::Window::GetTextWidth(posLabel, 18) / 2,
202202
(int) screenPosition.y + 40,
203203
18,
204-
currentMode == ROTATION ? BRIGHT_PINK : Siege::IPink);
204+
currentMode == ROTATION ? BRIGHT_PINK : Siege::IColour::Pink);
205205
}
206206

207207
void EditorController::SelectEntity(Entity* entity)

examples/game/src/tools/MessageDisplay.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void MessageDisplay::OnDraw2D()
1616
// Draw the display message to the screen while the display time is valid
1717
if (displayTime > 0.f)
1818
{
19-
Siege::Statics::Render().DrawText2D(displayMessage, 10, 10, 20, Siege::IPink);
19+
Siege::Statics::Render().DrawText2D(displayMessage, 10, 10, 20, Siege::IColour::Pink);
2020
displayTime -= 0.1f;
2121
}
2222
}

0 commit comments

Comments
 (0)