GL Studio C++ Runtime API
gls_color.h
Go to the documentation of this file.
1/// \file gls_color.h
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/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<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#ifndef INCLUDED_DISTI_GLS_COLOR_H
40#define INCLUDED_DISTI_GLS_COLOR_H
41
42#include "disti_include.h"
43#include "unhide_globals.h"
44#include <iosfwd> // Forward references for the stream operators
45#include <string.h> // For memcmp
46
47namespace disti
48{
49/** \class GlsColor
50 The Color class: Implements a 4 component RGBA color.
51*/
53{
54 unsigned char rgba[ 4 ]; /** Red, Green, Blue, Alpha 0-255 range */
55
56public:
57 /** default constructor. Intializes rgba data to zero */
59 {
60 rgba[ 0 ] = rgba[ 1 ] = rgba[ 2 ] = rgba[ 3 ] = 0;
61 }
62
63 /** 4ub constructor. Intializes using rgba values
64 * \param r Red value, range 0-255
65 * \param g Green value, range 0-255
66 * \param b Blue value, range 0-255
67 * \param a Alpha value, range 0-255
68 */
69 GlsColor( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 )
70 {
71 rgba[ 0 ] = r;
72 rgba[ 1 ] = g;
73 rgba[ 2 ] = b;
74 rgba[ 3 ] = a;
75 }
76
77 /** 4ubv constructor. Intializes using rgba value
78 * \param newColor 4 character RGBA array, range 0-255
79 */
80 GlsColor( const unsigned char newColor[] )
81 {
82 rgba[ 0 ] = newColor[ 0 ];
83 rgba[ 1 ] = newColor[ 1 ];
84 rgba[ 2 ] = newColor[ 2 ];
85 rgba[ 3 ] = newColor[ 3 ];
86 }
87
88 /** 4ul constructor.
89 * \param rgbaVal Unsigned long containing color in the form RRGGBBAA (8 bits per component
90 */
91 GlsColor( unsigned long rgbaVal )
92 {
93 rgba[ 0 ] = (unsigned char)( ( rgbaVal & 0xFF000000 ) >> 24 );
94 rgba[ 1 ] = (unsigned char)( ( rgbaVal & 0x00FF0000 ) >> 16 );
95 rgba[ 2 ] = (unsigned char)( ( rgbaVal & 0x0000FF00 ) >> 8 );
96 rgba[ 3 ] = (unsigned char)( ( rgbaVal & 0x000000FF ) );
97 }
98
99 /** Sets RGBA value using floats
100 * \param r Red value, range 0-1.0
101 * \param g Green value, range 0-1.0
102 * \param b Blue value, range 0-1.0
103 * \param a Alpha value, range 0-1.0
104 */
105 void SetRGBA4f( float r, float g, float b, float a = 1 )
106 {
107 rgba[ 0 ] = (unsigned char)( r * 255.0f );
108 rgba[ 1 ] = (unsigned char)( g * 255.0f );
109 rgba[ 2 ] = (unsigned char)( b * 255.0f );
110 rgba[ 3 ] = (unsigned char)( a * 255.0f );
111 }
112
113 /** Gets RGBA value using floats
114 * \param r Red value, range 0-1.0
115 * \param g Green value, range 0-1.0
116 * \param b Blue value, range 0-1.0
117 * \param a Alpha value, range 0-1.0
118 */
119 void GetRGBA4f( float& r, float& g, float& b, float& a ) const
120 {
121 r = (float)rgba[ 0 ] / 255.0f;
122 g = (float)rgba[ 1 ] / 255.0f;
123 b = (float)rgba[ 2 ] / 255.0f;
124 a = (float)rgba[ 3 ] / 255.0f;
125 }
126
127 /** Sets RGBA value using doubles
128 * \param r Red value, range 0-1.0
129 * \param g Green value, range 0-1.0
130 * \param b Blue value, range 0-1.0
131 * \param a Alpha value, range 0-1.0
132 */
133 void SetRGBA4d( double r, double g, double b, double a = 1 )
134 {
135 rgba[ 0 ] = (unsigned char)( r * 255.0 );
136 rgba[ 1 ] = (unsigned char)( g * 255.0 );
137 rgba[ 2 ] = (unsigned char)( b * 255.0 );
138 rgba[ 3 ] = (unsigned char)( a * 255.0 );
139 }
140
141 /** Gets RGBA value using doubles
142 * \param r Red value, range 0-1.0
143 * \param g Green value, range 0-1.0
144 * \param b Blue value, range 0-1.0
145 * \param a Alpha value, range 0-1.0
146 */
147 void GetRGBA4d( double& r, double& g, double& b, double& a ) const
148 {
149 r = (double)rgba[ 0 ] / 255.0;
150 g = (double)rgba[ 1 ] / 255.0;
151 b = (double)rgba[ 2 ] / 255.0;
152 a = (double)rgba[ 3 ] / 255.0;
153 }
154
155 /// Gets the complementary color by inverting RGB values.
156 /// \return The complementary GlsColor to this.
158 {
159 return GlsColor(
160 255 - R(),
161 255 - G(),
162 255 - B(),
163 A() ); // Use the same opacity
164 }
165
166 /** Sets RGBA value
167 * \param r Red value, range 0-255
168 * \param g Green value, range 0-255
169 * \param b Blue value, range 0-255
170 * \param a Alpha value, range 0-255
171 */
172 void RGBA( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 )
173 {
174 rgba[ 0 ] = r;
175 rgba[ 1 ] = g;
176 rgba[ 2 ] = b;
177 rgba[ 3 ] = a;
178 }
179
180 /** Sets RGBA value
181 * \param newColor 4 character RGBA array, range 0-255
182 */
183 void RGBA( const unsigned char newColor[] )
184 {
185 memcpy( rgba, newColor, sizeof( unsigned char ) * 4 );
186 }
187
188 /** Sets RGBA value
189 * \param rgbaVal Unsigned long containing color in the form RRGGBBAA (8 bits per component
190 */
191 void RGBA( unsigned long rgbaVal )
192 {
193 rgba[ 0 ] = (unsigned char)( ( rgbaVal & 0xFF000000 ) >> 24 );
194 rgba[ 1 ] = (unsigned char)( ( rgbaVal & 0x00FF0000 ) >> 16 );
195 rgba[ 2 ] = (unsigned char)( ( rgbaVal & 0x0000FF00 ) >> 8 );
196 rgba[ 3 ] = (unsigned char)( ( rgbaVal & 0x000000FF ) );
197 }
198
199 /// Gets RGBA value
200 /// \return A pointer to array of 4 unsigned chars containing rgba (0-255 range).
201 unsigned char* RGBA() { return rgba; }
202
203 /// Gets RGBA value
204 /// \return A const pointer to array of 4 unsigned chars containing rgba (0-255 range).
205 const unsigned char* RGBA() const { return rgba; }
206
207 /** \return RGBA value packed into a 32 bit unsigned int in host byte order */
208 unsigned long RGBA_ULong() const
209 {
210 return ( rgba[ 0 ] << 24 ) | ( rgba[ 1 ] << 16 ) | ( rgba[ 2 ] << 8 ) | rgba[ 3 ];
211 }
212
213 /** \return The Red component of this color */
214 unsigned char R() const { return rgba[ 0 ]; }
215
216 /** \return The Green component of this color */
217 unsigned char G() const { return rgba[ 1 ]; }
218
219 /** \return The Blue component of this color */
220 unsigned char B() const { return rgba[ 2 ]; }
221
222 /** \return The Alpha component of this color */
223 unsigned char A() const { return rgba[ 3 ]; }
224
225 /** Set the Red component of this color
226 * \param val The new value for the component
227 */
228 void R( const unsigned char val ) { rgba[ 0 ] = val; }
229
230 /** Set the Green component of this color
231 * \param val The new value for the component
232 */
233 void G( const unsigned char val ) { rgba[ 1 ] = val; }
234
235 /** Set the Blue component of this color
236 * \param val The new value for the component
237 */
238 void B( const unsigned char val ) { rgba[ 2 ] = val; }
239
240 /** Set the Alpha component of this color
241 * \param val The new value for the component
242 */
243 void A( const unsigned char val ) { rgba[ 3 ] = val; }
244
245 /** Determines if the color is identical to the supplied color
246 \param arg color to compare to
247 \return TRUE if equal, else FALSE
248 */
249 bool operator==( const GlsColor& arg ) const
250 {
251 return 0 == memcmp( rgba, arg.rgba, sizeof( unsigned char ) * 4 );
252 }
253
254 /** Determines if the color is NOT identical to the supplied color
255 \param arg color to compare to
256 \return TRUE if NOT equal, else FALSE
257 */
258 bool operator!=( const GlsColor& arg ) const
259 {
260 return 0 != memcmp( rgba, arg.rgba, sizeof( unsigned char ) * 4 );
261 }
262};
263
264/// Stream out operator
265/// \param outstr The stream to write to.
266/// \param color The color to write to the stream.
267/// \return The stream in its current state.
268DISTI_EXPORT std::ostream& operator<<( std::ostream& outstr, const GlsColor& color );
269
270/// Stream in operator
271/// \param instr The stream to read from.
272/// \param color The returned color to read from the stream.
273/// \return The stream in its current state.
274DISTI_EXPORT std::istream& operator>>( std::istream& instr, GlsColor& color );
275
276/// Alias for backwards compatibility
278
279} // namespace disti
280
281#endif
Definition: gls_color.h:53
GlsColor GetComplementary() const
Definition: gls_color.h:157
void A(const unsigned char val)
Definition: gls_color.h:243
unsigned char G() const
Definition: gls_color.h:217
void GetRGBA4f(float &r, float &g, float &b, float &a) const
Definition: gls_color.h:119
void R(const unsigned char val)
Definition: gls_color.h:228
void B(const unsigned char val)
Definition: gls_color.h:238
void RGBA(const unsigned char newColor[])
Definition: gls_color.h:183
void SetRGBA4f(float r, float g, float b, float a=1)
Definition: gls_color.h:105
unsigned char R() const
Definition: gls_color.h:214
void RGBA(unsigned long rgbaVal)
Definition: gls_color.h:191
void SetRGBA4d(double r, double g, double b, double a=1)
Definition: gls_color.h:133
bool operator==(const GlsColor &arg) const
Definition: gls_color.h:249
GlsColor()
Definition: gls_color.h:58
void G(const unsigned char val)
Definition: gls_color.h:233
GlsColor(unsigned long rgbaVal)
Definition: gls_color.h:91
bool operator!=(const GlsColor &arg) const
Definition: gls_color.h:258
void GetRGBA4d(double &r, double &g, double &b, double &a) const
Definition: gls_color.h:147
unsigned char B() const
Definition: gls_color.h:220
unsigned long RGBA_ULong() const
Definition: gls_color.h:208
const unsigned char * RGBA() const
Definition: gls_color.h:205
GlsColor(const unsigned char newColor[])
Definition: gls_color.h:80
GlsColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition: gls_color.h:69
unsigned char A() const
Definition: gls_color.h:223
unsigned char * RGBA()
Definition: gls_color.h:201
void RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition: gls_color.h:172
A file for all GL Studio files to include.
Definition: bmpimage.h:46
std::ostream & operator<<(std::ostream &outstr, const AttributeName &name)
GlsColor glsColor
Alias for backwards compatibility.
Definition: gls_color.h:277
std::istream & operator>>(std::istream &instr, GlsColor &color)
The DistiUnhideGlobalsDummyClass class.