如何自动生成 N 个“不同”的颜色? - How to automatically generate N "distinct" colors?

10/14 05:46
阅读数 0

问题:

I wrote the two methods below to automatically select N distinct colors.我写了下面的两种方法来自动选择N个不同的颜色。 It works by defining a piecewise linear function on the RGB cube.它的工作原理是在 RGB 立方体上定义分段线性函数。 The benefit of this is you can also get a progressive scale if that's what you want, but when N gets large the colors can start to look similar.这样做的好处是,如果这是您想要的,您还可以获得渐进式比例,但是当 N 变大时,颜色可能会开始看起来相似。 I can also imagine evenly subdividing the RGB cube into a lattice and then drawing points.我还可以想象将 RGB 立方体均匀地细分为一个点阵,然后绘制点。 Does anyone know any other methods?有谁知道其他方法吗? I'm ruling out defining a list and then just cycling through it.我排除了定义一个列表然后循环遍历它的可能性。 I should also say I don't generally care if they clash or don't look nice, they just have to be visually distinct.我还应该说,我通常不在乎它们是否发生冲突或看起来不好看,它们只需要在视觉上与众不同。

public static List<Color> pick(int num) {
    List<Color> colors = new ArrayList<Color>();
    if (num < 2)
        return colors;
    float dx = 1.0f / (float) (num - 1);
    for (int i = 0; i < num; i++) {
        colors.add(get(i * dx));
    }
    return colors;
}

public static Color get(float x) {
    float r = 0.0f;
    float g = 0.0f;
    float b = 1.0f;
    if (x >= 0.0f && x < 0.2f) {
        x = x / 0.2f;
        r = 0.0f;
        g = x;
        b = 1.0f;
    } else if (x >= 0.2f && x < 0.4f) {
        x = (x - 0.2f) / 0.2f;
        r = 0.0f;
        g = 1.0f;
        b = 1.0f - x;
    } else if (x >= 0.4f && x < 0.6f) {
        x = (x - 0.4f) / 0.2f;
        r = x;
        g = 1.0f;
        b = 0.0f;
    } else if (x >= 0.6f && x < 0.8f) {
        x = (x - 0.6f) / 0.2f;
        r = 1.0f;
        g = 1.0f - x;
        b = 0.0f;
    } else if (x >= 0.8f && x <= 1.0f) {
        x = (x - 0.8f) / 0.2f;
        r = 1.0f;
        g = 0.0f;
        b = x;
    }
    return new Color(r, g, b);
}

解决方案:

参考一: https://en.stackoom.com/question/1yRm
参考二: https://stackoom.com/question/1yRm
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部