Thursday, September 30, 2010

HW 4: Colors

For this homework you will implement a simple Color class which stores RGB colors prints them out and lets you mix colors.

Your program will have the constructors and methods such that this main:

 public static void main(String[] args) {
  //a constructor with all ints arguments, each is a number between 0 and 255.
  Color red = new Color(255,0,0);
  System.out.println(red);
  
  //a constructor that understands "red", "green" and "blue".
  Color blue = new Color("blue");
  System.out.println(blue);
  //a constructor with all doubles as arguments, each is a number between 0 and 1.
  Color green = new Color(0.0, 1.0, 0.0);
  System.out.println(green);
  //if a number is out of range you push it back into the range!
  Color reallyGreen = new Color(0.0, 100, 0.0);
  System.out.println(reallyGreen);
  
  //Mix colors by simply taking the average of each component (RGB).
  Color purple = red.mixWith(blue);
  System.out.println("purple=" + purple);
  purple = purple.mixWith(red);
  System.out.println(purple);
  purple = purple.mixWith(red);
  System.out.println(purple); 
 }
prints out these statements:
#ff0000
#0000ff
#00ff00
#00ff00
purple=#7f007f
#bf003f
#df001f

Those are hexadecimal numbers there, in the standard Hex triplet format. Notice how your code will not let the user create an illegal color.

Tip: your internal representation should use a number between 0 and 255 for each one of the channels (RGB). Also, try to make use of your own private methods.

The homework should be deposited in our dropbox by Thursday, October 7 @ 9am

No comments: