Gradient code (wth fixed point arithmetic)


Dear SDL dudez & Sammy:

I would like to know if any of you know how to write gradient code that
starts with any RGB value and ends with any RGB value. Currently I only
know how to create gradients that use only red, or only green, or
combinations of red and green, etc. green and blue…etc…

Paul Lowe
spazz at ulink.net

Linear interpolation is great, but you should not use 3 multiplications for
each line:

// slow code
// all variables are int
rd=r2-r1;
gd=g2-g1;
bd=b2-b1;

for(i=0;i<len;i++)
{
r = r1 + rdi/len;
g = g1 + gd
i/len;
b = b1 + bd*i/len;
plot(r,g,b);
}

Replace the multiplication by adding a constant value each iteration, and
use fixed point arithmetic instead of floating point to store the
fractional part of the constant value:

// fast code
// all variables are int
rd=((r2-r1)<<16)/len;
gd=((g2-g1)<<16)/len;
bd=((b2-b1)<<16)/len;
r=r1<<16;
g=g1<<16;
b=b1<<16;

for(i=0;i<len;i++)
{
plot(r>>16,g>>16,b>>16);
r += rd
g += gd;
b += bd;
}