Visual Basic 2010 (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 Then
            ' is in set - colour black
            bmpMandel.SetPixel(x, y, Color.Black)
         Else
            'not in set - colour greyscale
            grey = CInt(255 * (CDbl(maxIterations - numIterations) / CDbl(maxIterations)))
            pixelC = Color.FromArgb(255, grey, grey, grey)
            bmpMandel.SetPixel(x, y, pixelC)
         End If

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,

Dim cols() As Color = { 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 Mod 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 = (CDbl(numIterations) / CDbl(maxIterations))
If percentIterations >= 0.1 Then
   pixelC = Color.Red
Else
   pixelC = Color.Blue
End If
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