color3.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "vec3.h"
5 #include "ionUtils.h"
6 
7 
8 namespace ion
9 {
10 
11  namespace Color
12  {
13 
16  template <typename T>
17  struct Full
18  {};
19 
20  template <>
21  struct Full<float>
22  {
23  static float Value()
24  {
25  return 1.f;
26  }
27  };
28 
29  template <>
30  struct Full<byte>
31  {
32  static byte Value()
33  {
34  return 255;
35  }
36  };
37 
40  template <typename T, typename U>
41  struct Convert
42  {};
43 
44  template <>
45  struct Convert<byte, float>
46  {
47  static byte From(float const Value)
48  {
49  return Clamp((uint) roundf(Value * 255.f), 0u, 255u);
50  }
51  };
52 
53  template <>
54  struct Convert<float, byte>
55  {
56  static float From(byte const Value)
57  {
58  return Clamp((float) Value / 255.f, 0.f, 1.f);
59  }
60  };
61 
62  }
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 
134 }
General helper functions.
color3< byte > color3i
Definition: color3.h:131
3D vector
Definition: vec3.h:15
unsigned char byte
Definition: ionTypes.h:45
static float Value()
Definition: color3.h:23
static byte From(float const Value)
Definition: color3.h:47
static byte Value()
Definition: color3.h:32
static float From(byte const Value)
Definition: color3.h:56
color3< float > color3f
Definition: color3.h:132
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
unsigned int uint
Definition: ionTypes.h:42
color3()
Default constructor.
Definition: color3.h:75
Determines the maximum color value for a given type.
Definition: color3.h:17
T & Green
Definition: color3.h:72
color3(color3< U > const &Other)
Generic copy constructor.
Definition: color3.h:101
color3(T const in)
Scalar constructor.
Definition: color3.h:80
Implements a conversion between two color types.
Definition: color3.h:41
color3(T const r, T const g, T const b)
Explicit constructor.
Definition: color3.h:85
color3(vec3< T > const &Other)
Vector copy constructor.
Definition: color3.h:95
Customized variant of vec3 that intelligently handles conversion between floating point and integer c...
Definition: color3.h:67
T & Red
Definition: color3.h:72
T Clamp(T const v, T const min, T const max)
Definition: ionUtils.h:45
T & Blue
Definition: color3.h:72
color3(color3< T > const &Other)
Copy constructor.
Definition: color3.h:90