/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
public final class Interpolate extends ThreeArgumentOperator 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 ored[] = out.red;
    float ogrn[] = out.grn;
    float oblu[] = out.blu;

    ELMatrix a = argument[0];
    float ared[] = a.red;
    float agrn[] = a.grn;
    float ablu[] = a.blu;

    ELMatrix b = argument[2];
    float bred[] = b.red;
    float bgrn[] = b.grn;
    float bblu[] = b.blu;

    ELMatrix interp = argument[1];
    float ired[] = interp.red;
    float igrn[] = interp.grn;
    float iblu[] = interp.blu;

    for(int i = out.numElements - 1; i >= 0; i--) {
	float alpha;

	alpha = ired[i];
	if (alpha < 0) {
	    alpha = -alpha;
	}

	ored[i] = alpha * bred[i] + (1f - alpha) * ared[i];


	alpha = igrn[i];
	if (alpha < 0) {
	    alpha = -alpha;
	}

	ogrn[i] = alpha * bgrn[i] + (1f - alpha) * agrn[i];


	alpha = iblu[i];
	if (alpha < 0) {
	    alpha = -alpha;
	}

	oblu[i] = alpha * bblu[i] + (1f - alpha) * ablu[i];
    }
  }

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

  public String description()
  {
    return "Linearly interpolates between the first and third argument using the 2nd as the alpha value.";
  }  

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