/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
public class SpectrumAnalyzer extends ZeroArgumentOperator implements ELOperator 
{
  public void apply(ELMatrix argument[], ELMatrix out, ELEnvironment environment)
  {
    checkArguments(argument);
    
    // For extra performance, pull the arrays into locals so we avoid
    // excess dereferences.
    float red[] = out.red;
    float grn[] = out.grn;
    float blu[] = out.blu;

    int width = out.getWidth();
    int height = out.getHeight();

    double frequency[] = environment.frequency;
    int flength = frequency.length;
    
    out.setAll(0f, 0f, 0f);

    for(int x = width - 1; x >= 0; x--) {
	int f = (x * flength) / width;
	
	final float value = (float)frequency[f];
	final int y = (int)((1f - value) * (height - 4)) + 2;
	final int offset = x + ((y - 1) * width);

	final int o1 = offset;
	final int o2 = o1 + width;
	final int o3 = o2 + width;

	float r2 = value;
	float b2 = 1f - value;
	float r1 = value / 2;
	float b1 = (b2) / 2;

	red[o1] = r1; grn[o1] = r1; blu[o1] = b1;
	red[o2] = r1; grn[o2] = r2; blu[o2] = b2;
	red[o3] = r1; grn[o3] = r1; blu[o3] = b1;
    }      
  }

  public String name()
  {
    return "SpectrumAnalyzer";
  }

  public String description()
  {
    return "Spectrum analyzer";
  }

  protected String iconString()
  {
    return "Spe";
  }

  public boolean hasExternalInfluences() {
    return true;
  }
}
