24 #ifndef __VCGLIB_COLOR4
25 #define __VCGLIB_COLOR4
27 #include <vcg/space/point3.h>
28 #include <vcg/space/point4.h>
42 typedef Point4<T> Base;
59 LightGray =0xffc0c0c0,
61 LightGreen =0xff80ff80,
62 LightBlue =0xffff8080,
66 DarkGreen =0xff004000,
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) {}
74 inline Color4 (
unsigned int cc);
77 inline void Import(
const Color4<Q> & b )
86 inline void Import(
const Point4<Q> & b )
95 static inline Color4 Construct(
const Color4<Q> & b )
97 return Color4(T(b[0]),T(b[1]),T(b[2]),T(b[3]));
103 inline Color4 operator + (
const Color4 & p)
const
105 return Color4( (*
this)[0]+p.V()[0], (*
this)[1]+p.V()[1], (*
this)[2]+p.V()[2], (*
this)[3]+p.V()[3] );
108 template <
class ScalarInterpType>
109 inline void lerp(
const Color4 &c0,
const Color4 &c1,
const ScalarInterpType x)
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));
120 template <
class ScalarInterpType>
121 inline void lerp(
const Color4 &c0,
const Color4 &c1,
const Color4 &c2,
const Point3<ScalarInterpType> &ip)
123 assert(fabs(ip[0]+ip[1]+ip[2]-1)<0.00001);
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]);
133 inline void SetColorRamp(
const float &minf,
const float &maxf ,
float v )
135 if(minf>maxf) {
SetColorRamp(maxf,minf,maxf+(minf-v));
return; }
137 float step=(maxf-minf)/4;
151 inline void SetColorRampParula(
const float &minf,
const float &maxf ,
float v)
153 if(minf>maxf) { SetColorRampParula(maxf,minf,maxf+(minf-v));
return; }
154 SetColorRampParula((v-minf)/(maxf-minf));
157 inline void SetColorRampParula(
float v)
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);
168 else if(div>1) div=1;
169 lerp(Color4<T>(ParuVal[ind]), Color4<T>(ParuVal[ind+1]), div);
172 void SetHSVColor(
float h,
float s,
float v)
177 (*this)[0]=(
unsigned char)(255*r);
178 (*this)[1]=(
unsigned char)(255*g);
179 (*this)[2]=(
unsigned char)(255*b);
187 int i = int( floor(h*6.0f) );
188 float f = float(h*6.0f- floor(h*6.0f));
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));
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;
204 (*this)[0]=(
unsigned char)(255*r);
205 (*this)[1]=(
unsigned char)(255*g);
206 (*this)[2]=(
unsigned char)(255*b);
210 inline static Color4 GrayShade(
float f)
215 return Color4(f,f,f,1);
218 inline void SetGrayShade(
float f)
223 Import(Color4<float>(f,f,f,1));
233 inline static Color4 Scatter(
int range,
int value,
float Sat=.3f,
float Val=.9f)
238 for (b=0, k=1; k<range; k<<=1)
247 if (r>range-b) r = range-b;
251 rc.SetHSVColor(
float(b)/float(range),Sat,Val);
255 inline static Color4 ColorRamp(
const float &minf,
const float &maxf ,
float v )
262 inline static unsigned short ToUnsignedB5G5R5(
const Color4 &) {
return 0;}
263 inline static unsigned short ToUnsignedR5G5B5(
const Color4 &) {
return 0;}
264 inline static unsigned int ToUnsignedA8R8G8B8(
const Color4 &) {
return 0;}
266 inline static Color4 FromUnsignedB5G5R5(
unsigned short)
268 return Color4(Color4::White);
270 inline static Color4 FromUnsignedR5G5B5(
unsigned short)
272 return Color4(Color4::White);
277 template <>
template <>
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;
286 template <>
template <>
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;
295 template <>
template <>
296 inline void Color4<unsigned char>::Import(
const Color4<float> &b)
298 (*this)[0]=(
unsigned char)(b[0]*255.0f);
299 (*this)[1]=(
unsigned char)(b[1]*255.0f);
300 (*this)[2]=(
unsigned char)(b[2]*255.0f);
301 (*this)[3]=(
unsigned char)(b[3]*255.0f);
304 template <>
template <>
305 inline void Color4<unsigned char>::Import(
const Point4<float> &b)
307 (*this)[0]=(
unsigned char)(b[0]*255.0f);
308 (*this)[1]=(
unsigned char)(b[1]*255.0f);
309 (*this)[2]=(
unsigned char)(b[2]*255.0f);
310 (*this)[3]=(
unsigned char)(b[3]*255.0f);
313 template <>
template <>
314 inline void Color4<unsigned char>::Import(
const Point4<double> &b)
316 (*this)[0]=(
unsigned char)(b[0]*255.0);
317 (*this)[1]=(
unsigned char)(b[1]*255.0);
318 (*this)[2]=(
unsigned char)(b[2]*255.0);
319 (*this)[3]=(
unsigned char)(b[3]*255.0);
322 template <>
template <>
323 inline Color4<unsigned char> Color4<unsigned char>::Construct(
const Color4<float> & b )
325 return Color4<unsigned char>(
326 (
unsigned char)(b[0]*255.0f),
327 (
unsigned char)(b[1]*255.0f),
328 (
unsigned char)(b[2]*255.0f),
329 (
unsigned char)(b[3]*255.0f));
332 template <>
template <>
333 inline Color4<float> Color4<float>::Construct(
const Color4<unsigned char> & b )
335 return Color4<float>(
336 (
float)(b[0])/255.0f,
337 (
float)(b[1])/255.0f,
338 (
float)(b[2])/255.0f,
339 (
float)(b[3])/255.0f);
342 template <>
template <>
343 inline Color4<double> Color4<double>::Construct(
const Color4<unsigned char> & b )
345 return Color4<double>(
346 (
double)(b[0])/255.0,
347 (
double)(b[1])/255.0,
348 (
double)(b[2])/255.0,
349 (
double)(b[3])/255.0);
371 inline Color4<unsigned char>::Color4(
unsigned int cc)
377 inline Color4<float>::Color4(
unsigned int cc)
379 Import(Color4<unsigned char>(cc));
383 inline Color4<double>::Color4(
unsigned int cc)
385 Import(Color4<unsigned char>(cc));
388 inline Color4<float> Clamp(Color4<float> &c)
390 c[0]=math::Clamp(c[0],0.0f,1.0f);
391 c[1]=math::Clamp(c[1],0.0f,1.0f);
392 c[2]=math::Clamp(c[2],0.0f,1.0f);
393 c[3]=math::Clamp(c[3],0.0f,1.0f);
397 inline Color4<double> Clamp(Color4<double> &c)
399 c[0]=math::Clamp(c[0],0.0,1.0);
400 c[1]=math::Clamp(c[1],0.0,1.0);
401 c[2]=math::Clamp(c[2],0.0,1.0);
402 c[3]=math::Clamp(c[3],0.0,1.0);
407 inline Color4<unsigned char> Color4<unsigned char>::operator + (
const Color4<unsigned char> & p)
const
409 return Color4<unsigned char>(
410 (
unsigned char)(math::Clamp(
int((*
this)[0])+
int(p[0]),0,255)),
411 (
unsigned char)(math::Clamp(
int((*
this)[1])+
int(p[1]),0,255)),
412 (
unsigned char)(math::Clamp(
int((*
this)[2])+
int(p[2]),0,255)),
413 (
unsigned char)(math::Clamp(
int((*
this)[3])+
int(p[3]),0,255))
418 typedef Color4<unsigned char> Color4b;
419 typedef Color4<float> Color4f;
420 typedef Color4<double> Color4d;
424 inline unsigned short Color4<unsigned char>::ToUnsignedB5G5R5(
const Color4<unsigned char> &cc)
426 unsigned short r = cc[0]/8;
427 unsigned short g = cc[1]/8;
428 unsigned short b = cc[2]/8;
429 unsigned short res = b + g*32 + r*1024;
434 inline unsigned short Color4<unsigned char>::ToUnsignedR5G5B5(
const Color4<unsigned char> &cc)
436 unsigned short r = cc[0]/8;
437 unsigned short g = cc[1]/8;
438 unsigned short b = cc[2]/8;
439 unsigned short res = r + g*32 + b*1024;
444 inline unsigned int Color4<unsigned char>::ToUnsignedA8R8G8B8(
const Color4<unsigned char> &cc)
446 unsigned int r = cc[0];
447 unsigned int g = cc[1];
448 unsigned int b = cc[2];
449 unsigned int a = cc[3];
450 unsigned int res = (r << 16) | (g << 8) | (b) | (a << 24);
456 inline Color4<unsigned char> Color4<unsigned char>::FromUnsignedR5G5B5(
unsigned short val)
458 unsigned short r = val % 32 *8;
459 unsigned short g = ((val/32)%32)*8;
460 unsigned short b = ((val/1024)%32)*8;
461 Color4b cc((
unsigned char)r,(
unsigned char)g,(
unsigned char)b,(
unsigned char)255);
466 inline Color4<unsigned char> Color4<unsigned char>::FromUnsignedB5G5R5(
unsigned short val)
468 unsigned short b = val % 32 *8;
469 unsigned short g = ((val/32)%32)*8;
470 unsigned short r = ((val/1024)%32)*8;
471 Color4b cc((
unsigned char)r,(
unsigned char)g,(
unsigned char)b,(
unsigned char)255);
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: namespaces.dox:6