用整数乘法实现快速颜色混和
一段颜色混和的代码,研究了一下别人的alpha混和的代码,
这里把自己的代码贴出来,主要利用了整数乘法的特点
BYTE byte1,byte2;
byte1*byte2必然是一个不超过16位的数:
DWORD ColorLerp(DWORD color1,DWORD color2,float fWeight)
{
if(fWeight<=0.01f) return color1;
else if(fWeight>=1) return color2;
else
{
BYTE Weight=(BYTE)(fWeight*255);
BYTE IWeight=~Weight;
DWORD dwTemp=0;
dwTemp =(((0xFF00FF00&color1)>>8)*IWeight+
((0xFF00FF00&color2)>>8)*Weight
)&0xFF00FF00;
dwTemp|=(((0x00FF00FF&color1)*IWeight+
(0x00FF00FF&color2)*Weight
)&0xFF00FF00)>>8;
return dwTemp;
}
}
{
if(fWeight<=0.01f) return color1;
else if(fWeight>=1) return color2;
else
{
BYTE Weight=(BYTE)(fWeight*255);
BYTE IWeight=~Weight;
DWORD dwTemp=0;
dwTemp =(((0xFF00FF00&color1)>>8)*IWeight+
((0xFF00FF00&color2)>>8)*Weight
)&0xFF00FF00;
dwTemp|=(((0x00FF00FF&color1)*IWeight+
(0x00FF00FF&color2)*Weight
)&0xFF00FF00)>>8;
return dwTemp;
}
}