1 // SwiftShader Software Renderer
\r
3 // Copyright(c) 2005-2011 TransGaming Inc.
\r
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
\r
6 // transcribed, stored in a retrieval system, translated into any human or computer
\r
7 // language by any means, or disclosed to third parties without the explicit written
\r
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
\r
9 // or implied, including but not limited to any patent rights, are granted to you.
\r
12 #ifndef sw_Color_hpp
\r
13 #define sw_Color_hpp
\r
15 #include "Common/Types.hpp"
\r
16 #include "Common/Math.hpp"
\r
25 Color(const Color<byte> &c);
\r
26 Color(const Color<short> &c);
\r
27 Color(const Color<float> &c);
\r
30 Color(unsigned short c);
\r
31 Color(unsigned long c);
\r
32 Color(unsigned int c);
\r
34 Color(T r, T g, T b, T a = 1);
\r
36 operator unsigned int() const;
\r
38 T &operator[](int i);
\r
39 const T &operator[](int i) const;
\r
41 Color<T> operator+() const;
\r
42 Color<T> operator-() const;
\r
44 Color<T>& operator=(const Color<T>& c);
\r
46 Color<T> &operator+=(const Color<T> &c);
\r
47 Color<T> &operator*=(float l);
\r
49 static Color<T> gradient(const Color<T> &c1, const Color<T> &c2, float d);
\r
50 static Color<T> shade(const Color<T> &c1, const Color<T> &c2, float d);
\r
53 friend Color<S> operator+(const Color<S> &c1, const Color<S> &c2);
\r
55 friend Color<S> operator-(const Color<S> &c1, const Color<S> &c2);
\r
58 friend Color<S> operator*(float l, const Color<S> &c);
\r
60 friend Color<S> operator*(const Color<S> &c1, const Color<S> &c2);
\r
62 friend Color<S> operator/(const Color<S> &c, float l);
\r
71 #include "Common/Math.hpp"
\r
76 inline Color<T>::Color()
\r
81 inline Color<byte>::Color(const Color<byte> &c)
\r
90 inline Color<byte>::Color(const Color<short> &c)
\r
92 r = clamp(c.r >> 4, 0, 255);
\r
93 g = clamp(c.g >> 4, 0, 255);
\r
94 b = clamp(c.b >> 4, 0, 255);
\r
95 a = clamp(c.a >> 4, 0, 255);
\r
99 inline Color<byte>::Color(const Color<float> &c)
\r
101 r = ifloor(clamp(c.r * 256.0f, 0.0f, 255.0f));
\r
102 g = ifloor(clamp(c.g * 256.0f, 0.0f, 255.0f));
\r
103 b = ifloor(clamp(c.b * 256.0f, 0.0f, 255.0f));
\r
104 a = ifloor(clamp(c.a * 256.0f, 0.0f, 255.0f));
\r
108 inline Color<short>::Color(const Color<short> &c)
\r
117 inline Color<short>::Color(const Color<byte> &c)
\r
126 inline Color<float>::Color(const Color<float> &c)
\r
135 inline Color<short>::Color(const Color<float> &c)
\r
137 r = iround(clamp(c.r * 4095.0f, -4096.0f, 4095.0f));
\r
138 g = iround(clamp(c.g * 4095.0f, -4096.0f, 4095.0f));
\r
139 b = iround(clamp(c.b * 4095.0f, -4096.0f, 4095.0f));
\r
140 a = iround(clamp(c.a * 4095.0f, -4096.0f, 4095.0f));
\r
144 inline Color<float>::Color(const Color<byte> &c)
\r
153 inline Color<float>::Color(const Color<short> &c)
\r
162 inline Color<float>::Color(unsigned short c)
\r
164 r = (float)(c & 0xF800) / (float)0xF800;
\r
165 g = (float)(c & 0x07E0) / (float)0x07E0;
\r
166 b = (float)(c & 0x001F) / (float)0x001F;
\r
171 inline Color<short>::Color(unsigned short c)
\r
173 // 4.12 fixed-point format
\r
174 r = ((c & 0xF800) >> 4) + ((c & 0xF800) >> 9) + ((c & 0xF800) >> 14);
\r
175 g = ((c & 0x07E0) << 1) + ((c & 0x07E0) >> 5);
\r
176 b = ((c & 0x001F) << 7) + ((c & 0x001F) << 2) + ((c & 0x001F) >> 3);
\r
181 inline Color<byte>::Color(unsigned short c)
\r
183 r = (byte)(((c & 0xF800) >> 8) + ((c & 0xE000) >> 13));
\r
184 g = (byte)(((c & 0x07E0) >> 3) + ((c & 0x0600) >> 9));
\r
185 b = (byte)(((c & 0x001F) << 3) + ((c & 0x001C) >> 2));
\r
190 inline Color<float>::Color(int c)
\r
192 const float d = 1.0f / 255.0f;
\r
194 r = (float)((c & 0x00FF0000) >> 16) * d;
\r
195 g = (float)((c & 0x0000FF00) >> 8) * d;
\r
196 b = (float)((c & 0x000000FF) >> 0) * d;
\r
197 a = (float)((c & 0xFF000000) >> 24) * d;
\r
201 inline Color<short>::Color(int c)
\r
203 // 4.12 fixed-point format
\r
204 r = (short)((c & 0x00FF0000) >> 12);
\r
205 g = (short)((c & 0x0000FF00) >> 4);
\r
206 b = (short)((c & 0x000000FF) << 4);
\r
207 a = (short)((c & 0xFF000000) >> 20);
\r
211 inline Color<byte>::Color(int c)
\r
213 r = (byte)((c & 0x00FF0000) >> 16);
\r
214 g = (byte)((c & 0x0000FF00) >> 8);
\r
215 b = (byte)((c & 0x000000FF) >> 0);
\r
216 a = (byte)((c & 0xFF000000) >> 24);
\r
220 inline Color<float>::Color(unsigned int c)
\r
222 const float d = 1.0f / 255.0f;
\r
224 r = (float)((c & 0x00FF0000) >> 16) * d;
\r
225 g = (float)((c & 0x0000FF00) >> 8) * d;
\r
226 b = (float)((c & 0x000000FF) >> 0) * d;
\r
227 a = (float)((c & 0xFF000000) >> 24) * d;
\r
231 inline Color<short>::Color(unsigned int c)
\r
233 // 4.12 fixed-point format
\r
234 r = (short)((c & 0x00FF0000) >> 12);
\r
235 g = (short)((c & 0x0000FF00) >> 4);
\r
236 b = (short)((c & 0x000000FF) << 4);
\r
237 a = (short)((c & 0xFF000000) >> 20);
\r
241 inline Color<byte>::Color(unsigned int c)
\r
243 r = (byte)((c & 0x00FF0000) >> 16);
\r
244 g = (byte)((c & 0x0000FF00) >> 8);
\r
245 b = (byte)((c & 0x000000FF) >> 0);
\r
246 a = (byte)((c & 0xFF000000) >> 24);
\r
250 inline Color<float>::Color(unsigned long c)
\r
252 const float d = 1.0f / 255.0f;
\r
254 r = (float)((c & 0x00FF0000) >> 16) * d;
\r
255 g = (float)((c & 0x0000FF00) >> 8) * d;
\r
256 b = (float)((c & 0x000000FF) >> 0) * d;
\r
257 a = (float)((c & 0xFF000000) >> 24) * d;
\r
261 inline Color<short>::Color(unsigned long c)
\r
263 // 4.12 fixed-point format
\r
264 r = (short)((c & 0x00FF0000) >> 12);
\r
265 g = (short)((c & 0x0000FF00) >> 4);
\r
266 b = (short)((c & 0x000000FF) << 4);
\r
267 a = (short)((c & 0xFF000000) >> 20);
\r
271 inline Color<byte>::Color(unsigned long c)
\r
273 r = (byte)((c & 0x00FF0000) >> 16);
\r
274 g = (byte)((c & 0x0000FF00) >> 8);
\r
275 b = (byte)((c & 0x000000FF) >> 0);
\r
276 a = (byte)((c & 0xFF000000) >> 24);
\r
280 inline Color<T>::Color(T r_, T g_, T b_, T a_)
\r
289 inline Color<float>::operator unsigned int() const
\r
291 return ((unsigned int)min(b * 255.0f, 255.0f) << 0) |
\r
292 ((unsigned int)min(g * 255.0f, 255.0f) << 8) |
\r
293 ((unsigned int)min(r * 255.0f, 255.0f) << 16) |
\r
294 ((unsigned int)min(a * 255.0f, 255.0f) << 24);
\r
298 inline Color<short>::operator unsigned int() const
\r
300 return ((unsigned int)min(b >> 4, 255) << 0) |
\r
301 ((unsigned int)min(g >> 4, 255) << 8) |
\r
302 ((unsigned int)min(r >> 4, 255) << 16) |
\r
303 ((unsigned int)min(a >> 4, 255) << 24);
\r
307 inline Color<byte>::operator unsigned int() const
\r
316 inline T &Color<T>::operator[](int i)
\r
322 inline const T &Color<T>::operator[](int i) const
\r
328 inline Color<T> Color<T>::operator+() const
\r
334 inline Color<T> Color<T>::operator-() const
\r
336 return Color(-r, -g, -b, -a);
\r
340 inline Color<T> &Color<T>::operator=(const Color& c)
\r
351 inline Color<T> &Color<T>::operator+=(const Color &c)
\r
362 inline Color<T> &Color<T>::operator*=(float l)
\r
370 inline Color<T> operator+(const Color<T> &c1, const Color<T> &c2)
\r
372 return Color<T>(c1.r + c2.r,
\r
379 inline Color<T> operator-(const Color<T> &c1, const Color<T> &c2)
\r
381 return Color<T>(c1.r - c2.r,
\r
388 inline Color<T> operator*(float l, const Color<T> &c)
\r
390 T r = (T)(l * c.r);
\r
391 T g = (T)(l * c.g);
\r
392 T b = (T)(l * c.b);
\r
393 T a = (T)(l * c.a);
\r
395 return Color<T>(r, g, b, a);
\r
399 inline Color<T> operator*(const Color<T> &c1, const Color<T> &c2)
\r
406 return Color<T>(r, g, b, a);
\r
410 inline Color<short> operator*(const Color<short> &c1, const Color<short> &c2)
\r
412 short r = c1.r * c2.r >> 12;
\r
413 short g = c1.g * c2.g >> 12;
\r
414 short b = c1.b * c2.b >> 12;
\r
415 short a = c1.a * c2.a >> 12;
\r
417 return Color<short>(r, g, b, a);
\r
421 inline Color<byte> operator*(const Color<byte> &c1, const Color<byte> &c2)
\r
423 byte r = c1.r * c2.r >> 8;
\r
424 byte g = c1.g * c2.g >> 8;
\r
425 byte b = c1.b * c2.b >> 8;
\r
426 byte a = c1.a * c2.a >> 8;
\r
428 return Color<byte>(r, g, b, a);
\r
432 inline Color<T> operator/(const Color<T> &c, float l)
\r
436 T r = (T)(l * c.r);
\r
437 T g = (T)(l * c.g);
\r
438 T b = (T)(l * c.b);
\r
439 T a = (T)(l * c.a);
\r
441 return Color<T>(r, g, b, a);
\r
445 inline Color<T> Color<T>::gradient(const Color<T> &c1, const Color<T> &c2, float d)
\r
449 T r = (c2.r - c1.r) * d;
\r
450 T g = (c2.g - c1.g) * d;
\r
451 T b = (c2.b - c1.b) * d;
\r
452 T a = (c2.a - c1.a) * d;
\r
454 return Color<T>(r, g, b, a);
\r
458 inline Color<T> Color<T>::shade(const Color<T> &c1, const Color<T> &c2, float d)
\r
460 T r = c1.r + (T)(d * (c2.r - c1.r));
\r
461 T g = c1.g + (T)(d * (c2.g - c1.g));
\r
462 T b = c1.b + (T)(d * (c2.b - c1.b));
\r
463 T a = c1.a + (T)(d * (c2.a - c1.a));
\r
465 return Color<T>(r, g, b, a);
\r
469 #endif // sw_Color_hpp
\r