Java 生成光谱调色板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/223971/
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-08-11 11:40:31  来源:igfitidea点击:

Generating spectrum color palettes

javacolors

提问by ddimitrov

Is there an easy way to convert between color models in Java (RGB, HSV and Lab).

是否有一种简单的方法可以在 Java(RGB、HSV 和 Lab)中的颜色模型之间进行转换。

Assuming RGB color model:

假设RGB颜色模型:

  • How do I calculate black body spectrum color palette? I want to use it for a heatmap chart.
  • How about single-wavelength spectrum?
  • 如何计算黑体光谱调色板?我想将它用于热图图表。
  • 单波长光谱怎么样?

Edit:I found that the ColorSpaceclass can be used for conversions between RGB/CIE and many other color models.

编辑:我发现ColorSpace类可用于 RGB/CIE 和许多其他颜色模型之间的转换。

采纳答案by Nils Pipenbrinck

You can build such a palette using the HSV color-model. That's easy once you have the HSV to RGB code in place and play around with the numbers for some minutes.

您可以使用 HSV 颜色模型构建这样的调色板。一旦您拥有 HSV 到 RGB 代码并在几分钟内处理数字,这很容易。

However, I think it's not worth it to add the code to your project just to generate a little palette.

但是,我认为将代码添加到您的项目中只是为了生成一个小调色板是不值得的。

It's much easier and less work to extract the palettes you need from a file and add them as a static array.

从文件中提取您需要的调色板并将它们添加为静态数组要容易得多,工作也更少。

Photoshop let's you edit palettes and comes with a very nice black body palette as a preset.

Photoshop 可让您编辑调色板,并附带一个非常漂亮的黑色主体调色板作为预设。

You can simply save these as a .act file. The file itself is just a simple 256 color á 3 byte file (order is read, green, blue. 8 bits per channel).

您可以简单地将这些保存为 .act 文件。文件本身只是一个简单的 256 色 á 3 字节文件(顺序为读取、绿色、蓝色。每个通道 8 位)。

回答by Mark Bessey

Maybe I'm not understanding your question, but you can't really generate a true black-body spectrum from an RGB output device. Limited color gamut would be an issue, if nothing else. If all you want is something that visually resembles a black-body spectrum, that's probably a lot easier.

也许我不明白您的问题,但您无法真正从 RGB 输出设备生成真正的黑体光谱。如果不出意外,有限的色域将是一个问题。如果您想要的只是在视觉上类似于黑体光谱的东西,那可能要容易得多。

As an approximation, ramp from (R,G,B) (0,0,0) to (255,0,0), then to (255,255,0), then to (255,255,255). That'd give you the dull-red to orange, to yellow, to white transition.

作为近似值,从 (R,G,B) (0,0,0) 到 (255,0,0),然后到 (255,255,0),再到 (255,255,255)。这会给你暗红色到橙色、黄色、白色的过渡。

If you want something more scientific, the Wikipedia article on black body radiationhas some plots of color vs temperature. Once you figure out the CIE coordinates, you can translate those to RGB in your favorite color space.

如果你想要更科学的东西,维基百科关于黑体辐射的文章有一些颜色与温度的关系图。找出 CIE 坐标后,您可以将它们转换为您喜欢的色彩空间中的 RGB。

Edit: found some other online references: What color is the Sun?What color is a blackbody?

编辑:找到其他一些在线参考资料: 太阳是什么颜色的?黑体是什么颜色?

回答by luke

Java has built-in RGB to HSB conversion. Whenever I need a quick pallet of colors in Java I just do this:

Java 内置了 RGB 到 HSB 的转换。每当我需要一个快速的 Java 颜色托盘时,我就这样做:

public Color[] generateColors(int n)
{
    Color[] cols = new Color[n];
    for(int i = 0; i < n; i++)
    {
        cols[i] = Color.getHSBColor((float) i / (float) n, 0.85f, 1.0f);
    }
    return cols;
}

It is a quick and dirty hack (I would tweak the 'magic' numbers for your app), but for my simple uses it generates a nice bright pleasant pallet.

这是一个快速而肮脏的黑客(我会为您的应用程序调整“魔术”数字),但对于我的简单使用,它会生成一个漂亮的明亮宜人的托盘。

回答by ocodo

This is a nice way to make a HSL color square in AS3.

这是在 AS3 中制作 HSL 颜色方块的好方法。

/**
 * Generate a BitmapData HSL color square (n x n) of hue
 * At a low n dimension you get cool blocky color palettes (e.g. try n=10)
 */
function generateColorSquare(n:uint, hue:uint):BitmapData
            {
                var bd:BitmapData = new BitmapData(n, n, false, 0xFFFFFF);
                for (var i:uint=n*n; i > 0; i--)
                {
                    bd.setPixel(i % n, Math.floor(i / n),  HSBColor.convertHSBtoRGB(hue, i / (n*n), (1/n) * (i % n) ));
                }
                return bd;
            }

回答by Sean Carey

You can generate this color spectrum https://i.stack.imgur.com/ktLmt.jpg

您可以生成此色谱https://i.stack.imgur.com/ktLmt.jpg

using the following code:

使用以下代码:

public void render(Screen screen) {
    int green = 255;
    int red = 0;

    for (int i = 0; i <= 255 * 2; i++) {
        int rate = i / 255;

        screen.fillRect((x + (i * width)/6), y, width, height, new Color(red, green, 0));

        red += 1 - rate;
        green -= rate;
    }   
}