Neo  0.5.0
Developer Documentation
Color.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2003-2011 Anael Seghezzi <www.maratis3d.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 
26 #ifndef __COLOR_H
27 #define __COLOR_H
28 
29 namespace Neo
30 {
31 class NEO_CORE_EXPORT Color
32 {
33 public:
34 
35  unsigned char r;
36  unsigned char g;
37  unsigned char b;
38  unsigned char a;
39 
40 public:
41 
42  Color(void):
43  r(0),
44  g(0),
45  b(0),
46  a(0)
47  {}
48 
49  Color(unsigned char newR, unsigned char newG, unsigned char newB, unsigned char newA):
50  r(newR),
51  g(newG),
52  b(newB),
53  a(newA)
54  {}
55 
56  Color(const Color & color):
57  r(color.r),
58  g(color.g),
59  b(color.b),
60  a(color.a)
61  {}
62 
63  Color(const Vector3 & color):
64  r(CLAMP((int)(color.x*255), 0, 255)),
65  g(CLAMP((int)(color.y*255), 0, 255)),
66  b(CLAMP((int)(color.z*255), 0, 255)),
67  a(255)
68  {}
69 
70  Color(const Vector4 & color):
71  r(CLAMP((int)(color.x*255), 0, 255)),
72  g(CLAMP((int)(color.y*255), 0, 255)),
73  b(CLAMP((int)(color.z*255), 0, 255)),
74  a(CLAMP((int)(color.w*255), 0, 255))
75  {}
76 
77  ~Color()
78  {}
79 
80 public:
81 
82  inline Color operator + (const Color & color) const
83  {
84  int R = r + color.r; if(R > 255) R = 255;
85  int G = g + color.g; if(G > 255) G = 255;
86  int B = b + color.b; if(B > 255) B = 255;
87  int A = a + color.a; if(A > 255) A = 255;
88 
89  return Color(R, G, B, A);
90  }
91 
92  inline Color operator - (const Color & color) const
93  {
94  int R = r - color.r; if(R < 0) R = 0;
95  int G = g - color.g; if(G < 0) G = 0;
96  int B = b - color.b; if(B < 0) B = 0;
97  int A = a - color.a; if(A < 0) A = 0;
98 
99  return Color(R, G, B, A);
100  }
101 
102  inline Color operator + (const int value) const
103  {
104  int R = CLAMP((int)(r + value), 0, 255);
105  int G = CLAMP((int)(g + value), 0, 255);
106  int B = CLAMP((int)(b + value), 0, 255);
107  int A = CLAMP((int)(a + value), 0, 255);
108 
109  return Color(R, G, B, A);
110  }
111 
112  inline Color operator - (const int value) const
113  {
114  int R = CLAMP((int)(r - value), 0, 255);
115  int G = CLAMP((int)(g - value), 0, 255);
116  int B = CLAMP((int)(b - value), 0, 255);
117  int A = CLAMP((int)(a - value), 0, 255);
118 
119  return Color(R, G, B, A);
120  }
121 
122  inline Color operator * (const float value) const
123  {
124  int R = CLAMP((int)(r * value), 0, 255);
125  int G = CLAMP((int)(g * value), 0, 255);
126  int B = CLAMP((int)(b * value), 0, 255);
127  int A = CLAMP((int)(a * value), 0, 255);
128 
129  return Color(R, G, B, A);
130  }
131 
132  inline Color operator / (const float value) const
133  {
134  int R = CLAMP((int)(r / value), 0, 255);
135  int G = CLAMP((int)(g / value), 0, 255);
136  int B = CLAMP((int)(b / value), 0, 255);
137  int A = CLAMP((int)(a / value), 0, 255);
138 
139  return Color(R, G, B, A);
140  }
141 
142  inline bool operator == (const Color & color) const
143  {
144  if(r == color.r && g == color.g && b == color.b && a == color.a)
145  return true;
146 
147  return false;
148  }
149 
150  inline bool operator != (const Color & color) const
151  {
152  return !((*this) == color);
153  }
154 
155  inline operator unsigned char * () const
156  {
157  return (unsigned char *) this;
158  }
159 
160  inline operator const unsigned char * () const
161  {
162  return (const unsigned char *) this;
163  }
164 
165 public:
166 
167  inline void set(unsigned char newR, unsigned char newG, unsigned char newB, unsigned char newA)
168  {
169  r = newR;
170  g = newG;
171  b = newB;
172  a = newA;
173  }
174 };
175 }
176 #endif
Definition: Vector4.h:31
Color(unsigned char newR, unsigned char newG, unsigned char newB, unsigned char newA)
Definition: Color.h:49
Color(const Vector4 &color)
Definition: Color.h:70
Definition: Vector3.h:31
unsigned char a
Definition: Color.h:38
unsigned char g
Definition: Color.h:36
unsigned char b
Definition: Color.h:37
unsigned char r
Definition: Color.h:35
Color(void)
Definition: Color.h:42
Definition: Color.h:31
~Color()
Definition: Color.h:77
void set(unsigned char newR, unsigned char newG, unsigned char newB, unsigned char newA)
Definition: Color.h:167
Color(const Vector3 &color)
Definition: Color.h:63
Definition: Color.h:29
Color(const Color &color)
Definition: Color.h:56