-
Notifications
You must be signed in to change notification settings - Fork 299
/
RGBAColor.cpp
283 lines (231 loc) · 8.16 KB
/
RGBAColor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/type/RGBAColor.h>
#include <sstream>
#include <locale>
#include <sofa/type/fixed_array.h>
#include <sofa/type/fixed_array_algorithms.h>
#include <sofa/type/Vec.h>
using namespace sofa::type::pairwise;
namespace // anonymous
{
template<class T>
T rclamp(const T& value, const T& low, const T& high)
{
return value < low ? low : (value > high ? high : value);
}
} // anonymous namespace
namespace sofa::type
{
static bool ishexsymbol(const char c)
{
return (c>='0' && c<='9') || (c>='a' && c<='f') || (c>='A' && c<='F') ;
}
static int hexval(const char c)
{
if (c>='0' && c<='9') return c-'0';
if (c>='a' && c<='f') return (c-'a')+10;
if (c>='A' && c<='F') return (c-'A')+10;
return 0;
}
static void extractValidatedHexaString(std::istream& in, std::string& s)
{
s.reserve(9);
char c = in.get();
if(c!='#')
{
in.setstate(std::ios_base::failbit) ;
return;
}
s.push_back(c);
while(in.get(c)){
if( !ishexsymbol(c) )
return;
s.push_back(c) ;
if(s.size()>9){
in.setstate(std::ios_base::failbit) ;
return ;
}
}
/// we need to reset the failbit because it is set by the get function
/// on the last character.
in.clear(in.rdstate() & ~std::ios_base::failbit) ;
}
bool RGBAColor::read(const std::string& str, RGBAColor& color)
{
std::stringstream s(str);
s >> color ;
if(s.fail() || !s.eof())
return false;
return true ;
}
void RGBAColor::set(const float r, const float g, const float b, const float a)
{
this->m_components[0] = r;
this->m_components[1] = g;
this->m_components[2] = b;
this->m_components[3] = a;
}
RGBAColor RGBAColor::fromString(const std::string& str)
{
RGBAColor color(1.0,1.0,1.0,1.0) ;
if( !RGBAColor::read(str, color) )
{
throw std::invalid_argument("Unable to scan color from string '" + str + "'");
}
return color;
}
RGBAColor RGBAColor::fromFloat(const float r, const float g, const float b, const float a)
{
return RGBAColor(r,g,b,a);
}
RGBAColor RGBAColor::fromStdArray(const std::array<float, 4>& color)
{
return RGBAColor(color) ;
}
RGBAColor RGBAColor::fromStdArray(const std::array<double, 4>& color)
{
return RGBAColor(float(color[0]), float(color[1]), float(color[2]), float(color[3]));
}
RGBAColor RGBAColor::fromHSVA(const float h, const float s, const float v, const float a )
{
// H [0, 360] S, V and A [0.0, 1.0].
RGBAColor rgba;
const int i = (int)floor(h/60.0f) % 6;
const float f = h/60.0f - floor(h/60.0f);
const float p = v * (float)(1 - s);
const float q = v * (float)(1 - s * f);
const float t = v * (float)(1 - (1 - f) * s);
rgba[3]=a;
switch (i)
{
case 0:
rgba[0]=v; rgba[1]=t; rgba[2]=p;
break;
case 1: rgba[0]=q; rgba[1]=v; rgba[2]=p;
break;
case 2: rgba[0]=p; rgba[1]=v; rgba[2]=t;
break;
case 3: rgba[0]=p; rgba[1]=q; rgba[2]=v;
break;
case 4: rgba[0]=t; rgba[1]=p; rgba[2]=v;
break;
case 5: rgba[0]=v; rgba[1]=p; rgba[2]=q;
}
return rgba;
}
/// This function remove the leading space in the stream.
static std::istream& trimInitialSpaces(std::istream& in)
{
char first=in.peek();
while(!in.eof() && !in.fail() && std::isspace(first, std::locale()))
{
in.get();
first=in.peek();
}
return in;
}
SOFA_TYPE_API std::istream& operator>>(std::istream& i, RGBAColor& t)
{
float r=0.0,g=0.0, b=0.0, a=1.0;
trimInitialSpaces(i) ;
/// Let's remove the initial spaces.
if( i.eof() || i.fail() )
return i;
const char first = i.peek() ;
if (std::isdigit(first, std::locale()))
{
i >> r >> g >> b ;
if(!i.eof()){
i >> a;
}
}
else if (first=='#')
{
std::string str;
extractValidatedHexaString(i, str) ;
if(i.fail()){
return i;
}
if(str.length()>=7){
r = (hexval(str[1])*16+hexval(str[2]))/255.0f;
g = (hexval(str[3])*16+hexval(str[4]))/255.0f;
b = (hexval(str[5])*16+hexval(str[6]))/255.0f;
if (str.length()>8)
a = (hexval(str[7])*16+hexval(str[8]))/255.0f;
}else if (str.length()>=4){
r = (hexval(str[1])*17)/255.0f;
g = (hexval(str[2])*17)/255.0f;
b = (hexval(str[3])*17)/255.0f;
if (str.length()>4)
a = (hexval(str[4])*17)/255.0f;
}else{
/// If we cannot parse the field we returns that with the fail bit.
i.setstate(std::ios_base::failbit) ;
return i;
}
} else {
std::string str;
/// Search for the first word, it is not needed to read more char than size("magenta"
std::getline(i, str, ' ');
/// if end of line is returned before encountering ' ' or 7... is it fine
/// so we can clear the failure bitset.
/// Compare the resulting string with supported colors.
/// If you add more colors... please also add them in the test file.
if (str == "white") { r = 1.0f; g = 1.0f; b = 1.0f; }
else if (str == "black") { r = 0.0f; g = 0.0f; b = 0.0f; }
else if (str == "red") { r = 1.0f; g = 0.0f; b = 0.0f; }
else if (str == "green") { r = 0.0f; g = 1.0f; b = 0.0f; }
else if (str == "blue") { r = 0.0f; g = 0.0f; b = 1.0f; }
else if (str == "cyan") { r = 0.0f; g = 1.0f; b = 1.0f; }
else if (str == "magenta") { r = 1.0f; g = 0.0f; b = 1.0f; }
else if (str == "yellow") { r = 1.0f; g = 1.0f; b = 0.0f; }
else if (str == "gray") { r = 0.5f; g = 0.5f; b = 0.5f; }
else {
/// If we cannot parse the field we returns that with the fail bit.
i.setstate(std::ios_base::failbit) ;
return i;
}
}
t.set(r,g,b,a) ;
return i;
}
/// Write to an output stream
SOFA_TYPE_API std::ostream& operator << ( std::ostream& out, const RGBAColor& t )
{
for( int i=0; i<3; ++i )
out<<t[i]<<" ";
out<<t[3];
return out;
}
/// @brief enlight a color by a given factor.
RGBAColor RGBAColor::lighten(const RGBAColor& in, const SReal factor)
{
RGBAColor c = in + ( (RGBAColor::white() - RGBAColor::clamp(in, 0.0f, 1.0f)) * rclamp(float(factor), 0.0f, 1.0f));
c.a() = 1.0;
return c ;
}
RGBAColor RGBAColor::operator*(float f) const
{
return RGBAColor(r() * f, g() * f, b() * f, a() * f);
}
} // namespace sofa::type