public void FloodFill(int x, int y, BufferBrush brush)
{
if (!InBounds(x, y)) return;
var initColor = _buffer[y, x].BackColor;
if (brush.GetColor(x,y) == initColor) return;
List<Point> queue = new List<Point>(32);
queue.Add(new Point(x, y));
Point p;
int w, e, j;
for (int i = 0; i < queue.Count; i++)
{
p = queue[i];
w = e = p.X;
while (w - 1 >= 0)
{
if (_buffer[p.Y, w - 1].BackColor == initColor)
{
w--;
}
else
{
break;
}
}
while (e + 1 < _width)
{
if (_buffer[p.Y, e + 1].BackColor == initColor)
{
e++;
}
else
{
break;
}
}
for (j = w; j <= e; j++)
{
_buffer[p.Y, j].BackColor = brush.GetColor(j, p.Y);
if (p.Y + 1 < _height)
{
if (_buffer[p.Y + 1, j].BackColor == initColor)
{
queue.Add(new Point(j, p.Y + 1));
}
}
if (p.Y - 1 >= 0)
{
if (_buffer[p.Y - 1, j].BackColor == initColor)
{
queue.Add(new Point(j, p.Y - 1));
}
}
}
}
}