@@ -19,83 +19,198 @@ namespace Siege
19
19
template <typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr >
20
20
struct Colour ;
21
21
22
- typedef Colour<float > FColour;
23
- typedef Colour<uint8_t > IColour;
22
+ typedef struct Colour <float > FColour;
23
+ typedef struct Colour <uint8_t > IColour;
24
24
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 >
27
31
{
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
+
28
41
// 'Structors
29
42
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 ) :
31
54
r {red},
32
55
g {green},
33
56
b {blue},
34
57
a {alpha}
35
58
{}
36
59
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);
43
68
69
+ /* *
70
+ * A copy constructor for the IColour struct
71
+ * @param other the IColour to copy over
72
+ */
44
73
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 }
49
78
{}
50
79
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
52
86
{
53
87
return r == other.r && g == other.g && b == other.b && a == other.a ;
54
88
}
55
89
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
57
96
{
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 ;
59
98
}
60
99
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
62
175
{
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 ;
64
177
}
65
178
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
67
185
{
68
186
return r == other.r || g == other.g || b == other.b || a == other.a ;
69
187
}
70
188
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 };
77
193
};
78
194
79
195
// FColour constants
80
196
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 };
96
203
97
204
// Conversion functions
98
205
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
+ */
99
214
inline constexpr FColour ToFColour (const IColour& other)
100
215
{
101
216
return {(static_cast <float >(other.r ) - 0 .f ) / (255 .f - 0 .f ),
@@ -104,6 +219,14 @@ inline constexpr FColour ToFColour(const IColour& other)
104
219
(static_cast <float >(other.a ) - 0 .f ) / (255 .f - 0 .f )};
105
220
}
106
221
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
+ */
107
230
inline constexpr FColour ToIColour (const FColour& other)
108
231
{
109
232
return {static_cast <float >(other.r * 255 ),
@@ -117,7 +240,7 @@ inline constexpr FColour ToIColour(const FColour& other)
117
240
namespace std
118
241
{
119
242
template <>
120
- struct ::std:: hash<Siege::FColour>
243
+ struct hash <Siege::FColour>
121
244
{
122
245
size_t operator ()(const Siege::FColour& colour) const
123
246
{
@@ -128,7 +251,7 @@ struct ::std::hash<Siege::FColour>
128
251
};
129
252
130
253
template <>
131
- struct ::std:: hash<Siege::IColour>
254
+ struct hash <Siege::IColour>
132
255
{
133
256
size_t operator ()(const Siege::IColour& colour) const
134
257
{
0 commit comments