从CSDN上看到一个颜色渐变的算法,它是把前一个颜色的红蓝绿三色提出来,现去减后一个红蓝绿的颜色值,代码如下:
void paint (Graphics g, int x, int y, int w, int h, int colorBegin, int colorEnd) {
int r0 = (colorBegin >> 16) & 0xff;
int r1 = (colorEnd >> 16) & 0xff;
int g0 = (colorBegin >> 8) & 0xff;
int g1 = (colorEnd >> 8) & 0xff;
int b0 = colorBegin & 0xff;
int b1 = colorEnd & 0xff;
int F,rr,gg,bb;
for (int i = 0; i < h; i++)
{
F = (i << 16) / h;
rr = r0 + ((F * (r1 - r0)) >> 16);
gg = g0 + ((F * (g1 - g0)) >> 16);
bb = b0 + ((F * (b1 - b0)) >> 16);
g.setColor(rr << 16 | gg << 8 | bb);
g.drawRect(x , y + i, w - 1, 0);
}
}
时间: 2024-09-28 18:32:40