方法一:
雙線性插值的方法
1.首先計算縮放比例..bl=2/w...2是原圖寬度(因為只有2個顏色)...w是放大後的寬度
2.開始迴圈,從1迴圈到目標寬度w,假設迴圈變數為i...就用n=i*bl
3.取出n的小數部分存到變數p中..然後合成顏色,目標色R=R1*(1-p)+R2*p G=G1*(1-P)+G2*p B=B1*(1-P)+B2*p....R1/G1/B1是起始色的紅/綠/藍色,R2/G2/B2是結束色的紅/綠/藍色.
color g_color[3] = {{255, 0, 0, 255}, {0, 255, 0, 255}, {0, 0, 255, 255}};
U8 g_perc[2] = {30, 70};
gradient_color g_gc = {g_color, g_perc, 3};
void fill_gradient_color(S32 x1, S32 y1, S32 x2, S32 y2)
{
S32 i;
S32 done = 0;
S32 w;
S32 p;
S32 fill_width;
S32 x_start = 0, x_end;
S32 x;
FLOAT bl;
FLOAT n, m;
FLOAT tr, tg, tb;
color start_color, end_color, c;
gradient_color *gc = &g_gc;
fill_width = x2 - x1;
for (i = 0; (i < (gc->n - 1) && !done); i++)
{
start_color = gc->c[i];
end_color = gc->c[i + 1];
p = gc->p[i];
x_end = x_start + pixtel_percent(fill_width, p);
if (x_end > fill_width - 1)
{
x_end = fill_width - 1;
done = 1;
}
w = x_end - x_start;
bl = 1.0 / (FLOAT)w;
c.r = start_color.r;
c.g = start_color.g;
c.b = start_color.b;
c.alpha = start_color.alpha;
for (x = 1; x <= w; x++)
{
S32 tmp;
n = (FLOAT)x * bl;
tmp = (S32)n;
m = n - (FLOAT)tmp;
gui_draw_vertical_line(y1, y2, x1 + x_start + x, c);
tr = start_color.r * (1.0 - m) + end_color.r * m;
tg = start_color.g * (1.0 - m) + end_color.g * m;
tb = start_color.b * (1.0 - m) + end_color.b * m;
c.r = (S8) tr;
c.g = (S8) tg;
c.b = (S8) tb;
}
x_start = x_end - 1;
}
}
方法二:
divisor為漸層寬度,ir,ig,ib 為各色值分量的增量,ir = (R2 - R1) / divisor, ig = (G2 - G1) / divisor, ib = (B2 - B1) / divisor, R1/G1/B1是起始色的紅/綠/藍色,R2/G2/B2是結束色的紅/綠/藍色.然後從初始色值開始,每次繪畫都將色值分量各增加ir,ig,ib。
for (i = 0; (i < (gc->n - 1) && !done); i++)
{
start_color = gc->c[i];
end_color = gc->c[i + 1];
p = gc->p[i];
x_end = x_start + pixtel_percent(fill_width, p);
if (x_end > fill_width - 1)
{
x_end = fill_width - 1;
done = 1;
}
divisor = (FLOAT) (x_end - x_start) + 1;
if (divisor == 0)
{
divisor = 1;
}
ir = -(start_color.r - end_color.r) / divisor;
ig = -(start_color.g - end_color.g) / divisor;
ib = -(start_color.b - end_color.b) / divisor;
tr = c.r = start_color.r;
tg = c.g = start_color.g;
tb = c.b = start_color.b;
c.alpha = start_color.alpha;
for (x = x_start; x <= x_end; x++)
{
gui_draw_vertical_line(y1, y2, x + x1, c);
tr += ir;
tg += ig;
tb += ib;
c.r = (S8) tr;
c.g = (S8) tg;
c.b = (S8) tb;
}
x_start = x_end + 1;
}