Colormaps in JAVA images

In my previous post I showed how to dinamically create a JPEG from a java array.

The problem with these approach is that the color is not very intuitive.
Now I want to create a suitable colormap for the image.

Rainbow

The first colormap will be rainbow. We create this colormap working with HSB, we will change the hue and leave the brightness and saturation at a fixed value.

public static BufferedImage generateImage(double[][] data, int pixelsWidth, int pixelsHeight ) throws Exception{

        double x =0,y = 0;
        BufferedImage bi = new BufferedImage( pixelsWidth, pixelsHeight,  BufferedImage.TYPE_3BYTE_BGR );
        Graphics2D g=(Graphics2D)bi.getGraphics();

        int w=data[0].length;
        int l=data.length;
        double Dy=(double)pixelsHeight/(double)l;
        double Dx=(double)pixelsWidth/(double)w;
        int height = (int)Math.ceil(Dy);
        int width = (int)Math.ceil(Dx);	
        int gap=(int)Math.ceil(width/4);
        
    	
    	double max=-Double.MAX_VALUE;
    	double min=Double.MAX_VALUE;
    	for (int i=0;i<l;i++){
    		for (int j=0;jmax) max=data[i][j];
    			if (data[i][j]<min) min=data[i][j];
    		}
    	}        
        
        float fcolor;
        float saturation=(float)0.9;
        float bright=(float)0.7;
        
        g.setColor( Color.WHITE);
        g.fillRect( 0, 0 , pixelsWidth,pixelsHeight); 
        for( int i =0; i < l; i++ ){        	
            for( int j =0; j < w; j++ ){            	
            	fcolor=(float)((data[i][j]-min)/(max-min));
           	g.setColor(Color.getHSBColor(hue*0.7, saturation, bright));             		                  
                g.fillRect( (int)x, (int)y , width-gap,height);        
                x+=Dx;
            }
            y+=Dy;
            x=0;
        }
        g.dispose();

        return bi;
    }  

I multiply the hue by 0.7 to magnify the difference between the greater and smaller value because hue=1 and hue=0 are almost the same color.

This is an example of this colormap:
29

Dark body radiator

This colormap fixes the hue and changes the saturation and brightness. It is easily created from the previous one with the next change:

g.setColor(Color.getHSBColor((float)0.2, (float)Math.sqrt(1-fcolor), fcolor));

An example of this colormap is:
29

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s