PageRenderTime 160ms CodeModel.GetById 94ms app.highlight 61ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v4coloru.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 570 lines | 397 code | 80 blank | 93 comment | 28 complexity | a363eb378f5e041e4dad2e8d4b3a6cae MD5 | raw file
  1/** 
  2 * @file v4coloru.h
  3 * @brief The LLColor4U class.
  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_V4COLORU_H
 28#define LL_V4COLORU_H
 29
 30#include "llerror.h"
 31//#include "vmath.h"
 32#include "llmath.h"
 33//#include "v4color.h"
 34
 35#include "v3color.h"
 36#include "v4color.h"
 37
 38//class LLColor3U;
 39class LLColor4;
 40
 41//  LLColor4U = | red green blue alpha |
 42
 43static const U32 LENGTHOFCOLOR4U = 4;
 44
 45
 46class LLColor4U
 47{
 48public:
 49
 50	union
 51	{
 52		U8         mV[LENGTHOFCOLOR4U];
 53		U32        mAll;
 54		LLColor4*  mSources;
 55		LLColor4U* mSourcesU;
 56	};
 57
 58
 59	LLColor4U();						// Initializes LLColor4U to (0, 0, 0, 1)
 60	LLColor4U(U8 r, U8 g, U8 b);		// Initializes LLColor4U to (r, g, b, 1)
 61	LLColor4U(U8 r, U8 g, U8 b, U8 a);		// Initializes LLColor4U to (r. g, b, a)
 62	LLColor4U(const U8 *vec);			// Initializes LLColor4U to (vec[0]. vec[1], vec[2], 1)
 63	explicit LLColor4U(const LLSD& sd)
 64	{
 65		setValue(sd);
 66	}
 67
 68	void setValue(const LLSD& sd)
 69	{
 70		mV[0] = sd[0].asInteger();
 71		mV[1] = sd[1].asInteger();
 72		mV[2] = sd[2].asInteger();
 73		mV[3] = sd[3].asInteger();
 74	}
 75
 76	LLSD getValue() const
 77	{
 78		LLSD ret;
 79		ret[0] = mV[0];
 80		ret[1] = mV[1];
 81		ret[2] = mV[2];
 82		ret[3] = mV[3];
 83		return ret;
 84	}
 85
 86	const LLColor4U&	setToBlack();						// zero LLColor4U to (0, 0, 0, 1)
 87	const LLColor4U&	setToWhite();						// zero LLColor4U to (0, 0, 0, 1)
 88
 89	const LLColor4U&	set(U8 r, U8 g, U8 b, U8 a);// Sets LLColor4U to (r, g, b, a)
 90	const LLColor4U&	set(U8 r, U8 g, U8 b);		// Sets LLColor4U to (r, g, b) (no change in a)
 91	const LLColor4U&	set(const LLColor4U &vec);	// Sets LLColor4U to vec
 92	const LLColor4U&	set(const U8 *vec);			// Sets LLColor4U to vec
 93
 94	const LLColor4U&	setVec(U8 r, U8 g, U8 b, U8 a);	// deprecated -- use set()
 95	const LLColor4U&	setVec(U8 r, U8 g, U8 b);		// deprecated -- use set()
 96	const LLColor4U&	setVec(const LLColor4U &vec);	// deprecated -- use set()
 97	const LLColor4U&	setVec(const U8 *vec);			// deprecated -- use set()
 98
 99	const LLColor4U&    setAlpha(U8 a);
100
101	F32			magVec() const;				// deprecated -- use length()
102	F32			magVecSquared() const;		// deprecated -- use lengthSquared()
103
104	F32			length() const;				// Returns magnitude squared of LLColor4U
105	F32			lengthSquared() const;		// Returns magnitude squared of LLColor4U
106
107	friend std::ostream&	 operator<<(std::ostream& s, const LLColor4U &a);		// Print a
108	friend LLColor4U operator+(const LLColor4U &a, const LLColor4U &b);	// Return vector a + b
109	friend LLColor4U operator-(const LLColor4U &a, const LLColor4U &b);	// Return vector a minus b
110	friend LLColor4U operator*(const LLColor4U &a, const LLColor4U &b);	// Return a * b
111	friend bool operator==(const LLColor4U &a, const LLColor4U &b);		// Return a == b
112	friend bool operator!=(const LLColor4U &a, const LLColor4U &b);		// Return a != b
113
114	friend const LLColor4U& operator+=(LLColor4U &a, const LLColor4U &b);	// Return vector a + b
115	friend const LLColor4U& operator-=(LLColor4U &a, const LLColor4U &b);	// Return vector a minus b
116	friend const LLColor4U& operator*=(LLColor4U &a, U8 k);				// Return rgb times scaler k (no alpha change)
117	friend const LLColor4U& operator%=(LLColor4U &a, U8 k);				// Return alpha times scaler k (no rgb change)
118
119	LLColor4U addClampMax(const LLColor4U &color);						// Add and clamp the max
120
121	LLColor4U multAll(const F32 k);										// Multiply ALL channels by scalar k
122	const LLColor4U& combine();
123
124	inline void setVecScaleClamp(const LLColor3 &color);
125	inline void setVecScaleClamp(const LLColor4 &color);
126
127	static BOOL parseColor4U(const std::string& buf, LLColor4U* value);
128
129	// conversion
130	operator const LLColor4() const
131	{
132		return LLColor4(*this);
133	}
134
135	static LLColor4U white;
136	static LLColor4U black;
137	static LLColor4U red;
138	static LLColor4U green;
139	static LLColor4U blue;
140};
141
142
143// Non-member functions 
144F32		distVec(const LLColor4U &a, const LLColor4U &b);			// Returns distance between a and b
145F32		distVec_squared(const LLColor4U &a, const LLColor4U &b);	// Returns distance squared between a and b
146
147
148inline LLColor4U::LLColor4U()
149{
150	mV[VX] = 0;
151	mV[VY] = 0;
152	mV[VZ] = 0;
153	mV[VW] = 255;
154}
155
156inline LLColor4U::LLColor4U(U8 r, U8 g, U8 b)
157{
158	mV[VX] = r;
159	mV[VY] = g;
160	mV[VZ] = b;
161	mV[VW] = 255;
162}
163
164inline LLColor4U::LLColor4U(U8 r, U8 g, U8 b, U8 a)
165{
166	mV[VX] = r;
167	mV[VY] = g;
168	mV[VZ] = b;
169	mV[VW] = a;
170}
171
172inline LLColor4U::LLColor4U(const U8 *vec)
173{
174	mV[VX] = vec[VX];
175	mV[VY] = vec[VY];
176	mV[VZ] = vec[VZ];
177	mV[VW] = vec[VW];
178}
179
180/*
181inline LLColor4U::operator LLColor4()
182{
183	return(LLColor4((F32)mV[VRED]/255.f,(F32)mV[VGREEN]/255.f,(F32)mV[VBLUE]/255.f,(F32)mV[VALPHA]/255.f));
184}
185*/
186
187inline const LLColor4U&	LLColor4U::setToBlack(void)
188{
189	mV[VX] = 0;
190	mV[VY] = 0;
191	mV[VZ] = 0;
192	mV[VW] = 255;
193	return (*this);
194}
195
196inline const LLColor4U&	LLColor4U::setToWhite(void)
197{
198	mV[VX] = 255;
199	mV[VY] = 255;
200	mV[VZ] = 255;
201	mV[VW] = 255;
202	return (*this);
203}
204
205inline const LLColor4U&	LLColor4U::set(const U8 x, const U8 y, const U8 z)
206{
207	mV[VX] = x;
208	mV[VY] = y;
209	mV[VZ] = z;
210
211//  no change to alpha!
212//	mV[VW] = 255;  
213
214	return (*this);
215}
216
217inline const LLColor4U&	LLColor4U::set(const U8 r, const U8 g, const U8 b, U8 a)
218{
219	mV[0] = r;
220	mV[1] = g;
221	mV[2] = b;
222	mV[3] = a;  
223	return (*this);
224}
225
226inline const LLColor4U&	LLColor4U::set(const LLColor4U &vec)
227{
228	mV[VX] = vec.mV[VX];
229	mV[VY] = vec.mV[VY];
230	mV[VZ] = vec.mV[VZ];
231	mV[VW] = vec.mV[VW];
232	return (*this);
233}
234
235inline const LLColor4U&	LLColor4U::set(const U8 *vec)
236{
237	mV[VX] = vec[VX];
238	mV[VY] = vec[VY];
239	mV[VZ] = vec[VZ];
240	mV[VW] = vec[VW];
241	return (*this);
242}
243
244// deprecated
245inline const LLColor4U&	LLColor4U::setVec(const U8 x, const U8 y, const U8 z)
246{
247	mV[VX] = x;
248	mV[VY] = y;
249	mV[VZ] = z;
250
251//  no change to alpha!
252//	mV[VW] = 255;  
253
254	return (*this);
255}
256
257// deprecated
258inline const LLColor4U&	LLColor4U::setVec(const U8 r, const U8 g, const U8 b, U8 a)
259{
260	mV[0] = r;
261	mV[1] = g;
262	mV[2] = b;
263	mV[3] = a;  
264	return (*this);
265}
266
267// deprecated
268inline const LLColor4U&	LLColor4U::setVec(const LLColor4U &vec)
269{
270	mV[VX] = vec.mV[VX];
271	mV[VY] = vec.mV[VY];
272	mV[VZ] = vec.mV[VZ];
273	mV[VW] = vec.mV[VW];
274	return (*this);
275}
276
277// deprecated
278inline const LLColor4U&	LLColor4U::setVec(const U8 *vec)
279{
280	mV[VX] = vec[VX];
281	mV[VY] = vec[VY];
282	mV[VZ] = vec[VZ];
283	mV[VW] = vec[VW];
284	return (*this);
285}
286
287inline const LLColor4U&	LLColor4U::setAlpha(U8 a)
288{
289	mV[VW] = a;
290	return (*this);
291}
292
293// LLColor4U Magnitude and Normalization Functions
294
295inline F32		LLColor4U::length(void) const
296{
297	return (F32) sqrt( ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ] );
298}
299
300inline F32		LLColor4U::lengthSquared(void) const
301{
302	return ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ];
303}
304
305// deprecated
306inline F32		LLColor4U::magVec(void) const
307{
308	return (F32) sqrt( ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ] );
309}
310
311// deprecated
312inline F32		LLColor4U::magVecSquared(void) const
313{
314	return ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ];
315}
316
317inline LLColor4U operator+(const LLColor4U &a, const LLColor4U &b)
318{
319	return LLColor4U(
320		a.mV[VX] + b.mV[VX],
321		a.mV[VY] + b.mV[VY],
322		a.mV[VZ] + b.mV[VZ],
323		a.mV[VW] + b.mV[VW]);
324}
325
326inline LLColor4U operator-(const LLColor4U &a, const LLColor4U &b)
327{
328	return LLColor4U(
329		a.mV[VX] - b.mV[VX],
330		a.mV[VY] - b.mV[VY],
331		a.mV[VZ] - b.mV[VZ],
332		a.mV[VW] - b.mV[VW]);
333}
334
335inline LLColor4U  operator*(const LLColor4U &a, const LLColor4U &b)
336{
337	return LLColor4U(
338		a.mV[VX] * b.mV[VX],
339		a.mV[VY] * b.mV[VY],
340		a.mV[VZ] * b.mV[VZ],
341		a.mV[VW] * b.mV[VW]);
342}
343
344inline LLColor4U LLColor4U::addClampMax(const LLColor4U &color)
345{
346	return LLColor4U(llmin((S32)mV[VX] + color.mV[VX], 255),
347					llmin((S32)mV[VY] + color.mV[VY], 255),
348					llmin((S32)mV[VZ] + color.mV[VZ], 255),
349					llmin((S32)mV[VW] + color.mV[VW], 255));
350}
351
352inline LLColor4U LLColor4U::multAll(const F32 k)
353{
354	// Round to nearest
355	return LLColor4U(
356		(U8)llround(mV[VX] * k),
357		(U8)llround(mV[VY] * k),
358		(U8)llround(mV[VZ] * k),
359		(U8)llround(mV[VW] * k));
360}
361/*
362inline LLColor4U operator*(const LLColor4U &a, U8 k)
363{	
364	// only affects rgb (not a!)
365	return LLColor4U(
366		a.mV[VX] * k,
367		a.mV[VY] * k,
368		a.mV[VZ] * k,
369		a.mV[VW]);
370}
371
372inline LLColor4U operator*(U8 k, const LLColor4U &a)
373{
374	// only affects rgb (not a!)
375	return LLColor4U(
376		a.mV[VX] * k,
377		a.mV[VY] * k,
378		a.mV[VZ] * k,
379		a.mV[VW]);
380}
381
382inline LLColor4U operator%(U8 k, const LLColor4U &a)
383{
384	// only affects alpha (not rgb!)
385	return LLColor4U(
386		a.mV[VX],
387		a.mV[VY],
388		a.mV[VZ],
389		a.mV[VW] * k );
390}
391
392inline LLColor4U operator%(const LLColor4U &a, U8 k)
393{
394	// only affects alpha (not rgb!)
395	return LLColor4U(
396		a.mV[VX],
397		a.mV[VY],
398		a.mV[VZ],
399		a.mV[VW] * k );
400}
401*/
402
403inline bool operator==(const LLColor4U &a, const LLColor4U &b)
404{
405	return (  (a.mV[VX] == b.mV[VX])
406			&&(a.mV[VY] == b.mV[VY])
407			&&(a.mV[VZ] == b.mV[VZ])
408			&&(a.mV[VW] == b.mV[VW]));
409}
410
411inline bool operator!=(const LLColor4U &a, const LLColor4U &b)
412{
413	return (  (a.mV[VX] != b.mV[VX])
414			||(a.mV[VY] != b.mV[VY])
415			||(a.mV[VZ] != b.mV[VZ])
416			||(a.mV[VW] != b.mV[VW]));
417}
418
419inline const LLColor4U& operator+=(LLColor4U &a, const LLColor4U &b)
420{
421	a.mV[VX] += b.mV[VX];
422	a.mV[VY] += b.mV[VY];
423	a.mV[VZ] += b.mV[VZ];
424	a.mV[VW] += b.mV[VW];
425	return a;
426}
427
428inline const LLColor4U& operator-=(LLColor4U &a, const LLColor4U &b)
429{
430	a.mV[VX] -= b.mV[VX];
431	a.mV[VY] -= b.mV[VY];
432	a.mV[VZ] -= b.mV[VZ];
433	a.mV[VW] -= b.mV[VW];
434	return a;
435}
436
437inline const LLColor4U& operator*=(LLColor4U &a, U8 k)
438{
439	// only affects rgb (not a!)
440	a.mV[VX] *= k;
441	a.mV[VY] *= k;
442	a.mV[VZ] *= k;
443	return a;
444}
445
446inline const LLColor4U& operator%=(LLColor4U &a, U8 k)
447{
448	// only affects alpha (not rgb!)
449	a.mV[VW] *= k;
450	return a;
451}
452
453inline F32		distVec(const LLColor4U &a, const LLColor4U &b)
454{
455	LLColor4U vec = a - b;
456	return (vec.length());
457}
458
459inline F32		distVec_squared(const LLColor4U &a, const LLColor4U &b)
460{
461	LLColor4U vec = a - b;
462	return (vec.lengthSquared());
463}
464
465void LLColor4U::setVecScaleClamp(const LLColor4& color)
466{
467	F32 color_scale_factor = 255.f;
468	F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]);
469	if (max_color > 1.f)
470	{
471		color_scale_factor /= max_color;
472	}
473	const S32 MAX_COLOR = 255;
474	S32 r = llround(color.mV[0] * color_scale_factor);
475	if (r > MAX_COLOR)
476	{
477		r = MAX_COLOR;
478	}
479	else if (r < 0)
480	{
481		r = 0;
482	}
483	mV[0] = r;
484
485	S32 g = llround(color.mV[1] * color_scale_factor);
486	if (g > MAX_COLOR)
487	{
488		g = MAX_COLOR;
489	}
490	else if (g < 0)
491	{
492		g = 0;
493	}
494	mV[1] = g;
495
496	S32 b = llround(color.mV[2] * color_scale_factor);
497	if (b > MAX_COLOR)
498	{
499		b = MAX_COLOR;
500	}
501	else if (b < 0)
502	{
503		b = 0;
504	}
505	mV[2] = b;
506
507	// Alpha shouldn't be scaled, just clamped...
508	S32 a = llround(color.mV[3] * MAX_COLOR);
509	if (a > MAX_COLOR)
510	{
511		a = MAX_COLOR;
512	}
513	else if (a < 0)
514	{
515		a = 0;
516	}
517	mV[3] = a;
518}
519
520void LLColor4U::setVecScaleClamp(const LLColor3& color)
521{
522	F32 color_scale_factor = 255.f;
523	F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]);
524	if (max_color > 1.f)
525	{
526		color_scale_factor /= max_color;
527	}
528
529	const S32 MAX_COLOR = 255;
530	S32 r = llround(color.mV[0] * color_scale_factor);
531	if (r > MAX_COLOR)
532	{
533		r = MAX_COLOR;
534	}
535	else
536	if (r < 0)
537	{
538		r = 0;
539	}
540	mV[0] = r;
541
542	S32 g = llround(color.mV[1] * color_scale_factor);
543	if (g > MAX_COLOR)
544	{
545		g = MAX_COLOR;
546	}
547	else
548	if (g < 0)
549	{
550		g = 0;
551	}
552	mV[1] = g;
553
554	S32 b = llround(color.mV[2] * color_scale_factor);
555	if (b > MAX_COLOR)
556	{
557		b = MAX_COLOR;
558	}
559	if (b < 0)
560	{
561		b = 0;
562	}
563	mV[2] = b;
564
565	mV[3] = 255;
566}
567
568
569#endif
570