From 01ab3c4e689d631c780965a3ad0e3beed9a05877 Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 24 Jun 2014 08:46:49 +0000 Subject: [PATCH] Templated the Lerp color operator to allow use with double... --- vcg/space/color4.h | 137 +++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 68 deletions(-) diff --git a/vcg/space/color4.h b/vcg/space/color4.h index 954bcffe..8a184a99 100644 --- a/vcg/space/color4.h +++ b/vcg/space/color4.h @@ -39,33 +39,33 @@ namespace vcg { template class Color4 : public Point4 { - typedef Point4 Base; + typedef Point4 Base; public: /// Constant for storing standard colors. /// Each color is stored in a simple in so that the bit pattern match with the one of Color4b. - enum ColorConstant { - Black = 0xff000000, - Gray = 0xff808080, - White = 0xffffffff, + enum ColorConstant { + Black = 0xff000000, + Gray = 0xff808080, + White = 0xffffffff, - Red = 0xff0000ff, - Green = 0xff00ff00, - Blue = 0xffff0000, + Red = 0xff0000ff, + Green = 0xff00ff00, + Blue = 0xffff0000, - Cyan = 0xffffff00, - Yellow = 0xff00ffff, - Magenta = 0xffff00ff, + Cyan = 0xffffff00, + Yellow = 0xff00ffff, + Magenta = 0xffff00ff, - LightGray =0xffc0c0c0, - LightRed =0xff8080ff, - LightGreen =0xff80ff80, - LightBlue =0xffff8080, + LightGray =0xffc0c0c0, + LightRed =0xff8080ff, + LightGreen =0xff80ff80, + LightBlue =0xffff8080, - DarkGray =0xff404040, - DarkRed =0xff000040, - DarkGreen =0xff004000, - DarkBlue =0xff400000 - }; + DarkGray =0xff404040, + DarkRed =0xff000040, + DarkGreen =0xff004000, + DarkBlue =0xff400000 + }; inline Color4 ( const T nx, const T ny, const T nz , const T nw ) :Point4(nx,ny,nz,nw) {} inline Color4 ( const Point4 &c) :Point4(c) {} @@ -73,21 +73,21 @@ public: inline Color4 (ColorConstant cc); template - inline void Import(const Color4 & b ) + inline void Import(const Color4 & b ) { - (*this)[0] = T(b[0]); - (*this)[1] = T(b[1]); - (*this)[2] = T(b[2]); - (*this)[3] = T(b[3]); + (*this)[0] = T(b[0]); + (*this)[1] = T(b[1]); + (*this)[2] = T(b[2]); + (*this)[3] = T(b[3]); } template - inline void Import(const Point4 & b ) + inline void Import(const Point4 & b ) { - (*this)[0] = T(b[0]); - (*this)[1] = T(b[1]); - (*this)[2] = T(b[2]); - (*this)[3] = T(b[3]); + (*this)[0] = T(b[0]); + (*this)[1] = T(b[1]); + (*this)[2] = T(b[2]); + (*this)[3] = T(b[3]); } template @@ -100,12 +100,12 @@ public: //inline void Import(const Color4 &b); inline Color4 operator + ( const Color4 & p) const - { - return Color4( (*this)[0]+p.V()[0], (*this)[1]+p.V()[1], (*this)[2]+p.V()[2], (*this)[3]+p.V()[3] ); - } + { + return Color4( (*this)[0]+p.V()[0], (*this)[1]+p.V()[1], (*this)[2]+p.V()[2], (*this)[3]+p.V()[3] ); + } - - inline void lerp(const Color4 &c0, const Color4 &c1, const float x) + template + inline void lerp(const Color4 &c0, const Color4 &c1, const ScalarInterpType x) { assert(x>=0); assert(x<=1); @@ -116,7 +116,8 @@ public: (*this)[3]=(T)(c1.V()[3]*x + c0.V()[3]*(1.0f-x)); } - inline void lerp(const Color4 &c0, const Color4 &c1, const Color4 &c2, const Point3f &ip) + template + inline void lerp(const Color4 &c0, const Color4 &c1, const Color4 &c2, const Point3 &ip) { assert(fabs(ip[0]+ip[1]+ip[2]-1)<0.00001); @@ -204,20 +205,20 @@ inline static Color4 Scatter(int range, int value,float Sat=.3f,float Val=.9f) int b, k, m=range; int r =range; - for (b=0, k=1; k=m) { - if (b==0) r = k; - b += k; - value -= (m+1)>>1; - m >>= 1; - } - else m = (m+1)>>1; - if (r>range-b) r = range-b; + for (b=0, k=1; k=m) { + if (b==0) r = k; + b += k; + value -= (m+1)>>1; + m >>= 1; + } + else m = (m+1)>>1; + if (r>range-b) r = range-b; - //TRACE("Scatter range 0..%i, in %i out %i\n",n,a,b); - Color4 rc; - rc.SetHSVColor(float(b)/float(range),Sat,Val); - return rc; + //TRACE("Scatter range 0..%i, in %i out %i\n",n,a,b); + Color4 rc; + rc.SetHSVColor(float(b)/float(range),Sat,Val); + return rc; } inline static Color4 ColorRamp(const float &minf,const float &maxf ,float v ) @@ -272,20 +273,20 @@ template <> template <> inline Color4 Color4::Construct( const Color4 & b ) { return Color4( - (unsigned char)(b[0]*255.0f), - (unsigned char)(b[1]*255.0f), - (unsigned char)(b[2]*255.0f), - (unsigned char)(b[3]*255.0f)); + (unsigned char)(b[0]*255.0f), + (unsigned char)(b[1]*255.0f), + (unsigned char)(b[2]*255.0f), + (unsigned char)(b[3]*255.0f)); } template <> template <> inline Color4 Color4::Construct( const Color4 & b ) { return Color4( - (float)(b[0])/255.0f, - (float)(b[1])/255.0f, - (float)(b[2])/255.0f, - (float)(b[3])/255.0f); + (float)(b[0])/255.0f, + (float)(b[1])/255.0f, + (float)(b[2])/255.0f, + (float)(b[3])/255.0f); } template<> @@ -302,22 +303,22 @@ inline Color4::Color4(Color4::ColorConstant cc) inline Color4 Clamp(Color4 &c) { - c[0]=math::Clamp(c[0],0.0f,1.0f); - c[1]=math::Clamp(c[1],0.0f,1.0f); - c[2]=math::Clamp(c[2],0.0f,1.0f); - c[3]=math::Clamp(c[3],0.0f,1.0f); - return c; + c[0]=math::Clamp(c[0],0.0f,1.0f); + c[1]=math::Clamp(c[1],0.0f,1.0f); + c[2]=math::Clamp(c[2],0.0f,1.0f); + c[3]=math::Clamp(c[3],0.0f,1.0f); + return c; } template<> inline Color4 Color4::operator + ( const Color4 & p) const { - return Color4( - (unsigned char)(math::Clamp(int((*this)[0])+int(p[0]),0,255)), - (unsigned char)(math::Clamp(int((*this)[1])+int(p[1]),0,255)), - (unsigned char)(math::Clamp(int((*this)[2])+int(p[2]),0,255)), - (unsigned char)(math::Clamp(int((*this)[3])+int(p[3]),0,255)) - ); + return Color4( + (unsigned char)(math::Clamp(int((*this)[0])+int(p[0]),0,255)), + (unsigned char)(math::Clamp(int((*this)[1])+int(p[1]),0,255)), + (unsigned char)(math::Clamp(int((*this)[2])+int(p[2]),0,255)), + (unsigned char)(math::Clamp(int((*this)[3])+int(p[3]),0,255)) + ); }