GL Studio C++ Runtime API
gls_color.h
Go to the documentation of this file.
1 /*! \file
2  \brief The Color class: Implements a 4 component RGBA color.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef _GLS_COLOR_H
41 #define _GLS_COLOR_H
42 
43 #include "disti_include.h"
44 #include "unhide_globals.h"
45 #include <iosfwd> // Forward references for the stream operators
46 #include <string.h> // For memcmp
47 
48 namespace disti
49 {
50 /** \class GlsColor
51  The Color class: Implements a 4 component RGBA color.
52 */
53 class GlsColor
54 {
55  unsigned char rgba[ 4 ]; /** Red, Green, Blue, Alpha 0-255 range */
56 
57 public:
58  /** default constructor. Intializes rgba data to zero */
59  GlsColor( void )
60  {
61  rgba[ 0 ] = rgba[ 1 ] = rgba[ 2 ] = rgba[ 3 ] = 0;
62  }
63 
64  /** 4ub constructor. Intializes using rgba values
65  * \param r Red value, range 0-255
66  * \param g Green value, range 0-255
67  * \param b Blue value, range 0-255
68  * \param a Alpha value, range 0-255
69  */
70  GlsColor( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 )
71  {
72  rgba[ 0 ] = r;
73  rgba[ 1 ] = g;
74  rgba[ 2 ] = b;
75  rgba[ 3 ] = a;
76  }
77 
78  /** 4ubv constructor. Intializes using rgba value
79  * \param newColor 4 character RGBA array, range 0-255
80  */
81  GlsColor( const unsigned char newColor[] )
82  {
83  rgba[ 0 ] = newColor[ 0 ];
84  rgba[ 1 ] = newColor[ 1 ];
85  rgba[ 2 ] = newColor[ 2 ];
86  rgba[ 3 ] = newColor[ 3 ];
87  }
88 
89  /** 4ul constructor.
90  * \param rgbaVal Unsigned long containing color in the form RRGGBBAA (8 bits per component
91  */
92  GlsColor( unsigned long rgbaVal )
93  {
94  rgba[ 0 ] = (unsigned char)( ( rgbaVal & 0xFF000000 ) >> 24 );
95  rgba[ 1 ] = (unsigned char)( ( rgbaVal & 0x00FF0000 ) >> 16 );
96  rgba[ 2 ] = (unsigned char)( ( rgbaVal & 0x0000FF00 ) >> 8 );
97  rgba[ 3 ] = (unsigned char)( ( rgbaVal & 0x000000FF ) );
98  }
99 
100  /** copy constructor. */
101  GlsColor( const GlsColor& color )
102  {
103  memcpy( rgba, color.rgba, sizeof( unsigned char ) * 4 );
104  }
105 
106  /** Sets RGBA value using floats
107  * \param r Red value, range 0-1.0
108  * \param g Green value, range 0-1.0
109  * \param b Blue value, range 0-1.0
110  * \param a Alpha value, range 0-1.0
111  */
112  void SetRGBA4f( float r, float g, float b, float a = 1 )
113  {
114  rgba[ 0 ] = (unsigned char)( r * 255.0f );
115  rgba[ 1 ] = (unsigned char)( g * 255.0f );
116  rgba[ 2 ] = (unsigned char)( b * 255.0f );
117  rgba[ 3 ] = (unsigned char)( a * 255.0f );
118  }
119 
120  /** Gets RGBA value using floats
121  * \param r Red value, range 0-1.0
122  * \param g Green value, range 0-1.0
123  * \param b Blue value, range 0-1.0
124  * \param a Alpha value, range 0-1.0
125  */
126  void GetRGBA4f( float& r, float& g, float& b, float& a ) const
127  {
128  r = (float)rgba[ 0 ] / 255.0f;
129  g = (float)rgba[ 1 ] / 255.0f;
130  b = (float)rgba[ 2 ] / 255.0f;
131  a = (float)rgba[ 3 ] / 255.0f;
132  }
133 
134  /** Sets RGBA value using doubles
135  * \param r Red value, range 0-1.0
136  * \param g Green value, range 0-1.0
137  * \param b Blue value, range 0-1.0
138  * \param a Alpha value, range 0-1.0
139  */
140  void SetRGBA4d( double r, double g, double b, double a = 1 )
141  {
142  rgba[ 0 ] = (unsigned char)( r * 255.0 );
143  rgba[ 1 ] = (unsigned char)( g * 255.0 );
144  rgba[ 2 ] = (unsigned char)( b * 255.0 );
145  rgba[ 3 ] = (unsigned char)( a * 255.0 );
146  }
147 
148  /** Gets RGBA value using doubles
149  * \param r Red value, range 0-1.0
150  * \param g Green value, range 0-1.0
151  * \param b Blue value, range 0-1.0
152  * \param a Alpha value, range 0-1.0
153  */
154  void GetRGBA4d( double& r, double& g, double& b, double& a ) const
155  {
156  r = (double)rgba[ 0 ] / 255.0;
157  g = (double)rgba[ 1 ] / 255.0;
158  b = (double)rgba[ 2 ] / 255.0;
159  a = (double)rgba[ 3 ] / 255.0;
160  }
161 
162  /** Sets RGBA value
163  * \param r Red value, range 0-255
164  * \param g Green value, range 0-255
165  * \param b Blue value, range 0-255
166  * \param a Alpha value, range 0-255
167  */
168  void RGBA( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 )
169  {
170  rgba[ 0 ] = r;
171  rgba[ 1 ] = g;
172  rgba[ 2 ] = b;
173  rgba[ 3 ] = a;
174  }
175 
176  /** Sets RGBA value
177  * \param newColor 4 character RGBA array, range 0-255
178  */
179  void RGBA( const unsigned char newColor[] )
180  {
181  memcpy( rgba, newColor, sizeof( unsigned char ) * 4 );
182  }
183 
184  /** Sets RGBA value
185  * \param rgbaVal Unsigned long containing color in the form RRGGBBAA (8 bits per component
186  */
187  void RGBA( unsigned long rgbaVal )
188  {
189  rgba[ 0 ] = (unsigned char)( ( rgbaVal & 0xFF000000 ) >> 24 );
190  rgba[ 1 ] = (unsigned char)( ( rgbaVal & 0x00FF0000 ) >> 16 );
191  rgba[ 2 ] = (unsigned char)( ( rgbaVal & 0x0000FF00 ) >> 8 );
192  rgba[ 3 ] = (unsigned char)( ( rgbaVal & 0x000000FF ) );
193  }
194 
195  /** Gets RGBA value
196  * \return Pointer to array of 4 unsigned chars containing rgba (0-255 range)
197  */
198  unsigned char* RGBA( void ) { return rgba; }
199  const unsigned char* RGBA( void ) const { return rgba; }
200 
201  /** \return RGBA value packed into a 32 bit unsigned int in host byte order */
202  unsigned long RGBA_ULong( void ) const
203  {
204  return ( rgba[ 0 ] << 24 ) | ( rgba[ 1 ] << 16 ) | ( rgba[ 2 ] << 8 ) | rgba[ 3 ];
205  }
206 
207  /** \return The Red component of this color */
208  unsigned char R( void ) const { return rgba[ 0 ]; }
209 
210  /** \return The Green component of this color */
211  unsigned char G( void ) const { return rgba[ 1 ]; }
212 
213  /** \return The Blue component of this color */
214  unsigned char B( void ) const { return rgba[ 2 ]; }
215 
216  /** \return The Alpha component of this color */
217  unsigned char A( void ) const { return rgba[ 3 ]; }
218 
219  /** Set the Red component of this color
220  * \param val The new value for the component
221  */
222  void R( const unsigned char val ) { rgba[ 0 ] = val; }
223 
224  /** Set the Green component of this color
225  * \param val The new value for the component
226  */
227  void G( const unsigned char val ) { rgba[ 1 ] = val; }
228 
229  /** Set the Blue component of this color
230  * \param val The new value for the component
231  */
232  void B( const unsigned char val ) { rgba[ 2 ] = val; }
233 
234  /** Set the Alpha component of this color
235  * \param val The new value for the component
236  */
237  void A( const unsigned char val ) { rgba[ 3 ] = val; }
238 
239  /** Determines if the color is identical to the supplied color
240  \param arg color to compare to
241  \return TRUE if equal, else FALSE
242  */
243  bool operator==( const GlsColor& arg ) const
244  {
245  return 0 == memcmp( rgba, arg.rgba, sizeof( unsigned char ) * 4 );
246  }
247 
248  /** Determines if the color is NOT identical to the supplied color
249  \param arg color to compare to
250  \return TRUE if NOT equal, else FALSE
251  */
252  bool operator!=( const GlsColor& arg ) const
253  {
254  return 0 != memcmp( rgba, arg.rgba, sizeof( unsigned char ) * 4 );
255  }
256 
257  /** Copy another color into this one */
258  GlsColor& operator=( const GlsColor& color )
259  {
260  if( &color != this )
261  {
262  memcpy( rgba, color.rgba, sizeof( unsigned char ) * 4 );
263  }
264  return *this;
265  }
266 };
267 
268 DISTI_EXPORT std::ostream& operator<<( std::ostream& outstr, const GlsColor& color );
269 DISTI_EXPORT std::istream& operator>>( std::istream& instr, GlsColor& color );
270 
271 /// Alias for backwards compatibility
273 
274 } // namespace disti
275 
276 #endif
The DistiUnhideGlobalsDummyClass class.
GlsColor glsColor
Alias for backwards compatibility.
Definition: gls_color.h:272
void RGBA(const unsigned char newColor[])
Definition: gls_color.h:179
bool operator==(const GlsColor &arg) const
Definition: gls_color.h:243
unsigned char R(void) const
Definition: gls_color.h:208
void RGBA(unsigned long rgbaVal)
Definition: gls_color.h:187
void SetRGBA4d(double r, double g, double b, double a=1)
Definition: gls_color.h:140
unsigned char B(void) const
Definition: gls_color.h:214
void GetRGBA4f(float &r, float &g, float &b, float &a) const
Definition: gls_color.h:126
void R(const unsigned char val)
Definition: gls_color.h:222
A file for all GL Studio files to include.
GlsColor(const GlsColor &color)
Definition: gls_color.h:101
GlsColor(const unsigned char newColor[])
Definition: gls_color.h:81
GlsColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition: gls_color.h:70
GlsColor(unsigned long rgbaVal)
Definition: gls_color.h:92
void B(const unsigned char val)
Definition: gls_color.h:232
void GetRGBA4d(double &r, double &g, double &b, double &a) const
Definition: gls_color.h:154
unsigned char G(void) const
Definition: gls_color.h:211
Definition: gls_color.h:53
void SetRGBA4f(float r, float g, float b, float a=1)
Definition: gls_color.h:112
unsigned long RGBA_ULong(void) const
Definition: gls_color.h:202
GlsColor(void)
Definition: gls_color.h:59
unsigned char * RGBA(void)
Definition: gls_color.h:198
GlsColor & operator=(const GlsColor &color)
Definition: gls_color.h:258
unsigned char A(void) const
Definition: gls_color.h:217
void A(const unsigned char val)
Definition: gls_color.h:237
bool operator!=(const GlsColor &arg) const
Definition: gls_color.h:252
Definition: bmpimage.h:46
void RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition: gls_color.h:168
void G(const unsigned char val)
Definition: gls_color.h:227