如何动态计算颜色列表? [英] How to dynamically compute a list of colors?
问题描述
为了在GWT-Widget中表示不同颜色的对象列表,我们需要动态地获取与对象具有不同颜色的颜色列表。由于列表的大小可以不同,因此我们需要能够计算这种颜色列表。
In order to represent a List of Objects with different colors in a GWT-Widget, we need to get dynamically a List of colors with as much different colors as objects. Since the size of the List can vary, we need to be able to compute such a List of colors.
推荐答案
我的解决方案范围:
List<int> getUniqueColors(int amount) {
final int lowerLimit = 0x10;
final int upperLimit = 0xE0;
final int colorStep = (upperLimit-lowerLimit)/Math.pow(amount,1f/3);
final List<int> colors = new ArrayList<int>(amount);
for (int R = lowerLimit;R < upperLimit; R+=colorStep)
for (int G = lowerLimit;G < upperLimit; G+=colorStep)
for (int B = lowerLimit;B < upperLimit; B+=colorStep) {
if (colors.size() >= amount) { //The calculated step is not very precise, so this safeguard is appropriate
return colors;
} else {
int color = (R<<16)+(G<<8)+(B);
colors.add(color);
}
}
return colors;
}
这一个更加先进,因为它产生彼此不同的颜色尽可能多的东西(像@aiiobe做)。
This one is more advance as it generates the colors that differ from each other as much as possible (something like @aiiobe did).
一般来说,我们将范围分为红色绿色和蓝色的3个子范围,计算我们需要迭代多少步数(通过应用pow(range,1f / 3))并迭代它们。
Generally we split the range to 3 subranges of red green and blue, calculate how many steps do we need to iterate each of them (by applying a pow(range,1f/3)) and iterate them.
例如,给定数字3,它将生成 0x0000B1,0x00B100,0x00B1B1
。对于数字10,它将是: 0x000076,0x0000EC,0x007600,0x007676,0x0076EC,0x00EC00,0x00EC76,0x00ECEC,0x760000,0x760076
Given the number 3 for example, it will generate 0x0000B1, 0x00B100, 0x00B1B1
. For number 10 it will be: 0x000076, 0x0000EC, 0x007600, 0x007676, 0x0076EC, 0x00EC00, 0x00EC76, 0x00ECEC, 0x760000, 0x760076
这篇关于如何动态计算颜色列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!