RGBAColor.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/color/RGBAColor.h
22 
23  \brief A helper class for 24-bit RGBA (Red-Green-Blue-Alpha channel) color.
24 */
25 
26 #ifndef __TERRALIB_COLOR_INTERNAL_RGBACOLOR_H
27 #define __TERRALIB_COLOR_INTERNAL_RGBACOLOR_H
28 
29 // TerraLib
30 #include "../common/HexUtils.h"
31 #include "Config.h"
32 
33 // STL
34 #include <cassert>
35 #include <string>
36 #include <cstdio>
37 
38 namespace te
39 {
40  namespace color
41  {
42  /*!
43  \class RGBAColor
44 
45  \brief A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
46 
47  Internally the color is represented by an integer of 32-bits as the following:
48  <ul>
49  <li>bits 0-7: blue</li>
50  <li>bits 8-15: green</li>
51  <li>bits 16-23: red</li>
52  <li>bits 24-31: alpha. See the macro TE_OPAQUE and TE_TRANSPARENT for the values of totally transparent and totally opaque.</li>
53  </ul>
54 
55  \ingroup color
56  */
57  class RGBAColor
58  {
59  public:
60 
61  /** @name Initializer Methods
62  * Methods related to instantiation and destruction.
63  */
64  //@{
65 
66  /*! \brief Default constructor of a new color. */
67  RGBAColor();
68 
69  /*!
70  \brief It initializes a new color with the given value.
71 
72  \param rgba The color encoded as an integer.
73  */
74  RGBAColor(int rgba);
75 
76  /*!
77  \brief It initializes a new color.
78 
79  \param r Red component (range from 0 to 255).
80  \param g Green component (range from 0 to 255).
81  \param b Blue component (range from 0 to 255).
82  \param a Alpha component. A range from 0 to 255 (see the macro TE_OPAQUE and TE_TRANSPARENT for the values of totally transparent and totally opaque).
83  */
84  RGBAColor(int r, int g, int b, int a);
85 
86  /*!
87  \brief It initializes a new color from a given hex RGB-encoded string.
88 
89  \param hexColor The color value is RGB-encoded using
90  two hexadecimal digits per primary-color
91  component, in the order Red, Green, Blue,
92  prefixed with a hash (#) sign.
93  The hexadecimal digits between
94  A and F may be in uppercase
95  or lowercase.
96  */
97  RGBAColor(const std::string& hexColor);
98 
99  /*! \brief Destructor */
100  ~RGBAColor();
101 
102  //@}
103 
104  /** @name Accessor methods
105  * Methods used to get or set properties.
106  */
107  //@{
108 
109  /*!
110  \brief Assigns a copy of a color to this color, and returns a reference to it.
111 
112  \param color The rgba color to be copied.
113 
114  \return Returns a reference to it.
115  */
117 
118  /*!
119  \brief Assigns a copy of a color to this color.
120 
121  \param rgba the 32-bit RGBA (Red-Green-Blue-Alpha channel) color to be copied.
122 
123  \return Returns a reference to it.
124  */
125  te::color::RGBAColor& operator=(const int& rgba);
126 
127  /*!
128  \brief It compares the colors are iqual.
129 
130  \param color The rgba color to be compared.
131 
132  \return Returns true if this color has the same RGB and alpha values.
133  */
134  bool operator==(const RGBAColor& color) const;
135 
136  /*!
137  \brief It compares the colors are different.
138 
139  \param color The rgba color to be compared.
140 
141  \return Returns true if this color has not the same RGB and alpha values.
142  */
143  bool operator!=(const RGBAColor& color) const;
144 
145  /*!
146  \brief It returns the red component color value (a value from 0 to 255).
147 
148  \return The red component color value (a value from 0 to 255).
149  */
150  int getRed() const;
151 
152  /*!
153  \brief It returns the green component color value (a value from 0 to 255).
154 
155  \return The green component color value (a value from 0 to 255).
156  */
157  int getGreen() const;
158 
159  /*!
160  \brief It returns the blue component color value (a value from 0 to 255).
161 
162  \return The blue component color value (a value from 0 to 255).
163  */
164  int getBlue() const;
165 
166  /*!
167  \brief It returns the alpha component color value (a value from 0 to 255).
168 
169  \return The alpha component color value. It is a value from 0 to 255 (see the macro TE_OPAQUE and TE_TRANSPARENT for the values of totally transparent and totally opaque).
170  */
171  int getAlpha() const;
172 
173  /*!
174  \brief It gets the color value.
175 
176  \param r Red component.
177  \param g Green component.
178  \param b Blue component.
179  \param a Alpha component.
180  */
181  void getRgba(int* r, int* g, int* b, int* a=0) const;
182 
183  /*!
184  \brief It returns the color value (0xAARRGGBB).
185 
186  \return It returns a number of 4 bytes. The values are on the following order: alpha, red, green and blue. The alpha is the most significant byte.
187  */
188  int getRgba() const;
189 
190  /*!
191  \brief It sets the color using a two hexadecimal RGB-encoded color.
192 
193  \param hexColor The color value is RGB-encoded using
194  two hexadecimal digits per primary-color
195  component, in the order Red, Green, Blue,
196  prefixed with a hash (#) sign.
197  The hexadecimal digits between
198  A and F may be in either uppercase
199  or lowercase.
200  */
201  void setColor(const std::string& hexColor);
202 
203  /*!
204  \brief It gets the color encoded using two hexadecimal digits per primary-color component, in the order Red, Green, Blue, prefixed with a hash (#) sign.
205 
206  \return A string that represents the color encoded using two hexadecimal digits per primary-color component. e.g. #FF0000
207 
208  \note The hexadecimal digits between A and F will be uppercase.
209  */
210  std::string getColor() const;
211 
212  /*!
213  \brief It sets the color.
214 
215  \param r Red component (range from 0 to 255).
216  \param g Green component (range from 0 to 255).
217  \param b Blue component (range from 0 to 255).
218  \param a Alpha component. A range from 0 to 255 (see the macro TE_OPAQUE and TE_TRANSPARENT for the values of totally transparent and totally opaque).
219  */
220  void setColor(int r, int g, int b, int a=255);
221 
222  /*!
223  \brief It sets the color.
224 
225  \param rgba The color represented by a number of 4 bytes, where the values ??are in the following order: alpha, red, green and blue. The alpha is the most significant byte.
226  */
227  void setColor(int rgba);
228 
229  /*!
230  \brief It returns the internal representation as a 32-bits RGBA color.
231 
232  \return The internal representation as a 32-bit RGBA color.
233  */
234  //int value() const;
235 
236  //@}
237 
238  private:
239 
240  int m_rgba; //!< Internal color, stored as a 32-bit.
241  };
242 
244  : m_rgba(0)
245  {
246  }
247 
248  inline RGBAColor::RGBAColor(int rgba)
249  : m_rgba(rgba)
250  {
251  }
252 
253  inline RGBAColor::RGBAColor(int r, int g, int b, int a)
254  : m_rgba((a << 24) + (r << 16) + (g << 8) + b)
255  {
256  assert(r >= 0 && r <= 255);
257  assert(g >= 0 && g <= 255);
258  assert(b >= 0 && b <= 255);
259  assert(a >= 0 && a <= 255);
260  }
261 
262  inline RGBAColor::RGBAColor(const std::string& hexColor)
263  {
264  setColor(hexColor);
265  }
266 
268  {
269  }
270 
272  {
273  m_rgba = color.m_rgba;
274  return *this;
275  }
276 
278  {
279  m_rgba = rgba;
280  return *this;
281  }
282 
283  inline bool RGBAColor::operator==(const RGBAColor& color) const
284  {
285  if(m_rgba == color.m_rgba)
286  return true;
287  return false;
288  }
289 
290  inline bool RGBAColor::operator!=(const RGBAColor& color) const
291  {
292  return !operator==(color);
293  }
294 
295  inline int RGBAColor::getRed() const
296  {
297  return (((m_rgba) & 0xFF0000) >> 16);
298  }
299 
300  inline int RGBAColor::getGreen() const
301  {
302  return (((m_rgba) & 0x00FF00) >> 8);
303  }
304 
305  inline int RGBAColor::getBlue() const
306  {
307  return ((m_rgba) & 0x0000FF);
308  }
309 
310  inline int RGBAColor::getAlpha() const
311  {
312  return (((m_rgba) & 0xFF000000) >> 24);
313  }
314 
315  inline void RGBAColor::getRgba(int* r, int* g, int* b, int* a) const
316  {
317  if(a)
318  *a = (m_rgba & 0xFF000000) >> 24;
319  *r = (m_rgba & 0xFF0000) >> 16;
320  *g = (m_rgba & 0xFF00) >> 8;
321  *b = m_rgba & 0xFF;
322  }
323 
324  inline int RGBAColor::getRgba() const
325  {
326  return m_rgba;
327  }
328 
329  inline void RGBAColor::setColor(const std::string& hexColor)
330  {
331  assert(hexColor.size() == 7);
332  assert(hexColor[0] == '#');
333 
334  unsigned char highR = te::common::GetDecimalFromHexNotCS(hexColor[1]);
335  unsigned char lowR = te::common::GetDecimalFromHexNotCS(hexColor[2]);
336  int r = int((highR << 4) + lowR);
337 
338  unsigned char highG = te::common::GetDecimalFromHexNotCS(hexColor[3]);
339  unsigned char lowG = te::common::GetDecimalFromHexNotCS(hexColor[4]);
340  int g = int((highG << 4) + lowG);
341 
342  unsigned char highB = te::common::GetDecimalFromHexNotCS(hexColor[5]);
343  unsigned char lowB = te::common::GetDecimalFromHexNotCS(hexColor[6]);
344  int b = int((highB << 4) + lowB);
345 
346  setColor(r, g, b, TE_OPAQUE);
347  }
348 
349  inline std::string RGBAColor::getColor() const
350  {
351  char buffer[8];
352  sprintf(buffer, "#%02X%02X%02X", getRed(), getGreen(), getBlue());
353  std::string result(buffer);
354  return result;
355  }
356 
357  inline void RGBAColor::setColor(int r, int g, int b, int a)
358  {
359  m_rgba = (a << 24) + (r << 16) + (g << 8) + b;
360  }
361 
362  inline void RGBAColor::setColor(int rgba)
363  {
364  m_rgba = rgba;
365  }
366 
367  } // end namespace color
368 } // end namespace te
369 
370 #endif // __TERRALIB_COLOR_INTERNAL_RGBACOLOR_H
371 
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
~RGBAColor()
Destructor.
Definition: RGBAColor.h:267
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
Configuration flags for the Color module of TerraLib.
unsigned char GetDecimalFromHexNotCS(unsigned char hexValue)
It returns the decimal value in a char from a given hex value (for example: 'A' => 10...
Definition: HexUtils.h:49
RGBAColor()
Default constructor of a new color.
Definition: RGBAColor.h:243
te::color::RGBAColor & operator=(const RGBAColor &color)
Assigns a copy of a color to this color, and returns a reference to it.
Definition: RGBAColor.h:271
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:36
URI C++ Library.
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
bool operator!=(const RGBAColor &color) const
It compares the colors are different.
Definition: RGBAColor.h:290
std::string getColor() const
It gets the color encoded using two hexadecimal digits per primary-color component, in the order Red, Green, Blue, prefixed with a hash (#) sign.
Definition: RGBAColor.h:349
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
int getRgba() const
It returns the color value (0xAARRGGBB).
Definition: RGBAColor.h:324
bool operator==(const RGBAColor &color) const
It compares the colors are iqual.
Definition: RGBAColor.h:283
int m_rgba
It returns the internal representation as a 32-bits RGBA color.
Definition: RGBAColor.h:240