2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
4
// Contact: pgregory@aqsis.com
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.
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.
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
22
\brief Declares the CqColor class for handling generic 3 element colors.
23
\author Paul C. Gregory (pgregory@aqsis.com)
26
//? Is color.h included already?
27
#ifndef COLOR_H_INCLUDED
29
#define COLOR_H_INCLUDED 1
37
START_NAMESPACE( Aqsis )
40
//-----------------------------------------------------------------------
42
* Class to store and manipulate three component color information.
48
/// Default constructor.
49
CqColor() : m_fRed( 0.0f ), m_fGreen( 0.0f ), m_fBlue( 0.0f )
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
56
CqColor( TqFloat fRed, TqFloat fGreen, TqFloat fBlue ) :
61
CqColor( TqFloat f ) :
66
/** 3D vector constructor.
67
* \param From the vector to copy the component values from.
69
CqColor( const CqVector3D& From )
73
/** Array component constructor.
74
* \param From array of floats to use as the components.
76
CqColor( const TqFloat From[ 3 ] ) :
78
m_fGreen( From[ 1 ] ),
84
/** Get the red component.
85
* \return float red component 0.0-1.0.
91
/** Set the red component.
92
* \param fValue the new value for the red component 0.0-1.0.
94
void SetfRed( const TqFloat fValue )
98
/** Get the freen component.
99
* \return float green component 0.0-1.0.
101
TqFloat fGreen() const
105
/** Set the green component.
106
* \param fValue the new value for the green component 0.0-1.0.
108
void SetfGreen( const TqFloat fValue )
112
/** Get the blue component.
113
* \return float blue component 0.0-1.0.
115
TqFloat fBlue() const
119
/** Set the blue component.
120
* \param fValue the new value for the blue component 0.0-1.0.
122
void SetfBlue( const TqFloat fValue )
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.
131
void GetColorRGB( TqFloat* pfRed, TqFloat* pfGreen, TqFloat* pfBlue )
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.
142
void SetColorRGB( TqFloat fRed, TqFloat fGreen, TqFloat fBlue )
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;
160
/// Clamp the components to the range 0.0-1.0.
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;
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;
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.
176
TqFloat& operator[] ( TqInt i )
178
if ( i==0 ) return ( m_fRed );
179
else if ( i == 1 ) return ( m_fGreen );
180
else return ( m_fBlue );
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.
186
const TqFloat& operator[] ( TqInt i ) const
188
if ( i==0 ) return ( m_fRed );
189
else if ( i == 1 ) return ( m_fGreen );
190
else return ( m_fBlue );
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.
196
CqColor& operator=( const CqVector3D& From )
204
/** Additive assign operator.
205
* \param colFrom the color to add to this.
206
* \return a reference to this color.
208
CqColor& operator+=( const CqColor &colFrom )
210
m_fRed += colFrom.m_fRed; m_fGreen += colFrom.m_fGreen; m_fBlue += colFrom.m_fBlue;
213
/** Subtractive assign operator.
214
* \param colFrom the color to subtract from this.
215
* \return a reference to this color.
217
CqColor& operator-=( const CqColor &colFrom )
219
m_fRed -= colFrom.m_fRed; m_fGreen -= colFrom.m_fGreen; m_fBlue -= colFrom.m_fBlue;
222
/** Component wise multiplicative assign operator.
223
* \param colFrom the color to multiply this with.
224
* \return a reference to this color.
226
CqColor& operator*=( const CqColor& colFrom )
228
m_fRed *= colFrom.m_fRed; m_fGreen *= colFrom.m_fGreen; m_fBlue *= colFrom.m_fBlue;
231
/** Component wise multiplicative assign operator.
232
* \param fScale the float to multiply each component with.
233
* \return a reference to this color.
235
CqColor& operator*=( TqFloat fScale )
237
m_fRed *= fScale; m_fGreen *= fScale; m_fBlue *= fScale;
240
/** Component wise divisive assign operator.
241
* \param fScale the float to divide each component by.
242
* \return a reference to this color.
244
CqColor& operator/=( const TqFloat fScale )
246
m_fRed /= fScale; m_fGreen /= fScale; m_fBlue /= fScale;
249
/** Component wise divisive assign operator.
250
* \param colFrom the color to divide this by.
251
* \return a reference to this color.
253
CqColor& operator/=( const CqColor& colFrom )
255
m_fRed /= colFrom.m_fRed; m_fGreen /= colFrom.m_fGreen; m_fBlue /= colFrom.m_fBlue;
258
/** Component wise additive assign operator.
259
* \param Add the float to add to each component.
260
* \return a reference to this color.
262
CqColor& operator+=( const TqFloat Add )
264
m_fRed += Add; m_fGreen += Add; m_fBlue += Add;
267
/** Component wise subtractive assign operator.
268
* \param Sub the float to subtract from each component.
269
* \return a reference to this color.
271
CqColor& operator-=( const TqFloat Sub )
273
m_fRed -= Sub; m_fGreen -= Sub; m_fBlue -= Sub;
276
/** Component wise equality operator.
277
* \param colCmp the color to compare this with.
278
* \return boolean indicating equality.
280
TqBool operator==( const CqColor &colCmp ) const
282
return ( ( m_fRed == colCmp.m_fRed ) && ( m_fGreen == colCmp.m_fGreen ) && ( m_fBlue == colCmp.m_fBlue ) );
284
/** Component wise inequality operator.
285
* \param colCmp the color to compare this with.
286
* \return boolean indicating inequality.
288
TqBool operator!=( const CqColor &colCmp ) const
290
return ( !( *this == colCmp ) );
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.
296
TqBool operator>=( const CqColor &colCmp ) const
298
return ( ( m_fRed >= colCmp.m_fRed ) && ( m_fGreen >= colCmp.m_fGreen ) && ( m_fBlue >= colCmp.m_fBlue ) );
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.
304
TqBool operator<=( const CqColor &colCmp ) const
306
return ( ( m_fRed <= colCmp.m_fRed ) && ( m_fGreen <= colCmp.m_fGreen ) && ( m_fBlue <= colCmp.m_fBlue ) );
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.
312
TqBool operator>( const CqColor &colCmp ) const
314
return ( ( m_fRed > colCmp.m_fRed ) && ( m_fGreen > colCmp.m_fGreen ) && ( m_fBlue > colCmp.m_fBlue ) );
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.
320
TqBool operator<( const CqColor &colCmp ) const
322
return ( ( m_fRed < colCmp.m_fRed ) && ( m_fGreen < colCmp.m_fGreen ) && ( m_fBlue < colCmp.m_fBlue ) );
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.
330
friend CqColor operator+( const TqFloat f, const CqColor& c )
332
CqColor r( c ); return ( r += f );
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.
339
friend CqColor operator+( const CqColor& c, const TqFloat f )
341
CqColor r( c ); return ( r += f );
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.
348
friend CqColor operator-( const TqFloat f, const CqColor& c )
350
CqColor r( f, f, f ); return ( r -= c );
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.
357
friend CqColor operator-( const CqColor& c, const TqFloat f )
359
CqColor r( c ); return ( r -= f );
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.
366
friend CqColor operator*( const TqFloat f, const CqColor& c )
368
CqColor r( f, f, f ); return ( r *= c );
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.
375
friend CqColor operator*( const CqColor& c, const TqFloat f )
377
CqColor r( c ); return ( r *= f );
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.
384
friend CqColor operator/( const TqFloat f, const CqColor& c )
386
CqColor r( f, f, f ); return ( r /= c );
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.
393
friend CqColor operator/( const CqColor& c, const TqFloat f )
395
CqColor r( c ); return ( r /= f );
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.
403
friend CqColor operator+( const CqColor& a, const CqColor& b )
405
CqColor r( a ); return ( r += b );
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.
412
friend CqColor operator-( const CqColor& a, const CqColor& b )
414
CqColor r( a ); return ( r -= b );
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.
421
friend CqColor operator*( const CqColor& a, const CqColor& b )
423
CqColor r( a ); return ( r *= b );
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.
430
friend CqColor operator/( const CqColor& a, const CqColor& b )
432
CqColor r( a ); return ( r /= b );
434
/** Component wise friend negation operator.
435
* \param a color to negate.
436
* \return new color representing negation.
438
friend CqColor operator-( const CqColor& a )
440
return ( CqColor( -a.m_fRed, -a.m_fGreen, -a.m_fBlue ) );
442
/** Component wide stream output operator.
443
*\param Stream output stream.
444
*\param a color to serialize.
445
*\return input stream.
447
friend std::ostream& operator<<( std::ostream& Stream, const CqColor& a )
449
Stream << a.m_fRed << " " << a.m_fGreen << " " << a.m_fBlue;
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
461
/// Static white color
462
extern CqColor gColWhite;
463
/// Static black color
464
extern CqColor gColBlack;
466
extern CqColor gColRed;
467
/// Static green color
468
extern CqColor gColGreen;
469
/// Static blue color
470
extern CqColor gColBlue;
472
//-----------------------------------------------------------------------
474
END_NAMESPACE( Aqsis )
477
//} // End of #ifdef _H_INCLUDED