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/v3color.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/v3color.h')
-rw-r--r-- | linden/indra/llmath/v3color.h | 381 |
1 files changed, 381 insertions, 0 deletions
diff --git a/linden/indra/llmath/v3color.h b/linden/indra/llmath/v3color.h new file mode 100644 index 0000000..606a810 --- /dev/null +++ b/linden/indra/llmath/v3color.h | |||
@@ -0,0 +1,381 @@ | |||
1 | /** | ||
2 | * @file v3color.h | ||
3 | * @brief LLColor3 class header file. | ||
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_V3COLOR_H | ||
29 | #define LL_V3COLOR_H | ||
30 | |||
31 | class LLColor4; | ||
32 | |||
33 | #include "llerror.h" | ||
34 | #include "llmath.h" | ||
35 | #include "llsd.h" | ||
36 | |||
37 | // LLColor3 = |r g b| | ||
38 | |||
39 | static const U32 LENGTHOFCOLOR3 = 3; | ||
40 | |||
41 | class LLColor3 | ||
42 | { | ||
43 | public: | ||
44 | F32 mV[LENGTHOFCOLOR3]; | ||
45 | |||
46 | static LLColor3 white; | ||
47 | static LLColor3 black; | ||
48 | static LLColor3 grey; | ||
49 | |||
50 | public: | ||
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(char *color_string); // html format color ie "#FFDDEE" | ||
55 | explicit LLColor3(const LLColor4& color4); // "explicit" to avoid automatic conversion | ||
56 | LLColor3(const LLSD& sd); | ||
57 | |||
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 | return ret; | ||
66 | } | ||
67 | |||
68 | void setValue(const LLSD& sd) | ||
69 | { | ||
70 | mV[0] = (F32) sd[0].asReal();; | ||
71 | mV[1] = (F32) sd[1].asReal();; | ||
72 | mV[2] = (F32) sd[2].asReal();; | ||
73 | } | ||
74 | |||
75 | const LLColor3& setToBlack(); // Clears LLColor3 to (0, 0, 0) | ||
76 | const LLColor3& setToWhite(); // Zero LLColor3 to (0, 0, 0) | ||
77 | const LLColor3& setVec(F32 x, F32 y, F32 z); // Sets LLColor3 to (x, y, z) | ||
78 | const LLColor3& setVec(const LLColor3 &vec); // Sets LLColor3 to vec | ||
79 | const LLColor3& setVec(const F32 *vec); // Sets LLColor3 to vec | ||
80 | |||
81 | F32 magVec() const; // Returns magnitude of LLColor3 | ||
82 | F32 magVecSquared() const; // Returns magnitude squared of LLColor3 | ||
83 | F32 normVec(); // Normalizes and returns the magnitude of LLColor3 | ||
84 | |||
85 | const LLColor3& operator=(const LLColor4 &a); | ||
86 | |||
87 | friend std::ostream& operator<<(std::ostream& s, const LLColor3 &a); // Print a | ||
88 | friend LLColor3 operator+(const LLColor3 &a, const LLColor3 &b); // Return vector a + b | ||
89 | friend LLColor3 operator-(const LLColor3 &a, const LLColor3 &b); // Return vector a minus b | ||
90 | |||
91 | friend const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b); // Return vector a + b | ||
92 | friend const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b); // Return vector a minus b | ||
93 | friend const LLColor3& operator*=(LLColor3 &a, const LLColor3 &b); | ||
94 | |||
95 | friend LLColor3 operator*(const LLColor3 &a, const LLColor3 &b); // Return a dot b | ||
96 | friend LLColor3 operator*(const LLColor3 &a, F32 k); // Return a times scaler k | ||
97 | friend LLColor3 operator*(F32 k, const LLColor3 &a); // Return a times scaler k | ||
98 | |||
99 | friend bool operator==(const LLColor3 &a, const LLColor3 &b); // Return a == b | ||
100 | friend bool operator!=(const LLColor3 &a, const LLColor3 &b); // Return a != b | ||
101 | |||
102 | friend const LLColor3& operator*=(LLColor3 &a, F32 k); // Return a times scaler k | ||
103 | |||
104 | friend LLColor3 operator-(const LLColor3 &a); // Return vector 1-rgb (inverse) | ||
105 | |||
106 | inline void clamp(); | ||
107 | inline void exp(); // Do an exponential on the color | ||
108 | }; | ||
109 | |||
110 | LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u); | ||
111 | |||
112 | |||
113 | void LLColor3::clamp() | ||
114 | { | ||
115 | // Clamp the color... | ||
116 | if (mV[0] < 0.f) | ||
117 | { | ||
118 | mV[0] = 0.f; | ||
119 | } | ||
120 | else if (mV[0] > 1.f) | ||
121 | { | ||
122 | mV[0] = 1.f; | ||
123 | } | ||
124 | if (mV[1] < 0.f) | ||
125 | { | ||
126 | mV[1] = 0.f; | ||
127 | } | ||
128 | else if (mV[1] > 1.f) | ||
129 | { | ||
130 | mV[1] = 1.f; | ||
131 | } | ||
132 | if (mV[2] < 0.f) | ||
133 | { | ||
134 | mV[2] = 0.f; | ||
135 | } | ||
136 | else if (mV[2] > 1.f) | ||
137 | { | ||
138 | mV[2] = 1.f; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | // Non-member functions | ||
143 | F32 distVec(const LLColor3 &a, const LLColor3 &b); // Returns distance between a and b | ||
144 | F32 distVec_squared(const LLColor3 &a, const LLColor3 &b);// Returns distance sqaured between a and b | ||
145 | |||
146 | inline LLColor3::LLColor3(void) | ||
147 | { | ||
148 | mV[0] = 0.f; | ||
149 | mV[1] = 0.f; | ||
150 | mV[2] = 0.f; | ||
151 | } | ||
152 | |||
153 | inline LLColor3::LLColor3(F32 r, F32 g, F32 b) | ||
154 | { | ||
155 | mV[VX] = r; | ||
156 | mV[VY] = g; | ||
157 | mV[VZ] = b; | ||
158 | } | ||
159 | |||
160 | inline LLColor3::LLColor3(const F32 *vec) | ||
161 | { | ||
162 | mV[VX] = vec[VX]; | ||
163 | mV[VY] = vec[VY]; | ||
164 | mV[VZ] = vec[VZ]; | ||
165 | } | ||
166 | |||
167 | inline LLColor3::LLColor3(char* color_string) // takes a string of format "RRGGBB" where RR is hex 00..FF | ||
168 | { | ||
169 | if (strlen(color_string) < 6) | ||
170 | { | ||
171 | mV[0] = 0.f; | ||
172 | mV[1] = 0.f; | ||
173 | mV[2] = 0.f; | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | static char tempstr[7]; | ||
178 | strncpy(tempstr,color_string,6); | ||
179 | tempstr[6] = '\0'; | ||
180 | mV[VZ] = (F32)strtol(&tempstr[4],NULL,16)/255.f; | ||
181 | tempstr[4] = '\0'; | ||
182 | mV[VY] = (F32)strtol(&tempstr[2],NULL,16)/255.f; | ||
183 | tempstr[2] = '\0'; | ||
184 | mV[VX] = (F32)strtol(&tempstr[0],NULL,16)/255.f; | ||
185 | } | ||
186 | |||
187 | inline const LLColor3& LLColor3::setToBlack(void) | ||
188 | { | ||
189 | mV[0] = 0.f; | ||
190 | mV[1] = 0.f; | ||
191 | mV[2] = 0.f; | ||
192 | return (*this); | ||
193 | } | ||
194 | |||
195 | inline const LLColor3& LLColor3::setToWhite(void) | ||
196 | { | ||
197 | mV[0] = 1.f; | ||
198 | mV[1] = 1.f; | ||
199 | mV[2] = 1.f; | ||
200 | return (*this); | ||
201 | } | ||
202 | |||
203 | inline const LLColor3& LLColor3::setVec(F32 r, F32 g, F32 b) | ||
204 | { | ||
205 | mV[0] = r; | ||
206 | mV[1] = g; | ||
207 | mV[2] = b; | ||
208 | return (*this); | ||
209 | } | ||
210 | |||
211 | inline const LLColor3& LLColor3::setVec(const LLColor3 &vec) | ||
212 | { | ||
213 | mV[0] = vec.mV[0]; | ||
214 | mV[1] = vec.mV[1]; | ||
215 | mV[2] = vec.mV[2]; | ||
216 | return (*this); | ||
217 | } | ||
218 | |||
219 | inline const LLColor3& LLColor3::setVec(const F32 *vec) | ||
220 | { | ||
221 | mV[0] = vec[0]; | ||
222 | mV[1] = vec[1]; | ||
223 | mV[2] = vec[2]; | ||
224 | return (*this); | ||
225 | } | ||
226 | |||
227 | inline F32 LLColor3::magVec(void) const | ||
228 | { | ||
229 | return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]); | ||
230 | } | ||
231 | |||
232 | inline F32 LLColor3::magVecSquared(void) const | ||
233 | { | ||
234 | return mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]; | ||
235 | } | ||
236 | |||
237 | inline F32 LLColor3::normVec(void) | ||
238 | { | ||
239 | F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]); | ||
240 | F32 oomag; | ||
241 | |||
242 | if (mag) | ||
243 | { | ||
244 | oomag = 1.f/mag; | ||
245 | mV[0] *= oomag; | ||
246 | mV[1] *= oomag; | ||
247 | mV[2] *= oomag; | ||
248 | } | ||
249 | return (mag); | ||
250 | } | ||
251 | |||
252 | inline void LLColor3::exp() | ||
253 | { | ||
254 | #if 0 | ||
255 | mV[0] = ::exp(mV[0]); | ||
256 | mV[1] = ::exp(mV[1]); | ||
257 | mV[2] = ::exp(mV[2]); | ||
258 | #else | ||
259 | mV[0] = (F32)LL_FAST_EXP(mV[0]); | ||
260 | mV[1] = (F32)LL_FAST_EXP(mV[1]); | ||
261 | mV[2] = (F32)LL_FAST_EXP(mV[2]); | ||
262 | #endif | ||
263 | } | ||
264 | |||
265 | |||
266 | inline LLColor3 operator+(const LLColor3 &a, const LLColor3 &b) | ||
267 | { | ||
268 | return LLColor3( | ||
269 | a.mV[0] + b.mV[0], | ||
270 | a.mV[1] + b.mV[1], | ||
271 | a.mV[2] + b.mV[2]); | ||
272 | } | ||
273 | |||
274 | inline LLColor3 operator-(const LLColor3 &a, const LLColor3 &b) | ||
275 | { | ||
276 | return LLColor3( | ||
277 | a.mV[0] - b.mV[0], | ||
278 | a.mV[1] - b.mV[1], | ||
279 | a.mV[2] - b.mV[2]); | ||
280 | } | ||
281 | |||
282 | inline LLColor3 operator*(const LLColor3 &a, const LLColor3 &b) | ||
283 | { | ||
284 | return LLColor3( | ||
285 | a.mV[0] * b.mV[0], | ||
286 | a.mV[1] * b.mV[1], | ||
287 | a.mV[2] * b.mV[2]); | ||
288 | } | ||
289 | |||
290 | inline LLColor3 operator*(const LLColor3 &a, F32 k) | ||
291 | { | ||
292 | return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k ); | ||
293 | } | ||
294 | |||
295 | inline LLColor3 operator*(F32 k, const LLColor3 &a) | ||
296 | { | ||
297 | return LLColor3( a.mV[0] * k, a.mV[1] * k, a.mV[2] * k ); | ||
298 | } | ||
299 | |||
300 | inline bool operator==(const LLColor3 &a, const LLColor3 &b) | ||
301 | { | ||
302 | return ( (a.mV[0] == b.mV[0]) | ||
303 | &&(a.mV[1] == b.mV[1]) | ||
304 | &&(a.mV[2] == b.mV[2])); | ||
305 | } | ||
306 | |||
307 | inline bool operator!=(const LLColor3 &a, const LLColor3 &b) | ||
308 | { | ||
309 | return ( (a.mV[0] != b.mV[0]) | ||
310 | ||(a.mV[1] != b.mV[1]) | ||
311 | ||(a.mV[2] != b.mV[2])); | ||
312 | } | ||
313 | |||
314 | inline const LLColor3 &operator*=(LLColor3 &a, const LLColor3 &b) | ||
315 | { | ||
316 | a.mV[0] *= b.mV[0]; | ||
317 | a.mV[1] *= b.mV[1]; | ||
318 | a.mV[2] *= b.mV[2]; | ||
319 | return a; | ||
320 | } | ||
321 | |||
322 | inline const LLColor3& operator+=(LLColor3 &a, const LLColor3 &b) | ||
323 | { | ||
324 | a.mV[0] += b.mV[0]; | ||
325 | a.mV[1] += b.mV[1]; | ||
326 | a.mV[2] += b.mV[2]; | ||
327 | return a; | ||
328 | } | ||
329 | |||
330 | inline const LLColor3& operator-=(LLColor3 &a, const LLColor3 &b) | ||
331 | { | ||
332 | a.mV[0] -= b.mV[0]; | ||
333 | a.mV[1] -= b.mV[1]; | ||
334 | a.mV[2] -= b.mV[2]; | ||
335 | return a; | ||
336 | } | ||
337 | |||
338 | inline const LLColor3& operator*=(LLColor3 &a, F32 k) | ||
339 | { | ||
340 | a.mV[0] *= k; | ||
341 | a.mV[1] *= k; | ||
342 | a.mV[2] *= k; | ||
343 | return a; | ||
344 | } | ||
345 | |||
346 | inline LLColor3 operator-(const LLColor3 &a) | ||
347 | { | ||
348 | return LLColor3( | ||
349 | 1.f - a.mV[0], | ||
350 | 1.f - a.mV[1], | ||
351 | 1.f - a.mV[2] ); | ||
352 | } | ||
353 | |||
354 | // Non-member functions | ||
355 | |||
356 | inline F32 distVec(const LLColor3 &a, const LLColor3 &b) | ||
357 | { | ||
358 | F32 x = a.mV[0] - b.mV[0]; | ||
359 | F32 y = a.mV[1] - b.mV[1]; | ||
360 | F32 z = a.mV[2] - b.mV[2]; | ||
361 | return fsqrtf( x*x + y*y + z*z ); | ||
362 | } | ||
363 | |||
364 | inline F32 distVec_squared(const LLColor3 &a, const LLColor3 &b) | ||
365 | { | ||
366 | F32 x = a.mV[0] - b.mV[0]; | ||
367 | F32 y = a.mV[1] - b.mV[1]; | ||
368 | F32 z = a.mV[2] - b.mV[2]; | ||
369 | return x*x + y*y + z*z; | ||
370 | } | ||
371 | |||
372 | inline LLColor3 lerp(const LLColor3 &a, const LLColor3 &b, F32 u) | ||
373 | { | ||
374 | return LLColor3( | ||
375 | a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u, | ||
376 | a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u, | ||
377 | a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u); | ||
378 | } | ||
379 | |||
380 | |||
381 | #endif | ||