将彩虹颜色映射到 RGB
假设我有一个带有构造函数 RainbowColorsMapper(int n)
的 RainbowColorsMapper 类,其中 n>=2。现在我想要使用 Mapper.getColor(int number) 方法获得从红色到紫色的彩虹颜色的连续映射,其中低值对应于红色端,高值对应于紫色端。如果 n = 2,mapper.getColor(0)
返回光谱最左侧的颜色(接近红色),mapper.getColor(1)
返回最右侧的颜色。与具有自动缩放功能的较大 n 相同。
我的问题:这可以相对简单地完成吗?如果是的话,对算法有什么建议?
Suppose I have a class RainbowColorsMapper with the constructor RainbowColorsMapper(int n)
, where n>=2. Now I want to have continuous mapping of rainbow colors from red to violet which I get using the method mapper.getColor(int number)
where low values correspond to red end, and high near n to violet end. If n = 2, mapper.getColor(0)
returns most left color of the spectrum (near red), and mapper.getColor(1)
returns the most right color. Same with bigger n with automatic scaling.
My question: can this be done relatively easy, and if yes what are suggestions on the algorithm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简单的方法是在 HSL 颜色空间而不是 RGB 中工作。创建饱和度和亮度固定的颜色(我建议固定为 100% 和 50%),并且色调在合适的端点之间变化(您可能需要进行实验才能找到)。使用 Color.getHSBColor。
The easiest way to do this will be to work in the HSL colourspace rather than RGB. Create colours where the saturation and lightness are fixed (to 100% and 50%, i would suggest), and the hue varies between suitable endpoints (which you might need to experiment to find). Convert the HSL values to RGB using Color.getHSBColor.
请记住,彩虹的颜色是根据波长排序的,因此基本上在您的模型中,
n
在某种程度上与波长相关。所以你的问题本质上可以归结为将波长 (n
) 映射到 RGB。这不是一个完全微不足道的过程,但首先,您可以检查这个问题:Convert light频率到RGB?
Remember that the colours of the rainbow are ordered according to wavelength, so basically in your model,
n
is somehow related to wavelength. So your question essentially boils down to mapping wavelength (n
) to RGB. This is not an entirely trivial process, but for a start, you could check this question out:Convert light frequency to RGB?
或者使用色相饱和度值颜色模型,并迭代色相。
Or use a Hue Saturation Value color model, and iterate over Hue.
颜色模型中的色调基本上从 0 变化到 300
如何根据色调计算 RGB,您可以在 Wikipedia 上找到
You basically have a hue change from 0 to 300 in the colour model
How to calculate RGB from Hue you can find on Wikipedia
HSL Color 可以让您轻松做到这一点。
HSL Color allows you to do this easily.