~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to libaqsistypes/color.h

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Declares the CqColor class for handling generic 3 element colors.
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
*/
 
25
 
 
26
//? Is color.h included already?
 
27
#ifndef COLOR_H_INCLUDED 
 
28
//{
 
29
#define COLOR_H_INCLUDED 1
 
30
 
 
31
#include        "aqsis.h"
 
32
 
 
33
#include        "vector3d.h"
 
34
 
 
35
#include <iostream>
 
36
 
 
37
START_NAMESPACE( Aqsis )
 
38
 
 
39
 
 
40
//-----------------------------------------------------------------------
 
41
/** \class CqColor
 
42
 * Class to store and manipulate three component color information.
 
43
 */
 
44
 
 
45
class CqColor
 
46
{
 
47
public:
 
48
    /// Default constructor.
 
49
    CqColor() : m_fRed( 0.0f ), m_fGreen( 0.0f ), m_fBlue( 0.0f )
 
50
    {}
 
51
    /** Component constructor
 
52
     * \param fRed red component 0.0-1.0
 
53
     * \param fGreen green component 0.0-1.0
 
54
     * \param fBlue blue component 0.0-1.0
 
55
     */
 
56
    CqColor( TqFloat fRed, TqFloat fGreen, TqFloat fBlue ) :
 
57
            m_fRed( fRed ),
 
58
            m_fGreen( fGreen ),
 
59
            m_fBlue( fBlue )
 
60
    {}
 
61
    CqColor( TqFloat f ) :
 
62
            m_fRed( f ),
 
63
            m_fGreen( f ),
 
64
            m_fBlue( f )
 
65
    {}
 
66
    /** 3D vector constructor.
 
67
     * \param From the vector to copy the component values from.
 
68
     */
 
69
    CqColor( const CqVector3D& From )
 
70
    {
 
71
        *this = From;
 
72
    }
 
73
    /** Array component constructor.
 
74
     * \param From array of floats to use as the components.
 
75
     */
 
76
    CqColor( const TqFloat From[ 3 ] ) :
 
77
            m_fRed( From[ 0 ] ),
 
78
            m_fGreen( From[ 1 ] ),
 
79
            m_fBlue( From[ 2 ] )
 
80
    {}
 
81
    ~CqColor()
 
82
    {}
 
83
 
 
84
    /** Get the red component.
 
85
     * \return float red component 0.0-1.0.
 
86
     */
 
87
    TqFloat     fRed() const
 
88
    {
 
89
        return ( m_fRed );
 
90
    }
 
91
    /** Set the red component.
 
92
     * \param fValue the new value for the red component 0.0-1.0.
 
93
     */
 
94
    void        SetfRed( const TqFloat fValue )
 
95
    {
 
96
        m_fRed = fValue;
 
97
    }
 
98
    /** Get the freen component.
 
99
     * \return float green component 0.0-1.0.
 
100
     */
 
101
    TqFloat     fGreen() const
 
102
    {
 
103
        return ( m_fGreen );
 
104
    }
 
105
    /** Set the green component.
 
106
     * \param fValue the new value for the green component 0.0-1.0.
 
107
     */
 
108
    void        SetfGreen( const TqFloat fValue )
 
109
    {
 
110
        m_fGreen = fValue;
 
111
    }
 
112
    /** Get the blue component.
 
113
     * \return float blue component 0.0-1.0.
 
114
     */
 
115
    TqFloat     fBlue() const
 
116
    {
 
117
        return ( m_fBlue );
 
118
    }
 
119
    /** Set the blue component.
 
120
     * \param fValue the new value for the blue component 0.0-1.0.
 
121
     */
 
122
    void        SetfBlue( const TqFloat fValue )
 
123
    {
 
124
        m_fBlue = fValue;
 
125
    }
 
126
    /** Get the color as indiviual components.
 
127
     * \param pfRed pointer to the area to store the red component.
 
128
     * \param pfGreen pointer to the area to store the green component.
 
129
     * \param pfBlue pointer to the area to store the blue component.
 
130
     */
 
131
    void        GetColorRGB( TqFloat* pfRed, TqFloat* pfGreen, TqFloat* pfBlue )
 
132
    {
 
133
        *pfRed = m_fRed;
 
134
        *pfGreen = m_fGreen;
 
135
        *pfBlue = m_fBlue;
 
136
    }
 
137
    /** Set the color as individual components.
 
138
     * \param fRed the new value for the red component 0.0-1.0.
 
139
     * \param fGreen the new value for the green component 0.0-1.0.
 
140
     * \param fBlue the new value for the blue component 0.0-1.0.
 
141
     */
 
142
    void        SetColorRGB( TqFloat fRed, TqFloat fGreen, TqFloat fBlue )
 
143
    {
 
144
        m_fRed = fRed;
 
145
        m_fGreen = fGreen;
 
146
        m_fBlue = fBlue;
 
147
    }
 
148
 
 
149
    CqColor     rgbtohsv() const;
 
150
    CqColor     rgbtohsl() const;
 
151
    CqColor     rgbtoXYZ() const;
 
152
    CqColor     rgbtoxyY() const;
 
153
    CqColor     rgbtoYIQ() const;
 
154
    CqColor     hsvtorgb() const;
 
155
    CqColor     hsltorgb() const;
 
156
    CqColor     XYZtorgb() const;
 
157
    CqColor     xyYtorgb() const;
 
158
    CqColor     YIQtorgb() const;
 
159
 
 
160
    /// Clamp the components to the range 0.0-1.0.
 
161
    void        Clamp()
 
162
    {
 
163
        if ( m_fRed > 1.0 ) m_fRed = 1.0;
 
164
        if ( m_fGreen > 1.0 ) m_fGreen = 1.0;
 
165
        if ( m_fBlue > 1.0 ) m_fBlue = 1.0;
 
166
 
 
167
        if ( m_fRed < 0.0 ) m_fRed = 0.0;
 
168
        if ( m_fGreen < 0.0 ) m_fGreen = 0.0;
 
169
        if ( m_fBlue < 0.0 ) m_fBlue = 0.0;
 
170
    }
 
171
 
 
172
    /** Array based component access.
 
173
     * \param i integer component index, 0-2.
 
174
     * \return a reference to the float value of the appropriate component, returns blue if index is invalid.
 
175
     */
 
176
    TqFloat&    operator[] ( TqInt i )
 
177
    {
 
178
        if ( i==0 ) return ( m_fRed );
 
179
        else if ( i == 1 ) return ( m_fGreen );
 
180
        else    return ( m_fBlue );
 
181
    }
 
182
    /** Array based read only component access.
 
183
     * \param i integer component index, 0-2.
 
184
     * \return a constant reference the float value of the appropriate component, returns blue if index is invalid.
 
185
     */
 
186
    const TqFloat&      operator[] ( TqInt i ) const
 
187
    {
 
188
        if ( i==0 ) return ( m_fRed );
 
189
        else if ( i == 1 ) return ( m_fGreen );
 
190
        else    return ( m_fBlue );
 
191
    }
 
192
    /** Copy value from a 3D vector.
 
193
     * \param From the vector to get the color cmoponents from.
 
194
     * \return a reference to this color.
 
195
     */
 
196
    CqColor&    operator=( const CqVector3D& From )
 
197
    {
 
198
        m_fRed = From.x();
 
199
        m_fGreen = From.y();
 
200
        m_fBlue = From.z();
 
201
 
 
202
        return ( *this );
 
203
    }
 
204
    /** Additive assign operator.
 
205
     * \param colFrom the color to add to this.
 
206
     * \return a reference to this color.
 
207
     */
 
208
    CqColor&    operator+=( const CqColor &colFrom )
 
209
    {
 
210
        m_fRed += colFrom.m_fRed;       m_fGreen += colFrom.m_fGreen;   m_fBlue += colFrom.m_fBlue;
 
211
        return ( *this );
 
212
    }
 
213
    /** Subtractive assign operator.
 
214
     * \param colFrom the color to subtract from this.
 
215
     * \return a reference to this color.
 
216
     */
 
217
    CqColor&    operator-=( const CqColor &colFrom )
 
218
    {
 
219
        m_fRed -= colFrom.m_fRed;       m_fGreen -= colFrom.m_fGreen;   m_fBlue -= colFrom.m_fBlue;
 
220
        return ( *this );
 
221
    }
 
222
    /** Component wise multiplicative assign operator.
 
223
     * \param colFrom the color to multiply this with.
 
224
     * \return a reference to this color.
 
225
     */
 
226
    CqColor&    operator*=( const CqColor& colFrom )
 
227
    {
 
228
        m_fRed *= colFrom.m_fRed;       m_fGreen *= colFrom.m_fGreen;   m_fBlue *= colFrom.m_fBlue;
 
229
        return ( *this );
 
230
    }
 
231
    /** Component wise multiplicative assign operator.
 
232
     * \param fScale the float to multiply each component with.
 
233
     * \return a reference to this color.
 
234
     */
 
235
    CqColor&    operator*=( TqFloat fScale )
 
236
    {
 
237
        m_fRed *= fScale;       m_fGreen *= fScale;     m_fBlue *= fScale;
 
238
        return ( *this );
 
239
    }
 
240
    /** Component wise divisive assign operator.
 
241
     * \param fScale the float to divide each component by.
 
242
     * \return a reference to this color.
 
243
     */
 
244
    CqColor&    operator/=( const TqFloat fScale )
 
245
    {
 
246
        m_fRed /= fScale;       m_fGreen /= fScale;     m_fBlue /= fScale;
 
247
        return ( *this );
 
248
    }
 
249
    /** Component wise divisive assign operator.
 
250
     * \param colFrom the color to divide this by.
 
251
     * \return a reference to this color.
 
252
     */
 
253
    CqColor&    operator/=( const CqColor& colFrom )
 
254
    {
 
255
        m_fRed /= colFrom.m_fRed;       m_fGreen /= colFrom.m_fGreen;   m_fBlue /= colFrom.m_fBlue;
 
256
        return ( *this );
 
257
    }
 
258
    /** Component wise additive assign operator.
 
259
     * \param Add the float to add to each component.
 
260
     * \return a reference to this color.
 
261
     */
 
262
    CqColor&    operator+=( const TqFloat Add )
 
263
    {
 
264
        m_fRed += Add;  m_fGreen += Add;        m_fBlue += Add;
 
265
        return ( *this );
 
266
    }
 
267
    /** Component wise subtractive assign operator.
 
268
     * \param Sub the float to subtract from each component.
 
269
     * \return a reference to this color.
 
270
     */
 
271
    CqColor&    operator-=( const TqFloat Sub )
 
272
    {
 
273
        m_fRed -= Sub;  m_fGreen -= Sub;        m_fBlue -= Sub;
 
274
        return ( *this );
 
275
    }
 
276
    /** Component wise equality operator.
 
277
     * \param colCmp the color to compare this with.
 
278
     * \return boolean indicating equality.
 
279
     */
 
280
    TqBool      operator==( const CqColor &colCmp ) const
 
281
    {
 
282
        return ( ( m_fRed == colCmp.m_fRed ) && ( m_fGreen == colCmp.m_fGreen ) && ( m_fBlue == colCmp.m_fBlue ) );
 
283
    }
 
284
    /** Component wise inequality operator.
 
285
     * \param colCmp the color to compare this with.
 
286
     * \return boolean indicating inequality.
 
287
     */
 
288
    TqBool      operator!=( const CqColor &colCmp ) const
 
289
    {
 
290
        return ( !( *this == colCmp ) );
 
291
    }
 
292
    /** Component wise greater than or equal to operator.
 
293
     * \param colCmp the color to compare this with.
 
294
     * \return boolean indicating each component is greater than or equal to its counterpart in the argument.
 
295
     */
 
296
    TqBool      operator>=( const CqColor &colCmp ) const
 
297
    {
 
298
        return ( ( m_fRed >= colCmp.m_fRed ) && ( m_fGreen >= colCmp.m_fGreen ) && ( m_fBlue >= colCmp.m_fBlue ) );
 
299
    }
 
300
    /** Component wise less than or equal to operator.
 
301
     * \param colCmp the color to compare this with.
 
302
     * \return boolean indicating each component is less than or equal to its counterpart in the argument.
 
303
     */
 
304
    TqBool      operator<=( const CqColor &colCmp ) const
 
305
    {
 
306
        return ( ( m_fRed <= colCmp.m_fRed ) && ( m_fGreen <= colCmp.m_fGreen ) && ( m_fBlue <= colCmp.m_fBlue ) );
 
307
    }
 
308
    /** Component wise greater than to operator.
 
309
     * \param colCmp the color to compare this with.
 
310
     * \return boolean indicating each component is greater than its counterpart in the argument.
 
311
     */
 
312
    TqBool      operator>( const CqColor &colCmp ) const
 
313
    {
 
314
        return ( ( m_fRed > colCmp.m_fRed ) && ( m_fGreen > colCmp.m_fGreen ) && ( m_fBlue > colCmp.m_fBlue ) );
 
315
    }
 
316
    /** Component wise less than to operator.
 
317
     * \param colCmp the color to compare this with.
 
318
     * \return boolean indicating each component is less than its counterpart in the argument.
 
319
     */
 
320
    TqBool      operator<( const CqColor &colCmp ) const
 
321
    {
 
322
        return ( ( m_fRed < colCmp.m_fRed ) && ( m_fGreen < colCmp.m_fGreen ) && ( m_fBlue < colCmp.m_fBlue ) );
 
323
    }
 
324
 
 
325
    /** Component wise friend addition operator.
 
326
     * \param f float to add to each component.
 
327
     * \param c color to add to.
 
328
     * \return new color representing addition. 
 
329
     */
 
330
    friend CqColor      operator+( const TqFloat f, const CqColor& c )
 
331
    {
 
332
        CqColor r( c ); return ( r += f );
 
333
    }
 
334
    /** Component wise friend addition operator.
 
335
     * \param c color to add to.
 
336
     * \param f float to add to each component.
 
337
     * \return new color representing addition. 
 
338
     */
 
339
    friend CqColor      operator+( const CqColor& c, const TqFloat f )
 
340
    {
 
341
        CqColor r( c ); return ( r += f );
 
342
    }
 
343
    /** Component wise friend subtraction operator.
 
344
     * \param f float to subtract from each component.
 
345
     * \param c color to subtract from.
 
346
     * \return new color representing subtraction. 
 
347
     */
 
348
    friend CqColor      operator-( const TqFloat f, const CqColor& c )
 
349
    {
 
350
        CqColor r( f, f, f ); return ( r -= c );
 
351
    }
 
352
    /** Component wise friend subtraction operator.
 
353
     * \param c color to subtract from.
 
354
     * \param f float to subtract from each component.
 
355
     * \return new color representing subtraction. 
 
356
     */
 
357
    friend CqColor      operator-( const CqColor& c, const TqFloat f )
 
358
    {
 
359
        CqColor r( c ); return ( r -= f );
 
360
    }
 
361
    /** Component wise friend multiplication operator.
 
362
     * \param f float to multiply each component with.
 
363
     * \param c color to multiply with.
 
364
     * \return new color representing multiplication. 
 
365
     */
 
366
    friend CqColor      operator*( const TqFloat f, const CqColor& c )
 
367
    {
 
368
        CqColor r( f, f, f ); return ( r *= c );
 
369
    }
 
370
    /** Component wise friend multiplication operator.
 
371
     * \param c color to multiply with.
 
372
     * \param f float to multiply each component with.
 
373
     * \return new color representing multiplication. 
 
374
     */
 
375
    friend CqColor      operator*( const CqColor& c, const TqFloat f )
 
376
    {
 
377
        CqColor r( c ); return ( r *= f );
 
378
    }
 
379
    /** Component wise friend division operator.
 
380
     * \param f float to divide each component by.
 
381
     * \param c color to divide.
 
382
     * \return new color representing division. 
 
383
     */
 
384
    friend CqColor      operator/( const TqFloat f, const CqColor& c )
 
385
    {
 
386
        CqColor r( f, f, f ); return ( r /= c );
 
387
    }
 
388
    /** Component wise friend division operator.
 
389
     * \param c color to divide.
 
390
     * \param f float to divide each component by.
 
391
     * \return new color representing division. 
 
392
     */
 
393
    friend CqColor      operator/( const CqColor& c, const TqFloat f )
 
394
    {
 
395
        CqColor r( c ); return ( r /= f );
 
396
    }
 
397
 
 
398
    /** Component wise friend addition operator.
 
399
     * \param a color to add to.
 
400
     * \param b color to add.
 
401
     * \return new color representing addition. 
 
402
     */
 
403
    friend CqColor      operator+( const CqColor& a, const CqColor& b )
 
404
    {
 
405
        CqColor r( a ); return ( r += b );
 
406
    }
 
407
    /** Component wise friend subtraction operator.
 
408
     * \param a color to subtract from.
 
409
     * \param b color to subtract.
 
410
     * \return new color representing subtraction. 
 
411
     */
 
412
    friend CqColor      operator-( const CqColor& a, const CqColor& b )
 
413
    {
 
414
        CqColor r( a ); return ( r -= b );
 
415
    }
 
416
    /** Component wise friend multiplication operator.
 
417
     * \param a color to multiply.
 
418
     * \param b color to multiply by.
 
419
     * \return new color representing multiplication. 
 
420
     */
 
421
    friend CqColor      operator*( const CqColor& a, const CqColor& b )
 
422
    {
 
423
        CqColor r( a ); return ( r *= b );
 
424
    }
 
425
    /** Component wise friend division operator.
 
426
     * \param a color to divide.
 
427
     * \param b color to divide by.
 
428
     * \return new color representing division. 
 
429
     */
 
430
    friend CqColor      operator/( const CqColor& a, const CqColor& b )
 
431
    {
 
432
        CqColor r( a ); return ( r /= b );
 
433
    }
 
434
    /** Component wise friend negation operator.
 
435
     * \param a color to negate.
 
436
     * \return new color representing negation. 
 
437
     */
 
438
    friend CqColor      operator-( const CqColor& a )
 
439
    {
 
440
        return ( CqColor( -a.m_fRed, -a.m_fGreen, -a.m_fBlue ) );
 
441
    } // Negation
 
442
    /** Component wide stream output operator.
 
443
     *\param Stream output stream.
 
444
     *\param a color to serialize.
 
445
     *\return input stream.
 
446
     */
 
447
    friend std::ostream& operator<<( std::ostream& Stream, const CqColor& a )
 
448
    {
 
449
        Stream << a.m_fRed << " " << a.m_fGreen << " " << a.m_fBlue;
 
450
        return Stream;
 
451
    }
 
452
 
 
453
private:
 
454
    TqFloat     m_fRed,                                 ///< the red component 0.0-1.0
 
455
    m_fGreen,                           ///< the green component 0.0-1.0
 
456
    m_fBlue;                    ///< the blue component 0.0-1.0
 
457
}
 
458
;
 
459
 
 
460
 
 
461
/// Static white color
 
462
extern CqColor  gColWhite;
 
463
/// Static black color
 
464
extern CqColor  gColBlack;
 
465
/// Static red color
 
466
extern CqColor  gColRed;
 
467
/// Static green color
 
468
extern CqColor  gColGreen;
 
469
/// Static blue color
 
470
extern CqColor  gColBlue;
 
471
 
 
472
//-----------------------------------------------------------------------
 
473
 
 
474
END_NAMESPACE( Aqsis )
 
475
 
 
476
 
 
477
//}  // End of #ifdef _H_INCLUDED
 
478
#endif