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>
class Color4 : public Point4<T>
{
typedef Point4<T> Base;
typedef Point4<T> 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<T>(nx,ny,nz,nw) {}
inline Color4 ( const Point4<T> &c) :Point4<T>(c) {}
@ -73,21 +73,21 @@ public:
inline Color4 (ColorConstant cc);
template <class Q>
inline void Import(const Color4<Q> & b )
inline void Import(const Color4<Q> & 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 <class Q>
inline void Import(const Point4<Q> & b )
inline void Import(const Point4<Q> & 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 <class Q>
@ -100,12 +100,12 @@ public:
//inline void Import(const Color4<unsigned char> &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 <class ScalarInterpType>
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 <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);
@ -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<range; k<<=1)
if (value<<1>=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<range; k<<=1)
if (value<<1>=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<unsigned char> Color4<unsigned char>::Construct( const Color4<float> & b )
{
return Color4<unsigned char>(
(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<float> Color4<float>::Construct( const Color4<unsigned char> & b )
{
return Color4<float>(
(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<float>::Color4(Color4<float>::ColorConstant cc)
inline Color4<float> Clamp(Color4<float> &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<unsigned char> Color4<unsigned char>::operator + ( const Color4<unsigned char> & p) const
{
return Color4<unsigned char>(
(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>(
(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))
);
}