Color


  Tutorial Home
 Intro All of our objects up until now have been white on a black background. This is boring. Any object (except groups and axes) can have their color changed. In addition the background of the window can be changed from white.
RGB The colors visible by humans are composed of red, green and blue (RGB) because those are the color sensors in the human eye. All colors in the Phun system are defined by either a named color that was previously defined or by three numbers, one for each of red, green and blue.

Each component is defined as a floating point number between 0.0 (no light) and 1.0 (maximum light). The following are examples:

  • new Color(1,1,1) - white with the maximum of all three colors.
  • new Color(0,0,0) - black with no light from any of the colors.
  • new Color(0.5,0.5,0.5) - medium gray with half of each of the primary colors.
  • new Color(1,0,0) - a very bright red because red is at maximum while there is no green or blue.
  • new Color(1,0.5,0.5) - pink, which is maximum red with some green and blue added in to make it closer to white.
  • new Color(0.5,0.5,0.7) - a bluish gray. This is like gray only a little more blue has been added.
Named Colors

The Color class also provides several named colors that can be used directly rather than creating a new custom color.

  • Color.BLUE
  • Color.LIGHT_GRAY
  • Color.YELLOW

For a complete list of color constants see the Color class javadocs.

Background color

A Win2D object has a background color. Its default is white, however, this can be changed using the win.setBackground() methods. One of these methods takes a Color and the other takes three double values for red, green and blue. The following code shows how they are used.

    public static void main (String []args)
    {
        Win3D win = new Win3D("Tutorial",10,20,400,400);
        
        win.add(new Axes(3));
        win.setLookFrom(-3,4,7);
        win.setLookAt(0,1,0);
        
        win.setBackground(Color.LIGHT_GRAY);
        
        win.add(new SphereObj());
    }

The image below is what is produced.

Colored Objects

Any object can have its color changed using the obj.setColor(...) method. As with setBackground(...) there is a form with a Color object and a form that takes the RGB values for the desired color.

The following code shows a sphere receiving a named color for yellow and the floor receiving a medium brown color constructed from the RGB primaries.

	public static void main(String[] args) 
	{
		Win2D win = new Win2D("Tutorial",10,20,400,300);
		
		win.setBackground(Color.LIGHT_GRAY);
		
		Rect myRect = new Rect(2,2,3,5);
		win.add(myRect);
		myRect.setFillColor(Color.BLUE);
		myRect.setLineColor(null);
			// Setting the line color to null eliminates the border

		Oval myOval = new Oval(0,0,8,10);
		win.add(myOval);
		myOval.setFillColor(null);
			// Setting the fill color to null eliminates the fill
		myOval.setLineColor(new Color(0.2,0.6,0.6));
		myOval.setLineWidth(0.4);
		
		win.add(new Axes());

	}

The resulting image is shown below: