public PixelCollection FloodFill(PixelCollection pixels, Pixel fillPixel, Color targetColor, Color replacementColor) { if (targetColor.Equals(replacementColor)) { return(pixels); } if (!fillPixel.Color.Equals(targetColor)) { return(pixels); } var pixelQueue = new Queue <Pixel>(); pixels.SetPixel(fillPixel, replacementColor); pixelQueue.Enqueue(fillPixel); Pixel currentPixel; while (pixelQueue.Count > 0) { currentPixel = pixelQueue.Dequeue(); var northPixel = pixels.GetPixelOrNull(currentPixel.Column, currentPixel.Row - 1); var southPixel = pixels.GetPixelOrNull(currentPixel.Column, currentPixel.Row + 1); var eastPixel = pixels.GetPixelOrNull(currentPixel.Column + 1, currentPixel.Row); var westPixel = pixels.GetPixelOrNull(currentPixel.Column - 1, currentPixel.Row); TryToColorPixel(pixels, pixelQueue, northPixel, targetColor, replacementColor); TryToColorPixel(pixels, pixelQueue, southPixel, targetColor, replacementColor); TryToColorPixel(pixels, pixelQueue, eastPixel, targetColor, replacementColor); TryToColorPixel(pixels, pixelQueue, westPixel, targetColor, replacementColor); } return(pixels); }