Fill an array with smooth sequence of colours

HarryRogers
HarryRogers used Ask the Experts™
on
Hi
 I wish to populate the elements of an array (of arbitrary length) with a smooth progression of colour values. Using 'BlendColors' I can find a midway colour between two values. What i would like is a neat algorithm to fill each of these intermediate values in a given array.
e.g. for an array of length n
the value at index n div 2  (Call it Vi) is BlendColors( colour[0],color[n]...)
then
the value at index [n div 2] div 2 is the result  BlendColors( colour[0], Vi...)
etc.

 function BlendColors(Color1, Color2: TColor; Opacity: Byte): TColor;
var
  r, g, b: Byte;
  c1, c2: PByteArray;
begin
  Color1 := ColorToRGB(Color1);
  Color2 := ColorToRGB(Color2);
  c1 := @Color1;
  c2 := @Color2;

  r := Trunc(c1[0] + (c2[0] - c1[0]) * Opacity / 256);
  g := Trunc(c1[1] + (c2[1] - c1[1]) * Opacity / 256);
  b := Trunc(c1[2] + (c2[2] - c1[2]) * Opacity / 256);

  Result := RGB(r, g, b);
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose ParrotGraphics Expert

Commented:
First, we need the lenght. For example, the array is Nrow x Ncol.
Then do the loops as below.
Jose
 

for row := 1 to Nrow (number of rows)
  for col := 1 to Ncol
    factor := (row*col)/(Nrow*Ncol) <-- factor is Real
    r := Trunc(c1[0] + (c2[0] - c1[0]) * factor);
    g := Trunc(c1[1] + (c2[1] - c1[1]) * factor);
    b := Trunc(c1[2] + (c2[2] - c1[2]) * factor);
    Result[row][col] := RGB(r, g, b);
  end
end

Open in new window

Jose ParrotGraphics Expert

Commented:
As a matter of correctness:
As the loop is from 1 to N, the right math should be:
factor = ((row-1)*(col-1))/(float)((Nrow-1)*(Ncol-1));
Jose
Graphics Expert
Commented:
Assuming c1[0]=20 and c2[0]=250, Nrow=8 and Ncol=5, the matrix for red calculated by the above code will be the RESULT 1 below.
So, first color c1[0]=20 will appear in cel (1,1) and c2[0] in cel (8,5). Of course, Nrow can be equal to Ncol. Please note first col and first row have the same repeated value.  
If it is required a linear variation in the first row and col, then factor should be:
       factor = ((row-1)+(col-1))/(float)((Nrow-1)+(Ncol-1));
That way, the result matrix, for the same parameters as before, will be as in RESULT 2 below.

Jose

RESULT 1 for the red component
----------------------------------------------
        20      20      20      20      20
        20      28      36      44      52
        20      36      52      69      85
        20      44      69      93      118
        20      52      85      118     151
        20      61      102     143     184
        20      69      118     167     217
        20      77      135     192     250 
----------------------------------------------
RESULT 2
        20      40      61      82      103
        40      61      82      103     124
        61      82      103     124     145
        82      103     124     145     166
        103     124     145     166     187
        124     145     166     187     208
        145     166     187     208     229
        166     187     208     229     250  

Open in new window

Author

Commented:
Great works a treat thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial