import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;


/*

(c) Morten Sickel 2010
Licenced under GNU GPL v3 or later


*/


// $Id: Legend.java 547 2010-01-26 11:35:19Z radioecology $


class Legend extends JComponent {

/*
	This class makes a legend. It also defines the color array used in the waterfall
*/

	private BufferedImage legend;
	public static float max; 
	static final float DEFAULTMAX=200;  	// Default value for max
	static int[] colors; 				// set of colors to use
	static final int NCOLORS=1536;   // Numbers of steps in the colorscale
	private static boolean log;
	int w=70;
	int h=410;



	public static float getMax(){
		return log?(float)Math.log(max):max;
	}
	
	public static void setLog(boolean setlog){
		log=setlog;
	}
	public static boolean getLog(){
		return log;
	}
	
	public static int makeARGB(int a, int r, int g, int b) {
		// Creates an integer color value from an argb quartet. (0-255) 
		// (a=alpha cannel, transparency, set to 0 for non transparent colors
		return a << 24 | r << 16 | g << 8 | b;
	}
	
	public Legend(float setmax){
		colors=new int[NCOLORS+1];
		for(int i = 0; i <= 256; i++) {
			// Setting up a color scale
			// Goes along the egdes of the RGB color cube
			// cf http://www.poirrier.be/~jean-etienne/info/clibs/gd-rainbow.php
			// Red (255,0,0) to yellow (255,255,0)
			colors[NCOLORS-i] = makeARGB(0,255,i,0);
			// Yellow to green (0,255,0)
			colors[NCOLORS-(i + 256)] = makeARGB(0,255-i,255,0);
			// Green to cyan (0,255,255)
			colors[NCOLORS-(i + 512)] = makeARGB(0,0,255,i);
			// cyan to blue (0,0,255)
			colors[NCOLORS-(i + 768)] = makeARGB(0,0,255-i,255);
			// blue to mangenta( 255,0,255)
			colors[NCOLORS-(i + 1024)] = makeARGB(0,i,0,255);
			// Should go back to red, but
			// add the first -i to go down into black (0,0,0)
			// rather than back to red (255,0,0) to make an unique color scale
			colors[NCOLORS-(i + 1280)] = makeARGB(0,255-i,0,255-i); // Fades down into black
		}
		max=setmax;
	}

	public void paintComponent(Graphics g) {
		legend= new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
		Graphics2D leg=legend.createGraphics();
		Font font = new Font("Dialog", Font.PLAIN, 12);
		int tenth=Math.round(h/10); // Makes ten marks along the scale. Makes perfect sense for linear, may rethink for log
		int next=0; // Next value to mark
		int n=0; // Number of marks
		float drawmax=getMax();
		for (int i=0;i<=h-10;i++){
			int row=i+5; 
			if (i==next || i == h-10){ // draw a marker
				float num=drawmax*(10-n++)/10;  // Which number to mark. The numbers go the opposite direction of the y-axis numbers
				if(log){
					num=(float)Math.exp(num);
				}
				leg.setColor(Color.BLACK);
				leg.drawString(Float.toString(Math.round(num)),25,row+5);
				leg.drawLine(5,row,20,row); 
				next+=tenth;
			}else{ // Draw a normal colored line
				leg.setColor(new Color(colors[(h-10-i)*NCOLORS/(h-10)]));
				leg.drawLine(5,row,15,row);
			}
		}
		g.drawImage(legend,-5,5,null);
    }

	public Dimension getPreferredSize() {
        return new Dimension(w, h);
    }
}

