我写了下面两种方法来自动 Select N种不同的 colored颜色 .它的工作原理是在RGB立方体上定义一个分段线性函数.这样做的好处是,如果你想要的话,你也可以得到一个渐进的比例,但是当N变大时, colored颜色 可以开始看起来相似.我还可以想象将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);
}

推荐答案

你可以使用HSL color model来创建你的 colored颜色 .

如果你想要的只是不同的色调(可能),以及亮度或饱和度的轻微变化,你可以这样分配色调:

// assumes hue [0, 360), saturation [0, 100), lightness [0, 100)

for(i = 0; i < 360; i += 360 / num_colors) {
    HSLColor c;
    c.hue = i;
    c.saturation = 90 + randf() * 10;
    c.lightness = 50 + randf() * 10;

    addColor(c);
}

Java相关问答推荐

Kotlin ReadWriteProperty:无法使用T作为具体化类型参数.改为使用类

Java字符串常数池困惑

我应该避免在Android中创建类并在运行时编译它们吗?

Java 22模式匹配不适用于记录模式匹配.给出汇编问题

Java应用程序崩溃时试图读取联系人从电话

在Java中将Charsequence数组更改为String数组或List String<>

为什么我们不能实现两个接口,其中一个接口有相同的签名,其中一个接口有默认的实现在java?'

@org.springframework.beans.factory.annotation.Autowired(required=true)-注入点有以下注释:-SpringBoot

是否保证在事务性块的末尾标记违反约束?

Hibernate EmptyInterceptor可以工作,但不能拦截器

无法了解Java线程所消耗的时间

如何解释Java中for-each循环中对Iterable的强制转换方法引用?

Java Mooc.fi Part 12_01.Hideout -返回和删除方法

使SLF4J在Android中登录到Logcat,在测试中登录到控制台(Gradle依赖问题)

%This内置函数示例

根本不显示JavaFX阿拉伯字母

Java组件项目中的JavaFX对话框国际化

通过/失败的参数化junit测试方法执行数

多线程、并发和睡眠未按预期工作

java.lang.ClassCastException:com.google.firebase.FirebaseException无法转换为com.google.fire base.auth.FirebaseAuthException