Visual C# 2005 (Windows) Guide
Fractals: Colouring Algorithms

The images that you saw on the first page of this section are all made more interesting by using a variety of colours.

We are using a colouring algorithm in our program already. It is colouring pixels black if they represent a point in the set and varying shades of grey according to the number of iterations taken for z to go out of range.

The section of the CreateMandelbrotImage() procedure that we are concerned with is,

if (numIterations == maxIterations)
{
   // is in set - colour black
   bmpMandel.SetPixel(x, y, Color.Black);
}
else
{
   //not in set - colour greyscale
   grey = (int)(255 * ((double)(maxIterations - numIterations) / (double)maxIterations));
   pixelC = Color.FromArgb(255,grey, grey, grey);
   bmpMandel.SetPixel(x, y, pixelC);
}

Modulus Escape Time

If we have say, 4 colours, we can take the modulus of the number of iterations divided by the number of colours. To do this, you may wish to create a new drawing procedure.

Start by adding an array of colors to the procedure,

Color[] cols = { Color.Fuchsia, Color.Red, Color.Yellow, Color.Orange };

Adapt the colouring algorithm so that else clause reads as follows,

//not in set - colour
pixelC = cols[numIterations % cols.GetLength(0)];
bmpMandel.SetPixel(x, y, pixelC);

Zoom in on the new image to find the interesting parts.

colour algorithm

Notice that this algorithm creates bands of colour across the image that can appear like tentacles when you zoom in on various sections.

Percentage Escape Time

Having declared the double percentIterations at the start of the procedure. The following change to the colouring part, results in ranges of colour based on the number of iterations.

percentIterations = ((double)numIterations / (double)maxIterations);
if (percentIterations >= 0.1)
{
   pixelC = Color.Red;
}
else
{
   pixelC = Color.Blue;
}
bmpMandel.SetPixel(x, y, pixelC);

The following image was produced using these colours,

colour algorithm nicer

You can play around with this, adding else if statements to have as many different colours in each image as you want.

If you look back at how the greyscale colours were produced in the orginal version of the application, you could apply an adaptation of that to one part of the range of escape time values - this will give a slightly smoother image.

Don't be afraid if your image looks wrong when it is first coloured, zooming in can reveal some interesting patterns. The two images below were produced using the same colouring algorithm - the second image is zoomed in.

mandelbrot image

nicer mandelbrot image