Code_TYMPAN  4.4.0
Industrial site acoustic simulation
color.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 #ifndef TY_COLOR
17 #define TY_COLOR
18 
19 #include <qcolor.h>
20 #include <vector>
21 #include <cmath>
22 #include <algorithm>
23 #include "Tympan/core/smartptr.h"
24 
25 #ifdef TY_USE_IHM
26  #include <QColor>
27 #endif // TY_USE_IHM
28 
29 #undef min
30 #undef max
31 
35 class OColor
36 {
37 public:
38  float r, g, b, a;
39 
40 public:
44  OColor();
45  OColor(const OColor& c);
46  OColor(float* c);
47  OColor(float red, float green, float blue, float alpha = 1.0f);
48 
52  void operator=(const OColor& c);
53 
57  operator float*();
58  operator const float*() const;
59 #ifdef TY_USE_IHM
60  operator QColor() const;
61 #endif // TY_USE_IHM
62 
66  unsigned int getAsRGBA() const;
67 
71  void setAsRGBA(const unsigned int val);
72 
76  void getAsHSB(float& hue, float& saturation, float& brightness) const;
77 
81  void setAsHSB(const float hue, const float saturation, const float brightness);
82 
86  void clamp();
87  OColor clampCopy() const;
88 
89  bool operator==(const OColor& other) const;
90 
94  static const OColor BLACK;
95  static const OColor BLUE;
96  static const OColor GREEN;
97  static const OColor CYAN;
98  static const OColor RED;
99  static const OColor MAGENTA;
100  static const OColor YELLOW;
101  static const OColor WHITE;
102 
103 protected:
104  static const float equality_threshold;
105 
106  static bool float_eq(float a, float b, float epsilon = equality_threshold)
107  {
108  return (std::fabs(a - b) <= epsilon * std::max(std::fabs(a), std::fabs(b)));
109  }
110 };
111 
113 typedef std::vector<OColor> OLookupTable;
114 
115 inline OColor::OColor() : r(1.0f), g(1.0f), b(1.0f), a(1.0f) {}
116 
117 inline OColor::OColor(const OColor& c) : r(c.r), g(c.g), b(c.b), a(c.a) {}
118 
119 inline OColor::OColor(float red, float green, float blue, float alpha) : r(red), g(green), b(blue), a(alpha)
120 {
121 }
122 
123 inline OColor::OColor(float* c)
124 {
125  r = c[0];
126  g = c[1];
127  b = c[2];
128  a = c[3];
129 }
130 
131 inline void OColor::operator=(const OColor& c)
132 {
133  r = c.r;
134  g = c.g;
135  b = c.b;
136  a = c.a;
137 }
138 
139 inline OColor::operator float*()
140 {
141  return &r;
142 }
143 
144 inline OColor::operator const float*() const
145 {
146  return &r;
147 }
148 
149 #if TY_USE_IHM
150 inline OColor::operator QColor() const
151 {
152  return QColor::fromRgbF(r, g, b, a);
153 }
154 #endif // TY_USE_IHM
155 
161 {
162 public:
164  static void getSCurveColorTable(const unsigned int& nbColors, const float* hueRange,
165  const float* saturationRange, const float* valueRange,
166  OLookupTable& outColors);
167 
169  static void getLinearColorTable(const unsigned int& nbColors, const float* hueRange,
170  const float* saturationRange, const float* valueRange,
171  OLookupTable& outColors);
172 
174  static void getSqrtColorTable(const unsigned int& nbColors, const float* hueRange,
175  const float* saturationRange, const float* valueRange,
176  OLookupTable& outColors);
177 
179  static void getGaussColorTable(const unsigned int& nbColors, const float& alpha1, const float& alpha2,
180  OLookupTable& outColors);
181 };
182 
183 #endif // TY_COLOR
NxReal c
Definition: NxVec3.cpp:317
Definition: color.h:36
static const OColor GREEN
Definition: color.h:96
unsigned int getAsRGBA() const
Definition: color.cpp:44
static const OColor BLUE
Definition: color.h:95
float b
Definition: color.h:38
OColor clampCopy() const
Definition: color.cpp:276
void operator=(const OColor &c)
Definition: color.h:131
static const OColor RED
Definition: color.h:98
static const OColor BLACK
Definition: color.h:94
float a
Definition: color.h:38
static const OColor WHITE
Definition: color.h:101
void getAsHSB(float &hue, float &saturation, float &brightness) const
Definition: color.cpp:91
float r
Definition: color.h:38
void clamp()
Definition: color.cpp:233
static const float equality_threshold
Definition: color.h:104
static const OColor YELLOW
Definition: color.h:100
float g
Definition: color.h:38
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:106
void setAsRGBA(const unsigned int val)
Definition: color.cpp:71
static const OColor MAGENTA
Definition: color.h:99
OColor()
Definition: color.h:115
bool operator==(const OColor &other) const
Definition: color.cpp:39
static const OColor CYAN
Definition: color.h:97
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:113