PageRenderTime 292ms CodeModel.GetById 108ms app.highlight 66ms RepoModel.GetById 100ms app.codeStats 0ms

/opengles/src/FractionalColor.h

http://ftk.googlecode.com/
C++ Header | 189 lines | 119 code | 32 blank | 38 comment | 0 complexity | 74af123d9a593fe037a63cf348240df0 MD5 | raw file
  1#ifndef EGL_FRACTIONAL_COLOR_H
  2#define EGL_FRACTIONAL_COLOR_H 1
  3
  4// ==========================================================================
  5//
  6// FractionalColor	Fixed point representation of an RGBA color
  7//
  8// --------------------------------------------------------------------------
  9//
 10// 10-05-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
 42#include "OGLES.h"
 43#include "fixed.h"
 44#include "Color.h"
 45
 46
 47namespace EGL {
 48	class FractionalColor {
 49
 50	public:
 51		EGL_Fixed	r, g, b, a;			// rgba components
 52
 53	public:
 54		FractionalColor() {
 55			r = g = b = a = 0;
 56		}
 57
 58		FractionalColor(const Color & color) {
 59			r = (color.R() << 8 | color.R()) + (color.R() >> 7);
 60			g = (color.G() << 8 | color.G()) + (color.G() >> 7);
 61			b = (color.B() << 8 | color.B()) + (color.B() >> 7);
 62			a = (color.A() << 8 | color.A()) + (color.A() >> 7);
 63		}
 64
 65		FractionalColor(EGL_Fixed R, EGL_Fixed G, EGL_Fixed B, EGL_Fixed A) {
 66			r = R;
 67			g = G;
 68			b = B;
 69			a = A;
 70		}
 71
 72		FractionalColor(const EGL_Fixed * rgba) {
 73			r = rgba[0];
 74			g = rgba[1];
 75			b = rgba[2];
 76			a = rgba[3];
 77		}
 78
 79		static FractionalColor Clamp(const EGL_Fixed * rgba) {
 80			return FractionalColor(
 81				EGL_CLAMP(rgba[0], 0, EGL_ONE),
 82				EGL_CLAMP(rgba[1], 0, EGL_ONE),
 83				EGL_CLAMP(rgba[2], 0, EGL_ONE),
 84				EGL_CLAMP(rgba[3], 0, EGL_ONE));
 85		}
 86
 87		FractionalColor(const FractionalColor& other) {
 88			r = other.r;
 89			b = other.b;
 90			g = other.g;
 91			a = other.a;
 92		}
 93
 94		FractionalColor& operator=(const FractionalColor& other) {
 95			r = other.r;
 96			b = other.b;
 97			g = other.g;
 98			a = other.a;
 99			return *this;
100		}
101
102		// TODO; Decide: Is Fatcional color 8.16 or (1).16 format
103		inline U16 ConvertTo565() const {
104			return 
105				 EGL_IntFromFixed(b * 0xFF) >> 3 | 
106				(EGL_IntFromFixed(g * 0xFF) & 0xFC) << 3 | 
107				(EGL_IntFromFixed(r * 0xFF) & 0xF8) << 8;
108		}
109
110		inline U16 ConvertTo5551() const {
111			return 
112				 EGL_IntFromFixed(b * 0xFF) >> 3 | 
113				(EGL_IntFromFixed(g * 0xFF) & 0xF8) << 2 | 
114				(EGL_IntFromFixed(r * 0xFF) & 0xF8) << 7 | 
115				(EGL_IntFromFixed(a * 0xFF) & 0x80) << 8;
116		}
117
118		// convert fixed point to byte format
119		inline operator Color() const {
120			return Color(
121				EGL_IntFromFixed((0x1FF * EGL_CLAMP(r, 0, EGL_ONE)) >> 1),
122				EGL_IntFromFixed((0x1FF * EGL_CLAMP(g, 0, EGL_ONE)) >> 1),
123				EGL_IntFromFixed((0x1FF * EGL_CLAMP(b, 0, EGL_ONE)) >> 1),
124				EGL_IntFromFixed((0x1FF * EGL_CLAMP(a, 0, EGL_ONE)) >> 1));
125		}
126
127		inline void Clamp() {
128			r = EGL_CLAMP(r, 0, EGL_ONE);
129			g = EGL_CLAMP(g, 0, EGL_ONE);
130			b = EGL_CLAMP(b, 0, EGL_ONE);
131			a = EGL_CLAMP(a, 0, EGL_ONE);
132		}
133
134		inline FractionalColor operator*(const FractionalColor& other) const {
135			return FractionalColor(EGL_Mul(r, other.r),
136				EGL_Mul(g, other.g), EGL_Mul(b, other.b), a);
137		}
138
139
140		inline FractionalColor& operator*=(const FractionalColor& other) {
141			r = EGL_Mul(r, other.r);
142			g = EGL_Mul(g, other.g);
143			b = EGL_Mul(b, other.b);
144			return *this;
145		}
146
147		inline FractionalColor operator*(EGL_Fixed scale) const {
148			return FractionalColor(EGL_Mul(r, scale),
149				EGL_Mul(g, scale), EGL_Mul(b, scale), EGL_Mul(a, scale));
150		}
151
152
153		inline FractionalColor& operator*=(EGL_Fixed scale) {
154			r = EGL_Mul(r, scale);
155			g = EGL_Mul(g, scale);
156			b = EGL_Mul(b, scale);
157			a = EGL_Mul(a, scale);
158			return *this;
159		}
160
161		inline FractionalColor operator-(const FractionalColor& other) const {
162			return FractionalColor(r - other.r, g - other.g, b - other.b, a - other.a);
163		}
164
165		inline FractionalColor operator+(const FractionalColor& other) const {
166			return FractionalColor(r + other.r, g + other.g, b + other.b, a + other.a);
167		}
168
169		inline void Accumulate(const FractionalColor& color,
170							EGL_Fixed scale) {
171			r += EGL_Mul(color.r, scale);
172			g += EGL_Mul(color.g, scale);
173			b += EGL_Mul(color.b, scale);
174		}
175
176
177		inline FractionalColor& operator+=(const FractionalColor& color) {
178			r += color.r;
179			g += color.g;
180			b += color.b;
181			//a += color.a;
182
183			return *this;
184		}
185	};
186}
187
188
189#endif //ndef EGL_FRACTIONAL_COLOR_H