At the wikipedia's Mandelbrot page there are really beautiful generated images of the Mandelbrot set.
I also just implemented my own mandelbrot algorithm. Given n
is the number of iterations used to calculate each pixel, I color them pretty simple from black to green to white like that (with C++ and Qt 5.0):
QColor mapping(Qt::white);
if (n <= MAX_ITERATIONS){
double quotient = (double) n / (double) MAX_ITERATIONS;
double color = _clamp(0.f, 1.f, quotient);
if (quotient > 0.5) {
// Close to the mandelbrot set the color changes from green to white
mapping.setRgbF(color, 1.f, color);
}
else {
// Far away it changes from black to green
mapping.setRgbF(0.f, color, 0.f);
}
}
return mapping;
My result looks like that:
I like it pretty much already, but which color gradient is used for the images in Wikipedia? How to calculate that gradient with a given n
of iterations?
(This question is not about smoothing.)
Well, I did some reverse engineering on the colours used in wikipedia using the Photoshop eyedropper. There are 16 colours in this gradient:
Simply using a modulo and an QColor array allows me to iterate through all colours in the gradient:
The result looks pretty much like what I was looking for:
:)
I believe they're the default colours in Ultra Fractal. The evaluation version comes with source for a lot of the parameters, and I think that includes that colour map (if you can't infer it from the screenshot on the front page) and possibly also the logic behind dynamically scaling that colour map appropriately for each scene.
The gradient is probably from Ultra Fractal. It is defined by 5 control points:
where position is in range [0, 1] and color is RGB from 0 to 255.
The catch is that it is not linear gradient. The interpolation between points is cubic (or something similar). Following image shows the difference between linear and Monotone cubic interpolation:
As you can see the the cubic is much smoother and "prettier". I used monotone cubic interpolation to avoid "overshooting" of color range that can be caused by basic cubic. Monotone cubic ansures that interpolated values are always in the range of input points (0-255).
I use following code to compute the color based on iteration
i
:Where
i
is the diverged iteration number,re
andim
are diverged coordinates,gradient.Scale
is 256,gradient.Shift
is 0 and thecolors
is array with pre-discretized gradient showed above. Its length is usually 2048.