问题描述
我正在用C#编写Mandelbrot应用程序(并且正在使用Python进行测试).我已经从布景到其边界连续着色.我当前的问题是设置环境的背景色.我当前的获取颜色的代码现在看起来像这样,将颜色获取为两倍(对数函数之前已完成),并检查颜色是否为一部分,并创建一个相当平滑的渐变(从黑色到橙色).
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
}
如何将黑色环境(而不是Mandelbrot集)更改为另一种颜色,例如模糊的Python脚本( http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python )可以吗?我已经将脚本编辑为更好的形式,但是它不适合我的算法.
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.
忘了提一下,我不是在复杂的引用中使用类,而是使用Wikipedia上显示的算法来计算分形.
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.
推荐答案
这里是我对另一个问题的答案的快速n改编. 关于将一定范围的值映射到伪色,从而可以将它们映射到整个RGB颜色调色板,而不是仅映射到两个.请注意,中间的颜色是在RGB色彩空间中插值的,而不是HSV(在我看来,这通常看起来更好,但需要更多的计算).
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)))
输出:
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:
这篇关于Mandelbrot-算法-背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!