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

public final class ToHSV extends OneArgumentOperator 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;

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

    float hsv[] = new float[3];

    for(int i = out.numElements - 1; i >= 0; i--) {
      int r = ared[i] > 0 ? (int)(ared[i] * 256) : 0;
      if (r == 256) {
        r = 255;
      }

      int g = agrn[i] > 0 ? (int)(agrn[i] * 256) : 0;
      if (g == 256) {
        g = 255;
      }

      int b = ablu[i] > 0 ? (int)(ablu[i] * 256) : 0;
      if (b == 256) {
        b = 255;
      }

      Color.RGBtoHSB(r, g, b, hsv);

      red[i] = hsv[0];
      grn[i] = hsv[1];
      blu[i] = hsv[2];
    }
  }

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

  public String description()
  {
    return "Reinterprets channels as RGB then converts to HSV";
  }  

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