将彩虹颜色映射到 RGB

发布于 2024-10-28 12:47:26 字数 330 浏览 10 评论 0原文

假设我有一个带有构造函数 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 技术交流群。

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

发布评论

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

评论(6

半世晨晓 2024-11-04 12:47:26

最简单的方法是在 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.

吃素的狼 2024-11-04 12:47:26

请记住,彩虹的颜色是根据波长排序的,因此基本上在您的模型中,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?

旧梦荧光笔 2024-11-04 12:47:26

或者使用色相饱和度值颜色模型,并迭代色相。

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

海拔太高太耀眼 2024-11-04 12:47:26

颜色模型中的色调基本上从 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

未蓝澄海的烟 2024-11-04 12:47:26

HSL Color 可以让您轻松做到这一点。

HSL Color allows you to do this easily.

好倦 2024-11-04 12:47:26
 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);
 }
 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);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文