ColorTransform.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/ColorTransform.h
22 
23  \brief A class to transform .
24 */
25 
26 #ifndef __TERRALIB_COLOR_INTERNAL_COLOR_TRANSFORM_H
27 #define __TERRALIB_COLOR_INTERNAL_COLOR_TRANSFORM_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "RGBAColor.h"
32 
33 // STL
34 #include <cassert>
35 #include <cstdlib>
36 #include <climits>
37 #include <cmath>
38 
39 namespace te
40 {
41  namespace color
42  {
43  /*!
44  \class ColorTransform
45 
46  \brief A class to transform RGBA color to HSV, HSL and CMYK.
47 
48  \ingroup color
49  */
51  {
52  public:
53 
54  enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl };
55 
56  /** @name Initializer Methods
57  * Methods related to instantiation and destruction.
58  */
59  //@{
60 
61  /*! \brief Default constructor of a new color. */
63 
64  /*!
65  \brief It initializes a new with the given color transform.
66 
67  \param colt The color transform.
68  */
69  ColorTransform(const ColorTransform& colt);
70 
71  /*!
72  \brief It initializes a new color with the given RGBAColor.
73 
74  \param color The RGBAColor.
75  */
76  ColorTransform(const RGBAColor& color);
77 
78  /*!
79  \brief It initializes a new color with the given rgba value.
80 
81  \param rgba The rgba value.
82  */
83  ColorTransform(const 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  ColorTransform(const int r, const int g, const int b, const int a=255);
94 
95  /*! \brief Destructor */
96  ~ColorTransform();
97 
98  /*!
99  \brief Assigns a copy of a color transform and returns a reference to it.
100 
101  \param color The color transfom to be copied.
102 
103  \return Returns a reference to it.
104  */
105  te::color::ColorTransform& operator=(const ColorTransform& colt);
106 
107  /*!
108  \brief It sets the color.
109 
110  \param r Red component (range from 0 to 255).
111  \param g Green component (range from 0 to 255).
112  \param b Blue component (range from 0 to 255).
113  \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).
114  */
115  void setRgba(int r, int g, int b, int a);
116 
117  /*!
118  \brief It sets the color.
119 
120  \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.
121  */
122  void setRgba(int rgba);
123 
124  /*!
125  \brief It sets the color.
126 
127  \param cor The RGBA color.
128  */
129  void setColor(const RGBAColor& cor);
130 
131  /*!
132  \brief It gets the color value.
133 
134  \param r Red component.
135  \param g Green component.
136  \param b Blue component.
137  \param a Alpha component.
138  */
139  void getRgba(int* r, int* g, int* b, int* a=0) const;
140 
141  /*!
142  \brief It returns the color value (0xAARRGGBB).
143 
144  \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.
145  */
146  int getRgba() const;
147 
148  /*!
149  \brief It returns the RGBA color.
150 
151  \return The RGBA color.
152  */
153  te::color::RGBAColor getColor() const;
154 
155  /*!
156  \brief It returns the red component.
157 
158  \return The red component.
159  */
160  int getRed() const;
161 
162  /*!
163  \brief It returns the green component.
164 
165  \return The green component.
166  */
167  int getGreen() const;
168 
169  /*!
170  \brief It returns the blue component.
171 
172  \return The blue component.
173  */
174  int getBlue() const;
175 
176  /*!
177  \brief It returns the alpha component.
178 
179  \return The alpha component.
180  */
181  int getAlpha() const;
182 
183  /*!
184  \brief It sets the red component.
185  */
186  void setRed(int red);
187 
188  /*!
189  \brief It sets the green component.
190  */
191  void setGreen(int green);
192 
193  /*!
194  \brief It sets the blue component.
195  */
196  void setBlue(int blue);
197 
198  /*!
199  \brief It sets the alpha component.
200  */
201  void setAlpha(int alpha);
202 
203  /*!
204  \brief It returns the color tranform in Rgb spec.
205 
206  \return The color transform in Rgb spec.
207 
208  \note If spec is not RGB, it creates and returns an equivalent.
209  */
210  te::color::ColorTransform toRgb() const;
211 
212  /*!
213  \brief It returns the color tranform in Hsv spec.
214 
215  \return The color transform in Hsv spec.
216 
217  \note If spec is not HSV, it creates and returns an equivalent.
218  */
219  te::color::ColorTransform toHsv() const;
220 
221  /*!
222  \brief It returns the color tranform in Hsl spec.
223 
224  \return The color transform in Hsl spec.
225 
226  \note If spec is not HSL, it creates and returns an equivalent.
227  */
228  te::color::ColorTransform toHsl() const;
229 
230  /*!
231  \brief It returns the color tranform in Cmyk spec.
232 
233  \return The color transform in Cmyk spec.
234 
235  \note If spec is not CMYK, it creates and returns an equivalent.
236  */
237  te::color::ColorTransform toCmyk() const;
238 
239  /*!
240  \brief It returns the hue component of this color.
241 
242  \return The hue value.
243 
244  \note The color is implicitly converted to HSV.
245  */
246  int getHsvHue() const;
247 
248  /*!
249  \brief It returns the saturation component of this color.
250 
251  \return The saturation value.
252 
253  \note The color is implicitly converted to HSV.
254  */
255  int getHsvSaturation() const;
256 
257  /*!
258  \brief It returns the value component of this color.
259 
260  \return The value.
261 
262  \note The color is implicitly converted to HSV.
263  */
264  int getValue() const;
265 
266  /*!
267  \brief It gets the h, s, v components of this color.
268 
269  \param h The hue component.
270  \param s The saturation component.
271  \param v The value component.
272  \param a The alpha component.
273 
274  \note The color is implicitly converted to HSV.
275  */
276  void getHsv(int *h, int *s, int *v, int *a = 0) const;
277 
278  /*!
279  \brief It sets the h, s, v components of this color.
280 
281  \param h The hue component.
282  \param s The saturation component.
283  \param v The value component.
284  \param a The alpha component.
285 
286  \note The color is converted to HSV.
287  */
288  void setHsv(int h, int s, int v, int a = 255);
289 
290  /*!
291  \brief It gets the cyan component of this color.
292 
293  \note The color is implicitly converted to Cmyk.
294  */
295  int getCyan() const;
296 
297  /*!
298  \brief It gets the magenta component of this color.
299 
300  \note The color is implicitly converted to Cmyk.
301  */
302  int getMagenta() const;
303 
304  /*!
305  \brief It gets the yellow component of this color.
306 
307  \note The color is implicitly converted to Cmyk.
308  */
309  int getYellow() const;
310 
311  /*!
312  \brief It gets the black component of this color.
313 
314  \note The color is implicitly converted to Cmyk.
315  */
316  int getBlack() const;
317 
318  /*!
319  \brief It gets the c, m, y, k components of this color.
320 
321  \param c The cyan component.
322  \param m The magenta component.
323  \param y The yellow component.
324  \param k The black component.
325  \param a The alpha component.
326 
327  \note The color is implicitly converted to Cmyk.
328  */
329  void getCmyk(int *c, int *m, int *y, int *k, int *a = 0);
330 
331  /*!
332  \brief It sets the c, m, y, k components of this color.
333 
334  \param c The cyan component.
335  \param m The magenta component.
336  \param y The yellow component.
337  \param k The black component.
338  \param a The alpha component.
339 
340  \note The color is converted to Cmyk.
341  */
342  void setCmyk(int c, int m, int y, int k, int a = 255);
343 
344  /*!
345  \brief It returns the hue component of this color.
346 
347  \return The hue value.
348 
349  \note The color is implicitly converted to HSL.
350  */
351  int getHslHue() const;
352 
353  /*!
354  \brief It returns the saturation component of this color.
355 
356  \return The saturation value.
357 
358  \note The color is implicitly converted to HSL.
359  */
360  int getHslSaturation() const;
361 
362  /*!
363  \brief It returns the lightness component of this color.
364 
365  \return The lightness value.
366 
367  \note The color is implicitly converted to HSL.
368  */
369  int getLightness() const;
370 
371  /*!
372  \brief It gets the h, s, l components of this color.
373 
374  \param h The cyan component.
375  \param s The magenta component.
376  \param l The yellow component.
377  \param a The alpha component.
378 
379  \note The color is implicitly converted to HSL.
380  */
381  void getHsl(int *h, int *s, int *l, int *a = 0) const;
382 
383  /*!
384  \brief It sets the h, s, l components of this color.
385 
386  \param h The cyan component.
387  \param s The magenta component.
388  \param l The yellow component.
389  \param a The alpha component.
390 
391  \note The color is converted to HSL.
392  */
393  void setHsl(int h, int s, int l, int a = 255);
394 
395  /*!
396  \brief It compares the color tranforms are iqual.
397 
398  \param colt The color transform to be compared.
399 
400  \return Returns true if this color transform has the same properties.
401  */
402  bool operator==(const ColorTransform& colt) const;
403 
404  /*!
405  \brief It compares the color tranforms are different.
406 
407  \param colt The color transform to be compared.
408 
409  \return Returns true if this color transform has not the same properties.
410  */
411  bool operator!=(const ColorTransform& colt) const;
412 
413  //@}
414 
415  private:
416 
417  /*!
418  \brief Marks the color transform as invalid and sets all components to zero (alpha is set to fully opaque).
419  */
420  void invalidate();
421 
422  /*!
423  \brief It checks if the color is valid.
424 
425  \return True if valid color.
426  */
427  inline bool isValid() const;
428 
429  /*!
430  \brief It rounds the double value to int value.
431 
432  \return The int value rounded.
433  */
434  int round(double d) const;
435 
436  /*!
437  \brief It compares the double valuea are iqual.
438 
439  \param p1 First value.
440  \param p2 Second value.
441 
442  \return True if values are equal.
443  */
444  bool fuzzyCompare(double p1, double p2) const;
445 
446  /*!
447  \brief It verifies the double is null.
448 
449  \param d Value to be verified.
450 
451  \return True if value is null.
452  */
453  bool fuzzyIsNull(double d) const;
454 
455  template <typename T>
456  const T& vMin(const T &a, const T &b) const {if (a < b) return a; return b;}
457 
458 
459  private:
461 
462  union
463  {
464  struct
465  {
466  unsigned short alpha;
467  unsigned short red;
468  unsigned short green;
469  unsigned short blue;
470  unsigned short pad;
471  } argb;
472 
473  struct
474  {
475  unsigned short alpha;
476  unsigned short hue;
477  unsigned short saturation;
478  unsigned short value;
479  unsigned short pad;
480  } ahsv;
481 
482  struct
483  {
484  unsigned short alpha;
485  unsigned short cyan;
486  unsigned short magenta;
487  unsigned short yellow;
488  unsigned short black;
489  } acmyk;
490 
491  struct
492  {
493  unsigned short alpha;
494  unsigned short hue;
495  unsigned short saturation;
496  unsigned short lightness;
497  unsigned short pad;
498  } ahsl;
499 
500  unsigned short array[5];
501  } m_params;
502  };
503 
505  {
506  m_spec = Invalid;
507  m_params.argb.alpha = USHRT_MAX;
508  m_params.argb.red = 0;
509  m_params.argb.green = 0;
510  m_params.argb.blue = 0;
511  m_params.argb.pad = 0;
512  }
513 
514  inline bool ColorTransform::isValid() const
515  {
516  return m_spec != Invalid;
517  }
518 
519  inline int ColorTransform::round(double d) const
520  {
521  return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1);
522  }
523 
524  inline bool ColorTransform::fuzzyCompare(double p1, double p2) const
525  {
526  return (fabs(p1 - p2) <= 0.000000000001 * vMin(fabs(p1), fabs(p2)));
527  }
528 
529  inline bool ColorTransform::fuzzyIsNull(double d) const
530  {
531  return fabs(d) <= 0.000000000001;
532  }
533 
534  //template <typename T>
535  //inline const T& vMin(const T &a, const T &b) const
536  //{
537  // if (a < b)
538  // return a;
539  // return b;
540  //}
541 
542  } // end namespace color
543 } // end namespace te
544 
545 #endif // __TERRALIB_COLOR_INTERNAL_COLOR_TRANSFORM_H
546 
union te::color::ColorTransform::@0 m_params
bool isValid() const
It checks if the color is valid.
int round(double d) const
It rounds the double value to int value.
Configuration flags for the Color module of TerraLib.
TEDATAACCESSEXPORT te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
TEDATAACCESSEXPORT te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
bool fuzzyIsNull(double d) const
It verifies the double is null.
void invalidate()
Marks the color transform as invalid and sets all components to zero (alpha is set to fully opaque)...
A helper class for 24-bit RGBA (Red-Green-Blue-Alpha channel) color.
URI C++ Library.
bool fuzzyCompare(double p1, double p2) const
It compares the double valuea are iqual.
const T & vMin(const T &a, const T &b) const
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A class to transform RGBA color to HSV, HSL and CMYK.
#define TECOLOREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:73