private void ColorizeRegion(WriteableBitmap bmp, int originalX, int originalY, Color newColor, bool antiAliasing, int threshold)
{
Color oldColor = GetColorFromPixel(bmp, originalX, originalY);
int rgb = GetRgbFromColor(newColor);
int width = bmp.PixelWidth;
int height = bmp.PixelHeight;
// Set Q to the empty queue.
Queue<PointI> q = new Queue<PointI>(2048);
// Add starting node to Q.
q.Enqueue(new PointI(originalX, originalY));
// For each element n of Q:
while (q.Count > 0)
{
PointI n = q.Dequeue();
// If the color of n is equal to target-color
if (IsSimilarColor(GetColorFromPixel(bmp, n.X, n.Y), oldColor, threshold))
{
// Set startX and endX equal to n
int startX = n.X;
int endX = n.X;
// Move startX to the left until the color of the node to the left of startX no longer matches the old color.
while (startX > 0 && IsSimilarColor(GetColorFromRgb(GetRgbFromPixel(bmp, startX - 1, n.Y)), oldColor, threshold))
{
--startX;
}
// Move endX to the right until the color of the node to the right of endX no longer matches the old color.
while (endX < width - 1 && IsSimilarColor(GetColorFromRgb(GetRgbFromPixel(bmp, endX + 1, n.Y)), oldColor, threshold))
{
++endX;
}
if (antiAliasing)
{
// Anti-aliasing is performed on the edges (left, right) of the current line
if (startX - 1 >= 0)
{
Blend(bmp, startX - 1, n.Y, newColor);
}
if (endX + 1 < width)
{
Blend(bmp, endX + 1, n.Y, newColor);
}
}
// For each node between startX and endX
for (int x = startX; x <= endX; ++x)
{
// Set the color of nodes between w and e to replacement-color
SetPixelRgb(bmp, x, n.Y, rgb);
// If the color of the upper node of n is an old color, add that node to Q
if (n.Y > 0 && IsSimilarColor(GetColorFromRgb(GetRgbFromPixel(bmp, x, n.Y - 1)), oldColor, threshold))
{
q.Enqueue(new PointI(x, n.Y - 1));
}
else
{
if (antiAliasing && n.Y - 1 >= 0)
{
// Anti-aliasing is performed on the line up of the current one if we are on a frontier one
Blend(bmp, x, n.Y - 1, newColor);
}
}
// If the color of the lower node of n is an old color, add that node to Q
if (n.Y < height - 1 && IsSimilarColor(GetColorFromRgb(GetRgbFromPixel(bmp, x, n.Y + 1)), oldColor, threshold))
{
q.Enqueue(new PointI(x, n.Y + 1));
}
else
{
if (antiAliasing && n.Y + 1 < height)
{
// Anti-aliasing is performed on the line down of the current one if we are on a frontier one
Blend(bmp, x, n.Y + 1, newColor);
}
}
}
}
}
}