我写了下面两种方法来自动 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);
}