/// Evolver Copyright 2000, Morgan McGuire
/// matrix@alum.mit.edu   http://graphics3d.com/matrix
import java.awt.*;

public final class HGradient extends ZeroArgumentOperator implements ELOperator 
{
  static float coeff[];
  static {
    coeff = new float[128];
    for(int x = 0; x < 128; x++) {
      coeff[x] = 2.0f * (x / 127.0f) - 1.0f;
    }
  }

  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();
    float span = width - 1;

    if ((width == 128) && (height == 128)) {
      final float coeff[] = this.coeff;
      for(int y = 127; y >= 0; y--) {
        int offset = y * 128;
        System.arraycopy(coeff, 0, red, offset, 128);
        System.arraycopy(coeff, 0, grn, offset, 128);
        System.arraycopy(coeff, 0, blu, offset, 128);
      }
    } else {
      for(int x = width - 1; x >= 0; x--) {
        float value = 2.0f * (x / span) - 1.0f;
        int i = (out.getHeight() - 1) * width + x;
        for(int y = out.getHeight() - 1; y >= 0; y--) {
          red[i] = value;
          grn[i] = value;
          blu[i] = value;
          i -= width;
        }      
      }
    }
  }

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

  public String description()
  {
    return "A horizontal gradient from -1 to 1";
  }  


  public void paintIcon(Graphics2D graphics, int x, int y, int width, int height)
  {
    graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    for(int xx = 0; xx < width; xx++) {
      float v = xx / (float)(width - 1);
      graphics.setColor(new Color(v, v, v, 1.0f - v));
      graphics.drawLine(x + xx, y, x + xx, y + height - 1);
    }
  }

}
