将整数映射为深色

发布于 2024-10-09 15:54:40 字数 406 浏览 0 评论 0原文

我想知道如何将整数映射到颜色,但是这样做可以使序列对颜色没有太大影响(尽管它实际上并不重要),并且还可以将相同的数字插入函数一遍又一遍地给出相同的颜色。

它用于 JTable,并对表的行进行着色以突出显示具有相同 ID 的类似行。存储任何元数据都意味着大量混乱和低效率,这是我想避免的。

基本上,对于正整数范围内的所有 x,f(x)->c 满足 f(x)->c1、f(x+1)->c2、c1 !~ c2,其中 x 是 id,c 是结果颜色。我曾考虑过使用Random,但它会根据生成数字的顺序而改变。如果没有添加顺序,那就没问题了。

我忘了提及,颜色应该是深色的,这样白色文本才能正确显示。

干杯,
克里斯

I'm wondering how to go about mapping integers to colours, but doing it such that the sequence doesn't have much of an effect on the colour (although it really doesn't matter much) and also so that plugging the same number into the function over and over will give the same colour.

It's for a JTable, and colouring the row of the table to highlight those similarly that have the same ID. Storing any metadata means lots of messing about and inefficiency, which I'd like to avoid.

Basically, f(x)->c for all x in the positive range of integers such that f(x)->c1, f(x+1)->c2, c1 !~ c2, where x is the id, and c is the resulting colour. I had thought about using Random, but that changes based on the sequence of generating numbers. Without the sequentiality added in, it'd be okay.

I forgot to mention, the colours should be darkish so that the white text will show through properly.

Cheers,
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

温柔戏命师 2024-10-16 15:54:40

使用哈希函数根据整数计算RGB分量。如果每个组件通常使用 1 个字节,则生成 3 个字节的哈希值。任何合适的哈希值都将确保相似的输入将产生不相似的哈希值。

为了确保颜色足够暗,您可以将每个颜色分量中的其中一位(例如第 5 位)固定为 1。或者您可以使用 HSB 方案来固定黑暗级别。

Use a hash function to calculate the RGB components from your integer. If you're using the usual 1 byte per component, then generate a 3 byte hash value. Any decent hash value will ensure that similar inputs will result in non-similar hash values.

To make sure the colours are dark enough, you could fix one of they bits (e.g. the 5th) at 1 in each colour component. Or you could use the HSB scheme, fixing the darkness level.

左岸枫 2024-10-16 15:54:40

鉴于您对产生“深色”颜色的自动化方法的渴望,我会考虑 Color.getHSBColor(),改变色调,同时保持饱和度和亮度不变。您可能需要剪掉色轮中代表黄色的部分。

Given your desire for an automated approach that produces "darkish" colors, I would look to Color.getHSBColor(), and change the hue while keeping the saturation and brightness the same. You may have to cut out the section of the color wheel that represents yellows.

新雨望断虹 2024-10-16 15:54:40

另请考虑 Brighter()Colordarker()方法。此示例在其FocusHandler中使用前者。

Also consider the brighter() and darker() methods of Color. This example uses the former in it's FocusHandler.

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