PageRenderTime 58ms CodeModel.GetById 14ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v4color.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 649 lines | 497 code | 99 blank | 53 comment | 29 complexity | b7f84ac5520377b227a2964271189445 MD5 | raw file
  1/** 
  2 * @file v4color.h
  3 * @brief LLColor4 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_V4COLOR_H
 28#define LL_V4COLOR_H
 29
 30#include "llerror.h"
 31//#include "vmath.h"
 32#include "llmath.h"
 33#include "llsd.h"
 34
 35class LLColor3;
 36class LLColor4U;
 37class LLVector4;
 38
 39//  LLColor4 = |x y z w|
 40
 41static const U32 LENGTHOFCOLOR4 = 4;
 42
 43static const U32 MAX_LENGTH_OF_COLOR_NAME = 15; //Give plenty of room for additional colors...
 44
 45class LLColor4
 46{
 47	public:
 48		F32 mV[LENGTHOFCOLOR4];
 49		LLColor4();						// Initializes LLColor4 to (0, 0, 0, 1)
 50		LLColor4(F32 r, F32 g, F32 b);		// Initializes LLColor4 to (r, g, b, 1)
 51		LLColor4(F32 r, F32 g, F32 b, F32 a);		// Initializes LLColor4 to (r. g, b, a)
 52		LLColor4(U32 clr);							// Initializes LLColor4 to (r=clr>>24, etc))
 53		LLColor4(const F32 *vec);			// Initializes LLColor4 to (vec[0]. vec[1], vec[2], 1)
 54		LLColor4(const LLColor3 &vec, F32 a = 1.f);	// Initializes LLColor4 to (vec, a)
 55		explicit LLColor4(const LLSD& sd);
 56		explicit LLColor4(const LLColor4U& color4u);  // "explicit" to avoid automatic conversion
 57		explicit LLColor4(const LLVector4& vector4);  // "explicit" to avoid automatic conversion
 58
 59		LLSD getValue() const
 60		{
 61			LLSD ret;
 62			ret[0] = mV[0];
 63			ret[1] = mV[1];
 64			ret[2] = mV[2];
 65			ret[3] = mV[3];
 66			return ret;
 67		}
 68	
 69		void setValue(const LLSD& sd);
 70
 71		void setHSL(F32 hue, F32 saturation, F32 luminance);
 72		void calcHSL(F32* hue, F32* saturation, F32* luminance) const;
 73
 74		const LLColor4&	setToBlack();						// zero LLColor4 to (0, 0, 0, 1)
 75		const LLColor4&	setToWhite();						// zero LLColor4 to (0, 0, 0, 1)
 76
 77		const LLColor4&	setVec(F32 r, F32 g, F32 b, F32 a);	// deprecated -- use set()
 78		const LLColor4&	setVec(F32 r, F32 g, F32 b);		// deprecated -- use set()
 79		const LLColor4&	setVec(const LLColor4 &vec);		// deprecated -- use set()
 80		const LLColor4&	setVec(const LLColor3 &vec);		// deprecated -- use set()
 81		const LLColor4&	setVec(const LLColor3 &vec, F32 a);	// deprecated -- use set()
 82		const LLColor4&	setVec(const F32 *vec);				// deprecated -- use set()
 83		const LLColor4&	setVec(const LLColor4U& color4u); 	// deprecated -- use set()
 84
 85		const LLColor4&	set(F32 r, F32 g, F32 b, F32 a);	// Sets LLColor4 to (r, g, b, a)
 86		const LLColor4&	set(F32 r, F32 g, F32 b);	// Sets LLColor4 to (r, g, b) (no change in a)
 87		const LLColor4&	set(const LLColor4 &vec);	// Sets LLColor4 to vec
 88		const LLColor4&	set(const LLColor3 &vec);	// Sets LLColor4 to LLColor3 vec (no change in alpha)
 89		const LLColor4&	set(const LLColor3 &vec, F32 a);	// Sets LLColor4 to LLColor3 vec, with alpha specified
 90		const LLColor4&	set(const F32 *vec);			// Sets LLColor4 to vec
 91		const LLColor4&	set(const LLColor4U& color4u); // Sets LLColor4 to color4u, rescaled.
 92
 93
 94		const LLColor4&    setAlpha(F32 a);
 95
 96		F32			magVec() const;				// deprecated -- use length()
 97		F32			magVecSquared() const;		// deprecated -- use lengthSquared()
 98		F32			normVec();					// deprecated -- use normalize()
 99
100		F32			length() const;				// Returns magnitude of LLColor4
101		F32			lengthSquared() const;		// Returns magnitude squared of LLColor4
102		F32			normalize();				// deprecated -- use normalize()
103
104		BOOL		isOpaque() { return mV[VALPHA] == 1.f; }
105
106		F32 operator[](int idx) const { return mV[idx]; }
107		F32 &operator[](int idx) { return mV[idx]; }
108	
109	    const LLColor4& operator=(const LLColor3 &a);	// Assigns vec3 to vec4 and returns vec4
110		
111		bool operator<(const LLColor4& rhs) const;
112		friend std::ostream&	 operator<<(std::ostream& s, const LLColor4 &a);		// Print a
113		friend LLColor4 operator+(const LLColor4 &a, const LLColor4 &b);	// Return vector a + b
114		friend LLColor4 operator-(const LLColor4 &a, const LLColor4 &b);	// Return vector a minus b
115		friend LLColor4 operator*(const LLColor4 &a, const LLColor4 &b);	// Return component wise a * b
116		friend LLColor4 operator*(const LLColor4 &a, F32 k);				// Return rgb times scaler k (no alpha change)
117		friend LLColor4 operator*(F32 k, const LLColor4 &a);				// Return rgb times scaler k (no alpha change)
118		friend LLColor4 operator%(const LLColor4 &a, F32 k);				// Return alpha times scaler k (no rgb change)
119		friend LLColor4 operator%(F32 k, const LLColor4 &a);				// Return alpha times scaler k (no rgb change)
120		friend bool operator==(const LLColor4 &a, const LLColor4 &b);		// Return a == b
121		friend bool operator!=(const LLColor4 &a, const LLColor4 &b);		// Return a != b
122		
123		friend bool operator==(const LLColor4 &a, const LLColor3 &b);		// Return a == b
124		friend bool operator!=(const LLColor4 &a, const LLColor3 &b);		// Return a != b
125
126		friend const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b);	// Return vector a + b
127		friend const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b);	// Return vector a minus b
128		friend const LLColor4& operator*=(LLColor4 &a, F32 k);				// Return rgb times scaler k (no alpha change)
129		friend const LLColor4& operator%=(LLColor4 &a, F32 k);				// Return alpha times scaler k (no rgb change)
130
131		friend const LLColor4& operator*=(LLColor4 &a, const LLColor4 &b); // Doesn't multiply alpha! (for lighting)
132
133		// conversion
134		operator const LLColor4U() const;
135
136		// Basic color values.
137		static LLColor4 red;
138		static LLColor4 green;
139		static LLColor4 blue;
140		static LLColor4 black;
141		static LLColor4 white;
142		static LLColor4 yellow;
143		static LLColor4 magenta;
144		static LLColor4 cyan;
145		static LLColor4 smoke;
146		static LLColor4 grey;
147		static LLColor4 orange;
148		static LLColor4 purple;
149		static LLColor4 pink;
150		static LLColor4 transparent;
151
152		// Extra color values.
153		static LLColor4 grey1;
154		static LLColor4 grey2;
155		static LLColor4 grey3;
156		static LLColor4 grey4;
157
158		static LLColor4 red1;
159		static LLColor4 red2;
160		static LLColor4 red3;
161		static LLColor4 red4;
162		static LLColor4 red5;
163
164		static LLColor4 green1;
165		static LLColor4 green2;
166		static LLColor4 green3;
167		static LLColor4 green4;
168		static LLColor4 green5;
169		static LLColor4 green6;
170
171		static LLColor4 blue1;
172		static LLColor4 blue2;
173		static LLColor4 blue3;
174		static LLColor4 blue4;
175		static LLColor4 blue5;
176		static LLColor4 blue6;
177
178		static LLColor4 yellow1;
179		static LLColor4 yellow2;
180		static LLColor4 yellow3;
181		static LLColor4 yellow4;
182		static LLColor4 yellow5;
183		static LLColor4 yellow6;
184		static LLColor4 yellow7;
185		static LLColor4 yellow8;
186		static LLColor4 yellow9;
187
188		static LLColor4 orange1;
189		static LLColor4 orange2;
190		static LLColor4 orange3;
191		static LLColor4 orange4;
192		static LLColor4 orange5;
193		static LLColor4 orange6;
194
195		static LLColor4 magenta1;
196		static LLColor4 magenta2;
197		static LLColor4 magenta3;
198		static LLColor4 magenta4;
199
200		static LLColor4 purple1;
201		static LLColor4 purple2;
202		static LLColor4 purple3;
203		static LLColor4 purple4;
204		static LLColor4 purple5;
205		static LLColor4 purple6;
206
207		static LLColor4 pink1;
208		static LLColor4 pink2;
209
210		static LLColor4 cyan1;
211		static LLColor4 cyan2;
212		static LLColor4 cyan3;
213		static LLColor4 cyan4;
214		static LLColor4 cyan5;
215		static LLColor4 cyan6;
216	
217		static BOOL parseColor(const std::string& buf, LLColor4* color);
218		static BOOL parseColor4(const std::string& buf, LLColor4* color);
219
220		inline void clamp();
221};
222
223
224// Non-member functions 
225F32		distVec(const LLColor4 &a, const LLColor4 &b);			// Returns distance between a and b
226F32		distVec_squared(const LLColor4 &a, const LLColor4 &b);	// Returns distance squared between a and b
227LLColor3	vec4to3(const LLColor4 &vec);
228LLColor4	vec3to4(const LLColor3 &vec);
229LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u);
230
231inline LLColor4::LLColor4(void)
232{
233	mV[VX] = 0.f;
234	mV[VY] = 0.f;
235	mV[VZ] = 0.f;
236	mV[VW] = 1.f;
237}
238
239inline LLColor4::LLColor4(const LLSD& sd)
240{
241	this->setValue(sd);
242}
243
244inline LLColor4::LLColor4(F32 r, F32 g, F32 b)
245{
246	mV[VX] = r;
247	mV[VY] = g;
248	mV[VZ] = b;
249	mV[VW] = 1.f;
250}
251
252inline LLColor4::LLColor4(F32 r, F32 g, F32 b, F32 a)
253{
254	mV[VX] = r;
255	mV[VY] = g;
256	mV[VZ] = b;
257	mV[VW] = a;
258}
259
260inline LLColor4::LLColor4(U32 clr)
261{
262	mV[VX] = (clr&0xff) * (1.0f/255.0f);
263	mV[VY] = ((clr>>8)&0xff) * (1.0f/255.0f);
264	mV[VZ] = ((clr>>16)&0xff) * (1.0f/255.0f);
265	mV[VW] = (clr>>24) * (1.0f/255.0f);
266}
267
268
269inline LLColor4::LLColor4(const F32 *vec)
270{
271	mV[VX] = vec[VX];
272	mV[VY] = vec[VY];
273	mV[VZ] = vec[VZ];
274	mV[VW] = vec[VW];
275}
276
277inline const LLColor4&	LLColor4::setToBlack(void)
278{
279	mV[VX] = 0.f;
280	mV[VY] = 0.f;
281	mV[VZ] = 0.f;
282	mV[VW] = 1.f;
283	return (*this);
284}
285
286inline const LLColor4&	LLColor4::setToWhite(void)
287{
288	mV[VX] = 1.f;
289	mV[VY] = 1.f;
290	mV[VZ] = 1.f;
291	mV[VW] = 1.f;
292	return (*this);
293}
294
295inline const LLColor4&	LLColor4::set(F32 x, F32 y, F32 z)
296{
297	mV[VX] = x;
298	mV[VY] = y;
299	mV[VZ] = z;
300
301//  no change to alpha!
302//	mV[VW] = 1.f;  
303
304	return (*this);
305}
306
307inline const LLColor4&	LLColor4::set(F32 x, F32 y, F32 z, F32 a)
308{
309	mV[VX] = x;
310	mV[VY] = y;
311	mV[VZ] = z;
312	mV[VW] = a;  
313	return (*this);
314}
315
316inline const LLColor4&	LLColor4::set(const LLColor4 &vec)
317{
318	mV[VX] = vec.mV[VX];
319	mV[VY] = vec.mV[VY];
320	mV[VZ] = vec.mV[VZ];
321	mV[VW] = vec.mV[VW];
322	return (*this);
323}
324
325
326inline const LLColor4&	LLColor4::set(const F32 *vec)
327{
328	mV[VX] = vec[VX];
329	mV[VY] = vec[VY];
330	mV[VZ] = vec[VZ];
331	mV[VW] = vec[VW];
332	return (*this);
333}
334
335// deprecated
336inline const LLColor4&	LLColor4::setVec(F32 x, F32 y, F32 z)
337{
338	mV[VX] = x;
339	mV[VY] = y;
340	mV[VZ] = z;
341
342//  no change to alpha!
343//	mV[VW] = 1.f;  
344
345	return (*this);
346}
347
348// deprecated
349inline const LLColor4&	LLColor4::setVec(F32 x, F32 y, F32 z, F32 a)
350{
351	mV[VX] = x;
352	mV[VY] = y;
353	mV[VZ] = z;
354	mV[VW] = a;  
355	return (*this);
356}
357
358// deprecated
359inline const LLColor4&	LLColor4::setVec(const LLColor4 &vec)
360{
361	mV[VX] = vec.mV[VX];
362	mV[VY] = vec.mV[VY];
363	mV[VZ] = vec.mV[VZ];
364	mV[VW] = vec.mV[VW];
365	return (*this);
366}
367
368
369// deprecated
370inline const LLColor4&	LLColor4::setVec(const F32 *vec)
371{
372	mV[VX] = vec[VX];
373	mV[VY] = vec[VY];
374	mV[VZ] = vec[VZ];
375	mV[VW] = vec[VW];
376	return (*this);
377}
378
379inline const LLColor4&	LLColor4::setAlpha(F32 a)
380{
381	mV[VW] = a;
382	return (*this);
383}
384
385// LLColor4 Magnitude and Normalization Functions
386
387inline F32		LLColor4::length(void) const
388{
389	return (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
390}
391
392inline F32		LLColor4::lengthSquared(void) const
393{
394	return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
395}
396
397inline F32		LLColor4::normalize(void)
398{
399	F32 mag = (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
400	F32 oomag;
401
402	if (mag)
403	{
404		oomag = 1.f/mag;
405		mV[VX] *= oomag;
406		mV[VY] *= oomag;
407		mV[VZ] *= oomag;
408	}
409	return (mag);
410}
411
412// deprecated
413inline F32		LLColor4::magVec(void) const
414{
415	return (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
416}
417
418// deprecated
419inline F32		LLColor4::magVecSquared(void) const
420{
421	return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
422}
423
424// deprecated
425inline F32		LLColor4::normVec(void)
426{
427	F32 mag = (F32) sqrt(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
428	F32 oomag;
429
430	if (mag)
431	{
432		oomag = 1.f/mag;
433		mV[VX] *= oomag;
434		mV[VY] *= oomag;
435		mV[VZ] *= oomag;
436	}
437	return (mag);
438}
439
440// LLColor4 Operators
441
442
443inline LLColor4 operator+(const LLColor4 &a, const LLColor4 &b)
444{
445	return LLColor4(
446		a.mV[VX] + b.mV[VX],
447		a.mV[VY] + b.mV[VY],
448		a.mV[VZ] + b.mV[VZ],
449		a.mV[VW] + b.mV[VW]);
450}
451
452inline LLColor4 operator-(const LLColor4 &a, const LLColor4 &b)
453{
454	return LLColor4(
455		a.mV[VX] - b.mV[VX],
456		a.mV[VY] - b.mV[VY],
457		a.mV[VZ] - b.mV[VZ],
458		a.mV[VW] - b.mV[VW]);
459}
460
461inline LLColor4  operator*(const LLColor4 &a, const LLColor4 &b)
462{
463	return LLColor4(
464		a.mV[VX] * b.mV[VX],
465		a.mV[VY] * b.mV[VY],
466		a.mV[VZ] * b.mV[VZ],
467		a.mV[VW] * b.mV[VW]);
468}
469
470inline LLColor4 operator*(const LLColor4 &a, F32 k)
471{	
472	// only affects rgb (not a!)
473	return LLColor4(
474		a.mV[VX] * k,
475		a.mV[VY] * k,
476		a.mV[VZ] * k,
477		a.mV[VW]);
478}
479
480inline LLColor4 operator*(F32 k, const LLColor4 &a)
481{
482	// only affects rgb (not a!)
483	return LLColor4(
484		a.mV[VX] * k,
485		a.mV[VY] * k,
486		a.mV[VZ] * k,
487		a.mV[VW]);
488}
489
490inline LLColor4 operator%(F32 k, const LLColor4 &a)
491{
492	// only affects alpha (not rgb!)
493	return LLColor4(
494		a.mV[VX],
495		a.mV[VY],
496		a.mV[VZ],
497		a.mV[VW] * k);
498}
499
500inline LLColor4 operator%(const LLColor4 &a, F32 k)
501{
502	// only affects alpha (not rgb!)
503	return LLColor4(
504		a.mV[VX],
505		a.mV[VY],
506		a.mV[VZ],
507		a.mV[VW] * k);
508}
509
510inline bool operator==(const LLColor4 &a, const LLColor4 &b)
511{
512	return (  (a.mV[VX] == b.mV[VX])
513			&&(a.mV[VY] == b.mV[VY])
514			&&(a.mV[VZ] == b.mV[VZ])
515			&&(a.mV[VW] == b.mV[VW]));
516}
517
518inline bool operator!=(const LLColor4 &a, const LLColor4 &b)
519{
520	return (  (a.mV[VX] != b.mV[VX])
521			||(a.mV[VY] != b.mV[VY])
522			||(a.mV[VZ] != b.mV[VZ])
523			||(a.mV[VW] != b.mV[VW]));
524}
525
526inline const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b)
527{
528	a.mV[VX] += b.mV[VX];
529	a.mV[VY] += b.mV[VY];
530	a.mV[VZ] += b.mV[VZ];
531	a.mV[VW] += b.mV[VW];
532	return a;
533}
534
535inline const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b)
536{
537	a.mV[VX] -= b.mV[VX];
538	a.mV[VY] -= b.mV[VY];
539	a.mV[VZ] -= b.mV[VZ];
540	a.mV[VW] -= b.mV[VW];
541	return a;
542}
543
544inline const LLColor4& operator*=(LLColor4 &a, F32 k)
545{
546	// only affects rgb (not a!)
547	a.mV[VX] *= k;
548	a.mV[VY] *= k;
549	a.mV[VZ] *= k;
550	return a;
551}
552
553inline const LLColor4& operator *=(LLColor4 &a, const LLColor4 &b)
554{
555	a.mV[VX] *= b.mV[VX];
556	a.mV[VY] *= b.mV[VY];
557	a.mV[VZ] *= b.mV[VZ];
558//	a.mV[VW] *= b.mV[VW];
559	return a;
560}
561
562inline const LLColor4& operator%=(LLColor4 &a, F32 k)
563{
564	// only affects alpha (not rgb!)
565	a.mV[VW] *= k;
566	return a;
567}
568
569
570// Non-member functions
571
572inline F32		distVec(const LLColor4 &a, const LLColor4 &b)
573{
574	LLColor4 vec = a - b;
575	return (vec.length());
576}
577
578inline F32		distVec_squared(const LLColor4 &a, const LLColor4 &b)
579{
580	LLColor4 vec = a - b;
581	return (vec.lengthSquared());
582}
583
584inline LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u)
585{
586	return LLColor4(
587		a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
588		a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
589		a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u,
590		a.mV[VW] + (b.mV[VW] - a.mV[VW]) * u);
591}
592
593inline bool LLColor4::operator<(const LLColor4& rhs) const
594{
595	if (mV[0] != rhs.mV[0])
596	{
597		return mV[0] < rhs.mV[0];
598	}
599	if (mV[1] != rhs.mV[1])
600	{
601		return mV[1] < rhs.mV[1];
602	}
603	if (mV[2] != rhs.mV[2])
604	{
605		return mV[2] < rhs.mV[2];
606	}
607
608	return mV[3] < rhs.mV[3];
609}
610
611void LLColor4::clamp()
612{
613	// Clamp the color...
614	if (mV[0] < 0.f)
615	{
616		mV[0] = 0.f;
617	}
618	else if (mV[0] > 1.f)
619	{
620		mV[0] = 1.f;
621	}
622	if (mV[1] < 0.f)
623	{
624		mV[1] = 0.f;
625	}
626	else if (mV[1] > 1.f)
627	{
628		mV[1] = 1.f;
629	}
630	if (mV[2] < 0.f)
631	{
632		mV[2] = 0.f;
633	}
634	else if (mV[2] > 1.f)
635	{
636		mV[2] = 1.f;
637	}
638	if (mV[3] < 0.f)
639	{
640		mV[3] = 0.f;
641	}
642	else if (mV[3] > 1.f)
643	{
644		mV[3] = 1.f;
645	}
646}
647
648#endif
649