org.jrabbit.base.graphics.transforms
Class Color

java.lang.Object
  extended by org.jrabbit.base.graphics.transforms.Color
All Implemented Interfaces:
GLTransform

public class Color
extends java.lang.Object
implements GLTransform

A Color represents a color setting for OpenGL. In addition to setting the color and binding/releasing it, there are some additional methods that provide functionality to quickly manipulate and blend colors.

Author:
Chris Molini

Field Summary
private  float a
          The alpha (transparency) value of this color.
private  float b
          The blue value of this color.
static Color BLACK
          Solid black.
static Color BLUE
          A deep blue.
static Color CYAN
          A bright, electric blue.
static Color DARK_GRAY
          A dark gray.
private  float g
          The green value of this color.
static Color GRAY
          A medium gray.
static Color GREEN
          A vivid green.
static Color LIGHT_GRAY
          A light gray.
private  java.util.ArrayList<ColorListener> listeners
          A list of all objects listening to this color.
static Color MAGENTA
          A vivid magenta.
static Color ORANGE
          A warm orange.
static Color PINK
          A bright, acid pink.
private  float r
          The red value of this color.
static Color RED
          A full red.
static Color VIOLET
          A deep purple.
static Color WHITE
          Solid white.
static Color YELLOW
          A sunny yellow.
 
Constructor Summary
Color()
          Creates a white color.
Color(java.awt.Color color)
          Creates a color with the same values as the indicated java AWT Color.
Color(org.newdawn.slick.Color color)
          Creates a color with the same values as the indicated Slick Color.
Color(float red, float green, float blue)
          Creates a color with the indicated hue, but with full transparency.
Color(float red, float green, float blue, float alpha)
          Creates a color with the indicated hue and transparency.
 
Method Summary
 void addListener(ColorListener listener)
          Adds a ColorListener to this object.
protected  void alertChange()
          Alerts all listeners that the Color has changed.
 float alpha()
          Accesses the alpha value of the color.
 void bind()
          Applies the color to OpenGL.
 void bind(Color mult)
          Binds this color multiplied by the indicated color.
 void bind(float trans)
          Binds the color multiplied by the indicated transparency.
 void bind(float red, float green, float blue, float trans)
          Binds this color multiplied by the indicated color values.
 void bindClear()
          Sets the clear color to the hue of this Color.
static Color blend(Color... colors)
          Averages a series of other colors, resulting in a hue exactly between all of them.
static Color blend(Color a, Color b, float ratio)
          Creates a Color that is a mix of the two supplied Colors.
 void blendWith(Color... colors)
          Averages this color with a series of other colors, resulting in a hue exactly between all of them.
 void blendWith(Color color)
          Averages the color with another color.
 void blendWith(Color color, float ratio)
          Blends this color with another by the indicated amount.
 float blue()
          Accesses the blue value of the color.
 void brighten(float amount)
          Lightens the color by the indicated amount.
 Color copy()
          Creates a copy of the color.
 void darken(float amount)
          Multiplies the color values by the indicated amount.
 boolean equals(java.lang.Object object)
          As equals() in Object, but if the supplied object is a color, it checks to see if their color values are the same.
private  float getValidatedFloat(float value)
          Caps the indicated value so that it stays within the valid bounds for the color.
 float green()
          Accesses the green value of the color.
 void multiplyBy(Color color)
          Multiples the values of this Color by those of another.
 float red()
          Accesses the red value of the color.
 void release()
          Sets the OpenGL color to solid white.
 boolean removeListener(ColorListener listener)
          Removes a ColorListener from the object.
 void reset()
          Sets the Color to opaque white.
 void set(Color color)
          Redefines the color to have the same values as another.
 void set(org.newdawn.slick.Color color)
          Redefines the color to have the same values as a Slick Color.
 void set(java.awt.Color color)
          Redefines the color to have the same values as a java AWT Color.
 void set(float red, float green, float blue)
          Redefines the color.
 void set(float red, float green, float blue, float alpha)
          Redefines the color.
 void setAlpha(float alpha)
          Individually sets the alpha value of the color.
 void setBlue(float blue)
          Individually sets the blue value of the color.
 void setGreen(float green)
          Individually sets the green value of the color.
 void setRed(float red)
          Individually sets the red value of the color.
 void shiftAlpha(float amount)
          Linearly adjusts the alpha of the color.
 java.lang.String toString()
          Obtain the color as a String.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

WHITE

public static final Color WHITE
Solid white. [1, 1, 1]


LIGHT_GRAY

public static final Color LIGHT_GRAY
A light gray. [0.75, 0.75, 0.75]


GRAY

public static final Color GRAY
A medium gray. [0.5, 0.5, 0.5]


DARK_GRAY

public static final Color DARK_GRAY
A dark gray. [0.25, 0.25, 0.25]


BLACK

public static final Color BLACK
Solid black. [0, 0, 0]


RED

public static final Color RED
A full red. [1, 0, 0]


ORANGE

public static final Color ORANGE
A warm orange. [1, 0.5, 0]


YELLOW

public static final Color YELLOW
A sunny yellow. [1, 1, 0]


GREEN

public static final Color GREEN
A vivid green. [0, 1, 0]


CYAN

public static final Color CYAN
A bright, electric blue. [0, 1, 1]


BLUE

public static final Color BLUE
A deep blue. [0, 0, 1]


VIOLET

public static final Color VIOLET
A deep purple. [0.5, 0, 0.5]


MAGENTA

public static final Color MAGENTA
A vivid magenta. [1, 0, 1]


PINK

public static final Color PINK
A bright, acid pink. [1, 0.35, 0.65]


r

private float r
The red value of this color.


g

private float g
The green value of this color.


b

private float b
The blue value of this color.


a

private float a
The alpha (transparency) value of this color.


listeners

private java.util.ArrayList<ColorListener> listeners
A list of all objects listening to this color.

Constructor Detail

Color

public Color()
Creates a white color.


Color

public Color(float red,
             float green,
             float blue)
Creates a color with the indicated hue, but with full transparency. The allowed values are from 0 to 1, inclusive.

Parameters:
red - The red value.
green - The green value.
blue - The blue value.

Color

public Color(float red,
             float green,
             float blue,
             float alpha)
Creates a color with the indicated hue and transparency. The allowed values are from 0 to 1, inclusive.

Parameters:
red - The red value.
green - The green value.
blue - The blue value.
alpha - The alpha value.

Color

public Color(java.awt.Color color)
Creates a color with the same values as the indicated java AWT Color.

Parameters:
color - The color to copy.

Color

public Color(org.newdawn.slick.Color color)
Creates a color with the same values as the indicated Slick Color.

Parameters:
color - The color to copy.
Method Detail

addListener

public void addListener(ColorListener listener)
Adds a ColorListener to this object. This will cause the object to be notified whenever any of the Color's values change.

Parameters:
listener - The listener to remove.

removeListener

public boolean removeListener(ColorListener listener)
Removes a ColorListener from the object. This causes it to stop receiving updates.

Parameters:
listener - The listener to remove.
Returns:
True if the listener was removed, false if not.

alertChange

protected void alertChange()
Alerts all listeners that the Color has changed.


copy

public Color copy()
Creates a copy of the color.

Returns:
A new Color that has identical values to this one.

reset

public void reset()
Sets the Color to opaque white.


setRed

public void setRed(float red)
Individually sets the red value of the color. The value supplied is capped to [0, 1] if it is outside those bounds.

Parameters:
red - The new red value.

red

public float red()
Accesses the red value of the color.

Returns:
The strength of red in the color.

setGreen

public void setGreen(float green)
Individually sets the green value of the color. The value supplied is capped to [0, 1] if it is outside those bounds.

Parameters:
green - The new green value.

green

public float green()
Accesses the green value of the color.

Returns:
The strength of green in the color.

setBlue

public void setBlue(float blue)
Individually sets the blue value of the color. The value supplied is capped to [0, 1] if it is outside those bounds.

Parameters:
blue - The new blue value.

blue

public float blue()
Accesses the blue value of the color.

Returns:
The strength of blue in the color.

setAlpha

public void setAlpha(float alpha)
Individually sets the alpha value of the color. The value supplied is capped to [0, 1] if it is outside those bounds.

Parameters:
alpha - The new alpha value.

alpha

public float alpha()
Accesses the alpha value of the color.

Returns:
How opaque the color is. 1.0 = opaque, 0 = transparent.

shiftAlpha

public void shiftAlpha(float amount)
Linearly adjusts the alpha of the color. Example: if alpha is at 0.5f, supplying 0.25f will change it to 0.75.

Parameters:
amount - The amount to shift alpha.

set

public void set(float red,
                float green,
                float blue)
Redefines the color.

Parameters:
red - The new red value.
green - The new green value.
blue - The new blue value.

set

public void set(float red,
                float green,
                float blue,
                float alpha)
Redefines the color.

Parameters:
red - The new red value.
green - The new green value.
blue - The new blue value.
alpha - The new alpha value.

set

public void set(Color color)
Redefines the color to have the same values as another. NOTE: Alpha is not affected.

Parameters:
color - The color to copy.

set

public void set(org.newdawn.slick.Color color)
Redefines the color to have the same values as a Slick Color. NOTE: Alpha is not affected.

Parameters:
color - The color to copy.

set

public void set(java.awt.Color color)
Redefines the color to have the same values as a java AWT Color. NOTE: Alpha is not affected.

Parameters:
color - The color to copy.

getValidatedFloat

private float getValidatedFloat(float value)
Caps the indicated value so that it stays within the valid bounds for the color.

Parameters:
value - The value to cap.
Returns:
If the value is within the bounds, returns it. If not, returns the closest value within bounds.

brighten

public void brighten(float amount)
Lightens the color by the indicated amount. NOTE: This method does not preserve hue or saturation. Instead, it moves each of the color values closer to 1.0 by the indicated percent. So, supplying 1 will always make the color become white; supplying 0.5f will make the color 50% less dark.

Parameters:
amount - The percentage to adjust each color value closer to 1.0.

darken

public void darken(float amount)
Multiplies the color values by the indicated amount. This preserves hue and saturation, but makes the value change. Take the color [1, 0.6, 0]. Calling darken(0.5f) will result in [0.5f, 0.3f, 0] as the new hue.

Parameters:
amount - The amount to multiply the color by.

multiplyBy

public void multiplyBy(Color color)
Multiples the values of this Color by those of another.

Parameters:
color - The Color to multiply by.

blendWith

public void blendWith(Color color)
Averages the color with another color. After this method, the color's hue is exactly halfway between its original hue and the others.

Parameters:
color - The color to average with.

blendWith

public void blendWith(Color color,
                      float ratio)
Blends this color with another by the indicated amount. If the ratio is 1, this color becomes identical with the other. If the ratio is 0, this color does not change. If it is 0.5, the two are blended evenly, etc.

Parameters:
color - The color to blend with.
ratio - The amount the other color influences this one.

blendWith

public void blendWith(Color... colors)
Averages this color with a series of other colors, resulting in a hue exactly between all of them.

Parameters:
colors - The colors to blend with.

blend

public static Color blend(Color a,
                          Color b,
                          float ratio)
Creates a Color that is a mix of the two supplied Colors. The ratio indicated determines the weighting of the colors - 0.5 for equal blending, closer to 1.0 to give the second color more weight, and closer to 0 to give the first color more weight.

Parameters:
a - The first color to blend.
b - The second color to blend.
ratio - The weighting between the two colors.
Returns:
A color that is a mix of the two supplied colors.

blend

public static Color blend(Color... colors)
Averages a series of other colors, resulting in a hue exactly between all of them.

Parameters:
colors - The colors to blend together.

bind

public void bind()
Applies the color to OpenGL.

Specified by:
bind in interface GLTransform

release

public void release()
Sets the OpenGL color to solid white.

Specified by:
release in interface GLTransform

bind

public void bind(float trans)
Binds the color multiplied by the indicated transparency. The resulting transparency is (alpha * trans).

Parameters:
trans - The transparency multiplier.

bind

public void bind(float red,
                 float green,
                 float blue,
                 float trans)
Binds this color multiplied by the indicated color values.

Parameters:
red - The red multiplication factor.
green - The green multiplication factor.
blue - The blue multiplication factor.
trans - The transparency multiplication factor.

bind

public void bind(Color mult)
Binds this color multiplied by the indicated color.

Parameters:
mult - The color to multiply this color by.

bindClear

public void bindClear()
Sets the clear color to the hue of this Color.


equals

public boolean equals(java.lang.Object object)
As equals() in Object, but if the supplied object is a color, it checks to see if their color values are the same.

Overrides:
equals in class java.lang.Object
Parameters:
object - The object to check against.
Returns:
Whether or not this object is equivalent to the target.

toString

public java.lang.String toString()
Obtain the color as a String.

Overrides:
toString in class java.lang.Object
Returns:
A String representation of the Color.