package Main.Cells;
import java.awt.Color;
import java.util.Vector;
import Main.Tickable;
/**
* This class translates heat values into colors.
* Heats above the mean are tinted red. The hottest is (r,g,b)(1f,0f,0f)
* Heats below the mean are tinted blue. The coldest is (r,g,b)(0f,0f,1f)
* Heats at the mean are black. The meanest is (r,g,b)(0f,0f,0f)
* @author Rory
*
*/
public final class CellColorCoordinator implements Tickable{
private double maxInput = 0;
private double minInput = 0;
private double meanInput = 0;
private final Vector<Cell> cells = new Vector<Cell>();
private final Color[] coldColors = new Color[16];
private final Color[] hotColors = new Color[16];
public CellColorCoordinator(){
for(int i = 0; i < coldColors.length; i++)
coldColors[i] = new Color(0f,0f,(i/(float)(coldColors.length)));
for(int i = 0; i < hotColors.length; i++)
hotColors[i] = new Color((i/(float)(hotColors.length)),0f,0f);
}
public void addCell(Cell c){
cells.add(c);
}
public Color getColor(double value){
if (maxInput < value || minInput > value)
tick(0);
if(value < meanInput){
double range = meanInput - minInput;
value = meanInput - value;//TODO RIGHT HERE
value /= (range /(coldColors.length-1));
int i = (int)Math.round(value);
//System.out.println(i);
return coldColors[i];
}else if(value > meanInput){
double range = maxInput - meanInput;
value = value- meanInput;//TODO RIGHT HERE
value /= (range /(hotColors.length-1));
int i = (int)Math.round(value);
//System.out.println(i);
return hotColors[i];
}else
return Color.BLACK;
}
public void tick(double dt) {
maxInput = Double.MIN_VALUE;
minInput = Double.MAX_VALUE;
double sum = 0;
for(Cell c: cells){
if(c.getHeat() > maxInput)
maxInput = c.getHeat();
if(c.getHeat() < minInput)
minInput = c.getHeat();
sum += c.getHeat();
}
sum /= cells.size();
meanInput = sum;
}
}