如何自动生成N“不同”的颜色?

如何自动生成N“不同”的颜

我写下了两种方法来自动选择N种不同的颜色。它的工作原理是在RGB立方体上定义分段线性函数。这样做的好处是,如果这是你想要的,你也可以得到一个渐进的比例,但是当N变大时,颜色可以开始看起来相似。我还可以想象将RGB立方体均匀地细分为格子然后绘制点。有谁知道其他任何方法?我排除了定义一个列表然后只是循环通过它。我还应该说我一般不关心他们是否发生冲突或看起来不好看,他们只需要在视觉上截然不同。

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);}



largeQ
浏览 743回答 3
3回答

MMMHUHU

您可以使用HSL颜色模型来创建颜色。如果您想要的是不同的色调(可能),以及亮度或饱和度的轻微变化,您可以像这样分配色调://&nbsp;assumes&nbsp;hue&nbsp;[0,&nbsp;360),&nbsp;saturation&nbsp;[0,&nbsp;100),&nbsp;lightness&nbsp;[0,&nbsp;100)for(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;360;&nbsp;i&nbsp;+=&nbsp;360&nbsp;/&nbsp;num_colors)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;HSLColor&nbsp;c; &nbsp;&nbsp;&nbsp;&nbsp;c.hue&nbsp;=&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;c.saturation&nbsp;=&nbsp;90&nbsp;+&nbsp;randf()&nbsp;*&nbsp;10; &nbsp;&nbsp;&nbsp;&nbsp;c.lightness&nbsp;=&nbsp;50&nbsp;+&nbsp;randf()&nbsp;*&nbsp;10; &nbsp;&nbsp;&nbsp;&nbsp;addColor(c);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java