/************************************************}
{                                                }
{   JDK 1.1, 1.2, 1.3, 1.4                       }
{   Extensions graphiques compatibles QuickDraw  }
{   P.Chassignet, Ecole Polytechnique, 1998-2004 }
{                                                }
{   dernie`re mise a` jour:  28/5/01             }
{                                                }
{************************************************/
import java.awt.Color;

/**
 * This class implements QuickDraw-like definitions for RGB colors
 * where each component is valued between 
 * <code>0</code> and <code>65535</code>.
 * @version  28 may 2001
 * @author   Philippe Chassignet, Ecole Polytechnique
 * @see      MacLib
 * @see      java.awt.Color
 */
public class RGBColor {

  /**
   * The <em>red</em> component.
   */
  public int red = 0;

  /**
   * The <em>green</em> component.
   */
  public int green = 0;

  /**
   * The <em>blue</em> component.
   */
  public int blue = 0;

  /**
   * Constructs and initializes a color with components 
   * <code>(0,0,0,0)</code>, that is the black color.
   */
  public RGBColor() {
  }

  /**
   * Constructs and initializes a color with the given components.
   * @param       r  the <em>red</em> component of the new color.
   * @param       g  the <em>green</em> component of the new color.
   * @param       b  the <em>blue</em> component of the new color.
   */
  public RGBColor(int r, int g, int b) {
    red = r & 0xFFFF;
    green = g & 0xFFFF;
    blue = b & 0xFFFF;;
  }

  /**
   * Converts this color as a <code>java.awt.Color</code> object.
   * @return      a new Color object.
   * @see         java.awt.Color
   */
  public Color getColor() {
    return new Color((red>>8)&0xFF, (green>>8)&0xFF, (blue>>8)&0xFF);
  }

  /**
   * Sets the components of this color as specified by the given 
   * <code>java.awt.Color</code> object.
   * @param       color  the Color object used to specify the components.
   * @see         java.awt.Color
   */
  public RGBColor setColor(Color color) {
    red = color.getRed() << 8;
    green = color.getGreen() << 8;
    blue = color.getBlue() << 8;
    return this;
  }

  /**
   * Returns a string representation for this color.
   * @return      the string representation of the components.
   */
  public String toString() {
    return "[red=" + red + ",green=" + green + ",blue=" + blue
         + " " + Integer.toHexString(hashCode()) +"]";
  }

  /**
   * Returns a hash code for this color.
   * @return      the hash code build up from the components.
   */
  public int hashCode() {
    return ((red & 0xFF00) << 8) | (green & 0xFF00) | ((blue & 0xFF00) >> 8);
  }

}  // end class RGBColor

