Coloring Techniques


The purpose of this document is to explain a number of fractal coloring options. In reality the number of available coloring techniques is infinite but here we'll just go through a representative sample to provide a basis for futher experimentation.

We'll be using the same formula file throughout this discussion - a simple mandelbrot fractal. The portions of the file that will not change are as follows:

   fractal
   {
      mapping
      {
         (-2, -2, 2, 2) => (200, 200)
      }

      // 'colors' section (if used) will go here

      formula
      {
         z = [0, 0];

         while($count < 100 && ssq(z) < 4)
         {
            z = z * z + current;
         }

         // color assignment will go here
      }
   }

There are two ways to assign color:

Here is a minimal example that assigns the color red using an indexed color model. This example uses the formula above, its colors section is:

colors
{
   define
   {
      [255 0 0] // this is entry 0
      [0 255 0] // this is entry 1
      [0 0 255] // this is entry 2
   }
}
And it's color assigment is:
set_color(0);

Note that the value passed to set_color (in this case zero) is what is used to select a color from the color table. If we had wanted to assign a color of blue instead, we would have used:

set_color(2);

Now here is an equally straightforward example that also assigns the color red but this time using a direct color approach

// No need for a 'colors' section when using direct color

set_color(255, 0, 0);

In practice, of course, we wouldn't use the same value each time - this would result in a pretty uninteresting image (a solid color). When doing real color assignment we would compute the index (when doing color-table based color) or rgb value (when doing direct color). It is the flexibility allowed in the computation that allows us to color our fractal images in so many different ways.

Now we'll examine some "real world" examples, we'll cover indexed (color table based) color assignment first.

coloring techniques - indexed color

One very important point about using indexed color is this:

Care must be taken to ensure that the calculation you use to generate the appropriate color index falls within the bounds of the color table you have created

While this is important, the program is forgiving - if you supply an index that is outside the bounds of your color table, that pixel will simply be colored black and you will see a brief message once your image has been fully drawn detailing how many invalid indices were encountered while processing your image.

Now we'll cover some common techniques:

more indexed color techniques

Now that we've seen a number of the standard coloring methods, we'll quickly cover some additional variants just to give you some ideas for your own experimentation. For these examples, the color section will be fixed:

   colors
   {
      define
      {
         // green shades
         gradient(101)
         {
            [0 51 0]
            [204 255 204]
            [0 51 0]
         }
      }
      
      define(100)
      {
         // cyan shades
         gradient(101)
         {
            [0 51 51]
            [204 255 255]
            [0 51 51]
         }
      }
   }

We'll just show the color assignment section and resulting images so that we can cover a number of ideas rapidly.

Certainly some of the above techniques seem complicated, but their purpose is just to illustrate how many coloring possibilities are available. The best way to develop new techniques is just to experiment, there are an unlimited number of interesting and beautiful coloring approaches waiting to be explored.

direct color based techniques

With direct color assignment, you do not need a colors section (though it is not an error to have one). Direct color assignment just involves generating values for red, green and blue - each between 0 and 255.

If any of your generated values are less than 0 or greater than 255 the pixel will be colored black and (much like indexed color assignment) you will receive a notification when you image has fully drawn indicating the number of invalid color assignments.

Also note that direct and indexed color are not mutually exclusive alternatives - there is no problem with mixing them together within the same generated image.

Here is an example using direct color assignment: