canvas
Class BaseColor

java.lang.Object
  |
  +--canvas.BaseColor
Direct Known Subclasses:
AnyColor, WebSafeColor

public abstract class BaseColor
extends Object

Provides an abstract structure and general services for Colors. It provides methods that are common to the subclasses. For example, all color classes will have to return the color they hold, so getColor() is provided here. Also, each of the subclasses will have some common mathematical operations to carry out, so they are provided here as protected methods.


Field Summary
protected  Color color
          This is the color the object holds and operates on.
 
Constructor Summary
BaseColor(Color c)
          Takes the color that is to be held.
BaseColor(int n)
          Takes the single integer that gets piped into the Color() constructor.
BaseColor(int r, int g, int b)
          Creates new color with specified red, green, and blue components.
 
Method Summary
abstract  Color brighter()
          Return a brighter version of the color.
abstract  Color darker()
          Returns a darker version of the color.
abstract  Color desaturate()
          Returns a less saturated version of the color.
 Color getColor()
          Returns the color.
abstract  Color hueshift(int degrees)
          Returns a color shifted around the color wheel.
protected static float in_range(float f)
          Returns a float between 0 and 1.
protected static int in_range(int i)
          Makes sure the integer passed in is between 0 and 255 and returns it.
abstract  Color saturate()
          Returns a more saturated version of the color.
 String toString()
          Returns a string with the color's RGB components.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

color

protected final Color color
This is the color the object holds and operates on. It is immutable (You cannot change this variable once the object is constructed)

Constructor Detail

BaseColor

public BaseColor(int r,
                 int g,
                 int b)
Creates new color with specified red, green, and blue components.

Parameters:
r - Red component, 0-255
g - Green component, 0-255
b - Blue component, 0-255

BaseColor

public BaseColor(Color c)
Takes the color that is to be held.


BaseColor

public BaseColor(int n)
Takes the single integer that gets piped into the Color() constructor. Use hex values between 0x000000 and 0xFFFFFF. The first two digits represent red, the second pair represent green and the third pair represent blue.

Method Detail

getColor

public Color getColor()
Returns the color.


brighter

public abstract Color brighter()
Return a brighter version of the color. The red, green, and blue components of the color are each increased by the same amount, a new color is constructed with the updated rgb components and then returned.


darker

public abstract Color darker()
Returns a darker version of the color. Does the exact opposite of brighter().


saturate

public abstract Color saturate()
Returns a more saturated version of the color. Decomposes the RGB components into the HSB color model (which defines the Hue, Saturation, and Brightness) and adds to the Saturation component. Then creates and returns a new color with the new saturation.


desaturate

public abstract Color desaturate()
Returns a less saturated version of the color. Decrements the saturation instead of incrementing it.


hueshift

public abstract Color hueshift(int degrees)
Returns a color shifted around the color wheel. Decomposes the RGB components into HSB and changes the Hue. This is most useful to generate a rainbow by incrementally shifting the same color by different values. A full 360-degree circle will go all the way around the rainbow.

Parameters:
degrees - The number of degrees out of 360 to shift the color.

toString

public String toString()
Returns a string with the color's RGB components.

Overrides:
toString in class Object

in_range

protected static int in_range(int i)
Makes sure the integer passed in is between 0 and 255 and returns it. If the integer is outside the range, the returned value is either 0 or 255, whichever is closest. This is useful because it is common to represent an individual color component as an int with a value 0 to 255. This is especially useful when dealing with Color obects because if they are initialized with values outside that range, they throw an exception, and dealing with those takes some effort. In this case it is much easier to make sure the component values conform.


in_range

protected static float in_range(float f)
Returns a float between 0 and 1. Similar to in_range(int), but acts on a float and uses a different range. It is also common to describe color components as a float between 0 and 1.