diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmath/v4coloru.h | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llmath/v4coloru.h')
-rw-r--r-- | linden/indra/llmath/v4coloru.h | 520 |
1 files changed, 520 insertions, 0 deletions
diff --git a/linden/indra/llmath/v4coloru.h b/linden/indra/llmath/v4coloru.h new file mode 100644 index 0000000..a5124d0 --- /dev/null +++ b/linden/indra/llmath/v4coloru.h | |||
@@ -0,0 +1,520 @@ | |||
1 | /** | ||
2 | * @file v4coloru.h | ||
3 | * @brief The LLColor4U class. | ||
4 | * | ||
5 | * Copyright (c) 2001-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_V4COLORU_H | ||
29 | #define LL_V4COLORU_H | ||
30 | |||
31 | #include "llerror.h" | ||
32 | //#include "vmath.h" | ||
33 | #include "llmath.h" | ||
34 | //#include "v4color.h" | ||
35 | |||
36 | #include "v3color.h" | ||
37 | #include "v4color.h" | ||
38 | |||
39 | //class LLColor3U; | ||
40 | class LLColor4; | ||
41 | |||
42 | // LLColor4U = | red green blue alpha | | ||
43 | |||
44 | static const U32 LENGTHOFCOLOR4U = 4; | ||
45 | |||
46 | |||
47 | class LLColor4U | ||
48 | { | ||
49 | public: | ||
50 | |||
51 | union | ||
52 | { | ||
53 | U8 mV[LENGTHOFCOLOR4U]; | ||
54 | U32 mAll; | ||
55 | LLColor4* mSources; | ||
56 | LLColor4U* mSourcesU; | ||
57 | }; | ||
58 | |||
59 | |||
60 | LLColor4U(); // Initializes LLColor4U to (0, 0, 0, 1) | ||
61 | LLColor4U(U8 r, U8 g, U8 b); // Initializes LLColor4U to (r, g, b, 1) | ||
62 | LLColor4U(U8 r, U8 g, U8 b, U8 a); // Initializes LLColor4U to (r. g, b, a) | ||
63 | LLColor4U(const U8 *vec); // Initializes LLColor4U to (vec[0]. vec[1], vec[2], 1) | ||
64 | LLColor4U(const LLSD& sd) | ||
65 | { | ||
66 | setValue(sd); | ||
67 | } | ||
68 | |||
69 | void setValue(const LLSD& sd) | ||
70 | { | ||
71 | mV[0] = sd[0].asInteger(); | ||
72 | mV[1] = sd[1].asInteger(); | ||
73 | mV[2] = sd[2].asInteger(); | ||
74 | mV[3] = sd[3].asInteger(); | ||
75 | } | ||
76 | |||
77 | const LLColor4U& operator=(const LLSD& sd) | ||
78 | { | ||
79 | setValue(sd); | ||
80 | return *this; | ||
81 | } | ||
82 | |||
83 | LLSD getValue() const | ||
84 | { | ||
85 | LLSD ret; | ||
86 | ret[0] = mV[0]; | ||
87 | ret[1] = mV[1]; | ||
88 | ret[2] = mV[2]; | ||
89 | ret[3] = mV[3]; | ||
90 | return ret; | ||
91 | } | ||
92 | |||
93 | const LLColor4U& setToBlack(); // zero LLColor4U to (0, 0, 0, 1) | ||
94 | const LLColor4U& setToWhite(); // zero LLColor4U to (0, 0, 0, 1) | ||
95 | |||
96 | const LLColor4U& setVec(U8 r, U8 g, U8 b, U8 a); // Sets LLColor4U to (r, g, b, a) | ||
97 | const LLColor4U& setVec(U8 r, U8 g, U8 b); // Sets LLColor4U to (r, g, b) (no change in a) | ||
98 | const LLColor4U& setVec(const LLColor4U &vec); // Sets LLColor4U to vec | ||
99 | const LLColor4U& setVec(const U8 *vec); // Sets LLColor4U to vec | ||
100 | |||
101 | const LLColor4U& setAlpha(U8 a); | ||
102 | |||
103 | F32 magVec() const; // Returns magnitude of LLColor4U | ||
104 | F32 magVecSquared() const; // Returns magnitude squared of LLColor4U | ||
105 | |||
106 | friend std::ostream& operator<<(std::ostream& s, const LLColor4U &a); // Print a | ||
107 | friend LLColor4U operator+(const LLColor4U &a, const LLColor4U &b); // Return vector a + b | ||
108 | friend LLColor4U operator-(const LLColor4U &a, const LLColor4U &b); // Return vector a minus b | ||
109 | friend LLColor4U operator*(const LLColor4U &a, const LLColor4U &b); // Return a * b | ||
110 | friend bool operator==(const LLColor4U &a, const LLColor4U &b); // Return a == b | ||
111 | friend bool operator!=(const LLColor4U &a, const LLColor4U &b); // Return a != b | ||
112 | |||
113 | friend const LLColor4U& operator+=(LLColor4U &a, const LLColor4U &b); // Return vector a + b | ||
114 | friend const LLColor4U& operator-=(LLColor4U &a, const LLColor4U &b); // Return vector a minus b | ||
115 | friend const LLColor4U& operator*=(LLColor4U &a, U8 k); // Return rgb times scaler k (no alpha change) | ||
116 | friend const LLColor4U& operator%=(LLColor4U &a, U8 k); // Return alpha times scaler k (no rgb change) | ||
117 | |||
118 | LLColor4U addClampMax(const LLColor4U &color); // Add and clamp the max | ||
119 | |||
120 | LLColor4U multAll(const F32 k); // Multiply ALL channels by scalar k | ||
121 | const LLColor4U& combine(); | ||
122 | |||
123 | inline void setVecScaleClamp(const LLColor3 &color); | ||
124 | inline void setVecScaleClamp(const LLColor4 &color); | ||
125 | |||
126 | static BOOL parseColor4U(const char* buf, LLColor4U* value); | ||
127 | |||
128 | static LLColor4U white; | ||
129 | static LLColor4U black; | ||
130 | static LLColor4U red; | ||
131 | static LLColor4U green; | ||
132 | static LLColor4U blue; | ||
133 | }; | ||
134 | |||
135 | |||
136 | // Non-member functions | ||
137 | F32 distVec(const LLColor4U &a, const LLColor4U &b); // Returns distance between a and b | ||
138 | F32 distVec_squared(const LLColor4U &a, const LLColor4U &b); // Returns distance squared between a and b | ||
139 | |||
140 | |||
141 | inline LLColor4U::LLColor4U() | ||
142 | { | ||
143 | mV[VX] = 0; | ||
144 | mV[VY] = 0; | ||
145 | mV[VZ] = 0; | ||
146 | mV[VW] = 255; | ||
147 | } | ||
148 | |||
149 | inline LLColor4U::LLColor4U(U8 r, U8 g, U8 b) | ||
150 | { | ||
151 | mV[VX] = r; | ||
152 | mV[VY] = g; | ||
153 | mV[VZ] = b; | ||
154 | mV[VW] = 255; | ||
155 | } | ||
156 | |||
157 | inline LLColor4U::LLColor4U(U8 r, U8 g, U8 b, U8 a) | ||
158 | { | ||
159 | mV[VX] = r; | ||
160 | mV[VY] = g; | ||
161 | mV[VZ] = b; | ||
162 | mV[VW] = a; | ||
163 | } | ||
164 | |||
165 | inline LLColor4U::LLColor4U(const U8 *vec) | ||
166 | { | ||
167 | mV[VX] = vec[VX]; | ||
168 | mV[VY] = vec[VY]; | ||
169 | mV[VZ] = vec[VZ]; | ||
170 | mV[VW] = vec[VW]; | ||
171 | } | ||
172 | |||
173 | /* | ||
174 | inline LLColor4U::operator LLColor4() | ||
175 | { | ||
176 | return(LLColor4((F32)mV[VRED]/255.f,(F32)mV[VGREEN]/255.f,(F32)mV[VBLUE]/255.f,(F32)mV[VALPHA]/255.f)); | ||
177 | } | ||
178 | */ | ||
179 | |||
180 | inline const LLColor4U& LLColor4U::setToBlack(void) | ||
181 | { | ||
182 | mV[VX] = 0; | ||
183 | mV[VY] = 0; | ||
184 | mV[VZ] = 0; | ||
185 | mV[VW] = 255; | ||
186 | return (*this); | ||
187 | } | ||
188 | |||
189 | inline const LLColor4U& LLColor4U::setToWhite(void) | ||
190 | { | ||
191 | mV[VX] = 255; | ||
192 | mV[VY] = 255; | ||
193 | mV[VZ] = 255; | ||
194 | mV[VW] = 255; | ||
195 | return (*this); | ||
196 | } | ||
197 | |||
198 | inline const LLColor4U& LLColor4U::setVec(const U8 x, const U8 y, const U8 z) | ||
199 | { | ||
200 | mV[VX] = x; | ||
201 | mV[VY] = y; | ||
202 | mV[VZ] = z; | ||
203 | |||
204 | // no change to alpha! | ||
205 | // mV[VW] = 255; | ||
206 | |||
207 | return (*this); | ||
208 | } | ||
209 | |||
210 | inline const LLColor4U& LLColor4U::setVec(const U8 r, const U8 g, const U8 b, U8 a) | ||
211 | { | ||
212 | mV[0] = r; | ||
213 | mV[1] = g; | ||
214 | mV[2] = b; | ||
215 | mV[3] = a; | ||
216 | return (*this); | ||
217 | } | ||
218 | |||
219 | inline const LLColor4U& LLColor4U::setVec(const LLColor4U &vec) | ||
220 | { | ||
221 | mV[VX] = vec.mV[VX]; | ||
222 | mV[VY] = vec.mV[VY]; | ||
223 | mV[VZ] = vec.mV[VZ]; | ||
224 | mV[VW] = vec.mV[VW]; | ||
225 | return (*this); | ||
226 | } | ||
227 | |||
228 | /* | ||
229 | inline const LLColor4U& LLColor4U::setVec(const LLColor4 &vec) | ||
230 | { | ||
231 | mV[VX] = (U8) (llmin(1.f, vec.mV[VX]) * 255.f); | ||
232 | mV[VY] = (U8) (llmin(1.f, vec.mV[VY]) * 255.f); | ||
233 | mV[VZ] = (U8) (llmin(1.f, vec.mV[VZ]) * 255.f); | ||
234 | mV[VW] = (U8) (llmin(1.f, vec.mV[VW]) * 255.f); | ||
235 | return (*this); | ||
236 | } | ||
237 | */ | ||
238 | |||
239 | inline const LLColor4U& LLColor4U::setVec(const U8 *vec) | ||
240 | { | ||
241 | mV[VX] = vec[VX]; | ||
242 | mV[VY] = vec[VY]; | ||
243 | mV[VZ] = vec[VZ]; | ||
244 | mV[VW] = vec[VW]; | ||
245 | return (*this); | ||
246 | } | ||
247 | |||
248 | inline const LLColor4U& LLColor4U::setAlpha(U8 a) | ||
249 | { | ||
250 | mV[VW] = a; | ||
251 | return (*this); | ||
252 | } | ||
253 | |||
254 | // LLColor4U Magnitude and Normalization Functions | ||
255 | // bookmark | ||
256 | |||
257 | inline F32 LLColor4U::magVec(void) const | ||
258 | { | ||
259 | return fsqrtf( ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ] ); | ||
260 | } | ||
261 | |||
262 | inline F32 LLColor4U::magVecSquared(void) const | ||
263 | { | ||
264 | return ((F32)mV[VX]) * mV[VX] + ((F32)mV[VY]) * mV[VY] + ((F32)mV[VZ]) * mV[VZ]; | ||
265 | } | ||
266 | |||
267 | inline LLColor4U operator+(const LLColor4U &a, const LLColor4U &b) | ||
268 | { | ||
269 | return LLColor4U( | ||
270 | a.mV[VX] + b.mV[VX], | ||
271 | a.mV[VY] + b.mV[VY], | ||
272 | a.mV[VZ] + b.mV[VZ], | ||
273 | a.mV[VW] + b.mV[VW]); | ||
274 | } | ||
275 | |||
276 | inline LLColor4U operator-(const LLColor4U &a, const LLColor4U &b) | ||
277 | { | ||
278 | return LLColor4U( | ||
279 | a.mV[VX] - b.mV[VX], | ||
280 | a.mV[VY] - b.mV[VY], | ||
281 | a.mV[VZ] - b.mV[VZ], | ||
282 | a.mV[VW] - b.mV[VW]); | ||
283 | } | ||
284 | |||
285 | inline LLColor4U operator*(const LLColor4U &a, const LLColor4U &b) | ||
286 | { | ||
287 | return LLColor4U( | ||
288 | a.mV[VX] * b.mV[VX], | ||
289 | a.mV[VY] * b.mV[VY], | ||
290 | a.mV[VZ] * b.mV[VZ], | ||
291 | a.mV[VW] * b.mV[VW]); | ||
292 | } | ||
293 | |||
294 | inline LLColor4U LLColor4U::addClampMax(const LLColor4U &color) | ||
295 | { | ||
296 | return LLColor4U(llmin((S32)mV[VX] + color.mV[VX], 255), | ||
297 | llmin((S32)mV[VY] + color.mV[VY], 255), | ||
298 | llmin((S32)mV[VZ] + color.mV[VZ], 255), | ||
299 | llmin((S32)mV[VW] + color.mV[VW], 255)); | ||
300 | } | ||
301 | |||
302 | inline LLColor4U LLColor4U::multAll(const F32 k) | ||
303 | { | ||
304 | // Round to nearest | ||
305 | return LLColor4U( | ||
306 | (U8)llround(mV[VX] * k), | ||
307 | (U8)llround(mV[VY] * k), | ||
308 | (U8)llround(mV[VZ] * k), | ||
309 | (U8)llround(mV[VW] * k)); | ||
310 | } | ||
311 | /* | ||
312 | inline LLColor4U operator*(const LLColor4U &a, U8 k) | ||
313 | { | ||
314 | // only affects rgb (not a!) | ||
315 | return LLColor4U( | ||
316 | a.mV[VX] * k, | ||
317 | a.mV[VY] * k, | ||
318 | a.mV[VZ] * k, | ||
319 | a.mV[VW]); | ||
320 | } | ||
321 | |||
322 | inline LLColor4U operator*(U8 k, const LLColor4U &a) | ||
323 | { | ||
324 | // only affects rgb (not a!) | ||
325 | return LLColor4U( | ||
326 | a.mV[VX] * k, | ||
327 | a.mV[VY] * k, | ||
328 | a.mV[VZ] * k, | ||
329 | a.mV[VW]); | ||
330 | } | ||
331 | |||
332 | inline LLColor4U operator%(U8 k, const LLColor4U &a) | ||
333 | { | ||
334 | // only affects alpha (not rgb!) | ||
335 | return LLColor4U( | ||
336 | a.mV[VX], | ||
337 | a.mV[VY], | ||
338 | a.mV[VZ], | ||
339 | a.mV[VW] * k ); | ||
340 | } | ||
341 | |||
342 | inline LLColor4U operator%(const LLColor4U &a, U8 k) | ||
343 | { | ||
344 | // only affects alpha (not rgb!) | ||
345 | return LLColor4U( | ||
346 | a.mV[VX], | ||
347 | a.mV[VY], | ||
348 | a.mV[VZ], | ||
349 | a.mV[VW] * k ); | ||
350 | } | ||
351 | */ | ||
352 | |||
353 | inline bool operator==(const LLColor4U &a, const LLColor4U &b) | ||
354 | { | ||
355 | return ( (a.mV[VX] == b.mV[VX]) | ||
356 | &&(a.mV[VY] == b.mV[VY]) | ||
357 | &&(a.mV[VZ] == b.mV[VZ]) | ||
358 | &&(a.mV[VW] == b.mV[VW])); | ||
359 | } | ||
360 | |||
361 | inline bool operator!=(const LLColor4U &a, const LLColor4U &b) | ||
362 | { | ||
363 | return ( (a.mV[VX] != b.mV[VX]) | ||
364 | ||(a.mV[VY] != b.mV[VY]) | ||
365 | ||(a.mV[VZ] != b.mV[VZ]) | ||
366 | ||(a.mV[VW] != b.mV[VW])); | ||
367 | } | ||
368 | |||
369 | inline const LLColor4U& operator+=(LLColor4U &a, const LLColor4U &b) | ||
370 | { | ||
371 | a.mV[VX] += b.mV[VX]; | ||
372 | a.mV[VY] += b.mV[VY]; | ||
373 | a.mV[VZ] += b.mV[VZ]; | ||
374 | a.mV[VW] += b.mV[VW]; | ||
375 | return a; | ||
376 | } | ||
377 | |||
378 | inline const LLColor4U& operator-=(LLColor4U &a, const LLColor4U &b) | ||
379 | { | ||
380 | a.mV[VX] -= b.mV[VX]; | ||
381 | a.mV[VY] -= b.mV[VY]; | ||
382 | a.mV[VZ] -= b.mV[VZ]; | ||
383 | a.mV[VW] -= b.mV[VW]; | ||
384 | return a; | ||
385 | } | ||
386 | |||
387 | inline const LLColor4U& operator*=(LLColor4U &a, U8 k) | ||
388 | { | ||
389 | // only affects rgb (not a!) | ||
390 | a.mV[VX] *= k; | ||
391 | a.mV[VY] *= k; | ||
392 | a.mV[VZ] *= k; | ||
393 | return a; | ||
394 | } | ||
395 | |||
396 | inline const LLColor4U& operator%=(LLColor4U &a, U8 k) | ||
397 | { | ||
398 | // only affects alpha (not rgb!) | ||
399 | a.mV[VW] *= k; | ||
400 | return a; | ||
401 | } | ||
402 | |||
403 | inline F32 distVec(const LLColor4U &a, const LLColor4U &b) | ||
404 | { | ||
405 | LLColor4U vec = a - b; | ||
406 | return (vec.magVec()); | ||
407 | } | ||
408 | |||
409 | inline F32 distVec_squared(const LLColor4U &a, const LLColor4U &b) | ||
410 | { | ||
411 | LLColor4U vec = a - b; | ||
412 | return (vec.magVecSquared()); | ||
413 | } | ||
414 | |||
415 | void LLColor4U::setVecScaleClamp(const LLColor4& color) | ||
416 | { | ||
417 | F32 color_scale_factor = 255.f; | ||
418 | F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]); | ||
419 | if (max_color > 1.f) | ||
420 | { | ||
421 | color_scale_factor /= max_color; | ||
422 | } | ||
423 | const S32 MAX_COLOR = 255; | ||
424 | S32 r = llround(color.mV[0] * color_scale_factor); | ||
425 | if (r > MAX_COLOR) | ||
426 | { | ||
427 | r = MAX_COLOR; | ||
428 | } | ||
429 | else if (r < 0) | ||
430 | { | ||
431 | r = 0; | ||
432 | } | ||
433 | mV[0] = r; | ||
434 | |||
435 | S32 g = llround(color.mV[1] * color_scale_factor); | ||
436 | if (g > MAX_COLOR) | ||
437 | { | ||
438 | g = MAX_COLOR; | ||
439 | } | ||
440 | else if (g < 0) | ||
441 | { | ||
442 | g = 0; | ||
443 | } | ||
444 | mV[1] = g; | ||
445 | |||
446 | S32 b = llround(color.mV[2] * color_scale_factor); | ||
447 | if (b > MAX_COLOR) | ||
448 | { | ||
449 | b = MAX_COLOR; | ||
450 | } | ||
451 | else if (b < 0) | ||
452 | { | ||
453 | b = 0; | ||
454 | } | ||
455 | mV[2] = b; | ||
456 | |||
457 | // Alpha shouldn't be scaled, just clamped... | ||
458 | S32 a = llround(color.mV[3] * MAX_COLOR); | ||
459 | if (a > MAX_COLOR) | ||
460 | { | ||
461 | a = MAX_COLOR; | ||
462 | } | ||
463 | else if (a < 0) | ||
464 | { | ||
465 | a = 0; | ||
466 | } | ||
467 | mV[3] = a; | ||
468 | } | ||
469 | |||
470 | void LLColor4U::setVecScaleClamp(const LLColor3& color) | ||
471 | { | ||
472 | F32 color_scale_factor = 255.f; | ||
473 | F32 max_color = llmax(color.mV[0], color.mV[1], color.mV[2]); | ||
474 | if (max_color > 1.f) | ||
475 | { | ||
476 | color_scale_factor /= max_color; | ||
477 | } | ||
478 | |||
479 | const S32 MAX_COLOR = 255; | ||
480 | S32 r = llround(color.mV[0] * color_scale_factor); | ||
481 | if (r > MAX_COLOR) | ||
482 | { | ||
483 | r = MAX_COLOR; | ||
484 | } | ||
485 | else | ||
486 | if (r < 0) | ||
487 | { | ||
488 | r = 0; | ||
489 | } | ||
490 | mV[0] = r; | ||
491 | |||
492 | S32 g = llround(color.mV[1] * color_scale_factor); | ||
493 | if (g > MAX_COLOR) | ||
494 | { | ||
495 | g = MAX_COLOR; | ||
496 | } | ||
497 | else | ||
498 | if (g < 0) | ||
499 | { | ||
500 | g = 0; | ||
501 | } | ||
502 | mV[1] = g; | ||
503 | |||
504 | S32 b = llround(color.mV[2] * color_scale_factor); | ||
505 | if (b > MAX_COLOR) | ||
506 | { | ||
507 | b = MAX_COLOR; | ||
508 | } | ||
509 | if (b < 0) | ||
510 | { | ||
511 | b = 0; | ||
512 | } | ||
513 | mV[2] = b; | ||
514 | |||
515 | mV[3] = 255; | ||
516 | } | ||
517 | |||
518 | |||
519 | #endif | ||
520 | |||