Java:更好的颜色选择算法
我有一个 ArrayListArc2D.Pie
图的切片(即 {1d 数组) ,1d,2d,4d}
将创建一个包含四个切片的图,其中两个切片各占图的八分之一,其中一个占四分之一,其中一个合计占图的一半;被安排形成一个完整的圆圈)。每个切片的颜色都不同,既可以轻松地将切片映射到其他指标(名称等),又可以避免平淡无奇。
我选择颜色的算法最初非常简单:
public Color getColor(int index, int total) {
return Color.getHSBColor((float) index / (float) total, 1f, 1f);
}
然而,虽然这对于大多数颜色(仅举几例,红色和蓝色)都适用,但有些颜色,例如青色(作为一组两个元素中的第二个元素特别突出;例如, {1d, 3d}
) 的数组不容易被人眼看到,因为它们太亮了。
此后我更改了算法
return Color.getHSBColor((float) index / (float) total, 1f, 0.8f);
以尝试使颜色变暗;然而,青色仍然很难看清,进一步降低亮度会使颜色变得相当难以区分。
有没有人有一个很好的、最好是简单的算法来创建一系列易于查看的颜色?一个好的测试是生成淡黄色和青色,并查看它们是否在白色背景上易于查看。
谢谢!
WC
编辑:我现在改进了我的算法,通过改变饱和度和亮度来制作更多不同的颜色。不过,颜色看起来更加不稳定;还有什么想法吗?
新算法:
float hue = (float) seriesIndex / (float) totalSeries;
int steps = 4;
float saturation = ((seriesIndex) % steps / (2f * steps)) + (0.5f);
float brightness = ((seriesIndex + ((steps + 1) / 2f)) % (steps + 1f) / (2f * (steps + 1f))) + 0.5f;
Color c = Color.getHSBColor(hue, saturation, brightness);
I have an ArrayList<Double>
, the values of which are converted into slices of an Arc2D.Pie
graph based on size (i.e., an array of {1d,1d,2d,4d}
would create a graph with four slices, two of which occupy an eighth of the graph each, one of which occupies a fourth, and one of which occupies a half; together, the slices would be arranged to form a full circle). Each of these slices is colored differently both to allow for easy mapping of slices to other indicators (names, etc.) and to avoid blandness.
My algorithm for selecting colors was initially quite simple:
public Color getColor(int index, int total) {
return Color.getHSBColor((float) index / (float) total, 1f, 1f);
}
However, while this works well with most colors (red and blue, to name a few), some, such as cyan (especially prominent as the second element in a set of two; e.g., an array of {1d, 3d}
) are not easily viewable by the human eye, as they are too light.
I have since changed the algorithm to
return Color.getHSBColor((float) index / (float) total, 1f, 0.8f);
in an attempt to darken the colors; however, cyan is still hard to see, and lowering the brightness further makes the colors rather hard to distinguish.
Does anyone have a nice, preferably simple algorithm that will create a spectrum of colors that are all easily viewable? A good test would be to generate yellowish and cyanish colors and see if they are easily viewable on a white background.
Thanks!
WC
EDIT: I have now improved my algorithm to make more varied colors, by varying saturation and lightness. The colors look choppier, though; any more ideas?
New algorithm:
float hue = (float) seriesIndex / (float) totalSeries;
int steps = 4;
float saturation = ((seriesIndex) % steps / (2f * steps)) + (0.5f);
float brightness = ((seriesIndex + ((steps + 1) / 2f)) % (steps + 1f) / (2f * (steps + 1f))) + 0.5f;
Color c = Color.getHSBColor(hue, saturation, brightness);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我喜欢灰度
,否则颜色分割的领域相当大。有些人可以把它分成几个乐队。
否则,如果你只需要 4 种颜色,我会选择效果好的并从表格中挑选:例如红色、蓝色、绿色和黑色。
I like greyscale
Otherwise, the field of color segmentation is pretty big. Some people are okay breaking it up into bands.
Otherwise, if you just need 4 colors, I would pick ones that work good and pick from a table: Such as Red, Blue, Green and Black.
到目前为止,我发现的最好的算法使用的是我修改过的之前的SO算法。我将
MIX
保持在恒定的浅灰色 (Color.LIGHT_GRAY
),并使用种子Random
对象,以便颜色始终相同每次迭代,但之间有所不同。事实证明,它的颜色看起来很漂亮,而且相当容易辨认。
感谢 nmjohn 帮助我解决这个问题,给我一些想法,并为我指明方向方向正确!
WC
注意:我会将其开放两天。如果有人提出更好的算法,我会接受他的。否则,我会接受这个。
The best algorithm I've found so far uses a previous SO algorithm that's I've modified. I keep
MIX
at a constant light gray (Color.LIGHT_GRAY
), and use a seededRandom
object so that the color is always the same for each iteration, but different between.It turns out with nice-looking colors that are fairly discernable.
Thanks to nmjohn for helping me solve this, giving me ideas, and pointing me in the right direction!
WC
NOTE: I'll leave this open for two days. If anyone comes up with a nicer algorithm, I'll accept his. Otherwise, I'll accept this.