SColor.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "SVector.h"
5 #include "SVector3.h"
6 
7 
8 namespace Color
9 {
10 
13  template <typename T>
14  struct Full
15  {};
16 
17  template <>
18  struct Full<f32>
19  {
20  static f32 Value()
21  {
22  return 1.f;
23  }
24  };
25 
26  template <>
27  struct Full<u8>
28  {
29  static u8 Value()
30  {
31  return 255;
32  }
33  };
34 
37  template <typename T, typename U>
38  struct Convert
39  {};
40 
41  template <>
42  struct Convert<u8, f32>
43  {
44  static u8 From(f32 const Value)
45  {
46  return Clamp((uint) roundf(Value * 255.f), 0u, 255u);
47  }
48  };
49 
50  template <>
51  struct Convert<f32, u8>
52  {
53  static f32 From(u8 const Value)
54  {
55  return Clamp((float) Value / 255.f, 0.f, 1.f);
56  }
57  };
58 
59 }
60 
61 template <typename T>
62 class SColorA;
63 
66 template <typename T>
67 class color3 : public vec3<T>
68 {
69 
70 public:
71 
72  T & Red, & Green, & Blue;
73 
76  : Red(this->X), Green(this->Y), Blue(this->Z), vec3<T>()
77  {}
78 
80  color3(T const in)
81  : Red(this->X), Green(this->Y), Blue(this->Z), vec3<T>(in)
82  {}
83 
85  color3(T const r, T const g, T const b)
86  : Red(this->X), Green(this->Y), Blue(this->Z), vec3<T>(r, g, b)
87  {}
88 
90  color3(color3<T> const & Other)
91  : Red(this->X), Green(this->Y), Blue(this->Z), vec3<T>(Other)
92  {}
93 
95  color3(vec3<T> const & Other)
96  : Red(this->X), Green(this->Y), Blue(this->Z), vec3<T>(Other)
97  {}
98 
100  template <typename U>
101  color3(color3<U> const & Other)
102  : Red(this->X), Green(this->Y), Blue(this->Z), vec3<T>()
103  {
104  Red = Color::Convert<T, U>::From(Other.Red);
105  Green = Color::Convert<T, U>::From(Other.Green);
106  Blue = Color::Convert<T, U>::From(Other.Blue);
107  }
108 
110  color3<T> & operator = (color3<T> const & Other)
111  {
112  Red = Other.Red;
113  Green = Other.Green;
114  Blue = Other.Blue;
115  return * this;
116  }
117 
119  template <typename U>
120  color3<T> & operator = (color3<U> const & Other)
121  {
122  Red = Color::Convert<T, U>::From(Other.Red);
123  Green = Color::Convert<T, U>::From(Other.Green);
124  Blue = Color::Convert<T, U>::From(Other.Blue);
125  return *this;
126  }
127 
128 };
129 
130 
133 template <typename T>
134 class SColorA : public SVector<T, 4, SColorA<T> >
135 {
136 
137 public:
138 
141 
142  T & Red, & Green, & Blue, & Alpha;
143  static T const Full;
144 
146  : Red(Values[0]), Green(Values[1]), Blue(Values[2]), Alpha(Values[3])
147  {
148  set((T) 0);
149  }
150 
151  SColorA(T const in)
152  : Red(Values[0]), Green(Values[1]), Blue(Values[2]), Alpha(Values[3])
153  {
154  set(in);
155  }
156 
157  SColorA(T const r, T const g, T const b, T const a = Full)
158  : Red(Values[0]), Green(Values[1]), Blue(Values[2]), Alpha(Values[3])
159  {
160  Values[0] = r;
161  Values[1] = g;
162  Values[2] = b;
163  Values[3] = a;
164  }
165 
166  SColorA(SColorA<T> const & vec)
167  : Red(Values[0]), Green(Values[1]), Blue(Values[2]), Alpha(Values[3])
168  {
169  set(vec);
170  }
171 
172  template <typename U, int otherDimension, typename otherImplementation>
174  : Red(Values[0]), Green(Values[1]), Blue(Values[2]), Alpha(Values[3])
175  {
176  set(vec);
177  }
178 
181  : Red(Values[0]), Green(Values[1]), Blue(Values[2]), Alpha(Values[3])
182  {
183  Red = Other.Red;
184  Green = Other.Green;
185  Blue = Other.Blue;
186  Alpha = Color::Full<T>::Value();
187  }
188 
189  SColorA<T> & operator = (SColorA<T> const & vec)
190  {
191  set(vec);
192 
193  return * this;
194  }
195 
196  template <typename U, u32 OtherDimension>
197  SColorA<T> & operator = (SVectorBase<U, OtherDimension> const & vec)
198  {
199  set(vec);
200 
201  return * this;
202  }
203 
204  SColorA<T> & operator = (color3<T> const & col)
205  {
206  Red = col.Red;
207  Green = col.Green;
208  Blue = col.Blue;
209  Alpha = Color::Full<T>::Value();
210 
211  return * this;
212  }
213 
214  template <typename U>
215  SColorA<T> & operator = (color3<U> const & col)
216  {
217  Red = Color::Convert<T, U>::From(col.Red);
218  Green = Color::Convert<T, U>::From(col.Green);
219  Blue = Color::Convert<T, U>::From(col.Blue);
220  Alpha = Color::Full<T>::Value();
221 
222  return * this;
223  }
224 
225  template <typename U, u32 OtherDimension>
226  void set(SVectorBase<U, OtherDimension> const & other)
227  {
228  for (int i = 0; i < 4; ++ i)
229  Values[i] = (T) other[i];
230  }
231 
232  template <u32 OtherDimension>
233  void set(SVectorBase<u8, OtherDimension> const & other)
234  {
235  for (int i = 0; i < 4; ++ i)
236  Values[i] = (T) other[i];
237  }
238 
239  template <u32 OtherDimension>
240  void set(SVectorBase<f32, OtherDimension> const & other)
241  {
242  for (int i = 0; i < 4; ++ i)
243  Values[i] = (T) other[i];
244  }
245 
246  template <u32 OtherDimension>
247  void set(SVectorBase<f64, OtherDimension> const & other)
248  {
249  for (int i = 0; i < 4; ++ i)
250  Values[i] = (T) other[i];
251  }
252 
253  operator color3<T>()
254  {
255  color3<T> ret;
256  ret.Red = Red;
257  ret.Green = Green;
258  ret.Blue = Blue;
259  return ret;
260  }
261 
262 };
263 
264 template <typename T>
265 T const SColorA<T>::Full = 1;
266 
267 template <>
268 template <u32 OtherDimension>
270 {
271  for (int i = 0; i < 4; ++ i)
272  Values[i] = (f32) other[i] / 255.f;
273 }
274 
275 template <>
276 template <u32 OtherDimension>
278 {
279  for (int i = 0; i < 4; ++ i)
280  Values[i] = (f64) other[i] / 255.0;
281 }
282 
283 template <>
284 template <u32 OtherDimension>
286 {
287  for (int i = 0; i < 4; ++ i)
288  Values[i] = (u8) (other[i] * 255);
289 }
290 
291 template <>
292 template <u32 OtherDimension>
294 {
295  for (int i = 0; i < 4; ++ i)
296  Values[i] = (u8) (other[i] * 255);
297 }
298 
299 
306 
311 
316 
317 typedef SColori color3i;
318 typedef SColorf color3f;
321 
322 
325 namespace Colors
326 {
327  static color3f const White = color3f(1, 1, 1);
328  static color3f const Black = color3f(0, 0, 0);
329  static color3f const Red = color3f(1, 0, 0);
330  static color3f const Orange = color3f(1, 0.5f, 0);
331  static color3f const Yellow = color3f(1, 1, 0);
332  static color3f const Green = color3f(0, 1, 0);
333  static color3f const Cyan = color3f(0, 1, 1);
334  static color3f const Blue = color3f(0, 0, 1);
335  static color3f const Magenta = color3f(1, 0, 1);
336 }
337 
338 namespace Color
339 {
340 
343  struct Pack
344  {
345 
346  static uint Bits24(color3i const & Color)
347  {
348  uint const Red = Color.Red;
349  uint const Green = Color.Green;
350  uint const Blue = Color.Blue;
351  return (Red << 16) | (Green << 8) | (Blue);
352  }
353 
354  };
355 
358  struct Unpack
359  {
360 
361  static color3i Bits24(uint const Value)
362  {
363  color3i Color;
364  Color.Red = (Value >> 16) & 0xFF;
365  Color.Green = (Value >> 8) & 0xFF;
366  Color.Blue = Value & 0xFF;
367  return Color;
368  }
369 
370  };
371 
372 }
static u8 Value()
Definition: SColor.h:29
SColorA< u8 > SColor32
Definition: SColor.h:305
Definition: SColor.h:8
SColorA(T const r, T const g, T const b, T const a=Full)
Definition: SColor.h:157
color3< f32 > SColor72
Definition: SColor.h:309
SColorA< u8 > SColorAc
Definition: SColor.h:303
void set(SVectorBase< U, OtherDimension > const &other)
Definition: SColor.h:226
color3< u8 > SColori
Definition: SColor.h:300
T & Green
Definition: SColor.h:72
color3< u8 > SColorc
Definition: SColor.h:301
SColori color3i
Definition: SColor.h:317
3D vector
Definition: SVector3.h:15
Implements a conversion between two color types.
Definition: SColor.h:38
Unacks a color from 24 bits.
Definition: SColor.h:358
Uint8 u8
Definition: ionTypes.h:82
static uint Bits24(color3i const &Color)
Definition: SColor.h:346
SColorA< u8 > SColorAi
Definition: SColor.h:302
T & Red
Definition: SColor.h:72
SColorAi color4i
Definition: SColor.h:319
color3(color3< T > const &Other)
Copy constructor.
Definition: SColor.h:90
Customized variant of vec3 that intelligently handles conversion between floating point and integer c...
Definition: SColor.h:67
SColorA(SVector< U, otherDimension, otherImplementation > const &vec)
Definition: SColor.h:173
static u8 From(f32 const Value)
Definition: SColor.h:44
unsigned int uint
Definition: ionTypes.h:99
color3(T const r, T const g, T const b)
Explicit constructor.
Definition: SColor.h:85
Some very basic colors.
Definition: SColor.h:325
Packs a color into 24 bits.
Definition: SColor.h:343
color3< u8 > SColor24
Definition: SColor.h:304
color3< f64 > SColor192
Definition: SColor.h:314
float f32
Definition: ionTypes.h:94
color3(color3< U > const &Other)
Generic copy constructor.
Definition: SColor.h:101
static f32 Value()
Definition: SColor.h:20
Customized variant of vec4 that intelligently handles conversion between floating point and integer c...
Definition: SColor.h:62
color3()
Default constructor.
Definition: SColor.h:75
color3(vec3< T > const &Other)
Vector copy constructor.
Definition: SColor.h:95
Definition: SVector.h:8
Determines the maximum color value for a given type.
Definition: SColor.h:14
SColorA< f64 > SColor256
Definition: SColor.h:315
color3< f32 > SColorf
Definition: SColor.h:307
color3(T const in)
Scalar constructor.
Definition: SColor.h:80
static color3i Bits24(uint const Value)
Definition: SColor.h:361
color3< f64 > SColord
Definition: SColor.h:312
T & Blue
Definition: SColor.h:72
SColorA< f32 > SColor96
Definition: SColor.h:310
SColorf color3f
Definition: SColor.h:318
static f32 From(u8 const Value)
Definition: SColor.h:53
T Clamp(T const &v, T const &min, T const &max)
Definition: ionUtils.h:53
SColorA(T const in)
Definition: SColor.h:151
SColorA< f64 > SColorAd
Definition: SColor.h:313
SColorA< f32 > SColorAf
Definition: SColor.h:308
Definition: SVector.h:114
double f64
Definition: ionTypes.h:95
SColorA()
Definition: SColor.h:145
T & Red
Definition: SColor.h:142
SColorA(color3< T > const &Other)
Scalar constructor.
Definition: SColor.h:180
SColorA(SColorA< T > const &vec)
Definition: SColor.h:166
static T const Full
Definition: SColor.h:143
SColorAf color4f
Definition: SColor.h:320