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