1

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

编辑:忘了提,我没有使用一个类来进行复杂的引用,我使用维基百科上显示的算法计算分形。

4

1 回答 1

1

这是我对另一个关于将一系列值映射到伪色的问题的回答的快速'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)

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

来自红色、绿色、蓝色调色板的颜色渐变

于 2013-05-05T15:43:26.620 回答