2007. január 19., péntek

How to detect a color within a range of pixels around the mouse cursor


Problem/Question/Abstract:

I need to search for a pixel of a given TColor around the mouse cursor position. My main problem is that I have to search in a range of pixels around the center of the mouse's actual position starting with one pixel.

Answer:

function FindPixelOnCanvas(
  canvas: TCanvas; {canvas to find pixel on}
  const startAt: TPoint; {start position}
  tolerance: Integer; {pixel range to check}
  color: TColor; {color to look for}
  const rect: TRect; {dimension of canvas}
  var foundPos: TPoint {returns last position tested}
  ): Boolean; {returns true if color found, false if not}
var
  i, k, n: Integer;
begin
  Result := False;
  for n := 0 to tolerance do
    for i := -n to +n do
      for k := -n to +n do
        if (Abs(i) = n) or ((Abs(k) = n) then
          begin
            foundPos := Point(startAt.X + i, startAt.Y + k);
            if PtInRect(foundPos, rect) and (Canvas.Pixels[foundPos.X, foundPos.Y] = color) then
            begin
              Result := True;
              Exit;
            end;
          end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése