Base on RRUZ's answer and Anthony's reply, I made this code:
Procedure TForm1.FloodFill(BitmapData:TBitmapData; X, Y:Integer; OldColor, NewColor: TAlphaColor);
var
Current: TAlphaColor;
begin
Current := BitmapData.GetPixel(X, Y);
if Current = OldColor then begin
BitmapData.SetPixel(X,Y,NewColor);
FloodFill(BitmapData, X+1, Y, OldColor, NewColor);
FloodFill(BitmapData, X-1, Y, OldColor, NewColor);
FloodFill(BitmapData, X, Y+1, OldColor, NewColor);
FloodFill(BitmapData, X, Y-1, OldColor, NewColor);
end;
end;
And a usage sample:
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
var
MyBmpData: TBitmapData;
OldColor, NewColor: TAlphaColor;
ix, iy: integer;
begin
Image1.Bitmap.Canvas.Bitmap.Map(TMapAccess.ReadWrite, MyBmpData);
ix := round(X); iy := Round(Y);
OldColor := MyBmpData.GetPixel(ix, iy);
NewColor := ColorComboBox1.Color; // or use some other source for a new color
FloodFill(MyBmpData, ix, iy, OldColor, NewColor) ;
Image1.Bitmap.Canvas.Bitmap.Unmap(MyBmpData);
end;