将彩虹颜色映射到RGB

问题描述:

假设我有一个类RainbowColorsMapper,其构造函数为 RainbowColorsMapper(int n),其中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?

最简单的方法是使用 HSL colourspace 而不是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.