VCG Library
Loading...
Searching...
No Matches
color4.h
1/****************************************************************************
2* VCGLib o o *
3* Visual and Computer Graphics Library o o *
4* _ O _ *
5* Copyright(C) 2004-2016 \/)\/ *
6* Visual Computing Lab /\/| *
7* ISTI - Italian National Research Council | *
8* \ *
9* All rights reserved. *
10* *
11* This program is free software; you can redistribute it and/or modify *
12* it under the terms of the GNU General Public License as published by *
13* the Free Software Foundation; either version 2 of the License, or *
14* (at your option) any later version. *
15* *
16* This program is distributed in the hope that it will be useful, *
17* but WITHOUT ANY WARRANTY; without even the implied warranty of *
18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20* for more details. *
21* *
22****************************************************************************/
23
24#ifndef __VCGLIB_COLOR4
25#define __VCGLIB_COLOR4
26
27#include <vcg/space/point3.h>
28#include <vcg/space/point4.h>
29
30namespace vcg {
31
39template <class T>
40class Color4 : public Point4<T>
41{
42 typedef Point4<T> Base;
43public:
47 Black = 0xff000000,
48 Gray = 0xff808080,
49 White = 0xffffffff,
50
51 Red = 0xff0000ff,
52 Green = 0xff00ff00,
53 Blue = 0xffff0000,
54
55 Cyan = 0xffffff00,
56 Yellow = 0xff00ffff,
57 Magenta = 0xffff00ff,
58
59 LightGray =0xffc0c0c0,
60 LightRed =0xff8080ff,
61 LightGreen =0xff80ff80,
62 LightBlue =0xffff8080,
63
64 DarkGray =0xff404040,
65 DarkRed =0xff000040,
66 DarkGreen =0xff004000,
67 DarkBlue =0xff400000
68 };
69
70 inline Color4 ( const T nx, const T ny, const T nz , const T nw ) :Point4<T>(nx,ny,nz,nw) {}
71 inline Color4 ( const Point4<T> &c) :Point4<T>(c) {}
72 inline Color4 (){}
73 inline Color4 (ColorConstant cc);
74 inline Color4 (unsigned int cc);
75
76 template <class Q>
77 inline void Import(const Color4<Q> & b )
78 {
79 (*this)[0] = T(b[0]);
80 (*this)[1] = T(b[1]);
81 (*this)[2] = T(b[2]);
82 (*this)[3] = T(b[3]);
83 }
84
85 template <class Q>
86 inline void Import(const Point4<Q> & b )
87 {
88 (*this)[0] = T(b[0]);
89 (*this)[1] = T(b[1]);
90 (*this)[2] = T(b[2]);
91 (*this)[3] = T(b[3]);
92 }
93
94 template <class Q>
95 static inline Color4 Construct( const Color4<Q> & b )
96 {
97 return Color4(T(b[0]),T(b[1]),T(b[2]),T(b[3]));
98 }
99
100 //inline void Import(const Color4<float> &b);
101 //inline void Import(const Color4<unsigned char> &b);
102
103 inline Color4 operator + ( const Color4 & p) const
104 {
105 return Color4( (*this)[0]+p.V()[0], (*this)[1]+p.V()[1], (*this)[2]+p.V()[2], (*this)[3]+p.V()[3] );
106 }
107
108 template <class ScalarInterpType>
109 inline void lerp(const Color4 &c0, const Color4 &c1, const ScalarInterpType x)
110 {
111 assert(x>=0);
112 assert(x<=1);
113
114 (*this)[0]=(T)(c1.V()[0]*x + c0.V()[0]*(1.0f-x));
115 (*this)[1]=(T)(c1.V()[1]*x + c0.V()[1]*(1.0f-x));
116 (*this)[2]=(T)(c1.V()[2]*x + c0.V()[2]*(1.0f-x));
117 (*this)[3]=(T)(c1.V()[3]*x + c0.V()[3]*(1.0f-x));
118 }
119
120 template <class ScalarInterpType>
121 inline void lerp(const Color4 &c0, const Color4 &c1, const Color4 &c2, const Point3<ScalarInterpType> &ip)
122 {
123 assert(fabs(ip[0]+ip[1]+ip[2]-1)<0.00001);
124
125 (*this)[0]=(T)(c0[0]*ip[0] + c1[0]*ip[1]+ c2[0]*ip[2]);
126 (*this)[1]=(T)(c0[1]*ip[0] + c1[1]*ip[1]+ c2[1]*ip[2]);
127 (*this)[2]=(T)(c0[2]*ip[0] + c1[2]*ip[1]+ c2[2]*ip[2]);
128 (*this)[3]=(T)(c0[3]*ip[0] + c1[3]*ip[1]+ c2[3]*ip[2]);
129 }
130
131
133 inline void SetColorRamp(const float &minf,const float &maxf ,float v )
134 {
135 if(minf>maxf) { SetColorRamp(maxf,minf,maxf+(minf-v)); return; }
136
137 float step=(maxf-minf)/4;
138 if(v < minf ) { *this=Color4<T>(Color4<T>::Red); return; }
139 v-=minf;
140 if(v<step) { lerp(Color4<T>(Color4<T>::Red), Color4<T>(Color4<T>::Yellow),v/step); return;}
141 v-=step;
142 if(v<step) { lerp(Color4<T>(Color4<T>::Yellow),Color4<T>(Color4<T>::Green), v/step); return;}
143 v-=step;
144 if(v<step) { lerp(Color4<T>(Color4<T>::Green), Color4<T>(Color4<T>::Cyan), v/step); return;}
145 v-=step;
146 if(v<step) { lerp(Color4<T>(Color4<T>::Cyan), Color4<T>(Color4<T>::Blue), v/step); return;}
147
149 }
150
151 inline void SetColorRampParula(const float &minf,const float &maxf ,float v)
152 {
153 if(minf>maxf) { SetColorRampParula(maxf,minf,maxf+(minf-v)); return; }
154 SetColorRampParula((v-minf)/(maxf-minf));
155 }
156
157 inline void SetColorRampParula(float v)
158 {
159 if(v<0) v=0;
160 else if(v>1) v=1;
161
162 unsigned int ParuVal[9]={0xff801627, 0xffe16303, 0xffd48514,
163 0xffc6a706, 0xff9eb938, 0xff73bf92,
164 0xff56bad9, 0xff2ecefc, 0xff0afaff};
165 int ind = int(floor(v*8.0f));
166 float div = (v*8.0f - ind);
167 if(div<0) div=0;
168 else if(div>1) div=1;
169 lerp(Color4<T>(ParuVal[ind]), Color4<T>(ParuVal[ind+1]), div);
170 }
171
172 void SetHSVColor( float h, float s, float v)
173 {
174 float r,g,b;
175 if(s==0.0){ // gray color
176 r = g = b = v;
177 (*this)[0]=(unsigned char)(255*r);
178 (*this)[1]=(unsigned char)(255*g);
179 (*this)[2]=(unsigned char)(255*b);
180 (*this)[3]=255;
181 return;
182 }
183 float dummy;
184 h = modff(h,&dummy);
185 if(h==1.0) h = 0.0;
186
187 int i = int( floor(h*6.0f) );
188 float f = float(h*6.0f- floor(h*6.0f));
189
190 float p = v*(1.0f-s);
191 float q = v*(1.0f-s*f);
192 float t = v*(1.0f-s*(1.0f-f));
193
194 switch(i)
195 {
196 case 0: r=v; g=t; b=p; break;
197 case 1: r=q; g=v; b=p; break;
198 case 2: r=p; g=v; b=t; break;
199 case 3: r=p; g=q; b=v; break;
200 case 4: r=t; g=p; b=v; break;
201 case 5: r=v; g=p; b=q; break;
202 default: r=0;g=0;b=0; assert(0);break;
203 }
204 (*this)[0]=(unsigned char)(255*r);
205 (*this)[1]=(unsigned char)(255*g);
206 (*this)[2]=(unsigned char)(255*b);
207 (*this)[3]=255;
208 }
209
210inline static Color4 GrayShade(float f)
211{
212 if(f<0) f=0.0f;
213 else if(f>1) f=1.0f;
214
215 return Color4(f,f,f,1);
216}
217
218inline void SetGrayShade(float f)
219{
220 if(f<0) f=0.0f;
221 else if(f>1) f=1.0f;
222
223 Import(Color4<float>(f,f,f,1));
224}
225
226
233inline static Color4 Scatter(int range, int value,float Sat=.3f,float Val=.9f)
234{
235 int b, k, m=range;
236 int r =range;
237
238 for (b=0, k=1; k<range; k<<=1)
239 if (value<<1>=m) {
240 if (b==0) r = k;
241 b += k;
242 value -= (m+1)>>1;
243 m >>= 1;
244 }
245 else m = (m+1)>>1;
246
247 if (r>range-b) r = range-b;
248
249 //TRACE("Scatter range 0..%i, in %i out %i\n",n,a,b);
250 Color4 rc;
251 rc.SetHSVColor(float(b)/float(range),Sat,Val);
252 return rc;
253}
254
255inline static Color4 ColorRamp(const float &minf,const float &maxf ,float v )
256{
257 Color4 rc;
258 rc.SetColorRamp(minf,maxf,v);
259 return rc;
260}
261
262inline static unsigned short ToUnsignedB5G5R5(const Color4 &) { return 0;}
263inline static unsigned short ToUnsignedR5G5B5(const Color4 &) { return 0;}
264inline static unsigned int ToUnsignedA8R8G8B8(const Color4 &) { return 0;}
265
266inline static Color4 FromUnsignedB5G5R5(unsigned short)
267{
268 return Color4(Color4::White);
269}
270inline static Color4 FromUnsignedR5G5B5(unsigned short)
271{
272 return Color4(Color4::White);
273}
274
275};
276
277template <> template <>
279{
280 (*this)[0]=b[0]/255.0f;
281 (*this)[1]=b[1]/255.0f;
282 (*this)[2]=b[2]/255.0f;
283 (*this)[3]=b[3]/255.0f;
284}
285
286template <> template <>
288{
289 (*this)[0]=b[0]/255.0;
290 (*this)[1]=b[1]/255.0;
291 (*this)[2]=b[2]/255.0;
292 (*this)[3]=b[3]/255.0;
293}
294
295// A float component outside [0, 1] does not fit a byte, and converting it would be undefined
296// behaviour; such values come from files (an OFF colour of 2.0), so they are clamped first, and a
297// NaN reads as 0. Components within [0, 1] convert as they always did, truncated.
298inline unsigned char ColorComponentToByte(float v)
299{
300 if (!(v > 0.0f)) return 0;
301 if (v >= 1.0f) return 255;
302 return (unsigned char)(v*255.0f);
303}
304
305template <> template <>
306inline void Color4<unsigned char>::Import(const Color4<float> &b)
307{
308 (*this)[0]=ColorComponentToByte(b[0]);
309 (*this)[1]=ColorComponentToByte(b[1]);
310 (*this)[2]=ColorComponentToByte(b[2]);
311 (*this)[3]=ColorComponentToByte(b[3]);
312}
313
314template <> template <>
315inline void Color4<unsigned char>::Import(const Point4<float> &b)
316{
317 (*this)[0]=ColorComponentToByte(b[0]);
318 (*this)[1]=ColorComponentToByte(b[1]);
319 (*this)[2]=ColorComponentToByte(b[2]);
320 (*this)[3]=ColorComponentToByte(b[3]);
321}
322
323template <> template <>
324inline void Color4<unsigned char>::Import(const Point4<double> &b)
325{
326 for (int i = 0; i < 4; ++i)
327 (*this)[i] = !(b[i] > 0.0) ? 0 : b[i] >= 1.0 ? 255 : (unsigned char)(b[i]*255.0);
328}
329
330template <> template <>
331inline Color4<unsigned char> Color4<unsigned char>::Construct( const Color4<float> & b )
332{
333 return Color4<unsigned char>(
334 (unsigned char)(b[0]*255.0f),
335 (unsigned char)(b[1]*255.0f),
336 (unsigned char)(b[2]*255.0f),
337 (unsigned char)(b[3]*255.0f));
338}
339
340template <> template <>
341inline Color4<float> Color4<float>::Construct( const Color4<unsigned char> & b )
342{
343 return Color4<float>(
344 (float)(b[0])/255.0f,
345 (float)(b[1])/255.0f,
346 (float)(b[2])/255.0f,
347 (float)(b[3])/255.0f);
348}
349
350template <> template <>
351inline Color4<double> Color4<double>::Construct( const Color4<unsigned char> & b )
352{
353 return Color4<double>(
354 (double)(b[0])/255.0,
355 (double)(b[1])/255.0,
356 (double)(b[2])/255.0,
357 (double)(b[3])/255.0);
358}
359
360template<>
361inline Color4<unsigned char>::Color4(Color4<unsigned char>::ColorConstant cc)
362{
363 *((int *)this )= cc;
364}
365
366template<>
367inline Color4<float>::Color4(Color4<float>::ColorConstant cc)
368{
369 Import(Color4<unsigned char>((Color4<unsigned char>::ColorConstant)cc));
370}
371
372template<>
373inline Color4<double>::Color4(Color4<double>::ColorConstant cc)
374{
375 Import(Color4<unsigned char>((Color4<unsigned char>::ColorConstant)cc));
376}
377
378template<>
379inline Color4<unsigned char>::Color4(unsigned int cc)
380{
381 *((int *)this )= cc;
382}
383
384template<>
385inline Color4<float>::Color4(unsigned int cc)
386{
387 Import(Color4<unsigned char>(cc));
388}
389
390template<>
391inline Color4<double>::Color4(unsigned int cc)
392{
393 Import(Color4<unsigned char>(cc));
394}
395
396inline Color4<float> Clamp(Color4<float> &c)
397{
398 c[0]=math::Clamp(c[0],0.0f,1.0f);
399 c[1]=math::Clamp(c[1],0.0f,1.0f);
400 c[2]=math::Clamp(c[2],0.0f,1.0f);
401 c[3]=math::Clamp(c[3],0.0f,1.0f);
402 return c;
403}
404
405inline Color4<double> Clamp(Color4<double> &c)
406{
407 c[0]=math::Clamp(c[0],0.0,1.0);
408 c[1]=math::Clamp(c[1],0.0,1.0);
409 c[2]=math::Clamp(c[2],0.0,1.0);
410 c[3]=math::Clamp(c[3],0.0,1.0);
411 return c;
412}
413
414template<>
415inline Color4<unsigned char> Color4<unsigned char>::operator + ( const Color4<unsigned char> & p) const
416{
417 return Color4<unsigned char>(
418 (unsigned char)(math::Clamp(int((*this)[0])+int(p[0]),0,255)),
419 (unsigned char)(math::Clamp(int((*this)[1])+int(p[1]),0,255)),
420 (unsigned char)(math::Clamp(int((*this)[2])+int(p[2]),0,255)),
421 (unsigned char)(math::Clamp(int((*this)[3])+int(p[3]),0,255))
422 );
423}
424
425
426typedef Color4<unsigned char> Color4b;
427typedef Color4<float> Color4f;
428typedef Color4<double> Color4d;
429
430
431template<>
432inline unsigned short Color4<unsigned char>::ToUnsignedB5G5R5(const Color4<unsigned char> &cc)
433{
434 unsigned short r = cc[0]/8;
435 unsigned short g = cc[1]/8;
436 unsigned short b = cc[2]/8;
437 unsigned short res = b + g*32 + r*1024;
438 return res;
439}
440
441template<>
442inline unsigned short Color4<unsigned char>::ToUnsignedR5G5B5(const Color4<unsigned char> &cc)
443{
444 unsigned short r = cc[0]/8;
445 unsigned short g = cc[1]/8;
446 unsigned short b = cc[2]/8;
447 unsigned short res = r + g*32 + b*1024;
448 return res;
449}
450
451template<>
452inline unsigned int Color4<unsigned char>::ToUnsignedA8R8G8B8(const Color4<unsigned char> &cc)
453{
454 unsigned int r = cc[0];
455 unsigned int g = cc[1];
456 unsigned int b = cc[2];
457 unsigned int a = cc[3];
458 unsigned int res = (r << 16) | (g << 8) | (b) | (a << 24);
459 return res;
460}
461
462
463template<>
464inline Color4<unsigned char> Color4<unsigned char>::FromUnsignedR5G5B5(unsigned short val)
465{
466 unsigned short r = val % 32 *8;
467 unsigned short g = ((val/32)%32)*8;
468 unsigned short b = ((val/1024)%32)*8;
469 Color4b cc((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)255);
470 return cc;
471}
472
473template<>
474inline Color4<unsigned char> Color4<unsigned char>::FromUnsignedB5G5R5(unsigned short val)
475{
476 unsigned short b = val % 32 *8;
477 unsigned short g = ((val/32)%32)*8;
478 unsigned short r = ((val/1024)%32)*8;
479 Color4b cc((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)255);
480 return cc;
481}
482
486} // end of NameSpace
487
488#endif
Definition: color4.h:41
ColorConstant
Definition: color4.h:46
static Color4 Scatter(int range, int value, float Sat=.3f, float Val=.9f)
Definition: color4.h:233
void SetColorRamp(const float &minf, const float &maxf, float v)
given a float and a range set the corresponding color in the well known red->green->blue color ramp....
Definition: color4.h:133
Definition: color4.h:30