Advertisement
Guest User

FloodFill fix

a guest
Feb 20th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1.         public static void FloodFill(PixelManipulator manipulator, Point position, Pixel targetColor, Pixel replaceColor, byte threshold)
  2.         {
  3.             unchecked
  4.             {
  5.                 Queue<Point> q = new Queue<Point>((manipulator.Width * manipulator.Height));
  6.                 manipulator.Position = position;
  7.  
  8.                 if (!manipulator.Current.Similar(targetColor, threshold))
  9.                     return;
  10.  
  11.                 q.Enqueue(manipulator.Position);
  12.  
  13.                 while (q.Count > 0)
  14.                 {
  15.                     Point n = q.Dequeue();
  16.                     if (n.X < 0 || n.Y < 0 || n.X > manipulator.Width || n.Y > manipulator.Height)
  17.                     {
  18.                         continue;
  19.                     }
  20.  
  21.                     manipulator.Position = n;
  22.                     if (manipulator.Current == replaceColor || !manipulator.Current.Similar(targetColor, threshold))
  23.                     {
  24.                         continue;
  25.                     }
  26.  
  27.                     manipulator.Current = replaceColor;
  28.                     q.Enqueue(new Point(n.X - 1, n.Y));  //Left
  29.                     q.Enqueue(new Point(n.X + 1, n.Y));  //Right
  30.                     q.Enqueue(new Point(n.X, n.Y-1));  //Top
  31.                     q.Enqueue(new Point(n.X, n.Y+1));  //Bottom
  32.                 }
  33.             }
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement