aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/v4color.h
blob: ab82d22ae376d498e5e0141bdf26fa0270a3d482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/** 
 * @file v4color.h
 * @brief LLColor4 class header file.
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2008, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#ifndef LL_V4COLOR_H
#define LL_V4COLOR_H

#include "llerror.h"
//#include "vmath.h"
#include "llmath.h"
#include "llsd.h"

class LLColor3;
class LLColor4U;

//  LLColor4 = |x y z w|

static const U32 LENGTHOFCOLOR4 = 4;

static const U32 MAX_LENGTH_OF_COLOR_NAME = 15; //Give plenty of room for additional colors...

class LLColor4
{
	public:
		F32 mV[LENGTHOFCOLOR4];
		LLColor4();						// Initializes LLColor4 to (0, 0, 0, 1)
		LLColor4(F32 r, F32 g, F32 b);		// Initializes LLColor4 to (r, g, b, 1)
		LLColor4(F32 r, F32 g, F32 b, F32 a);		// Initializes LLColor4 to (r. g, b, a)
		LLColor4(U32 clr);							// Initializes LLColor4 to (r=clr>>24, etc))
		LLColor4(const F32 *vec);			// Initializes LLColor4 to (vec[0]. vec[1], vec[2], 1)
		LLColor4(const LLColor3 &vec, F32 a = 1.f);	// Initializes LLColor4 to (vec, a)
		LLColor4(const LLSD& sd);
		explicit LLColor4(const LLColor4U& color4u);  // "explicit" to avoid automatic conversion

		LLSD getValue() const
		{
			LLSD ret;
			ret[0] = mV[0];
			ret[1] = mV[1];
			ret[2] = mV[2];
			ret[3] = mV[3];
			return ret;
		}
	
		void setValue(const LLSD& sd)
		{
			mV[0] = (F32) sd[0].asReal();
			mV[1] = (F32) sd[1].asReal();
			mV[2] = (F32) sd[2].asReal();
			mV[3] = (F32) sd[3].asReal();
		}

		void calcHSL(F32* hue, F32* saturation, F32* luminance) const;

		const LLColor4&	setToBlack();						// zero LLColor4 to (0, 0, 0, 1)
		const LLColor4&	setToWhite();						// zero LLColor4 to (0, 0, 0, 1)

		const LLColor4&	setVec(F32 r, F32 g, F32 b, F32 a);	// Sets LLColor4 to (r, g, b, a)
		const LLColor4&	setVec(F32 r, F32 g, F32 b);	// Sets LLColor4 to (r, g, b) (no change in a)
		const LLColor4&	setVec(const LLColor4 &vec);	// Sets LLColor4 to vec
		const LLColor4&	setVec(const LLColor3 &vec);	// Sets LLColor4 to LLColor3 vec (no change in alpha)
		const LLColor4&	setVec(const LLColor3 &vec, F32 a);	// Sets LLColor4 to LLColor3 vec, with alpha specified
		const LLColor4&	setVec(const F32 *vec);			// Sets LLColor4 to vec
		const LLColor4&	setVec(const LLColor4U& color4u); // Sets LLColor4 to color4u, rescaled.


		const LLColor4&    setAlpha(F32 a);

		F32			magVec() const;				// Returns magnitude of LLColor4
		F32			magVecSquared() const;		// Returns magnitude squared of LLColor4
		F32			normVec();					// Normalizes and returns the magnitude of LLColor4
		BOOL		isOpaque() { return mV[VALPHA] == 1.f; }

		F32 operator[](int idx) const { return mV[idx]; }
		F32 &operator[](int idx) { return mV[idx]; }
	
	    const LLColor4& operator=(const LLColor3 &a);	// Assigns vec3 to vec4 and returns vec4
		const LLColor4& operator=(const LLSD& sd);
		
		friend std::ostream&	 operator<<(std::ostream& s, const LLColor4 &a);		// Print a
		friend LLColor4 operator+(const LLColor4 &a, const LLColor4 &b);	// Return vector a + b
		friend LLColor4 operator-(const LLColor4 &a, const LLColor4 &b);	// Return vector a minus b
		friend LLColor4 operator*(const LLColor4 &a, const LLColor4 &b);	// Return a * b
		friend LLColor4 operator*(const LLColor4 &a, F32 k);				// Return rgb times scaler k (no alpha change)
		friend LLColor4 operator*(F32 k, const LLColor4 &a);				// Return rgb times scaler k (no alpha change)
		friend LLColor4 operator%(const LLColor4 &a, F32 k);				// Return alpha times scaler k (no rgb change)
		friend LLColor4 operator%(F32 k, const LLColor4 &a);				// Return alpha times scaler k (no rgb change)
		friend bool operator==(const LLColor4 &a, const LLColor4 &b);		// Return a == b
		friend bool operator!=(const LLColor4 &a, const LLColor4 &b);		// Return a != b
		
		friend bool operator==(const LLColor4 &a, const LLColor3 &b);		// Return a == b
		friend bool operator!=(const LLColor4 &a, const LLColor3 &b);		// Return a != b

		friend const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b);	// Return vector a + b
		friend const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b);	// Return vector a minus b
		friend const LLColor4& operator*=(LLColor4 &a, F32 k);				// Return rgb times scaler k (no alpha change)
		friend const LLColor4& operator%=(LLColor4 &a, F32 k);				// Return alpha times scaler k (no rgb change)

		friend const LLColor4& operator*=(LLColor4 &a, const LLColor4 &b); // Doesn't multiply alpha! (for lighting)

		// conversion
		operator const LLColor4U() const;

		// Basic color values.
		static LLColor4 red;
		static LLColor4 green;
		static LLColor4 blue;
		static LLColor4 black;
		static LLColor4 white;
		static LLColor4 yellow;
		static LLColor4 magenta;
		static LLColor4 cyan;
		static LLColor4 smoke;
		static LLColor4 grey;
		static LLColor4 orange;
		static LLColor4 purple;
		static LLColor4 pink;
		static LLColor4 transparent;

		// Extra color values.
		static LLColor4 grey1;
		static LLColor4 grey2;
		static LLColor4 grey3;
		static LLColor4 grey4;

		static LLColor4 red1;
		static LLColor4 red2;
		static LLColor4 red3;
		static LLColor4 red4;
		static LLColor4 red5;

		static LLColor4 green1;
		static LLColor4 green2;
		static LLColor4 green3;
		static LLColor4 green4;
		static LLColor4 green5;
		static LLColor4 green6;

		static LLColor4 blue1;
		static LLColor4 blue2;
		static LLColor4 blue3;
		static LLColor4 blue4;
		static LLColor4 blue5;
		static LLColor4 blue6;

		static LLColor4 yellow1;
		static LLColor4 yellow2;
		static LLColor4 yellow3;
		static LLColor4 yellow4;
		static LLColor4 yellow5;
		static LLColor4 yellow6;
		static LLColor4 yellow7;
		static LLColor4 yellow8;
		static LLColor4 yellow9;

		static LLColor4 orange1;
		static LLColor4 orange2;
		static LLColor4 orange3;
		static LLColor4 orange4;
		static LLColor4 orange5;
		static LLColor4 orange6;

		static LLColor4 magenta1;
		static LLColor4 magenta2;
		static LLColor4 magenta3;
		static LLColor4 magenta4;

		static LLColor4 purple1;
		static LLColor4 purple2;
		static LLColor4 purple3;
		static LLColor4 purple4;
		static LLColor4 purple5;
		static LLColor4 purple6;

		static LLColor4 pink1;
		static LLColor4 pink2;

		static LLColor4 cyan1;
		static LLColor4 cyan2;
		static LLColor4 cyan3;
		static LLColor4 cyan4;
		static LLColor4 cyan5;
		static LLColor4 cyan6;
	
		static BOOL parseColor(const char* buf, LLColor4* color);
		static BOOL parseColor4(const char* buf, LLColor4* color);

		inline void clamp();
};


// Non-member functions 
F32		distVec(const LLColor4 &a, const LLColor4 &b);			// Returns distance between a and b
F32		distVec_squared(const LLColor4 &a, const LLColor4 &b);	// Returns distance squared between a and b
LLColor3	vec4to3(const LLColor4 &vec);
LLColor4	vec3to4(const LLColor3 &vec);
LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u);

inline LLColor4::LLColor4(void)
{
	mV[VX] = 0.f;
	mV[VY] = 0.f;
	mV[VZ] = 0.f;
	mV[VW] = 1.f;
}

inline LLColor4::LLColor4(const LLSD& sd)
{
	*this = sd;
}

inline LLColor4::LLColor4(F32 r, F32 g, F32 b)
{
	mV[VX] = r;
	mV[VY] = g;
	mV[VZ] = b;
	mV[VW] = 1.f;
}

inline LLColor4::LLColor4(F32 r, F32 g, F32 b, F32 a)
{
	mV[VX] = r;
	mV[VY] = g;
	mV[VZ] = b;
	mV[VW] = a;
}

inline LLColor4::LLColor4(U32 clr)
{
	mV[VX] = (clr&0xff) * (1.0f/255.0f);
	mV[VY] = ((clr>>8)&0xff) * (1.0f/255.0f);
	mV[VZ] = ((clr>>16)&0xff) * (1.0f/255.0f);
	mV[VW] = (clr>>24) * (1.0f/255.0f);
}

inline LLColor4::LLColor4(const F32 *vec)
{
	mV[VX] = vec[VX];
	mV[VY] = vec[VY];
	mV[VZ] = vec[VZ];
	mV[VW] = vec[VW];
}

inline const LLColor4&	LLColor4::setToBlack(void)
{
	mV[VX] = 0.f;
	mV[VY] = 0.f;
	mV[VZ] = 0.f;
	mV[VW] = 1.f;
	return (*this);
}

inline const LLColor4&	LLColor4::setToWhite(void)
{
	mV[VX] = 1.f;
	mV[VY] = 1.f;
	mV[VZ] = 1.f;
	mV[VW] = 1.f;
	return (*this);
}

inline const LLColor4&	LLColor4::setVec(F32 x, F32 y, F32 z)
{
	mV[VX] = x;
	mV[VY] = y;
	mV[VZ] = z;

//  no change to alpha!
//	mV[VW] = 1.f;  

	return (*this);
}

inline const LLColor4&	LLColor4::setVec(F32 x, F32 y, F32 z, F32 a)
{
	mV[VX] = x;
	mV[VY] = y;
	mV[VZ] = z;
	mV[VW] = a;  
	return (*this);
}

inline const LLColor4&	LLColor4::setVec(const LLColor4 &vec)
{
	mV[VX] = vec.mV[VX];
	mV[VY] = vec.mV[VY];
	mV[VZ] = vec.mV[VZ];
	mV[VW] = vec.mV[VW];
	return (*this);
}


inline const LLColor4&	LLColor4::setVec(const F32 *vec)
{
	mV[VX] = vec[VX];
	mV[VY] = vec[VY];
	mV[VZ] = vec[VZ];
	mV[VW] = vec[VW];
	return (*this);
}

inline const LLColor4&	LLColor4::setAlpha(F32 a)
{
	mV[VW] = a;
	return (*this);
}

// LLColor4 Magnitude and Normalization Functions

inline F32		LLColor4::magVec(void) const
{
	return fsqrtf(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
}

inline F32		LLColor4::magVecSquared(void) const
{
	return mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ];
}

inline F32		LLColor4::normVec(void)
{
	F32 mag = fsqrtf(mV[VX]*mV[VX] + mV[VY]*mV[VY] + mV[VZ]*mV[VZ]);
	F32 oomag;

	if (mag)
	{
		oomag = 1.f/mag;
		mV[VX] *= oomag;
		mV[VY] *= oomag;
		mV[VZ] *= oomag;
	}
	return (mag);
}

// LLColor4 Operators


inline LLColor4 operator+(const LLColor4 &a, const LLColor4 &b)
{
	return LLColor4(
		a.mV[VX] + b.mV[VX],
		a.mV[VY] + b.mV[VY],
		a.mV[VZ] + b.mV[VZ],
		a.mV[VW] + b.mV[VW]);
}

inline LLColor4 operator-(const LLColor4 &a, const LLColor4 &b)
{
	return LLColor4(
		a.mV[VX] - b.mV[VX],
		a.mV[VY] - b.mV[VY],
		a.mV[VZ] - b.mV[VZ],
		a.mV[VW] - b.mV[VW]);
}

inline LLColor4  operator*(const LLColor4 &a, const LLColor4 &b)
{
	return LLColor4(
		a.mV[VX] * b.mV[VX],
		a.mV[VY] * b.mV[VY],
		a.mV[VZ] * b.mV[VZ],
		a.mV[VW] * b.mV[VW]);
}

inline LLColor4 operator*(const LLColor4 &a, F32 k)
{	
	// only affects rgb (not a!)
	return LLColor4(
		a.mV[VX] * k,
		a.mV[VY] * k,
		a.mV[VZ] * k,
		a.mV[VW]);
}

inline LLColor4 operator*(F32 k, const LLColor4 &a)
{
	// only affects rgb (not a!)
	return LLColor4(
		a.mV[VX] * k,
		a.mV[VY] * k,
		a.mV[VZ] * k,
		a.mV[VW]);
}

inline LLColor4 operator%(F32 k, const LLColor4 &a)
{
	// only affects alpha (not rgb!)
	return LLColor4(
		a.mV[VX],
		a.mV[VY],
		a.mV[VZ],
		a.mV[VW] * k);
}

inline LLColor4 operator%(const LLColor4 &a, F32 k)
{
	// only affects alpha (not rgb!)
	return LLColor4(
		a.mV[VX],
		a.mV[VY],
		a.mV[VZ],
		a.mV[VW] * k);
}

inline bool operator==(const LLColor4 &a, const LLColor4 &b)
{
	return (  (a.mV[VX] == b.mV[VX])
			&&(a.mV[VY] == b.mV[VY])
			&&(a.mV[VZ] == b.mV[VZ])
			&&(a.mV[VW] == b.mV[VW]));
}

inline bool operator!=(const LLColor4 &a, const LLColor4 &b)
{
	return (  (a.mV[VX] != b.mV[VX])
			||(a.mV[VY] != b.mV[VY])
			||(a.mV[VZ] != b.mV[VZ])
			||(a.mV[VW] != b.mV[VW]));
}

inline const LLColor4& operator+=(LLColor4 &a, const LLColor4 &b)
{
	a.mV[VX] += b.mV[VX];
	a.mV[VY] += b.mV[VY];
	a.mV[VZ] += b.mV[VZ];
	a.mV[VW] += b.mV[VW];
	return a;
}

inline const LLColor4& operator-=(LLColor4 &a, const LLColor4 &b)
{
	a.mV[VX] -= b.mV[VX];
	a.mV[VY] -= b.mV[VY];
	a.mV[VZ] -= b.mV[VZ];
	a.mV[VW] -= b.mV[VW];
	return a;
}

inline const LLColor4& operator*=(LLColor4 &a, F32 k)
{
	// only affects rgb (not a!)
	a.mV[VX] *= k;
	a.mV[VY] *= k;
	a.mV[VZ] *= k;
	return a;
}

inline const LLColor4& operator *=(LLColor4 &a, const LLColor4 &b)
{
	a.mV[VX] *= b.mV[VX];
	a.mV[VY] *= b.mV[VY];
	a.mV[VZ] *= b.mV[VZ];
//	a.mV[VW] *= b.mV[VW];
	return a;
}

inline const LLColor4& operator%=(LLColor4 &a, F32 k)
{
	// only affects alpha (not rgb!)
	a.mV[VW] *= k;
	return a;
}


// Non-member functions

inline F32		distVec(const LLColor4 &a, const LLColor4 &b)
{
	LLColor4 vec = a - b;
	return (vec.magVec());
}

inline F32		distVec_squared(const LLColor4 &a, const LLColor4 &b)
{
	LLColor4 vec = a - b;
	return (vec.magVecSquared());
}

inline LLColor4 lerp(const LLColor4 &a, const LLColor4 &b, F32 u)
{
	return LLColor4(
		a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
		a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
		a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u,
		a.mV[VW] + (b.mV[VW] - a.mV[VW]) * u);
}


void LLColor4::clamp()
{
	// Clamp the color...
	if (mV[0] < 0.f)
	{
		mV[0] = 0.f;
	}
	else if (mV[0] > 1.f)
	{
		mV[0] = 1.f;
	}
	if (mV[1] < 0.f)
	{
		mV[1] = 0.f;
	}
	else if (mV[1] > 1.f)
	{
		mV[1] = 1.f;
	}
	if (mV[2] < 0.f)
	{
		mV[2] = 0.f;
	}
	else if (mV[2] > 1.f)
	{
		mV[2] = 1.f;
	}
	if (mV[3] < 0.f)
	{
		mV[3] = 0.f;
	}
	else if (mV[3] > 1.f)
	{
		mV[3] = 1.f;
	}
}

inline const LLColor4& LLColor4::operator=(const LLSD& sd)
{
	mV[0] = (F32) sd[0].asReal();
	mV[1] = (F32) sd[1].asReal();
	mV[2] = (F32) sd[2].asReal();
	mV[3] = (F32) sd[3].asReal();

	return *this;
}

#endif