Coverage Report

Created: 2026-02-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/include/OgreColourValue.h
Line
Count
Source
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
    (Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
6
7
Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
#ifndef _COLOURVALUE_H__
29
#define _COLOURVALUE_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreMath.h"
33
34
namespace Ogre {
35
    /** \addtogroup Core
36
    *  @{
37
    */
38
    /** \addtogroup General
39
    *  @{
40
    */
41
42
    typedef uint32 RGBA;
43
    typedef uint32 ARGB;
44
    typedef uint32 ABGR;
45
    typedef uint32 BGRA;
46
47
    /** Class representing colour.
48
49
        Colour is represented as 4 components, each of which is a
50
        floating-point value from 0.0 to 1.0.
51
        @par
52
            The 3 'normal' colour components are red, green and blue, a higher
53
            number indicating greater amounts of that component in the colour.
54
            The forth component is the 'alpha' value, which represents
55
            transparency. In this case, 0.0 is completely transparent and 1.0 is
56
            fully opaque.
57
    */
58
    class _OgreExport ColourValue
59
    {
60
    public:
61
        static const ColourValue &ZERO;
62
        static const ColourValue &Black;
63
        static const ColourValue &White;
64
        static const ColourValue &Red;
65
        static const ColourValue &Green;
66
        static const ColourValue &Blue;
67
68
        explicit constexpr ColourValue( float red = 1.0f,
69
                    float green = 1.0f,
70
                    float blue = 1.0f,
71
0
                    float alpha = 1.0f ) : r(red), g(green), b(blue), a(alpha)
72
0
        { }
73
74
0
        explicit ColourValue(const uchar* byte) : r(byte[0]), g(byte[1]), b(byte[2]), a(byte[3])
75
0
        {
76
0
            *this /= 255;
77
0
        }
78
79
        bool operator==(const ColourValue& rhs) const
80
0
        {
81
0
            return (r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a);
82
0
        }
83
0
        bool operator!=(const ColourValue& rhs) const { return !(*this == rhs); }
84
85
        float r,g,b,a;
86
87
        /// @name conversions from/ to native-endian packed formats
88
        /// @{
89
90
        /// value packed as #PF_R8G8B8A8
91
        RGBA getAsRGBA(void) const;
92
93
        /// value packed as #PF_A8R8G8B8
94
        ARGB getAsARGB(void) const;
95
96
        /// value packed as #PF_B8G8R8A8
97
        BGRA getAsBGRA(void) const;
98
99
        /// value packed as #PF_A8B8G8R8
100
        ABGR getAsABGR(void) const;
101
102
        /// value packed as #PF_BYTE_RGBA
103
        RGBA getAsBYTE() const
104
0
        {
105
#if OGRE_ENDIAN == OGRE_ENDIAN_BIG
106
            return getAsRGBA();
107
#else
108
0
            return getAsABGR();
109
0
#endif
110
0
        }
111
112
        /// Set value from #PF_R8G8B8A8
113
        void setAsRGBA(RGBA val);
114
115
        /// Set value from #PF_A8R8G8B8
116
        void setAsARGB(ARGB val);
117
118
        /// Set value from #PF_B8G8R8A8
119
        void setAsBGRA(BGRA val);
120
121
        /// Set value from #PF_A8B8G8R8
122
        void setAsABGR(ABGR val);
123
        /// @}
124
125
        /** Clamps colour value to the range [0, 1].
126
        */
127
        void saturate(void)
128
0
        {
129
0
            r = Math::saturate(r);
130
0
            g = Math::saturate(g);
131
0
            b = Math::saturate(b);
132
0
            a = Math::saturate(a);
133
0
        }
134
135
        /** As saturate, except that this colour value is unaffected and
136
            the saturated colour value is returned as a copy. */
137
        ColourValue saturateCopy(void) const
138
0
        {
139
0
            ColourValue ret = *this;
140
0
            ret.saturate();
141
0
            return ret;
142
0
        }
143
144
        /// Convert from gamma space to linear space
145
0
        ColourValue gammaToLinear() const { return ColourValue(powf(r, 2.2f), powf(g, 2.2f), powf(b, 2.2f), a); }
146
147
        /// Array accessor operator
148
        float operator [] ( const size_t i ) const
149
0
        {
150
0
            assert( i < 4 );
151
0
152
0
            return *(&r+i);
153
0
        }
154
155
        /// Array accessor operator
156
        float& operator [] ( const size_t i )
157
0
        {
158
0
            assert( i < 4 );
159
0
160
0
            return *(&r+i);
161
0
        }
162
163
        /// Pointer accessor for direct copying
164
        float* ptr()
165
0
        {
166
0
            return &r;
167
0
        }
168
        /// Pointer accessor for direct copying
169
        const float* ptr() const
170
0
        {
171
0
            return &r;
172
0
        }
173
174
        
175
        // arithmetic operations
176
        ColourValue operator + ( const ColourValue& rkVector ) const
177
0
        {
178
0
            ColourValue kSum;
179
180
0
            kSum.r = r + rkVector.r;
181
0
            kSum.g = g + rkVector.g;
182
0
            kSum.b = b + rkVector.b;
183
0
            kSum.a = a + rkVector.a;
184
185
0
            return kSum;
186
0
        }
187
188
        ColourValue operator - ( const ColourValue& rkVector ) const
189
0
        {
190
0
            ColourValue kDiff;
191
192
0
            kDiff.r = r - rkVector.r;
193
0
            kDiff.g = g - rkVector.g;
194
0
            kDiff.b = b - rkVector.b;
195
0
            kDiff.a = a - rkVector.a;
196
197
0
            return kDiff;
198
0
        }
199
200
        ColourValue operator * (const float fScalar ) const
201
0
        {
202
0
            ColourValue kProd;
203
204
0
            kProd.r = fScalar*r;
205
0
            kProd.g = fScalar*g;
206
0
            kProd.b = fScalar*b;
207
0
            kProd.a = fScalar*a;
208
209
0
            return kProd;
210
0
        }
211
212
        ColourValue operator * ( const ColourValue& rhs) const
213
0
        {
214
0
            ColourValue kProd;
215
0
216
0
            kProd.r = rhs.r * r;
217
0
            kProd.g = rhs.g * g;
218
0
            kProd.b = rhs.b * b;
219
0
            kProd.a = rhs.a * a;
220
0
221
0
            return kProd;
222
0
        }
223
224
        ColourValue operator / ( const ColourValue& rhs) const
225
0
        {
226
0
            ColourValue kProd;
227
0
228
0
            kProd.r = r / rhs.r;
229
0
            kProd.g = g / rhs.g;
230
0
            kProd.b = b / rhs.b;
231
0
            kProd.a = a / rhs.a;
232
0
233
0
            return kProd;
234
0
        }
235
236
        ColourValue operator / (const float fScalar ) const
237
0
        {
238
0
            assert( fScalar != 0.0 );
239
0
240
0
            ColourValue kDiv;
241
0
242
0
            float fInv = 1.0f / fScalar;
243
0
            kDiv.r = r * fInv;
244
0
            kDiv.g = g * fInv;
245
0
            kDiv.b = b * fInv;
246
0
            kDiv.a = a * fInv;
247
0
248
0
            return kDiv;
249
0
        }
250
251
        friend ColourValue operator * (const float fScalar, const ColourValue& rkVector )
252
0
        {
253
0
            ColourValue kProd;
254
255
0
            kProd.r = fScalar * rkVector.r;
256
0
            kProd.g = fScalar * rkVector.g;
257
0
            kProd.b = fScalar * rkVector.b;
258
0
            kProd.a = fScalar * rkVector.a;
259
260
0
            return kProd;
261
0
        }
262
263
        // arithmetic updates
264
        ColourValue& operator += ( const ColourValue& rkVector )
265
0
        {
266
0
            r += rkVector.r;
267
0
            g += rkVector.g;
268
0
            b += rkVector.b;
269
0
            a += rkVector.a;
270
0
271
0
            return *this;
272
0
        }
273
274
        ColourValue& operator -= ( const ColourValue& rkVector )
275
0
        {
276
0
            r -= rkVector.r;
277
0
            g -= rkVector.g;
278
0
            b -= rkVector.b;
279
0
            a -= rkVector.a;
280
0
281
0
            return *this;
282
0
        }
283
284
        ColourValue& operator *= (const float fScalar )
285
0
        {
286
0
            r *= fScalar;
287
0
            g *= fScalar;
288
0
            b *= fScalar;
289
0
            a *= fScalar;
290
0
            return *this;
291
0
        }
292
293
        ColourValue& operator /= (const float fScalar )
294
0
        {
295
0
            assert( fScalar != 0.0 );
296
297
0
            float fInv = 1.0f / fScalar;
298
299
0
            r *= fInv;
300
0
            g *= fInv;
301
0
            b *= fInv;
302
0
            a *= fInv;
303
304
0
            return *this;
305
0
        }
306
307
        /** Set a colour value from Hue, Saturation and Brightness.
308
        @param hue Hue value, scaled to the [0,1] range as opposed to the 0-360
309
        @param saturation Saturation level, [0,1]
310
        @param brightness Brightness level, [0,1]
311
        */
312
        void setHSB(float hue, float saturation, float brightness);
313
314
        /** Convert the current colour to Hue, Saturation and Brightness values. 
315
        @param hue Output hue value, scaled to the [0,1] range as opposed to the 0-360
316
        @param saturation Output saturation level, [0,1]
317
        @param brightness Output brightness level, [0,1]
318
        */
319
        void getHSB(float& hue, float& saturation, float& brightness) const;
320
321
        /// @deprecated
322
        OGRE_DEPRECATED void getHSB(float* hue, float* saturation, float* brightness) const
323
0
        {
324
0
            getHSB(*hue, *saturation, *brightness);
325
0
        }
326
327
        /** Function for writing to a stream.
328
        */
329
        inline friend std::ostream& operator <<
330
            ( std::ostream& o, const ColourValue& c )
331
0
        {
332
0
            o << "ColourValue(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
333
0
            return o;
334
0
        }
335
336
    };
337
    /** @} */
338
    /** @} */
339
340
} // namespace
341
342
#endif