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

public final class ToRGB 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;

    for(int i = out.numElements - 1; i >= 0; i--) {
      float h = ared[i];
      if (h < 0.0f) {
        h = 0.0f;
      } else if (h > 1.0f) {
        h = 1.0f;
      }

      float s = agrn[i];
      if (s < 0.0f) {
        s = 0.0f;
      } else if (s > 1.0f) {
        s = 1.0f;
      }

      float v = ablu[i];
      if (v < 0.0f) {
        v = 0.0f;
      } else if (v > 1.0f) {
        v = 1.0f;
      }

      int c = Color.HSBtoRGB(h, s, v);

      red[i] = ((c & 0x00FF0000) >> 16) / 256.0f;
      grn[i] = ((c & 0x0000FF00) >> 8) / 256.0f;
      blu[i] = ((c & 0x000000FF)) / 256.0f;
    }
  }

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

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

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