I know several questions about mandelbrot fractal smooth coloring techniques have already been asked (and answered). However none of them details how to build an array of continuous colors in plain C.
I read extensively about smoothing color algorithms and I can calculate a continuous number of iterations through a basic escape time algorithm. Basically I use this number to set manually the reba components of the color. (Please don't suggest me to use OPENGL, or similar... I just want to do it manually cause I want to understand it).
here is my code:
#define W 800 //window width
#define H 800 //window height
#define MAX_ITERS 1000 // number of iterations
union color_u
{
unsigned int num; //color as an integer
unsigned char comp[4]; //color as components (brga) -> beacause of the endianness
};
unsigned int getColor(int i, double *z)
{
double k;
color_u color;
k = i + 1 - log(log(sqrt(z[R] * z[R] + z[I] * z[I]))) / log(2);
k /= MAX_ITERS;
if (i > 700)
{
color.comp[0] = (unsigned char) (255.0 * k); //blue
color.comp[1] = (unsigned char) 0; //green
color.comp[2] = (unsigned char) 0; //red
color.comp[3] = (unsigned char) 0; //alpha
}
else
{
color.comp[0] = (unsigned char) 0; //blue
color.comp[1] = (unsigned char) 0; //green
color.comp[2] = (unsigned char) (255.0 * k) //red
color.comp[3] = (unsigned char) 0; //alpha
}
return color.num;
}
void mandelbrot(void)
{
int i;
double z[2];
double c[2];
double tmp;
c[R] = fr->x + (pixel[X] - W) / (e->zoom * W);
c[I] = fr->y + (pixel[Y] - H) / (e->zoom * H);
z[R] = 0;
z[I] = 0;
i = 0;
while (z[R] * z[R] + z[I] * z[I] < 4 && ++i < MAX_ITERS)
{
tmp = z[R] * z[R] - z[I] * z[I] + c[R];
z[I] = 2 * z[R] * z[I] + c[I];
z[R] = tmp;
}
if (i < MAX_ITERS)
{
draw_pixel(getColor(i, z));
}
}
The result is just cool, but not awesome.
Now I would like to build a pre-calculated array of continuous colors that has size of MAX_ITERATIONS
... in order to use my i
as index and look-up in the table.
However I can't understand how to do it. Almost everybody uses functions to do it in Java or C++, but I would like to build it myself.
Thank you