summaryrefslogtreecommitdiff
path: root/indra/llmath/v3color.h
blob: 3763fc67258fbc46130af8b7058cafb367c7c2ba (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
/**
 * @file v3color.h
 * @brief LLColor3 class header file.
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_V3COLOR_H
#define LL_V3COLOR_H

class LLColor4;
class LLVector4;

#include "llerror.h"
#include "llmath.h"
#include "llsd.h"
#include "v3math.h" // needed for linearColor3v implemtation below
#include <string.h>

//  LLColor3 = |r g b|

static constexpr U32 LENGTHOFCOLOR3 = 3;

class LLColor3
{
public:
    F32 mV[LENGTHOFCOLOR3];

    static LLColor3 white;
    static LLColor3 black;
    static LLColor3 grey;

public:
    LLColor3();                                  // Initializes LLColor3 to (0, 0, 0)
    LLColor3(F32 r, F32 g, F32 b);               // Initializes LLColor3 to (r, g, b)
    LLColor3(const F32* vec);                    // Initializes LLColor3 to (vec[0]. vec[1], vec[2])
    LLColor3(const char* color_string);          // html format color ie "#FFDDEE"
    explicit LLColor3(const LLColor4& color4);   // "explicit" to avoid automatic conversion
    explicit LLColor3(const LLVector4& vector4); // "explicit" to avoid automatic conversion
    LLColor3(const LLSD& sd);

    LLSD getValue() const
    {
        LLSD ret;
        ret[0] = mV[VRED];
        ret[1] = mV[VGREEN];
        ret[2] = mV[VBLUE];
        return ret;
    }

    void setValue(const LLSD& sd)
    {
        mV[VRED]   = (F32)sd[0].asReal();
        mV[VGREEN] = (F32)sd[1].asReal();
        mV[VBLUE]  = (F32)sd[2].asReal();
    }

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

    const LLColor3& setToBlack(); // Clears LLColor3 to (0, 0, 0)
    const LLColor3& setToWhite(); // Zero LLColor3 to (0, 0, 0)

    const LLColor3& setVec(F32 x, F32 y, F32 z); // deprecated
    const LLColor3& setVec(const LLColor3& vec); // deprecated
    const LLColor3& setVec(const F32* vec);      // deprecated

    const LLColor3& set(F32 x, F32 y, F32 z); // Sets LLColor3 to (x, y, z)
    const LLColor3& set(const LLColor3& vec); // Sets LLColor3 to vec
    const LLColor3& set(const F32* vec);      // Sets LLColor3 to vec

    // set from a vector of unknown type and size
    // may leave some data unmodified
    template<typename T>
    const LLColor3& set(const std::vector<T>& v);

    // write to a vector of unknown type and size
    // maye leave some data unmodified
    template<typename T>
    void write(std::vector<T>& v) const;

    F32 magVec() const;        // deprecated
    F32 magVecSquared() const; // deprecated
    F32 normVec();             // deprecated

    F32 length() const;        // Returns magnitude of LLColor3
    F32 lengthSquared() const; // Returns magnitude squared of LLColor3
    F32 normalize();           // Normalizes and returns the magnitude of LLColor3

    F32 brightness() const; // Returns brightness of LLColor3

    const LLColor3& operator=(const LLColor4& a);

    LL_FORCE_INLINE LLColor3 divide(const LLColor3& col2) const
    {
        return LLColor3(mV[VRED] / col2.mV[VRED], mV[VGREEN] / col2.mV[VGREEN], mV[VBLUE] / col2.mV[VBLUE]);
    }

    LL_FORCE_INLINE LLColor3 color_norm() const
    {
        F32 l = length();
        return LLColor3(mV[VRED] / l, mV[VGREEN] / l, mV[VBLUE] / l);
    }

    friend std::ostream& operator<<(std::ostream& s, const LLColor3& a);  // Print a
    friend LLColor3      operator+(const LLColor3& a, const LLColor3& b); // Return vector a + b
    friend LLColor3      operator-(const LLColor3& a, const LLColor3& b); // Return vector a minus b

    friend const LLColor3& operator+=(LLColor3& a, const LLColor3& b); // Return vector a + b
    friend const LLColor3& operator-=(LLColor3& a, const LLColor3& b); // Return vector a minus b
    friend const LLColor3& operator*=(LLColor3& a, const LLColor3& b);

    friend LLColor3 operator*(const LLColor3& a, const LLColor3& b); // Return component wise a * b
    friend LLColor3 operator*(const LLColor3& a, F32 k);             // Return a times scaler k
    friend LLColor3 operator*(F32 k, const LLColor3& a);             // Return a times scaler k

    friend bool operator==(const LLColor3& a, const LLColor3& b); // Return a == b
    friend bool operator!=(const LLColor3& a, const LLColor3& b); // Return a != b

    friend const LLColor3& operator*=(LLColor3& a, F32 k); // Return a times scaler k

    friend LLColor3 operator-(const LLColor3& a); // Return vector 1-rgb (inverse)

    inline void clamp();
    inline void exp(); // Do an exponential on the color
};

LLColor3 lerp(const LLColor3& a, const LLColor3& b, F32 u);

void LLColor3::clamp()
{
    // Clamp the color...
    if (mV[VRED] < 0.f)
    {
        mV[VRED] = 0.f;
    }
    else if (mV[VRED] > 1.f)
    {
        mV[VRED] = 1.f;
    }
    if (mV[VGREEN] < 0.f)
    {
        mV[VGREEN] = 0.f;
    }
    else if (mV[VGREEN] > 1.f)
    {
        mV[VGREEN] = 1.f;
    }
    if (mV[VBLUE] < 0.f)
    {
        mV[VBLUE] = 0.f;
    }
    else if (mV[VBLUE] > 1.f)
    {
        mV[VBLUE] = 1.f;
    }
}

// Non-member functions
F32 distVec(const LLColor3& a, const LLColor3& b);         // Returns distance between a and b
F32 distVec_squared(const LLColor3& a, const LLColor3& b); // Returns distance squared between a and b

inline LLColor3::LLColor3()
{
    mV[VRED]   = 0.f;
    mV[VGREEN] = 0.f;
    mV[VBLUE]  = 0.f;
}

inline LLColor3::LLColor3(F32 r, F32 g, F32 b)
{
    mV[VRED]   = r;
    mV[VGREEN] = g;
    mV[VBLUE]  = b;
}

inline LLColor3::LLColor3(const F32* vec)
{
    mV[VRED]   = vec[VRED];
    mV[VGREEN] = vec[VGREEN];
    mV[VBLUE]  = vec[VBLUE];
}

inline LLColor3::LLColor3(const char* color_string) // takes a string of format "RRGGBB" where RR is hex 00..FF
{
    if (strlen(color_string) < 6) /* Flawfinder: ignore */
    {
        mV[VRED]   = 0.f;
        mV[VGREEN] = 0.f;
        mV[VBLUE]  = 0.f;
        return;
    }

    char tempstr[7];
    strncpy(tempstr, color_string, 6); /* Flawfinder: ignore */
    tempstr[6] = '\0';
    mV[VBLUE]  = (F32)strtol(&tempstr[4], nullptr, 16) / 255.f;
    tempstr[4] = '\0';
    mV[VGREEN] = (F32)strtol(&tempstr[2], nullptr, 16) / 255.f;
    tempstr[2] = '\0';
    mV[VRED]   = (F32)strtol(&tempstr[0], nullptr, 16) / 255.f;
}

inline const LLColor3& LLColor3::setToBlack()
{
    mV[VRED]   = 0.f;
    mV[VGREEN] = 0.f;
    mV[VBLUE]  = 0.f;
    return (*this);
}

inline const LLColor3& LLColor3::setToWhite()
{
    mV[VRED]   = 1.f;
    mV[VGREEN] = 1.f;
    mV[VBLUE]  = 1.f;
    return (*this);
}

inline const LLColor3& LLColor3::set(F32 r, F32 g, F32 b)
{
    mV[VRED]   = r;
    mV[VGREEN] = g;
    mV[VBLUE]  = b;
    return (*this);
}

inline const LLColor3& LLColor3::set(const LLColor3& vec)
{
    mV[VRED]   = vec.mV[VRED];
    mV[VGREEN] = vec.mV[VGREEN];
    mV[VBLUE]  = vec.mV[VBLUE];
    return (*this);
}

inline const LLColor3& LLColor3::set(const F32* vec)
{
    mV[VRED]   = vec[0];
    mV[VGREEN] = vec[1];
    mV[VBLUE]  = vec[2];
    return (*this);
}

// deprecated
inline const LLColor3& LLColor3::setVec(F32 r, F32 g, F32 b)
{
    mV[VRED]   = r;
    mV[VGREEN] = g;
    mV[VBLUE]  = b;
    return (*this);
}

// deprecated
inline const LLColor3& LLColor3::setVec(const LLColor3& vec)
{
    mV[VRED]   = vec.mV[VRED];
    mV[VGREEN] = vec.mV[VGREEN];
    mV[VBLUE]  = vec.mV[VBLUE];
    return (*this);
}

// deprecated
inline const LLColor3& LLColor3::setVec(const F32* vec)
{
    mV[VRED]   = vec[0];
    mV[VGREEN] = vec[1];
    mV[VBLUE]  = vec[2];
    return (*this);
}

inline F32 LLColor3::brightness() const
{
    return (mV[VRED] + mV[VGREEN] + mV[VBLUE]) / 3.0f;
}

inline F32 LLColor3::length() const
{
    return sqrt(mV[VRED] * mV[VRED] + mV[VGREEN] * mV[VGREEN] + mV[VBLUE] * mV[VBLUE]);
}

inline F32 LLColor3::lengthSquared() const
{
    return mV[VRED] * mV[VRED] + mV[VGREEN] * mV[VGREEN] + mV[VBLUE] * mV[VBLUE];
}

inline F32 LLColor3::normalize()
{
    F32 mag = sqrt(mV[VRED] * mV[VRED] + mV[VGREEN] * mV[VGREEN] + mV[VBLUE] * mV[VBLUE]);
    F32 oomag;

    if (mag)
    {
        oomag = 1.f / mag;
        mV[VRED] *= oomag;
        mV[VGREEN] *= oomag;
        mV[VBLUE] *= oomag;
    }
    return mag;
}

// deprecated
inline F32 LLColor3::magVec() const
{
    return sqrt(mV[VRED] * mV[VRED] + mV[VGREEN] * mV[VGREEN] + mV[VBLUE] * mV[VBLUE]);
}

// deprecated
inline F32 LLColor3::magVecSquared() const
{
    return mV[VRED] * mV[VRED] + mV[VGREEN] * mV[VGREEN] + mV[VBLUE] * mV[VBLUE];
}

// deprecated
inline F32 LLColor3::normVec()
{
    F32 mag = sqrt(mV[VRED] * mV[VRED] + mV[VGREEN] * mV[VGREEN] + mV[VBLUE] * mV[VBLUE]);
    F32 oomag;

    if (mag)
    {
        oomag = 1.f / mag;
        mV[VRED] *= oomag;
        mV[VGREEN] *= oomag;
        mV[VBLUE] *= oomag;
    }
    return mag;
}

inline void LLColor3::exp()
{
#if 0
    mV[VRED] = ::exp(mV[VRED]);
    mV[VGREEN] = ::exp(mV[VGREEN]);
    mV[VBLUE] = ::exp(mV[VBLUE]);
#else
    mV[VRED]   = (F32)LL_FAST_EXP(mV[VRED]);
    mV[VGREEN] = (F32)LL_FAST_EXP(mV[VGREEN]);
    mV[VBLUE]  = (F32)LL_FAST_EXP(mV[VBLUE]);
#endif
}

inline LLColor3 operator+(const LLColor3& a, const LLColor3& b)
{
    return LLColor3(a.mV[VRED] + b.mV[VRED], a.mV[VGREEN] + b.mV[VGREEN], a.mV[VBLUE] + b.mV[VBLUE]);
}

inline LLColor3 operator-(const LLColor3& a, const LLColor3& b)
{
    return LLColor3(a.mV[VRED] - b.mV[VRED], a.mV[VGREEN] - b.mV[VGREEN], a.mV[VBLUE] - b.mV[VBLUE]);
}

inline LLColor3 operator*(const LLColor3& a, const LLColor3& b)
{
    return LLColor3(a.mV[VRED] * b.mV[VRED], a.mV[VGREEN] * b.mV[VGREEN], a.mV[VBLUE] * b.mV[VBLUE]);
}

inline LLColor3 operator*(const LLColor3& a, F32 k)
{
    return LLColor3(a.mV[VRED] * k, a.mV[VGREEN] * k, a.mV[VBLUE] * k);
}

inline LLColor3 operator*(F32 k, const LLColor3& a)
{
    return LLColor3(a.mV[VRED] * k, a.mV[VGREEN] * k, a.mV[VBLUE] * k);
}

inline bool operator==(const LLColor3& a, const LLColor3& b)
{
    return ((a.mV[VRED] == b.mV[VRED]) && (a.mV[VGREEN] == b.mV[VGREEN]) && (a.mV[VBLUE] == b.mV[VBLUE]));
}

inline bool operator!=(const LLColor3& a, const LLColor3& b)
{
    return ((a.mV[VRED] != b.mV[VRED]) || (a.mV[VGREEN] != b.mV[VGREEN]) || (a.mV[VBLUE] != b.mV[VBLUE]));
}

inline const LLColor3& operator*=(LLColor3& a, const LLColor3& b)
{
    a.mV[VRED] *= b.mV[VRED];
    a.mV[VGREEN] *= b.mV[VGREEN];
    a.mV[VBLUE] *= b.mV[VBLUE];
    return a;
}

inline const LLColor3& operator+=(LLColor3& a, const LLColor3& b)
{
    a.mV[VRED] += b.mV[VRED];
    a.mV[VGREEN] += b.mV[VGREEN];
    a.mV[VBLUE] += b.mV[VBLUE];
    return a;
}

inline const LLColor3& operator-=(LLColor3& a, const LLColor3& b)
{
    a.mV[VRED] -= b.mV[VRED];
    a.mV[VGREEN] -= b.mV[VGREEN];
    a.mV[VBLUE] -= b.mV[VBLUE];
    return a;
}

inline const LLColor3& operator*=(LLColor3& a, F32 k)
{
    a.mV[VRED] *= k;
    a.mV[VGREEN] *= k;
    a.mV[VBLUE] *= k;
    return a;
}

inline LLColor3 operator-(const LLColor3& a)
{
    return LLColor3(1.f - a.mV[VRED], 1.f - a.mV[VGREEN], 1.f - a.mV[VBLUE]);
}

// Non-member functions

inline F32 distVec(const LLColor3& a, const LLColor3& b)
{
    F32 x = a.mV[VRED] - b.mV[VRED];
    F32 y = a.mV[VGREEN] - b.mV[VGREEN];
    F32 z = a.mV[VBLUE] - b.mV[VBLUE];
    return sqrt(x * x + y * y + z * z);
}

inline F32 distVec_squared(const LLColor3& a, const LLColor3& b)
{
    F32 x = a.mV[VRED] - b.mV[VRED];
    F32 y = a.mV[VGREEN] - b.mV[VGREEN];
    F32 z = a.mV[VBLUE] - b.mV[VBLUE];
    return x * x + y * y + z * z;
}

inline LLColor3 lerp(const LLColor3& a, const LLColor3& b, F32 u)
{
    return LLColor3(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);
}

inline const LLColor3 srgbColor3(const LLColor3& a)
{
    LLColor3 srgbColor;
    srgbColor.mV[VRED]   = linearTosRGB(a.mV[VRED]);
    srgbColor.mV[VGREEN] = linearTosRGB(a.mV[VGREEN]);
    srgbColor.mV[VBLUE]  = linearTosRGB(a.mV[VBLUE]);

    return srgbColor;
}

inline const LLColor3 linearColor3p(const F32* v)
{
    LLColor3 linearColor;
    linearColor.mV[VRED]   = sRGBtoLinear(v[0]);
    linearColor.mV[VGREEN] = sRGBtoLinear(v[1]);
    linearColor.mV[VBLUE]  = sRGBtoLinear(v[2]);

    return linearColor;
}

template<class T>
inline const LLColor3 linearColor3(const T& a)
{
    return linearColor3p(a.mV);
}

template<class T>
inline const LLVector3 linearColor3v(const T& a)
{
    return LLVector3(linearColor3p(a.mV).mV);
}

template<typename T>
const LLColor3& LLColor3::set(const std::vector<T>& v)
{
    for (size_t i = 0; i < llmin(v.size(), 3); ++i)
    {
        mV[i] = (F32)v[i];
    }

    return *this;
}

// write to a vector of unknown type and size
// maye leave some data unmodified
template<typename T>
void LLColor3::write(std::vector<T>& v) const
{
    for (size_t i = 0; i < llmin(v.size(), 3); ++i)
    {
        v[i] = (T)mV[i];
    }
}

#endif