Generating spectrum color palettes

2020-05-18 03:36发布

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

Assuming RGB color model:

  • 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 ColorSpace class can be used for conversions between RGB/CIE and many other color models.

标签: java colors
5条回答
家丑人穷心不美
2楼-- · 2020-05-18 04:05

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.

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.

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

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

查看更多
做个烂人
3楼-- · 2020-05-18 04:07

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.

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.

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).

查看更多
手持菜刀,她持情操
4楼-- · 2020-05-18 04:24

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

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.

查看更多
Summer. ? 凉城
5楼-- · 2020-05-18 04:25

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

/**
 * 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;
            }
查看更多
仙女界的扛把子
6楼-- · 2020-05-18 04:28

You can generate this color spectrum 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;
    }   
}
查看更多
登录 后发表回答