/********************************************************************
 * This class implements a color object.  Colors are defined by
 * three component colors: red, green, blue.  Each of these three
 * components has a value betwen 0 and 255.
 * 
 * @author Arch Harris - modified Nancy Harris
 * @V2 10/29/2007
 *********************************************************************/
public class Color139
{
   /*************************************************************
    * Some constants that define the 8 "standard" computer colors.
    *************************************************************/
   public static final Color139 BLACK = new Color139(0,0,0);
   public static final Color139 RED = new Color139(255,0,0);
   public static final Color139 GREEN = new Color139(0,255,0);
   public static final Color139 YELLOW = new Color139(255,255,0);
   public static final Color139 BLUE = new Color139(0,0,255);
   public static final Color139 MAGENTA = new Color139(255,0,255);
   public static final Color139 CYAN = new Color139(0,255,255);
   public static final Color139 WHITE = new Color139(255,255,255);


   /*************************************************************
    * Below are the three "instance" variables which store the
    * "attribute" values in objects of type Color139.
    *************************************************************/
	 // Book reference - Chapter 6.2
   private int red;   // Red component of the Color139 object
   private int green; // Green component of the Color139 object
   private int blue;  // Blue component of the Color139 Object

   /*************************************************************
    * This constructor creates a Color139 object with the specified
    * RGB component values. If a component value less than 0 is specified, 
    * that component value is set to 0. If a component value greater 
    * than 255 is specified, that component's value is set to 255.
    * @param redComponent   The color's red component as a value 0-255
    * @param greenComponent The color's green component as a value 0-255
    * @param blueComponent  The color's blue component as a value 0-255
    *************************************************************/
	 // Book reference 6.3
	 // this Book reference 9.8
   public Color139(int redComponent, int greenComponent, int blueComponent)
   {
   	this.red = redComponent;
		this.green = greenComponent;
		this.blue = blueComponent;
		
		// take care of "problem values"
		if (this.red > 255)
			this.red = 255;
		else if (this.red < 0)
			this.red = 0;
		
		if (this.green > 255)
			this.green = 255;
		else if (this.green < 0)
			this.green = 0;
		
		if (this.blue > 255)
			this.blue = 255;
		else if (this.blue < 0)
			this.blue = 0;  
	}
	
	 /*************************************************************
    * This constructor creates a Color139 object with the specified
    * as a clone of the passed Color139 object.
    * @param other The color we want to duplicate
    *************************************************************/
	 // book reference - 6.4
	public Color139(Color139 other)
   {
   	this.red = other.red;
		this.green = other.green;
		this.blue = other.blue;
		
	}

   /*************************************************************
    * This method returns a string representation of the color
    * in the form "#RRGGBB" where RR, GG, and BB are the red, green
    * and blue component values expressed as hexidecimal digits.
    * @return The string representation of the color
    *************************************************************/
	// book reference 9.4
   public String toString()
   {
      String redStr;      // red component hex value as a string
      String greenStr;    // green component hex value as a string
      String blueStr;     // blue component hex value as a string

      redStr = Integer.toHexString(this.red);
      if (redStr.length() == 1)
         redStr = "0" + redStr;
      greenStr = Integer.toHexString(this.green);
      if (greenStr.length() == 1)
         greenStr = "0" + greenStr;
      blueStr = Integer.toHexString(this.blue);
      if (blueStr.length() == 1)
         blueStr = "0" + blueStr;
      return "#" + redStr + greenStr + blueStr;
   }

   /*************************************************************
    * The method returns a new Color139 value that is the sum of
    * this color and the other color. The value of the red components of
    * the two colors are added, as are the green and blue components.
    * Any result greater than 255 is set to 255.
    * @param other The color to be added to this color
    * @return      The sum of the two colors
    *************************************************************/
 	// book reference 9.2/9.3  
	public Color139 add(Color139 other)
   {
      Color139 newcolor;      // new color object that is dimmer than this color
      int      newRed;        // new red color value
      int      newGreen;      // new green color value
      int      newBlue;       // new blue color value

      newRed = this.red + other.red;
      newGreen = this.green + other.green;
      newBlue = this.blue + other.blue;
      
      newcolor = new Color139 (newRed, newGreen, newBlue);
      return newcolor;
   }

   /*************************************************************
    * The method returns a new Color139 value that is the difference between
    * this color and the other color. The value of the red components of
    * the other color is subtracted from the red component of this color.
    * The green and blue components are calculated similarly.
    * Any result less than 0 is set to 0.
    * @param other The color to be subtracted from this color
    * @return      The sum of the two colors
    *************************************************************/
   public Color139 sub(Color139 other)
   {
      Color139 newcolor;      // new color object that is dimmer than this color
      int      newRed;        // new red color value
      int      newGreen;      // new green color value
      int      newBlue;       // new blue color value

      newRed = this.red - other.red;
      newGreen = this.green - other.green;
      newBlue = this.blue - other.blue;
      
      newcolor = new Color139 (newRed, newGreen, newBlue);
      return newcolor;
   }

   /*************************************************************
    * The method returns a dimmer new Color139 value. Each of
    * the red, green, and blue components is decreased by 20%.
    *
    * @return The dimmer color
    *************************************************************/
   public Color139 dim()
   {
      Color139 newcolor;      // new color object that is dimmer than this color
      int newRed;             // new red color value
      int newGreen;           // new green color value
      int newBlue;            // new blue color value

      newRed = (int)(this.red / 1.2);
      newGreen = (int)(this.green / 1.2);
      newBlue = (int)(this.blue / 1.2);
      
      newcolor = new Color139 (newRed, newGreen, newBlue);

      return newcolor;
   }

   /*************************************************************
    * The method returns a darker new Color139 value. Each of
    * the red, green, and blue components is decreased by 32 down
    * to a minimum of 0.
    * @return The darker color
    *************************************************************/
   public Color139 darken()
   {
      Color139 newcolor;      // new color object that is dimmer than this color
      int newRed;             // new red color value
      int newGreen;           // new green color value
      int newBlue;            // new blue color value

      newRed = (this.red - 32);
      newGreen = (this.green - 32);
      newBlue = (this.blue - 32);
      
      newcolor = new Color139 (newRed, newGreen, newBlue);

      return newcolor;

   }

   /*************************************************************
    * This method returns true if this color is equal to the
    * other color and returns false if it is not equal.
    * @param other The color to be compared to this color
    * @return      Whether or not the two colors are equal
    *************************************************************/
	 // Book reference 9.5
   public boolean equals(Color139 other)
   {
		boolean same;
		same = false;
      if (this.red == other.red && this.blue == other.blue &&
			this.green == other.green)
			same = true;
			
      return same;
   }

   /*************************************************************
    * The method returns a lighter new Color139 value. Each of
    * the red, green, and blue components is increased by 32 up
    * to a maximum of 255.
    * @return The lighter color
    *************************************************************/
   public Color139 lighten()
   {
      Color139 newcolor;      // new color object that is dimmer than this color
      int newRed;             // new red color value
      int newGreen;           // new green color value
      int newBlue;            // new blue color value

      newRed = (this.red + 32);
      newGreen = (this.green + 32);
      newBlue = (this.blue + 32);
      
      newcolor = new Color139 (newRed, newGreen, newBlue);

      return newcolor;

   }

   /*************************************************************
    * The method returns a brighter new Color139 value. Each of
    * the red, green, and blue components is increased by 20%.
    * @return The brighter color
    *************************************************************/
   public Color139 brighten()
   {
      Color139 newcolor;      // new color object that is dimmer than this color
      int newRed;             // new red color value
      int newGreen;           // new green color value
      int newBlue;            // new blue color value

      newRed = (int)(this.red * 1.2);
      newGreen = (int)(this.green * 1.2);
      newBlue = (int)(this.blue * 1.2);
      
      newcolor = new Color139 (newRed, newGreen, newBlue);

      return newcolor;
   }
}
