How to detect a color within a range of pixels around the mouse cursor (Views: 100)


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.


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}
  i, k, n: Integer;
  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
            foundPos := Point(startAt.X + i, startAt.Y + k);
            if PtInRect(foundPos, rect) and (Canvas.Pixels[foundPos.X, foundPos.Y] = color) then
              Result := True;

<< Back to main page