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 "../core/utils/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  /** NameFormat enum.
62  * Possible ways for color name format.
63  */
65  {
66  HexRgb, /**< enum value, format #RRGGBB */
67  HexArgb /**< enum value, format #AARRGGBB */
68  };
69 
70  /** @name Initializer Methods
71  * Methods related to instantiation and destruction.
72  */
73  //@{
74 
75  /*! \brief Default constructor of a new color. */
76  RGBAColor();
77 
78  /*!
79  \brief It initializes a new color with the given value.
80 
81  \param rgba The color encoded as an integer.
82  */
83  RGBAColor(int rgba);
84 
85  /*!
86  \brief It initializes a new color.
87 
88  \param r Red component (range from 0 to 255).
89  \param g Green component (range from 0 to 255).
90  \param b Blue component (range from 0 to 255).
91  \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).
92  */
93  RGBAColor(int r, int g, int b, int a);
94 
95  /*!
96  \brief It initializes a new color from a given hex RGB-encoded string.
97 
98  \param hexColor The color value is RGB-encoded using
99  two hexadecimal digits per primary-color
100  component, in the order Red, Green, Blue,
101  prefixed with a hash (#) sign.
102  The hexadecimal digits between
103  A and F may be in uppercase
104  or lowercase.
105  */
106  RGBAColor(const std::string& hexColor);
107 
108  /*! \brief Destructor */
109  ~RGBAColor();
110 
111  //@}
112 
113  /** @name Accessor methods
114  * Methods used to get or set properties.
115  */
116  //@{
117 
118  /*!
119  \brief Assigns a copy of a color to this color, and returns a reference to it.
120 
121  \param color The rgba color to be copied.
122 
123  \return Returns a reference to it.
124  */
126 
127  /*!
128  \brief Assigns a copy of a color to this color.
129 
130  \param rgba the 32-bit RGBA (Red-Green-Blue-Alpha channel) color to be copied.
131 
132  \return Returns a reference to it.
133  */
134  te::color::RGBAColor& operator=(const int& rgba);
135 
136  /*!
137  \brief It compares the colors are iqual.
138 
139  \param color The rgba color to be compared.
140 
141  \return Returns true if this color has the same RGB and alpha values.
142  */
143  bool operator==(const RGBAColor& color) const;
144 
145  /*!
146  \brief It compares the colors are different.
147 
148  \param color The rgba color to be compared.
149 
150  \return Returns true if this color has not the same RGB and alpha values.
151  */
152  bool operator!=(const RGBAColor& color) const;
153 
154  /*!
155  \brief It returns the red component color value (a value from 0 to 255).
156 
157  \return The red component color value (a value from 0 to 255).
158  */
159  int getRed() const;
160 
161  /*!
162  \brief It returns the green component color value (a value from 0 to 255).
163 
164  \return The green component color value (a value from 0 to 255).
165  */
166  int getGreen() const;
167 
168  /*!
169  \brief It returns the blue component color value (a value from 0 to 255).
170 
171  \return The blue component color value (a value from 0 to 255).
172  */
173  int getBlue() const;
174 
175  /*!
176  \brief It returns the alpha component color value (a value from 0 to 255).
177 
178  \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).
179  */
180  int getAlpha() const;
181 
182  /*!
183  \brief It gets the color value.
184 
185  \param r Red component.
186  \param g Green component.
187  \param b Blue component.
188  \param a Alpha component.
189  */
190  void getRgba(int* r, int* g, int* b, int* a=0) const;
191 
192  /*!
193  \brief It returns the color value (0xAARRGGBB).
194 
195  \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.
196  */
197  int getRgba() const;
198 
199  /*!
200  \brief It sets the color using a two hexadecimal RGB-encoded color.
201 
202  \param hexColor The color value is RGB-encoded using
203  two hexadecimal digits per primary-color
204  component, in the order Red, Green, Blue,
205  prefixed with a hash (#) sign.
206  The hexadecimal digits between
207  A and F may be in either uppercase
208  or lowercase.
209  */
210  void setColor(const std::string& hexColor);
211 
212  /*!
213  \brief It gets the color encoded using two hexadecimal digits per primary-color component, in the order Red, Green, Blue, if HexRgb format, or
214  Alpha, Red, Green, Blue, if HexArgb format, prefixed with a hash (#) sign.
215 
216  \param format Formar of the string return.
217 
218  \return A string that represents the color encoded using two hexadecimal digits per primary-color component. e.g. #FF0000
219 
220  \note The hexadecimal digits between A and F will be uppercase.
221  */
222  std::string getColor(const NameFormat format = HexRgb) const;
223 
224  /*!
225  \brief It sets the color.
226 
227  \param r Red component (range from 0 to 255).
228  \param g Green component (range from 0 to 255).
229  \param b Blue component (range from 0 to 255).
230  \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).
231  */
232  void setColor(int r, int g, int b, int a=255);
233 
234  /*!
235  \brief It sets the color.
236 
237  \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.
238  */
239  void setColor(int rgba);
240 
241  /*!
242  \brief It returns the internal representation as a 32-bits RGBA color.
243 
244  \return The internal representation as a 32-bit RGBA color.
245  */
246  //int value() const;
247 
248  //@}
249 
250  private:
251 
252  int m_rgba; //!< Internal color, stored as a 32-bit.
253  };
254 
256  : m_rgba(0)
257  {
258  }
259 
260  inline RGBAColor::RGBAColor(int rgba)
261  : m_rgba(rgba)
262  {
263  }
264 
265  inline RGBAColor::RGBAColor(int r, int g, int b, int a)
266  : m_rgba((a << 24) + (r << 16) + (g << 8) + b)
267  {
268  assert(r >= 0 && r <= 255);
269  assert(g >= 0 && g <= 255);
270  assert(b >= 0 && b <= 255);
271  assert(a >= 0 && a <= 255);
272  }
273 
274  inline RGBAColor::RGBAColor(const std::string& hexColor)
275  {
276  setColor(hexColor);
277  }
278 
280  {
281  }
282 
284  {
285  m_rgba = color.m_rgba;
286  return *this;
287  }
288 
290  {
291  m_rgba = rgba;
292  return *this;
293  }
294 
295  inline bool RGBAColor::operator==(const RGBAColor& color) const
296  {
297  if(m_rgba == color.m_rgba)
298  return true;
299  return false;
300  }
301 
302  inline bool RGBAColor::operator!=(const RGBAColor& color) const
303  {
304  return !operator==(color);
305  }
306 
307  inline int RGBAColor::getRed() const
308  {
309  return (((m_rgba) & 0xFF0000) >> 16);
310  }
311 
312  inline int RGBAColor::getGreen() const
313  {
314  return (((m_rgba) & 0x00FF00) >> 8);
315  }
316 
317  inline int RGBAColor::getBlue() const
318  {
319  return ((m_rgba) & 0x0000FF);
320  }
321 
322  inline int RGBAColor::getAlpha() const
323  {
324  return (((m_rgba) & 0xFF000000) >> 24);
325  }
326 
327  inline void RGBAColor::getRgba(int* r, int* g, int* b, int* a) const
328  {
329  if(a)
330  *a = (m_rgba & 0xFF000000) >> 24;
331  *r = (m_rgba & 0xFF0000) >> 16;
332  *g = (m_rgba & 0xFF00) >> 8;
333  *b = m_rgba & 0xFF;
334  }
335 
336  inline int RGBAColor::getRgba() const
337  {
338  return m_rgba;
339  }
340 
341  inline void RGBAColor::setColor(const std::string& hexColor)
342  {
343  assert((hexColor.size() >= 7) && (hexColor.size() <= 9));
344  assert(hexColor[0] == '#');
345 
346  int i = 0;
347  int a = TE_OPAQUE;
348 
349  if (hexColor.size() == 9)
350  {
351  unsigned char highA = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
352  unsigned char lowA = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
353  a = int((highA << 4) + lowA);
354  }
355 
356  unsigned char highR = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
357  unsigned char lowR = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
358  int r = int((highR << 4) + lowR);
359 
360  unsigned char highG = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
361  unsigned char lowG = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
362  int g = int((highG << 4) + lowG);
363 
364  unsigned char highB = te::core::GetDecimalFromHexNotCS(hexColor[++i]);
365  unsigned char lowB = te::core::GetDecimalFromHexNotCS(hexColor[i]);
366  int b = int((highB << 4) + lowB);
367 
368  setColor(r, g, b, a);
369  }
370 
371  inline std::string RGBAColor::getColor(const NameFormat format) const
372  {
373  if (format == HexRgb)
374  {
375  char buffer[8];
376  sprintf(buffer, "#%02X%02X%02X", getRed(), getGreen(), getBlue());
377  std::string result(buffer);
378  return result;
379  }
380  else
381  {
382  char buffer[10];
383  sprintf(buffer, "#%02X%02X%02X%02X", getAlpha(), getRed(), getGreen(), getBlue());
384  std::string result(buffer);
385  return result;
386  }
387  }
388 
389  inline void RGBAColor::setColor(int r, int g, int b, int a)
390  {
391  m_rgba = (a << 24) + (r << 16) + (g << 8) + b;
392  }
393 
394  inline void RGBAColor::setColor(int rgba)
395  {
396  m_rgba = rgba;
397  }
398 
399  } // end namespace color
400 } // end namespace te
401 
402 #endif // __TERRALIB_COLOR_INTERNAL_RGBACOLOR_H
403 
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
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:341
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:307
~RGBAColor()
Destructor.
Definition: RGBAColor.h:279
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:317
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:312
Configuration flags for the Color module of TerraLib.
enum value, format #RRGGBB
Definition: RGBAColor.h:66
RGBAColor()
Default constructor of a new color.
Definition: RGBAColor.h:255
enum value, format #AARRGGBB
Definition: RGBAColor.h:67
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:283
#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.
NameFormat
NameFormat enum.
Definition: RGBAColor.h:64
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:322
bool operator!=(const RGBAColor &color) const
It compares the colors are different.
Definition: RGBAColor.h:302
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:336
bool operator==(const RGBAColor &color) const
It compares the colors are iqual.
Definition: RGBAColor.h:295
int m_rgba
It returns the internal representation as a 32-bits RGBA color.
Definition: RGBAColor.h:252
std::string getColor(const NameFormat format=HexRgb) const
It gets the color encoded using two hexadecimal digits per primary-color component, in the order Red, Green, Blue, if HexRgb format, or Alpha, Red, Green, Blue, if HexArgb format, prefixed with a hash (#) sign.
Definition: RGBAColor.h:371