Java:更好的颜色选择算法

发布于 2024-12-25 12:48:34 字数 1182 浏览 4 评论 0原文

我有一个 ArrayList,其值根据大小转换为 Arc2D.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2025-01-01 12:48:34

我喜欢灰度

 Color.rgb( 255 * (float)index/(float)total, 255 * (float)index/(float)total, 255 *      (float)index/(float)total);

,否则颜色分割的领域相当大。有些人可以把它分成几个乐队。

否则,如果你只需要 4 种颜色,我会选择效果好的并从表格中挑选:例如红色、蓝色、绿色和黑色。

I like greyscale

 Color.rgb( 255 * (float)index/(float)total, 255 * (float)index/(float)total, 255 *      (float)index/(float)total);

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.

冰雪之触 2025-01-01 12:48:34

到目前为止,我发现的最好的算法使用的是我修改过的之前的SO算法。我将 MIX 保持在恒定的浅灰色 (Color.LIGHT_GRAY),并使用种子 Random 对象,以便颜色始终相同每次迭代,但之间有所不同。

Random random = new Random(seriesIndex);
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);

// mix the color
if (MIX != null) {
    red = (red + MIX.getRed()) / 2;
    green = (green + MIX.getGreen()) / 2;
    blue = (blue + MIX.getBlue()) / 2;
}

Color c = new Color(red, green, blue);

事实证明,它的颜色看起来很漂亮,而且相当容易辨认。

Example image

感谢 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 seeded Random object so that the color is always the same for each iteration, but different between.

Random random = new Random(seriesIndex);
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);

// mix the color
if (MIX != null) {
    red = (red + MIX.getRed()) / 2;
    green = (green + MIX.getGreen()) / 2;
    blue = (blue + MIX.getBlue()) / 2;
}

Color c = new Color(red, green, blue);

It turns out with nice-looking colors that are fairly discernable.

Example image

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文