/** Set RGB color to specified index */
 public void setRGB(int index, int rgb) {
   alpha.setValue(index, MAX_LEVEL);
   red.setValue(index, (rgb >> 16) & 0xFF);
   green.setValue(index, (rgb >> 8) & 0xFF);
   blue.setValue(index, (rgb >> 0) & 0xFF);
   gray.setValue(index, ColorUtil.getGrayMix(rgb));
 }
 /** Set ARGB color to specified index */
 public void setARGB(int index, int argb) {
   alpha.setValue(index, (argb >> 24) & 0xFF);
   red.setValue(index, (argb >> 16) & 0xFF);
   green.setValue(index, (argb >> 8) & 0xFF);
   blue.setValue(index, (argb >> 0) & 0xFF);
   gray.setValue(index, ColorUtil.getGrayMix(argb));
 }
 /** Set ARGB control point values to specified index */
 public void setARGBControlPoint(int index, Color value) {
   alpha.setControlPoint(index, (short) value.getAlpha());
   red.setControlPoint(index, (short) value.getRed());
   green.setControlPoint(index, (short) value.getGreen());
   blue.setControlPoint(index, (short) value.getBlue());
   gray.setControlPoint(index, (short) ColorUtil.getGrayMix(value));
 }
  /** @see icy.common.UpdateEventHandler#beginUpdate() */
  public void beginUpdate() {
    updater.beginUpdate();

    red.beginUpdate();
    green.beginUpdate();
    blue.beginUpdate();
    gray.beginUpdate();
    alpha.beginUpdate();
  }
  /** @see icy.common.UpdateEventHandler#endUpdate() */
  public void endUpdate() {
    alpha.endUpdate();
    gray.endUpdate();
    blue.endUpdate();
    green.endUpdate();
    red.endUpdate();

    updater.endUpdate();
  }
 /**
  * Return true is this is a linear type colormap.<br>
  * Linear colormap are used to display plain gray or color image.<br>
  * A non linear colormap means you usually have an indexed color image or you want to enhance
  * contrast/color in display.
  */
 public boolean isLinear() {
   switch (type) {
     default:
       return red.isLinear() && green.isLinear() && blue.isLinear();
     case GRAY:
       return gray.isLinear();
     case ALPHA:
       return alpha.isLinear();
   }
 }
  /**
   * Copy data from specified 2D short array.
   *
   * @param copyAlpha Also copy the alpha information.
   */
  public void copyFrom(short[][] maps, boolean copyAlpha) {
    final int len = maps.length;

    beginUpdate();
    try {
      // red component
      if (len > 0) red.copyFrom(maps[0], 8);
      if (len > 1) green.copyFrom(maps[1], 8);
      if (len > 2) blue.copyFrom(maps[2], 8);
      if (copyAlpha && (len > 3)) alpha.copyFrom(maps[3], 8);
    } finally {
      endUpdate();
    }
  }
 /**
  * Copy data from specified source colormap.
  *
  * @param copyAlpha Also copy the alpha information.
  */
 public void copyFrom(IcyColorMap srcColorMap, boolean copyAlpha) {
   beginUpdate();
   try {
     // copy colormap band
     red.copyFrom(srcColorMap.red);
     green.copyFrom(srcColorMap.green);
     blue.copyFrom(srcColorMap.blue);
     gray.copyFrom(srcColorMap.gray);
     if (copyAlpha) alpha.copyFrom(srcColorMap.alpha);
     // copy type
     setType(srcColorMap.type);
   } finally {
     endUpdate();
   }
 }
  /** @see IcyColorMap#setTypeFromData() */
  public void setTypeFromData(boolean notifyChange) {
    boolean grayColor = true;
    boolean noColor = true;
    boolean hasAlpha = false;
    IcyColorMapType cmType;

    for (int i = 0; i < MAX_INDEX; i++) {
      final short r = red.map[i];
      final short g = green.map[i];
      final short b = blue.map[i];
      final short a = alpha.map[i];

      grayColor &= (r == g) && (r == b);
      noColor &= (r == 0) && (g == 0) && (b == 0);
      hasAlpha |= (a != MAX_LEVEL);
    }

    if (noColor && hasAlpha) cmType = IcyColorMapType.ALPHA;
    else if (grayColor && !noColor) {
      // set gray map
      gray.copyFrom(red.map, 0);
      cmType = IcyColorMapType.GRAY;
    } else cmType = IcyColorMapType.RGB;

    if (notifyChange) setType(cmType);
    else type = cmType;
  }
  @Override
  public boolean saveToXML(Node node) {
    if (node == null) return false;

    XMLUtil.setElementValue(node, ID_NAME, getName());
    XMLUtil.setElementBooleanValue(node, ID_ENABLED, isEnabled());
    XMLUtil.setElementValue(node, ID_TYPE, getType().toString());

    boolean result = true;

    result = result && red.saveToXML(XMLUtil.setElement(node, ID_RED));
    result = result && green.saveToXML(XMLUtil.setElement(node, ID_GREEN));
    result = result && blue.saveToXML(XMLUtil.setElement(node, ID_BLUE));
    result = result && gray.saveToXML(XMLUtil.setElement(node, ID_GRAY));
    result = result && alpha.saveToXML(XMLUtil.setElement(node, ID_ALPHA));

    return result;
  }
  @Override
  public boolean loadFromXML(Node node) {
    if (node == null) return false;

    beginUpdate();
    try {
      setName(XMLUtil.getElementValue(node, ID_NAME, ""));
      setEnabled(XMLUtil.getElementBooleanValue(node, ID_ENABLED, true));
      setType(IcyColorMapType.valueOf(XMLUtil.getElementValue(node, ID_TYPE, "")));

      boolean result = true;

      result = result && red.loadFromXML(XMLUtil.getElement(node, ID_RED));
      result = result && green.loadFromXML(XMLUtil.getElement(node, ID_GREEN));
      result = result && blue.loadFromXML(XMLUtil.getElement(node, ID_BLUE));
      result = result && gray.loadFromXML(XMLUtil.getElement(node, ID_GRAY));
      result = result && alpha.loadFromXML(XMLUtil.getElement(node, ID_ALPHA));

      return result;
    } finally {
      endUpdate();
    }
  }
 /** Set green intensity (normalized) to specified index */
 public void setNormalizedGreen(int index, float value) {
   green.setNormalizedValue(index, value);
 }
 /** Set a green control point to specified index and value */
 public void setGreenControlPoint(int index, int value) {
   green.setControlPoint(index, value);
 }
 /** Set a gray control point to specified index and value */
 public void setGrayControlPoint(int index, int value) {
   gray.setControlPoint(index, value);
 }
 /** Set a blue control point to specified index and value */
 public void setBlueControlPoint(int index, int value) {
   blue.setControlPoint(index, value);
 }
 /** Set red intensity to specified index */
 public void setRed(int index, short value) {
   red.setValue(index, value);
 }
 /** Set a alpha control point to specified index and value */
 public void setAlphaControlPoint(int index, int value) {
   alpha.setControlPoint(index, value);
 }
 /** Set blue intensity to specified index */
 public void setBlue(int index, short value) {
   blue.setValue(index, value);
 }
 /** Set green intensity to specified index */
 public void setGreen(int index, short value) {
   green.setValue(index, value);
 }
 /** Set gray intensity (normalized) to specified index */
 public void setNormalizedGray(int index, float value) {
   gray.setNormalizedValue(index, value);
 }
 /** Set alpha intensity (normalized) to specified index */
 public void setNormalizedAlpha(int index, float value) {
   alpha.setNormalizedValue(index, value);
 }
 /** Set blue intensity (normalized) to specified index */
 public void setNormalizedBlue(int index, float value) {
   blue.setNormalizedValue(index, value);
 }
 /** Set a red control point to specified index and value */
 public void setRedControlPoint(int index, int value) {
   // set control point
   red.setControlPoint(index, value);
 }
 /** Set red intensity (normalized) to specified index */
 public void setNormalizedRed(int index, float value) {
   red.setNormalizedValue(index, value);
 }
 /** Set alpha intensity to specified index */
 public void setAlpha(int index, short value) {
   alpha.setValue(index, value);
 }
 /** Set gray intensity to specified index */
 public void setGray(int index, short value) {
   gray.setValue(index, value);
 }