Templated the Lerp color operator to allow use with double...

This commit is contained in:
Paolo Cignoni 2014-06-24 08:46:49 +00:00
parent eb642099f1
commit 01ab3c4e68
1 changed files with 69 additions and 68 deletions

View File

@ -39,33 +39,33 @@ namespace vcg {
template <class T> template <class T>
class Color4 : public Point4<T> class Color4 : public Point4<T>
{ {
typedef Point4<T> Base; typedef Point4<T> Base;
public: public:
/// Constant for storing standard colors. /// Constant for storing standard colors.
/// Each color is stored in a simple in so that the bit pattern match with the one of Color4b. /// Each color is stored in a simple in so that the bit pattern match with the one of Color4b.
enum ColorConstant { enum ColorConstant {
Black = 0xff000000, Black = 0xff000000,
Gray = 0xff808080, Gray = 0xff808080,
White = 0xffffffff, White = 0xffffffff,
Red = 0xff0000ff, Red = 0xff0000ff,
Green = 0xff00ff00, Green = 0xff00ff00,
Blue = 0xffff0000, Blue = 0xffff0000,
Cyan = 0xffffff00, Cyan = 0xffffff00,
Yellow = 0xff00ffff, Yellow = 0xff00ffff,
Magenta = 0xffff00ff, Magenta = 0xffff00ff,
LightGray =0xffc0c0c0, LightGray =0xffc0c0c0,
LightRed =0xff8080ff, LightRed =0xff8080ff,
LightGreen =0xff80ff80, LightGreen =0xff80ff80,
LightBlue =0xffff8080, LightBlue =0xffff8080,
DarkGray =0xff404040, DarkGray =0xff404040,
DarkRed =0xff000040, DarkRed =0xff000040,
DarkGreen =0xff004000, DarkGreen =0xff004000,
DarkBlue =0xff400000 DarkBlue =0xff400000
}; };
inline Color4 ( const T nx, const T ny, const T nz , const T nw ) :Point4<T>(nx,ny,nz,nw) {} inline Color4 ( const T nx, const T ny, const T nz , const T nw ) :Point4<T>(nx,ny,nz,nw) {}
inline Color4 ( const Point4<T> &c) :Point4<T>(c) {} inline Color4 ( const Point4<T> &c) :Point4<T>(c) {}
@ -73,21 +73,21 @@ public:
inline Color4 (ColorConstant cc); inline Color4 (ColorConstant cc);
template <class Q> template <class Q>
inline void Import(const Color4<Q> & b ) inline void Import(const Color4<Q> & b )
{ {
(*this)[0] = T(b[0]); (*this)[0] = T(b[0]);
(*this)[1] = T(b[1]); (*this)[1] = T(b[1]);
(*this)[2] = T(b[2]); (*this)[2] = T(b[2]);
(*this)[3] = T(b[3]); (*this)[3] = T(b[3]);
} }
template <class Q> template <class Q>
inline void Import(const Point4<Q> & b ) inline void Import(const Point4<Q> & b )
{ {
(*this)[0] = T(b[0]); (*this)[0] = T(b[0]);
(*this)[1] = T(b[1]); (*this)[1] = T(b[1]);
(*this)[2] = T(b[2]); (*this)[2] = T(b[2]);
(*this)[3] = T(b[3]); (*this)[3] = T(b[3]);
} }
template <class Q> template <class Q>
@ -100,12 +100,12 @@ public:
//inline void Import(const Color4<unsigned char> &b); //inline void Import(const Color4<unsigned char> &b);
inline Color4 operator + ( const Color4 & p) const 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] );
} }
template <class ScalarInterpType>
inline void lerp(const Color4 &c0, const Color4 &c1, const float x) inline void lerp(const Color4 &c0, const Color4 &c1, const ScalarInterpType x)
{ {
assert(x>=0); assert(x>=0);
assert(x<=1); assert(x<=1);
@ -116,7 +116,8 @@ public:
(*this)[3]=(T)(c1.V()[3]*x + c0.V()[3]*(1.0f-x)); (*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 <class ScalarInterpType>
inline void lerp(const Color4 &c0, const Color4 &c1, const Color4 &c2, const Point3<ScalarInterpType> &ip)
{ {
assert(fabs(ip[0]+ip[1]+ip[2]-1)<0.00001); 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 b, k, m=range;
int r =range; int r =range;
for (b=0, k=1; k<range; k<<=1) for (b=0, k=1; k<range; k<<=1)
if (value<<1>=m) { if (value<<1>=m) {
if (b==0) r = k; if (b==0) r = k;
b += k; b += k;
value -= (m+1)>>1; value -= (m+1)>>1;
m >>= 1; m >>= 1;
} }
else m = (m+1)>>1; else m = (m+1)>>1;
if (r>range-b) r = range-b; if (r>range-b) r = range-b;
//TRACE("Scatter range 0..%i, in %i out %i\n",n,a,b); //TRACE("Scatter range 0..%i, in %i out %i\n",n,a,b);
Color4 rc; Color4 rc;
rc.SetHSVColor(float(b)/float(range),Sat,Val); rc.SetHSVColor(float(b)/float(range),Sat,Val);
return rc; return rc;
} }
inline static Color4 ColorRamp(const float &minf,const float &maxf ,float v ) inline static Color4 ColorRamp(const float &minf,const float &maxf ,float v )
@ -272,20 +273,20 @@ template <> template <>
inline Color4<unsigned char> Color4<unsigned char>::Construct( const Color4<float> & b ) inline Color4<unsigned char> Color4<unsigned char>::Construct( const Color4<float> & b )
{ {
return Color4<unsigned char>( return Color4<unsigned char>(
(unsigned char)(b[0]*255.0f), (unsigned char)(b[0]*255.0f),
(unsigned char)(b[1]*255.0f), (unsigned char)(b[1]*255.0f),
(unsigned char)(b[2]*255.0f), (unsigned char)(b[2]*255.0f),
(unsigned char)(b[3]*255.0f)); (unsigned char)(b[3]*255.0f));
} }
template <> template <> template <> template <>
inline Color4<float> Color4<float>::Construct( const Color4<unsigned char> & b ) inline Color4<float> Color4<float>::Construct( const Color4<unsigned char> & b )
{ {
return Color4<float>( return Color4<float>(
(float)(b[0])/255.0f, (float)(b[0])/255.0f,
(float)(b[1])/255.0f, (float)(b[1])/255.0f,
(float)(b[2])/255.0f, (float)(b[2])/255.0f,
(float)(b[3])/255.0f); (float)(b[3])/255.0f);
} }
template<> template<>
@ -302,22 +303,22 @@ inline Color4<float>::Color4(Color4<float>::ColorConstant cc)
inline Color4<float> Clamp(Color4<float> &c) inline Color4<float> Clamp(Color4<float> &c)
{ {
c[0]=math::Clamp(c[0],0.0f,1.0f); c[0]=math::Clamp(c[0],0.0f,1.0f);
c[1]=math::Clamp(c[1],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[2]=math::Clamp(c[2],0.0f,1.0f);
c[3]=math::Clamp(c[3],0.0f,1.0f); c[3]=math::Clamp(c[3],0.0f,1.0f);
return c; return c;
} }
template<> template<>
inline Color4<unsigned char> Color4<unsigned char>::operator + ( const Color4<unsigned char> & p) const inline Color4<unsigned char> Color4<unsigned char>::operator + ( const Color4<unsigned char> & p) const
{ {
return Color4<unsigned char>( return Color4<unsigned char>(
(unsigned char)(math::Clamp(int((*this)[0])+int(p[0]),0,255)), (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)[1])+int(p[1]),0,255)),
(unsigned char)(math::Clamp(int((*this)[2])+int(p[2]),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)) (unsigned char)(math::Clamp(int((*this)[3])+int(p[3]),0,255))
); );
} }