function TColorFinder.Find(Buffer: PRGB32; BufferWidth: Integer; SearchWidth, SearchHeight: Integer; Offset: TPoint; MaxToFind: Integer): TPointArray; var X, Y, RowSize: Integer; RowPtr, Ptr: PByte; PointBuffer: TSimbaPointBuffer; PrevColor: record Hit: Boolean; Color: TColor; end; Hit: Boolean; label Finished; begin Result := nil; if (SearchWidth <= 0) or (SearchHeight <= 0) or (Buffer = nil) or (BufferWidth <= 0) then Exit; RowSize := BufferWidth * SizeOf(TRGB32); RowPtr := PByte(Buffer); PrevColor.Hit := Self.FCompareFunc(FColor, PColorBGRA(RowPtr)^, FMultipliers) / FMaxDistance * 100 <= FTolerance; PrevColor.Color := PColorBGRA(RowPtr)^; Dec(SearchHeight); Dec(SearchWidth); for Y := 0 to SearchHeight do begin Ptr := RowPtr; for X := 0 to SearchWidth do begin if (PrevColor.Hit and (PrevColor.Color = PColorBGRA(Ptr)^)) or (Self.FCompareFunc(FColor, PColorBGRA(Ptr)^, FMultipliers) / FMaxDistance * 100 <= FTolerance) then begin PointBuffer.Add(X + Offset.X, Y + Offset.Y); if (PointBuffer.Count = MaxToFind) then goto Finished; end else begin PrevColor.Hit := False; PrevColor.Color := PColorBGRA(Ptr)^; end; Inc(Ptr, SizeOf(TRGB32)); end; Inc(RowPtr, RowSize); end; Finished: Result := PointBuffer.Trim(); end;