The number of individual points that your existing code (that you mentioned in your question) draws for this line must be:
l=max(abs(x2-x1), abs(y2-y1))+1
That is, if the line is mostly oriented vertically, there should be a point drawn in every row. If its mostly oriented horizontally, there should be a point drawn in every column. So we can derive the total number of individual points drawn, l
, as simply the maximum difference between the starting and the ending x/y coordinates. This is what your existing code should be doing.
So, you know in advance that you're going to be drawing l
points. This now becomes a simple linear interpolation between each individual r
, g
, b
and a
component. If "R1" is the R component at (x1, y1), and "R2" is the R component at (x2, y2): then when drawing your i
th point, with i
starting at 0 for (x1, y1), and reaching l
at (x2, y2), the linearly-interpolated R is simply:
R1+i/l*(R2-R1)
So, when i=0
this is R1, and when i=l
this is R2. You must compute this linear interpolation: either by using floating point math; or by doing integer multiplication first, then integer division (presuming your integer precision is sufficient to avoid overflow during multiplication).
Lather, rinse, and repeat the same process for the G
, B
, and A
components.