Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to FloodFill in FMX for a TBitmap?

I am converting from VCL to FMX. In VCL, there was a function in a TBitmap's TCanvas called FloodFill, which allowed for a TBitmap's canvas to be flooded with a specific color, until another specific color was reached on the bitmap's canvas.

Is there an equivalent to this function in FMX?

like image 239
Anthony Burg Avatar asked Jan 26 '26 13:01

Anthony Burg


1 Answers

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;
like image 51
kungfu panda Avatar answered Jan 29 '26 08:01

kungfu panda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!