Code_TYMPAN  4.4.0
Industrial site acoustic simulation
color.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2014> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #ifndef TY_COLOR
17 #define TY_COLOR
18 
19 #include <vector>
20 #include <cmath>
21 #include <algorithm>
22 #include "Tympan/core/smartptr.h"
23 
24 #undef min
25 #undef max
26 
30 class OColor
31 {
32 public:
33  float r, g, b, a;
34 
35 public:
39  OColor();
40  OColor(const OColor& c);
41  OColor(float* c);
42  OColor(float red, float green, float blue, float alpha = 1.0f);
43 
47  void operator=(const OColor& c);
48 
52  operator float*();
53  operator const float*() const;
54 
58  unsigned int getAsRGBA() const;
59 
63  void setAsRGBA(const unsigned int val);
64 
68  void getAsHSB(float& hue, float& saturation, float& brightness) const;
69 
73  void setAsHSB(const float hue, const float saturation, const float brightness);
74 
78  void clamp();
79  OColor clampCopy() const;
80 
81  bool operator==(const OColor& other) const;
82 
86  static const OColor BLACK;
87  static const OColor BLUE;
88  static const OColor GREEN;
89  static const OColor CYAN;
90  static const OColor RED;
91  static const OColor MAGENTA;
92  static const OColor YELLOW;
93  static const OColor WHITE;
94 
95 protected:
96  static const float equality_threshold;
97 
98  static bool float_eq(float a, float b, float epsilon = equality_threshold)
99  {
100  return (std::fabs(a - b) <= epsilon * std::max(std::fabs(a), std::fabs(b)));
101  }
102 };
103 
105 typedef std::vector<OColor> OLookupTable;
106 
107 inline OColor::OColor() : r(1.0f), g(1.0f), b(1.0f), a(1.0f) {}
108 
109 inline OColor::OColor(const OColor& c) : r(c.r), g(c.g), b(c.b), a(c.a) {}
110 
111 inline OColor::OColor(float red, float green, float blue, float alpha) : r(red), g(green), b(blue), a(alpha)
112 {
113 }
114 
115 inline OColor::OColor(float* c)
116 {
117  r = c[0];
118  g = c[1];
119  b = c[2];
120  a = c[3];
121 }
122 
123 inline void OColor::operator=(const OColor& c)
124 {
125  r = c.r;
126  g = c.g;
127  b = c.b;
128  a = c.a;
129 }
130 
131 inline OColor::operator float*()
132 {
133  return &r;
134 }
135 
136 inline OColor::operator const float*() const
137 {
138  return &r;
139 }
140 
146 {
147 public:
149  static void getSCurveColorTable(const unsigned int& nbColors, const float* hueRange,
150  const float* saturationRange, const float* valueRange,
151  OLookupTable& outColors);
152 
154  static void getLinearColorTable(const unsigned int& nbColors, const float* hueRange,
155  const float* saturationRange, const float* valueRange,
156  OLookupTable& outColors);
157 
159  static void getSqrtColorTable(const unsigned int& nbColors, const float* hueRange,
160  const float* saturationRange, const float* valueRange,
161  OLookupTable& outColors);
162 
164  static void getGaussColorTable(const unsigned int& nbColors, const float& alpha1, const float& alpha2,
165  OLookupTable& outColors);
166 };
167 
168 #endif // TY_COLOR
NxReal c
Definition: NxVec3.cpp:317
Definition: color.h:31
static const OColor GREEN
Definition: color.h:88
unsigned int getAsRGBA() const
Definition: color.cpp:44
static const OColor BLUE
Definition: color.h:87
float b
Definition: color.h:33
OColor clampCopy() const
Definition: color.cpp:276
void operator=(const OColor &c)
Definition: color.h:123
static const OColor RED
Definition: color.h:90
static const OColor BLACK
Definition: color.h:86
float a
Definition: color.h:33
static const OColor WHITE
Definition: color.h:93
void getAsHSB(float &hue, float &saturation, float &brightness) const
Definition: color.cpp:91
float r
Definition: color.h:33
void clamp()
Definition: color.cpp:233
static const float equality_threshold
Definition: color.h:96
static const OColor YELLOW
Definition: color.h:92
float g
Definition: color.h:33
void setAsHSB(const float hue, const float saturation, const float brightness)
Definition: color.cpp:141
static bool float_eq(float a, float b, float epsilon=equality_threshold)
Definition: color.h:98
void setAsRGBA(const unsigned int val)
Definition: color.cpp:71
static const OColor MAGENTA
Definition: color.h:91
OColor()
Definition: color.h:107
bool operator==(const OColor &other) const
Definition: color.cpp:39
static const OColor CYAN
Definition: color.h:89
static void getLinearColorTable(const unsigned int &nbColors, const float *hueRange, const float *saturationRange, const float *valueRange, OLookupTable &outColors)
Generation de la table de couleurs basee sur une rampe lineaire ( y = x+0.5/255 )
Definition: color.cpp:308
static void getGaussColorTable(const unsigned int &nbColors, const float &alpha1, const float &alpha2, OLookupTable &outColors)
Generation de la table de couleurs a partir d'une repartition gaussienne en R, G, B.
Definition: color.cpp:359
static void getSqrtColorTable(const unsigned int &nbColors, const float *hueRange, const float *saturationRange, const float *valueRange, OLookupTable &outColors)
Generation de la table de couleurs basee sur une rampe sqrt ( y = sqrt(x)+0.5/255 )
Definition: color.cpp:334
static void getSCurveColorTable(const unsigned int &nbColors, const float *hueRange, const float *saturationRange, const float *valueRange, OLookupTable &outColors)
Generation de la table de couleurs basee sur une rampe S-Curve ( y = (1-cos(pi*x))/2 )
Definition: color.cpp:283
std::vector< OColor > OLookupTable
Collection de OColor.
Definition: color.h:105