1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2011 TransGaming Inc.
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
15 #include "Common/Types.hpp"
16 #include "Common/Math.hpp"
25 Color(const Color<byte> &c);
26 Color(const Color<short> &c);
27 Color(const Color<float> &c);
30 Color(unsigned short c);
31 Color(unsigned long c);
32 Color(unsigned int c);
34 Color(T r, T g, T b, T a = 1);
36 operator unsigned int() const;
39 const T &operator[](int i) const;
41 Color operator+() const;
42 Color operator-() const;
44 Color& operator=(const Color& c);
46 Color &operator+=(const Color &c);
47 Color &operator*=(float l);
49 static Color gradient(const Color &c1, const Color &c2, float d);
50 static Color shade(const Color &c1, const Color &c2, float d);
53 friend Color operator+(const Color &c1, const Color &c2);
55 friend Color operator-(const Color &c1, const Color &c2);
58 friend Color operator*(float l, const Color &c);
60 friend Color operator*(const Color &c1, const Color &c2);
62 friend Color operator/(const Color &c, float l);
71 #include "Common/Math.hpp"
76 inline Color<T>::Color()
80 inline Color<byte>::Color(const Color<byte> &c)
88 inline Color<byte>::Color(const Color<short> &c)
90 r = clamp(c.r >> 4, 0, 255);
91 g = clamp(c.g >> 4, 0, 255);
92 b = clamp(c.b >> 4, 0, 255);
93 a = clamp(c.a >> 4, 0, 255);
96 inline Color<byte>::Color(const Color<float> &c)
98 r = ifloor(clamp(c.r * 256.0f, 0.0f, 255.0f));
99 g = ifloor(clamp(c.g * 256.0f, 0.0f, 255.0f));
100 b = ifloor(clamp(c.b * 256.0f, 0.0f, 255.0f));
101 a = ifloor(clamp(c.a * 256.0f, 0.0f, 255.0f));
104 inline Color<short>::Color(const Color<short> &c)
112 inline Color<short>::Color(const Color<byte> &c)
120 inline Color<float>::Color(const Color<float> &c)
128 inline Color<short>::Color(const Color<float> &c)
130 r = iround(clamp(c.r * 4095.0f, -4096.0f, 4095.0f));
131 g = iround(clamp(c.g * 4095.0f, -4096.0f, 4095.0f));
132 b = iround(clamp(c.b * 4095.0f, -4096.0f, 4095.0f));
133 a = iround(clamp(c.a * 4095.0f, -4096.0f, 4095.0f));
136 inline Color<float>::Color(const Color<byte> &c)
144 inline Color<float>::Color(const Color<short> &c)
152 inline Color<float>::Color(unsigned short c)
154 r = (float)(c & 0xF800) / (float)0xF800;
155 g = (float)(c & 0x07E0) / (float)0x07E0;
156 b = (float)(c & 0x001F) / (float)0x001F;
160 inline Color<short>::Color(unsigned short c)
162 // 4.12 fixed-point format
163 r = ((c & 0xF800) >> 4) + ((c & 0xF800) >> 9) + ((c & 0xF800) >> 14);
164 g = ((c & 0x07E0) << 1) + ((c & 0x07E0) >> 5);
165 b = ((c & 0x001F) << 7) + ((c & 0x001F) << 2) + ((c & 0x001F) >> 3);
169 inline Color<byte>::Color(unsigned short c)
171 r = (byte)(((c & 0xF800) >> 8) + ((c & 0xE000) >> 13));
172 g = (byte)(((c & 0x07E0) >> 3) + ((c & 0x0600) >> 9));
173 b = (byte)(((c & 0x001F) << 3) + ((c & 0x001C) >> 2));
177 inline Color<float>::Color(int c)
179 const float d = 1.0f / 255.0f;
181 r = (float)((c & 0x00FF0000) >> 16) * d;
182 g = (float)((c & 0x0000FF00) >> 8) * d;
183 b = (float)((c & 0x000000FF) >> 0) * d;
184 a = (float)((c & 0xFF000000) >> 24) * d;
187 inline Color<short>::Color(int c)
189 // 4.12 fixed-point format
190 r = (short)((c & 0x00FF0000) >> 12);
191 g = (short)((c & 0x0000FF00) >> 4);
192 b = (short)((c & 0x000000FF) << 4);
193 a = (short)((c & 0xFF000000) >> 20);
196 inline Color<byte>::Color(int c)
198 r = (byte)((c & 0x00FF0000) >> 16);
199 g = (byte)((c & 0x0000FF00) >> 8);
200 b = (byte)((c & 0x000000FF) >> 0);
201 a = (byte)((c & 0xFF000000) >> 24);
204 inline Color<float>::Color(unsigned int c)
206 const float d = 1.0f / 255.0f;
208 r = (float)((c & 0x00FF0000) >> 16) * d;
209 g = (float)((c & 0x0000FF00) >> 8) * d;
210 b = (float)((c & 0x000000FF) >> 0) * d;
211 a = (float)((c & 0xFF000000) >> 24) * d;
214 inline Color<short>::Color(unsigned int c)
216 // 4.12 fixed-point format
217 r = (short)((c & 0x00FF0000) >> 12);
218 g = (short)((c & 0x0000FF00) >> 4);
219 b = (short)((c & 0x000000FF) << 4);
220 a = (short)((c & 0xFF000000) >> 20);
223 inline Color<byte>::Color(unsigned int c)
225 r = (byte)((c & 0x00FF0000) >> 16);
226 g = (byte)((c & 0x0000FF00) >> 8);
227 b = (byte)((c & 0x000000FF) >> 0);
228 a = (byte)((c & 0xFF000000) >> 24);
231 inline Color<float>::Color(unsigned long c)
233 const float d = 1.0f / 255.0f;
235 r = (float)((c & 0x00FF0000) >> 16) * d;
236 g = (float)((c & 0x0000FF00) >> 8) * d;
237 b = (float)((c & 0x000000FF) >> 0) * d;
238 a = (float)((c & 0xFF000000) >> 24) * d;
241 inline Color<short>::Color(unsigned long c)
243 // 4.12 fixed-point format
244 r = (short)((c & 0x00FF0000) >> 12);
245 g = (short)((c & 0x0000FF00) >> 4);
246 b = (short)((c & 0x000000FF) << 4);
247 a = (short)((c & 0xFF000000) >> 20);
250 inline Color<byte>::Color(unsigned long c)
252 r = (byte)((c & 0x00FF0000) >> 16);
253 g = (byte)((c & 0x0000FF00) >> 8);
254 b = (byte)((c & 0x000000FF) >> 0);
255 a = (byte)((c & 0xFF000000) >> 24);
259 inline Color<T>::Color(T r_, T g_, T b_, T a_)
267 inline Color<float>::operator unsigned int() const
269 return ((unsigned int)min(b * 255.0f, 255.0f) << 0) |
270 ((unsigned int)min(g * 255.0f, 255.0f) << 8) |
271 ((unsigned int)min(r * 255.0f, 255.0f) << 16) |
272 ((unsigned int)min(a * 255.0f, 255.0f) << 24);
275 inline Color<short>::operator unsigned int() const
277 return ((unsigned int)min(b >> 4, 255) << 0) |
278 ((unsigned int)min(g >> 4, 255) << 8) |
279 ((unsigned int)min(r >> 4, 255) << 16) |
280 ((unsigned int)min(a >> 4, 255) << 24);
283 inline Color<byte>::operator unsigned int() const
292 inline T &Color<T>::operator[](int i)
298 inline const T &Color<T>::operator[](int i) const
304 inline Color<T> Color<T>::operator+() const
310 inline Color<T> Color<T>::operator-() const
312 return Color(-r, -g, -b, -a);
316 inline Color<T> &Color<T>::operator=(const Color& c)
327 inline Color<T> &Color<T>::operator+=(const Color &c)
338 inline Color<T> &Color<T>::operator*=(float l)
346 inline Color<T> operator+(const Color<T> &c1, const Color<T> &c2)
348 return Color<T>(c1.r + c2.r,
355 inline Color<T> operator-(const Color<T> &c1, const Color<T> &c2)
357 return Color<T>(c1.r - c2.r,
364 inline Color<T> operator*(float l, const Color<T> &c)
371 return Color<T>(r, g, b, a);
375 inline Color<T> operator*(const Color<T> &c1, const Color<T> &c2)
382 return Color<T>(r, g, b, a);
385 inline Color<short> operator*(const Color<short> &c1, const Color<short> &c2)
387 short r = c1.r * c2.r >> 12;
388 short g = c1.g * c2.g >> 12;
389 short b = c1.b * c2.b >> 12;
390 short a = c1.a * c2.a >> 12;
392 return Color<short>(r, g, b, a);
395 inline Color<byte> operator*(const Color<byte> &c1, const Color<byte> &c2)
397 byte r = c1.r * c2.r >> 8;
398 byte g = c1.g * c2.g >> 8;
399 byte b = c1.b * c2.b >> 8;
400 byte a = c1.a * c2.a >> 8;
402 return Color<byte>(r, g, b, a);
406 inline Color<T> operator/(const Color<T> &c, float l)
415 return Color<T>(r, g, b, a);
419 inline Color<T> Color<T>::gradient(const Color<T> &c1, const Color<T> &c2, float d)
423 T r = (c2.r - c1.r) * d;
424 T g = (c2.g - c1.g) * d;
425 T b = (c2.b - c1.b) * d;
426 T a = (c2.a - c1.a) * d;
428 return Color<T>(r, g, b, a);
432 inline Color<T> Color<T>::shade(const Color<T> &c1, const Color<T> &c2, float d)
434 T r = c1.r + (T)(d * (c2.r - c1.r));
435 T g = c1.g + (T)(d * (c2.g - c1.g));
436 T b = c1.b + (T)(d * (c2.b - c1.b));
437 T a = c1.a + (T)(d * (c2.a - c1.a));
439 return Color<T>(r, g, b, a);
443 #endif // sw_Color_hpp