PageRenderTime 40ms CodeModel.GetById 20ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/opengles/src/Color.h

http://ftk.googlecode.com/
C++ Header | 261 lines | 171 code | 52 blank | 38 comment | 2 complexity | 020aba49beb278e651deffb7af99ccaf MD5 | raw file
  1#ifndef EGL_COLOR_H
  2#define EGL_COLOR_H 1
  3
  4// ==========================================================================
  5//
  6// Color			Integer 8-8-8-8 representation of an RGBA color
  7//
  8// --------------------------------------------------------------------------
  9//
 10// 10-09-2003		Hans-Martin Will	initial version
 11//
 12// --------------------------------------------------------------------------
 13//
 14// Copyright (c) 2004, Hans-Martin Will. All rights reserved.
 15// 
 16// Redistribution and use in source and binary forms, with or without 
 17// modification, are permitted provided that the following conditions are 
 18// met:
 19// 
 20//	 *  Redistributions of source code must retain the above copyright
 21// 		notice, this list of conditions and the following disclaimer. 
 22//   *	Redistributions in binary form must reproduce the above copyright
 23// 		notice, this list of conditions and the following disclaimer in the 
 24// 		documentation and/or other materials provided with the distribution. 
 25// 
 26// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 27// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 29// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 30// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 31// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 32// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 33// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 34// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 35// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 36// THE POSSIBILITY OF SUCH DAMAGE.
 37//
 38// ==========================================================================
 39
 40
 41#include "OGLES.h"
 42#include "fixed.h"
 43
 44
 45namespace EGL {
 46
 47	class Color {
 48
 49	public:
 50		enum {
 51			MAX = 0xff
 52		};
 53
 54	public:
 55		U8	r, g, b, a;							// rgba components
 56
 57	public:
 58		Color() {
 59			r = g = b = a = 0;
 60		}
 61
 62		Color(U32 rgba) { 
 63			r = (rgba >> 24) & 0xff;
 64			g = (rgba >> 16) & 0xff;
 65			b = (rgba >>  8) & 0xff;
 66			a = rgba & 0xff;
 67		}
 68
 69		Color(U8 R, U8 G, U8 B, U8 A) {
 70			r = R;
 71			g = G;
 72			b = B;
 73			a = A;
 74		}
 75
 76		Color(const Color& other) {
 77			r = other.r;
 78			g = other.g;
 79			b = other.b;
 80			a = other.a;
 81		}
 82
 83		Color& operator=(const Color& other) {
 84			r = other.r;
 85			g = other.g;
 86			b = other.b;
 87			a = other.a;
 88			return *this;
 89		}
 90
 91		bool operator==(const Color& other) const {
 92			return ConvertToRGBA() == other.ConvertToRGBA();
 93		}
 94
 95		inline U8 A() const {
 96			return a;
 97		}
 98
 99		inline U8 R() const {
100			return r;
101		}
102
103		inline U8 G() const {
104			return g;
105		}
106
107		inline U8 B() const {
108			return b;
109		}
110
111		inline Color Mask(bool maskR, bool maskG, bool maskB, bool maskA) const {
112			return Color(maskR ? r : 0, maskG ? g : 0, maskB ? b : 0, maskA ? a : 0);
113		}
114
115		inline U16 ConvertTo565() const {
116			return (b & 0xF8) >> 3 | (g & 0xFC) << 3 | (r & 0xF8) << 8;
117		}
118
119		inline U32 ConvertToRGBA() const {
120			return r << 24 | g << 16 | b << 8 | a;
121		}
122
123		inline U16 ConvertTo5551() const {
124			return (b & 0xF8) >> 2 | (g & 0xF8) << 3 | (r & 0xF8) << 8 | (a & 0x80) >> 7;
125		}
126
127		inline U16 ConvertTo4444() const {
128			return (r & 0xf0) << 8 |
129				(g & 0xf0) << 4 |
130				(b & 0xf0) |
131				a >> 4;
132		}
133
134		static inline Color FromRGBA(U32 rgba) {
135			U8 r = (rgba & 0xFF000000) >> 24;
136			U8 g = (rgba & 0x00FF0000) >> 16;
137			U8 b = (rgba & 0x0000FF00) >>  8;
138			U8 a = (rgba & 0x000000FF);
139
140			return Color(r, g, b, a);
141		}
142
143		static inline Color From4444(U16 u4444) {
144			U8 r = (u4444 & 0xF000u) >> 8;
145			U8 g = (u4444 & 0x0F00u) >> 4;
146			U8 b = (u4444 & 0x00F0u);
147			U8 a = (u4444 & 0x000Fu) << 4;
148
149			r |= r >> 4;
150			g |= g >> 4;
151			b |= b >> 4;
152			a |= a >> 4;
153
154			return Color(r, g, b, a);
155		}
156
157		static inline Color From565(U16 u565) {
158			U8 b = (u565 & 0x001Fu) << 3;
159			U8 g = (u565 & 0x07E0u) >> 3;
160			U8 r = (u565 & 0xF800u) >> 8;
161
162			r |= r >> 5;
163			g |= g >> 6;
164			b |= b >> 5;
165
166			return Color(r, g, b, 0xFF);
167		}
168
169		static inline Color From565A(U16 u565, U8 alpha) {
170			U8 b = (u565 & 0x001Fu) << 3;
171			U8 g = (u565 & 0x07E0u) >> 3;
172			U8 r = (u565 & 0xF800u) >> 8;
173
174			r |= r >> 5;
175			g |= g >> 6;
176			b |= b >> 5;
177
178			return Color(r, g, b, alpha);
179		}
180
181		static inline Color From5551(U16 u5551) {
182			U8 b = (u5551 & 0x003Eu) << 2;
183			U8 g = (u5551 & 0x07C0u) >> 3;
184			U8 r = (u5551 & 0xF800u) >> 8;
185			U8 a = (u5551 & 0x0001u) << 7;
186
187			r |= r >> 5;
188			g |= g >> 5;
189			b |= b >> 5;
190			if (a) a |= 0x7f;
191
192			return Color(r, g, b, a);
193		}
194
195		static inline Color FromLuminanceAlpha(U16 la) {
196			U8 l = (la & 0xff);
197			U8 a = (la & 0xff00) >> 8;
198
199			return Color(l, l, l, a);
200		}
201
202		Color operator+(const Color& other) const {
203			return Color(clamp(r + other.r), clamp(g + other.g), 
204				clamp(b + other.b), clamp(a + other.a));
205		}
206
207		Color operator*(const Color& factor) const {
208			return Color(mul(r, factor.r), mul(g, factor.g), mul(b, factor.b), mul(a, factor.a));
209		}
210
211		// -------------------------------------------------------------------------
212		// return the blend of src and dst, where alpha is a value between 0 and 0x100
213		// -------------------------------------------------------------------------
214		static inline Color Blend(const Color& src, const Color& dst, U32 alpha) {
215			U32 oneMinusAlpha = 0x100 - alpha;
216
217			return Color((src.R() * alpha + dst.R() * oneMinusAlpha) >> 8,
218				(src.G() * alpha + dst.G() * oneMinusAlpha) >> 8,
219				(src.B() * alpha + dst.B() * oneMinusAlpha) >> 8,
220				src.A());
221		}
222
223		static inline Color BlendAlpha(const Color& src, const Color& dst, U32 alpha) {
224			U32 oneMinusAlpha = 0x100 - alpha;
225
226			return Color((src.R() * alpha + dst.R() * oneMinusAlpha) >> 8,
227				(src.G() * alpha + dst.G() * oneMinusAlpha) >> 8,
228				(src.B() * alpha + dst.B() * oneMinusAlpha) >> 8,
229				(src.A() * alpha + dst.A() * oneMinusAlpha) >> 8);
230		}
231
232		static inline Color Average(const Color & a, const Color & b) {
233			return Color((a.R() + b.R()) / 2,
234						 (a.G() + b.G()) / 2,
235						 (a.B() + b.B()) / 2,
236						 (a.A() + b.A()) / 2);
237		}
238
239		static inline Color Average(const Color & a, const Color & b,
240									const Color & c, const Color & d) {
241			return Color((a.R() + b.R() + c.R() + d.R()) / 4,
242						 (a.G() + b.G() + c.G() + d.G()) / 4,
243						 (a.B() + b.B() + c.B() + d.B()) / 4,
244						 (a.A() + b.A() + c.A() + d.A()) / 4);
245		}
246
247	private:
248		static inline U8 clamp(U16 value) {
249			return (value > MAX) ? (U8) MAX : (U8) value;
250		}
251
252		static inline U8 mul(U8 color, U8 factor) {
253			U16 prod = color * factor;
254
255			return (prod + (prod >> 7)) >> 8;
256		}
257	};
258
259}
260
261#endif //ndef EGL_COLOR_H