3

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

Mike Andrews
  • 3,045
  • 18
  • 28
user3154898
  • 271
  • 1
  • 3
  • 13
  • Since you reference to "other questions": did you see the answer http://stackoverflow.com/a/1243788/2564301? – Jongware May 01 '15 at 16:37
  • Yes and it says that the solution should be building a continuos color palette, but it doesn't show how to do it. – user3154898 May 01 '15 at 16:44
  • ".. `smoothcolor` is in the interval `(0,max_iter)`. Divide `smoothcolor` with `max_iter` to get a value between 0 and 1." -- `max_iter` is your array size. The division by it is to force it into a `0..1` range for the next function, the Hue part of a HSB-to-RGB conversion. – Jongware May 01 '15 at 19:08
  • http://krazydad.com/tutorials/makecolors.php this is the solution. – user3154898 May 02 '15 at 11:01
  • For arbitrary color sequences (not only rainbow colors), you may want to have a look at the `initColorMap` function of http://stackoverflow.com/questions/23711681/generating-custom-color-palette-for-julia-set/23713317#23713317 (it's in Java, but rather low-level - you can easily port it to C). – Marco13 May 02 '15 at 11:36

0 Answers0