java 将彩虹色映射到 RGB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5513690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:29:42  来源:igfitidea点击:

Map rainbow colors to RGB

javargb

提问by bvk256

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.

假设我有一个带有构造函数的 RainbowColorsMapper 类RainbowColorsMapper(int n),其中 n>=2。现在我想要连续映射从红色到紫色的彩虹色,我使用的方法mapper.getColor(int number)是低值对应于红色端,高值接近 n 到紫色端。如果 n = 2,则mapper.getColor(0)返回光谱中最左边的颜色(接近红色),并mapper.getColor(1)返回最右边的颜色。与具有自动缩放功能的更大 n 相同。

My question: can this be done relatively easy, and if yes what are suggestions on the algorithm?

我的问题:这是否可以相对容易地完成,如果是,对算法有什么建议?

回答by Tom Anderson

The easiest way to do this will be to work in the HSL colourspacerather 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.

最简单的方法是在HSL 色彩空间而不是 RGB 中工作。创建饱和度和亮度固定的颜色(我建议为 100% 和 50%),并且色调在合适的端点之间变化(您可能需要尝试找到)。使用Color.getHSBColor将 HSL 值转换为 RGB 。

回答by Dunnie

Remember that the colours of the rainbow are ordered according to wavelength, so basically in your model, nis 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:

请记住,彩虹的颜色是根据波长排序的,所以基本上在您的模型中,n它与波长有某种关系。所以你的问题基本上归结为将波长 ( n)映射到 RGB。这不是一个完全微不足道的过程,但首先,您可以查看以下问题:

Convert light frequency to RGB?

将光频转换为RGB?

回答by Peter

 private int r = 255;
 private int g = 0;
 private int b = 0;

 private void nextRGB() {
     if (r == 255 && g < 255 && b == 0) {
         g++;
     }
     if (g == 255 && r > 0 && b == 0) {
         r--;
     }
     if (g == 255 && b < 255 && r == 0) {
         b++;
     }
     if (b == 255 && g > 0 && r == 0) {
         g--;
     }
     if (b == 255 && r < 255 && g == 0) {
         r++;
     }
     if (r == 255 && b > 0 && g == 0) {
         b--;
     }
 }

 public Color nextColor() {
     nextRGB();
     return makeColor();
 }

 private Color makeColor() {
     return new Color(r, g, b);
 }

回答by Mikeb

Or use a Hue Saturation Value color model, and iterate over Hue.

或者使用 Hue Saturation Value 颜色模型,并迭代 Hue。

回答by Fabian Tschachtli

You basically have a hue change from 0 to 300 in the colour model

您基本上在颜色模型中有一个从 0 到 300 的色调变化

How to calculate RGB from Hue you can find on Wikipedia

如何从 Hue 计算 RGB,您可以在维基百科上找到

回答by camickr

HSL Colorallows you to do this easily.

HSL Color可让您轻松完成此操作。