tags:

views:

3005

answers:

4

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.

+2  A: 

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

Nils Pipenbrinck
+1  A: 

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?

Mark Bessey
Sorry, I meant a projection of BBS in the monitor's color space (the kind of gradient that is used for heatmaps). The links are interesting.
ddimitrov
+4  A: 

java has builtin RGB to HSB conversion, when ever 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;
}

its a quick dirty hack (i would tweak the 'magic' numbers for your app) but for my simple uses it generates a nice bright pleasant pallet.

luke
Thanks. This solves my rainbow palette needs.
ddimitrov
2 years later, this helped me too. Thanks.
Coronatus
A: 

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;
            }
slomojo