PageRenderTime 64ms CodeModel.GetById 2ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v3color.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 461 lines | 352 code | 74 blank | 35 comment | 18 complexity | dd53a09c7f6c91bb931c20620a4348c4 MD5 | raw file
  1/** 
  2 * @file v3color.h
  3 * @brief LLColor3 class header file.
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_V3COLOR_H
 28#define LL_V3COLOR_H
 29
 30class LLColor4;
 31class LLVector4;
 32
 33#include "llerror.h"
 34#include "llmath.h"
 35#include "llsd.h"
 36
 37//  LLColor3 = |r g b|
 38
 39static const U32 LENGTHOFCOLOR3 = 3;
 40
 41class LLColor3
 42{
 43public:
 44	F32 mV[LENGTHOFCOLOR3];
 45
 46	static LLColor3 white;
 47	static LLColor3 black;
 48	static LLColor3 grey;
 49
 50public:
 51	LLColor3();							// Initializes LLColor3 to (0, 0, 0)
 52	LLColor3(F32 r, F32 g, F32 b);		// Initializes LLColor3 to (r, g, b)
 53	LLColor3(const F32 *vec);			// Initializes LLColor3 to (vec[0]. vec[1], vec[2])
 54	LLColor3(const char *color_string);       // html format color ie "#FFDDEE"
 55	explicit LLColor3(const LLColor4& color4);  // "explicit" to avoid automatic conversion
 56	explicit LLColor3(const LLVector4& vector4);  // "explicit" to avoid automatic conversion
 57	LLColor3(const LLSD& sd);
 58	
 59
 60	LLSD getValue() const
 61	{
 62		LLSD ret;
 63		ret[0] = mV[0];
 64		ret[1] = mV[1];
 65		ret[2] = mV[2];
 66		return ret;
 67	}
 68
 69	void setValue(const LLSD& sd)
 70	{
 71		mV[0] = (F32) sd[0].asReal();;
 72		mV[1] = (F32) sd[1].asReal();;
 73		mV[2] = (F32) sd[2].asReal();;
 74	}
 75
 76	void setHSL(F32 hue, F32 saturation, F32 luminance);
 77	void calcHSL(F32* hue, F32* saturation, F32* luminance) const;
 78	
 79	const LLColor3&	setToBlack();					// Clears LLColor3 to (0, 0, 0)
 80	const LLColor3&	setToWhite();					// Zero LLColor3 to (0, 0, 0)
 81	
 82	const LLColor3&	setVec(F32 x, F32 y, F32 z);	// deprecated
 83	const LLColor3&	setVec(const LLColor3 &vec);	// deprecated
 84	const LLColor3&	setVec(const F32 *vec);			// deprecated
 85
 86	const LLColor3&	set(F32 x, F32 y, F32 z);	// Sets LLColor3 to (x, y, z)
 87	const LLColor3&	set(const LLColor3 &vec);	// Sets LLColor3 to vec
 88	const LLColor3&	set(const F32 *vec);		// Sets LLColor3 to vec
 89
 90	F32		magVec() const;				// deprecated
 91	F32		magVecSquared() const;		// deprecated
 92	F32		normVec();					// deprecated
 93
 94	F32		length() const;				// Returns magnitude of LLColor3
 95	F32		lengthSquared() const;		// Returns magnitude squared of LLColor3
 96	F32		normalize();				// Normalizes and returns the magnitude of LLColor3
 97
 98	F32		brightness() const;			// Returns brightness of LLColor3
 99
100	const LLColor3&	operator=(const LLColor4 &a);
101	
102	friend std::ostream&	 operator<<(std::ostream& s, const LLColor3 &a);		// Print a
103	friend LLColor3 operator+(const LLColor3 &a, const LLColor3 &b);	// Return vector a + b
104	friend LLColor3 operator-(const LLColor3 &a, const LLColor3 &b);	// Return vector a minus b
105
106	friend const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b);	// Return vector a + b
107	friend const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b);	// Return vector a minus b
108	friend const LLColor3& operator*=(LLColor3 &a, const LLColor3 &b);
109
110	friend LLColor3 operator*(const LLColor3 &a, const LLColor3 &b);	// Return component wise a * b
111	friend LLColor3 operator*(const LLColor3 &a, F32 k);				// Return a times scaler k
112	friend LLColor3 operator*(F32 k, const LLColor3 &a);				// Return a times scaler k
113
114	friend bool operator==(const LLColor3 &a, const LLColor3 &b);		// Return a == b
115	friend bool operator!=(const LLColor3 &a, const LLColor3 &b);		// Return a != b
116
117	friend const LLColor3& operator*=(LLColor3 &a, F32 k);				// Return a times scaler k
118
119	friend LLColor3 operator-(const LLColor3 &a);					// Return vector 1-rgb (inverse)
120
121	inline void clamp();
122	inline void exp();	// Do an exponential on the color
123};
124
125LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u);
126
127
128void LLColor3::clamp()
129{
130	// Clamp the color...
131	if (mV[0] < 0.f)
132	{
133		mV[0] = 0.f;
134	}
135	else if (mV[0] > 1.f)
136	{
137		mV[0] = 1.f;
138	}
139	if (mV[1] < 0.f)
140	{
141		mV[1] = 0.f;
142	}
143	else if (mV[1] > 1.f)
144	{
145		mV[1] = 1.f;
146	}
147	if (mV[2] < 0.f)
148	{
149		mV[2] = 0.f;
150	}
151	else if (mV[2] > 1.f)
152	{
153		mV[2] = 1.f;
154	}
155}
156
157// Non-member functions 
158F32		distVec(const LLColor3 &a, const LLColor3 &b);		// Returns distance between a and b
159F32		distVec_squared(const LLColor3 &a, const LLColor3 &b);// Returns distance squared between a and b
160
161inline LLColor3::LLColor3(void)
162{
163	mV[0] = 0.f;
164	mV[1] = 0.f;
165	mV[2] = 0.f;
166}
167
168inline LLColor3::LLColor3(F32 r, F32 g, F32 b)
169{
170	mV[VX] = r;
171	mV[VY] = g;
172	mV[VZ] = b;
173}
174
175
176inline LLColor3::LLColor3(const F32 *vec)
177{
178	mV[VX] = vec[VX];
179	mV[VY] = vec[VY];
180	mV[VZ] = vec[VZ];
181}
182
183#if LL_WINDOWS
184# pragma warning( disable : 4996 ) // strncpy teh sux0r
185#endif
186
187inline LLColor3::LLColor3(const char* color_string) // takes a string of format "RRGGBB" where RR is hex 00..FF 
188{
189	if (strlen(color_string) <  6)		/* Flawfinder: ignore */
190	{
191		mV[0] = 0.f;
192		mV[1] = 0.f;
193		mV[2] = 0.f;		
194		return;
195	}
196
197	char tempstr[7];
198	strncpy(tempstr,color_string,6);		/* Flawfinder: ignore */
199	tempstr[6] = '\0';
200	mV[VZ] = (F32)strtol(&tempstr[4],NULL,16)/255.f;
201	tempstr[4] = '\0';
202	mV[VY] = (F32)strtol(&tempstr[2],NULL,16)/255.f;
203	tempstr[2] = '\0';
204	mV[VX] = (F32)strtol(&tempstr[0],NULL,16)/255.f;
205}
206
207inline const LLColor3&	LLColor3::setToBlack(void)
208{
209	mV[0] = 0.f;
210	mV[1] = 0.f;
211	mV[2] = 0.f;
212	return (*this);
213}
214
215inline const LLColor3&	LLColor3::setToWhite(void)
216{
217	mV[0] = 1.f;
218	mV[1] = 1.f;
219	mV[2] = 1.f;
220	return (*this);
221}
222
223inline const LLColor3&	LLColor3::set(F32 r, F32 g, F32 b)
224{
225	mV[0] = r;
226	mV[1] = g;
227	mV[2] = b;
228	return (*this);
229}
230
231inline const LLColor3&	LLColor3::set(const LLColor3 &vec)
232{
233	mV[0] = vec.mV[0];
234	mV[1] = vec.mV[1];
235	mV[2] = vec.mV[2];
236	return (*this);
237}
238
239inline const LLColor3&	LLColor3::set(const F32 *vec)
240{
241	mV[0] = vec[0];
242	mV[1] = vec[1];
243	mV[2] = vec[2];
244	return (*this);
245}
246
247// deprecated
248inline const LLColor3&	LLColor3::setVec(F32 r, F32 g, F32 b)
249{
250	mV[0] = r;
251	mV[1] = g;
252	mV[2] = b;
253	return (*this);
254}
255
256// deprecated
257inline const LLColor3&	LLColor3::setVec(const LLColor3 &vec)
258{
259	mV[0] = vec.mV[0];
260	mV[1] = vec.mV[1];
261	mV[2] = vec.mV[2];
262	return (*this);
263}
264
265// deprecated
266inline const LLColor3&	LLColor3::setVec(const F32 *vec)
267{
268	mV[0] = vec[0];
269	mV[1] = vec[1];
270	mV[2] = vec[2];
271	return (*this);
272}
273
274inline F32		LLColor3::brightness(void) const
275{
276	return (mV[0] + mV[1] + mV[2]) / 3.0f;
277}
278
279inline F32		LLColor3::length(void) const
280{
281	return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
282}
283
284inline F32		LLColor3::lengthSquared(void) const
285{
286	return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2];
287}
288
289inline F32		LLColor3::normalize(void)
290{
291	F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
292	F32 oomag;
293
294	if (mag)
295	{
296		oomag = 1.f/mag;
297		mV[0] *= oomag;
298		mV[1] *= oomag;
299		mV[2] *= oomag;
300	}
301	return (mag);
302}
303
304// deprecated
305inline F32		LLColor3::magVec(void) const
306{
307	return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
308}
309
310// deprecated
311inline F32		LLColor3::magVecSquared(void) const
312{
313	return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2];
314}
315
316// deprecated
317inline F32		LLColor3::normVec(void)
318{
319	F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
320	F32 oomag;
321
322	if (mag)
323	{
324		oomag = 1.f/mag;
325		mV[0] *= oomag;
326		mV[1] *= oomag;
327		mV[2] *= oomag;
328	}
329	return (mag);
330}
331
332inline void LLColor3::exp()
333{
334#if 0
335	mV[0] = ::exp(mV[0]);
336	mV[1] = ::exp(mV[1]);
337	mV[2] = ::exp(mV[2]);
338#else
339	mV[0] = (F32)LL_FAST_EXP(mV[0]);
340	mV[1] = (F32)LL_FAST_EXP(mV[1]);
341	mV[2] = (F32)LL_FAST_EXP(mV[2]);
342#endif
343}
344
345
346inline LLColor3 operator+(const LLColor3 &a, const LLColor3 &b)
347{
348	return LLColor3(
349		a.mV[0] + b.mV[0],
350		a.mV[1] + b.mV[1],
351		a.mV[2] + b.mV[2]);
352}
353
354inline LLColor3 operator-(const LLColor3 &a, const LLColor3 &b)
355{
356	return LLColor3(
357		a.mV[0] - b.mV[0],
358		a.mV[1] - b.mV[1],
359		a.mV[2] - b.mV[2]);
360}
361
362inline LLColor3  operator*(const LLColor3 &a, const LLColor3 &b)
363{
364	return LLColor3(
365		a.mV[0] * b.mV[0],
366		a.mV[1] * b.mV[1],
367		a.mV[2] * b.mV[2]);
368}
369
370inline LLColor3 operator*(const LLColor3 &a, F32 k)
371{
372	return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k );
373}
374
375inline LLColor3 operator*(F32 k, const LLColor3 &a)
376{
377	return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k );
378}
379
380inline bool operator==(const LLColor3 &a, const LLColor3 &b)
381{
382	return (  (a.mV[0] == b.mV[0])
383			&&(a.mV[1] == b.mV[1])
384			&&(a.mV[2] == b.mV[2]));
385}
386
387inline bool operator!=(const LLColor3 &a, const LLColor3 &b)
388{
389	return (  (a.mV[0] != b.mV[0])
390			||(a.mV[1] != b.mV[1])
391			||(a.mV[2] != b.mV[2]));
392}
393
394inline const LLColor3 &operator*=(LLColor3 &a, const LLColor3 &b)
395{
396	a.mV[0] *= b.mV[0];
397	a.mV[1] *= b.mV[1];
398	a.mV[2] *= b.mV[2];
399	return a;
400}
401
402inline const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b)
403{
404	a.mV[0] += b.mV[0];
405	a.mV[1] += b.mV[1];
406	a.mV[2] += b.mV[2];
407	return a;
408}
409
410inline const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b)
411{
412	a.mV[0] -= b.mV[0];
413	a.mV[1] -= b.mV[1];
414	a.mV[2] -= b.mV[2];
415	return a;
416}
417
418inline const LLColor3& operator*=(LLColor3 &a, F32 k)
419{
420	a.mV[0] *= k;
421	a.mV[1] *= k;
422	a.mV[2] *= k;
423	return a;
424}
425
426inline LLColor3 operator-(const LLColor3 &a)
427{
428	return LLColor3(
429		1.f - a.mV[0],
430		1.f - a.mV[1],
431		1.f - a.mV[2] );
432}
433
434// Non-member functions
435
436inline F32		distVec(const LLColor3 &a, const LLColor3 &b)
437{
438	F32 x = a.mV[0] - b.mV[0];
439	F32 y = a.mV[1] - b.mV[1];
440	F32 z = a.mV[2] - b.mV[2];
441	return (F32) sqrt( x*x + y*y + z*z );
442}
443
444inline F32		distVec_squared(const LLColor3 &a, const LLColor3 &b)
445{
446	F32 x = a.mV[0] - b.mV[0];
447	F32 y = a.mV[1] - b.mV[1];
448	F32 z = a.mV[2] - b.mV[2];
449	return x*x + y*y + z*z;
450}
451
452inline LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u)
453{
454	return LLColor3(
455		a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
456		a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
457		a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u);
458}
459
460
461#endif