1

I'm writing a Mandelbrot app in C# (and I'm testing with Python). I already have the continous coloring from the set to its borders. My current problem is to set the background color of the environment. My current code for getting the color now looks like this, it gets the color as double (the logarithm function is done before) and checks wether it's part or not and creates a quite smooth gradient (from black to orange).

    private Color getColor(double i)
    {
        double ratio = i / (double)(iterations);
        int col = (int)(i / iterations * 255);
        int alpha = 255;

        if (ratio >= 0 && ratio < 0.25)
            return Color.FromArgb(alpha, col, col/5, 0);

        if (ratio >= 0.25 && ratio < 0.50)
            return Color.FromArgb(alpha, col, col/4, 0);

        if (ratio >= 0.50 && ratio < 0.75)
            return Color.FromArgb(alpha, col, col/3, 0);

        if (ratio >= 0.75 && ratio < 1)
            return Color.FromArgb(alpha, col, col/2, 0);

        return Color.Black; //color of the set itself
    }

How can I change the black environement (not the Mandelbrot set) to another color like the obfuscated Python script (http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python) does? I already edited the script to a nicer form, but it doesn't fit my algorithm.

EDIT: Forgot to mention, I'm not using a class for the complex equotation, I compute the fractal with the algorithm that's shown on Wikipedia.

michaeln
  • 1,032
  • 17
  • 33
  • So, is what you want to know is how to interpolate between two RGB colors (black and orange in this case)? – martineau Apr 27 '13 at 18:05
  • No, like I wrote, I already use the double-logarithm before, so I get quite a smooth gradient (black to orange in my case). My problem is to change the background color of the entire fractal image, like in the link I've posted. Or am I misunderstanding you? – michaeln Apr 27 '13 at 20:58
  • double-logarithm: log( log( sqrt(x^2 + y^2) ) ) / log(2) Yes, your guess with the iterations is right, that's the maximum count. How would you interpolate between a set of colors? – michaeln Apr 27 '13 at 22:41
  • I don't understand what the double-logarithm formula has to do with smoothing gradients, the code in your answer, or your question. The obfuscated code you linked to was not very helpful either. Anyway, do you mean you'd like to have a background color like the outer-most bluish gradient shown in the first image in the link? – martineau Apr 27 '13 at 22:55
  • My answers to the question [Python - Range values to pseudocolor](http://stackoverflow.com/questions/10901085/python-range-values-to-pseudocolor) gives the general idea of interpolating through different colorspaces. – martineau Apr 27 '13 at 23:00
  • The double-logarithm smoothes out the edges of the less hit areas and I included the code I use to colorize the fractal. The link was just to get an impression of what I was trying to do. I implemented your interpolation, but it only creates a smooth gradient (smoother than mine, because of HSV). Thanks for this. But do you know how to get a blurry background like in the link I've posted? Something like blurring the fractal and add it behind the sharp one. – michaeln Apr 28 '13 at 11:13
  • I don't understand how your double-logarithm formula relates to the value of `iterations` in your code. As you can see for this [black&white-posterized](https://dl.dropboxusercontent.com/u/5508445/stackoverflow/M-hi-res-posterized.jpg) version of the image in the link you posted, there are a lot fine variation of color values _outside_ the Julia set which are being used to make the fractal look blurred. I can expand on my pseudocolor answers and show how to make them interpolate between a whole palette (list) of different rgb colors instead of just two, if you think that would be helpful. – martineau Apr 28 '13 at 22:18
  • If you could do this, I would be very thankfull. (I guess, you did this posterized image yourself, so could you please share your knowledge?) – michaeln May 04 '13 at 14:35
  • OK, I'll work on doing pseudocolor through a list of different colors this weekend, time permitting, and post something. I've done it before, but not in Python. It occurred to me that perhaps you're trying to graph that double-logarithm formula instead of the Julia-set or some other fractal (I still want to know how your double-logarithm formula relates what you're trying to do). – martineau May 04 '13 at 16:21
  • Very kind of you. My main target is to render zooms of the Mandelbrot fractal. This double-logarithm formula allows smoothing out the color between the points (is this point likely to be in the set or not --> color), so you get something like a gradient. – michaeln May 04 '13 at 17:49

1 Answers1

1

Here's a quick `n dirty adaptation of my answer to another question about mapping a range of values to pseudocolors that allows them to be mapped to a whole palette of RGB colors instead of only two. Note that the in-between colors are being interpolated in RGB colorspace, not HSV (which is generally nicer looking in my opinion, but requires more computation).

I'm not completely happy with it, but my time is very limited this weekend and at least what I have so far seems to work, even if it's sub-optimal, so I'll post it for you to play around with:

def palette_pseudocolor(val, minval, maxval, palette):
    max_index = len(palette)-1
    # convert val in range minval...maxval to range 0..max_index
    v = (float(val-minval) / (maxval-minval)) * max_index
    # split result into integer and fractional parts
    i = int(v); f = v-i
    # interpolate between two colors in the palette
    c0, c1 = palette[i], palette[min(i+1, max_index)]
    d = c1[0]-c0[0], c1[1]-c0[1], c1[2]-c0[2]
    return c0[0]+f*d[0], c0[1]+f*d[1], c0[2]+f*d[2]

if __name__ == '__main__':
    numsteps = 10
    palette = [(1,0,0), (0,1,0), (0,0,1)] # [RED, GREEN, BLUE]
    print 'val       R      G      B'
    for val in xrange(0, 100+numsteps, numsteps):
        print ('%3d -> (%.3f, %.3f, %.3f)' %
               ((val,) + palette_pseudocolor(val, 0, 100, palette)))

Output:

val       R      G      B
  0 -> (1.000, 0.000, 0.000)
 10 -> (0.800, 0.200, 0.000)
 20 -> (0.600, 0.400, 0.000)
 30 -> (0.400, 0.600, 0.000)
 40 -> (0.200, 0.800, 0.000)
 50 -> (0.000, 1.000, 0.000)
 60 -> (0.000, 0.800, 0.200)
 70 -> (0.000, 0.600, 0.400)
 80 -> (0.000, 0.400, 0.600)
 90 -> (0.000, 0.200, 0.800)
100 -> (0.000, 0.000, 1.000)

Here's a color gradient produced with the red, green, and blue palette in the example:

color gradient from a red, green, blue palette

Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301