gpt4 book ai didi

c# - 曼德尔布罗特算法 - 背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 19:00:04 25 4
gpt4 key购买 nike

我正在用 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 ) 所做的那样?我已经将脚本编辑为更好的形式,但它不适合我的算法。

编辑:忘了提及,我没有使用复杂方程的类,我使用维基百科上显示的算法计算分形。

最佳答案

这是我的 answer to another question 的快速“肮脏改编”关于将一系列值映射到伪彩色,使它们能够映射到整个 RGB 颜色调色板,而不是仅两个。请注意,中间颜色是在 RGB 色彩空间中插值的,而不是 HSV(在我看来,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)

以下是示例中使用红色、绿色和蓝色调色板生成的颜色渐变:

color gradient from a red, green, blue palette

关于c# - 曼德尔布罗特算法 - 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16253547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com