Mandelbrot-算法-背景色
问题内容:
我正在用C#编写Mandelbrot应用程序(并且正在使用Python进行测试)。我已经从布景到其边界连续着色。我当前的问题是设置环境的背景色。我当前的获取颜色的代码现在看起来像这样,将颜色获取为两倍(对数函数之前已完成),并检查颜色是否存在,并创建一个相当平滑的渐变(从黑色到橙色)。
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
)?我已经将脚本编辑为更好的形式,但是它不适合我的算法。
编辑:忘了提一下,我不是使用复杂的引号类,而是使用Wikipedia上显示的算法计算分形。
问题答案:
这是我对另一个问题的回答的快速`n肮脏改编,该问题涉及将一系列值映射到伪颜色,从而使它们可以映射到整个RGB颜色调色板,而不是仅映射到两个。请注意,中间颜色是在RGB色彩空间而不是HSV中插值的(在我看来,这通常看起来更好,但需要更多的计算)。
我对它并不完全满意,但是这个周末我的时间非常有限,至少到目前为止,我的工作似乎可行,即使它不是最佳选择,因此我将其发布给您,以供您试用:
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)
这是示例中使用红色,绿色和蓝色调色板产生的颜色渐变: