ColorHSB

Color class with red, green, blue (RGB) and hue, saturation, and brightness (HSB) components.

The class provides conversion, extraction and manipulation of individual color components.  Grey scale intensity and complementary values can be obtained as well as a byte array with saturation and brightness scales.

The color components are represented with integer red, green, blue (24 bit uint) and hue (0-360 degrees Number), saturation (0-1 Number) and brightness (0-1 Number) components.

Note: Rounding Errors Since the color value is stored as a rgb value rounding errors can occur in the color representation because of its limited 24 bit range.  This is done for efficiency.  More precise representations would be to store the rgb components as floating point numbers or to store the hue, saturation and brightness in addition to the rgb value.

Summary
ColorHSBColor class with red, green, blue (RGB) and hue, saturation, and brightness (HSB) components.
Implementation Details
norm255Multiplier used to normalize from a 0-255 range
_colorThe rgb color value represented by the ColorHSB object
hsbCalculates a RGB color value from hue, saturation and brightness components
rgbShifts red, green, blue components into a uint RGB color value
Constructors
ColorHSB(rgbColor)Creates a ColorHSB object from a RGB uint color definition if only a color value is given
ColorHSB(hue, saturation, brightness)Creates a ColorHSB object from given hue, saturation and brightness values.
HSB methods
HSBSet the ColorHSB to the RGB value corresponding to the given hue, saturation and brightness.
hueCalculates the hue of the current color.
hueChange the hue of the current color in degrees (0-360)
saturationCalculates the saturation of the current color.
saturationChange the saturation of the current color (0-1)
brightnessCalculates the brightness of the current color.
brightnessChange the brightness of the current color (0-1)
Complementary methods
complementHueRotates the hue 180 degrees to find the complementary hue.
complementSaturationCalculates the inverse of the current color saturation
complementBrightnessCalculates the inverse of the current color brightness
complementRGBCalculates the complementary color based on the hue, saturation and brightness
RGB methods
colorGets the current color
colorSets the current color to the given 24 bit color value
RGBSets the current color value from the given red, green, blue parts
redGet the red component of the current color
redSet the red component of the current color
greenGet the green component of the current color
greenSet the green component of the current color
blueGet the blue component of the current color
blueSet the blue component of the current color
Helper methods
gradientByteArrayCreates a byte array with color values of varying saturation and brightness based on the hue of the current color.
intensityCalculates the grey scale intensity of the current color.
String conversion methods
parseColorSets the current color value from the string containing a named, hex or decimal color.
toStringConverts the current color into a string representation.

Implementation Details

norm255

protected const norm255: Number

Multiplier used to normalize from a 0-255 range

_color

protected var _color: uint

The rgb color value represented by the ColorHSB object

hsb

private function hsb(h: Number,
s: Number,
v: Number):uint

Calculates a RGB color value from hue, saturation and brightness components

Returns

The uint RGB color value

rgb

private function rgb(r: int,
g: int,
b: int):uint

Shifts red, green, blue components into a uint RGB color value

Returns

The uint RGB color value

Constructors

ColorHSB(rgbColor)

public function ColorHSB(colorOrHue: Number = 0,
sat: Number = NaN,
bright: Number = NaN)

Creates a ColorHSB object from a RGB uint color definition if only a color value is given

ColorHSB(hue, saturation, brightness)

Creates a ColorHSB object from given hue, saturation and brightness values.

HSB methods

HSB

public function HSB(hue: Number,
sat: Number,
bright: Number):uint

Set the ColorHSB to the RGB value corresponding to the given hue, saturation and brightness.

Returns

The RGB color value

hue

public function get hue():Number

Calculates the hue of the current color.  If the color is black, white or a shade of grey then hue is set to 0 degrees.

Returns

Hue in degrees (0-360)

hue

public function set hue(value: Number):void

Change the hue of the current color in degrees (0-360)

saturation

public function get saturation():Number

Calculates the saturation of the current color.

Returns

Saturation as a fraction in the interval (0-1)

saturation

public function set saturation(value: Number):void

Change the saturation of the current color (0-1)

brightness

public function get brightness():Number

Calculates the brightness of the current color.

Returns

Brightness as a fraction in the interval (0-1)

brightness

public function set brightness(value: Number):void

Change the brightness of the current color (0-1)

Complementary methods

complementHue

public function get complementHue():Number

Rotates the hue 180 degrees to find the complementary hue.

Returns

The complementary hue of the current color

complementSaturation

public function get complementSaturation():Number

Calculates the inverse of the current color saturation

Returns

The complementary saturation of the current color

complementBrightness

public function get complementBrightness():Number

Calculates the inverse of the current color brightness

Returns

The complementary brightness of the current color

complementRGB

public function get complementRGB():uint

Calculates the complementary color based on the hue, saturation and brightness

Returns

The complementary RGB color

RGB methods

color

public function get color():uint

Gets the current color

Returns

The current RGB color value

color

public function set color(rgb: uint):void

Sets the current color to the given 24 bit color value

RGB

public function RGB(r: int,
g: int,
b: int):uint

Sets the current color value from the given red, green, blue parts

Returns

The 24 bit color value representation

red

public function get red():int

Get the red component of the current color

Returns

The red component of the current color

red

public function set red(r: int):void

Set the red component of the current color

green

public function get green():int

Get the green component of the current color

Returns

The green component of the current color

green

public function set green(g: int):void

Set the green component of the current color

blue

public function get blue():int

Get the blue component of the current color

Returns

The blue component of the current color

blue

public function set blue(b: int):void

Set the blue component of the current color

Helper methods

gradientByteArray

public function gradientByteArray(width: int,
height: int):ByteArray

Creates a byte array with color values of varying saturation and brightness based on the hue of the current color.  The array contains uint values and has the given width and height size.  The method could be extended to implement transparency.

Example

// Assumes that gradientHue, gradientSizeX and gradientSizeY have been set.
// Creates a bitmap with the hue and gradient saturation and brightness.
var hsb:ColorHSB = new ColorHSB(gradientHue, 1, 1);
var bytes:ByteArray = hsb.gradientByteArray(gradientSizeX, gradientSizeY);
var rect:Rectangle = new Rectangle(0, 0, gradientSizeX, gradientSizeY);
var gradientData:BitmapData = new BitmapData(gradientSizeX, gradientSizeY, false);
gradientData.setPixels(rect, bytes);
var gradientRect:Bitmap = new Bitmap(gradientData);
addChild(gradientRect);

Returns

A ByteArray with saturation and brightness gradients

intensity

public function get intensity():Number

Calculates the grey scale intensity of the current color.  Since the intensity of red, green and blue is perceived differently by the human eye, experimentally derived adjustments are made to each color separately.

Returns

A relative grey scale intensity measure of the color in the interval (0-1)

String conversion methods

parseColor

public function parseColor(colorString: String):void

Sets the current color value from the string containing a named, hex or decimal color.

Parameters

colorStringa string representing a color

See Also

toString

toString

public function toString():String

Converts the current color into a string representation.

Returns

A string representing the color compatible with parseColor

protected const norm255: Number
Multiplier used to normalize from a 0-255 range
protected var _color: uint
The rgb color value represented by the ColorHSB object
private function hsb(h: Number,
s: Number,
v: Number):uint
Calculates a RGB color value from hue, saturation and brightness components
private function rgb(r: int,
g: int,
b: int):uint
Shifts red, green, blue components into a uint RGB color value
public function ColorHSB(colorOrHue: Number = 0,
sat: Number = NaN,
bright: Number = NaN)
Creates a ColorHSB object from a RGB uint color definition if only a color value is given
public function HSB(hue: Number,
sat: Number,
bright: Number):uint
Set the ColorHSB to the RGB value corresponding to the given hue, saturation and brightness.
public function get hue():Number
Calculates the hue of the current color.
public function get saturation():Number
Calculates the saturation of the current color.
public function get brightness():Number
Calculates the brightness of the current color.
public function get complementHue():Number
Rotates the hue 180 degrees to find the complementary hue.
public function get complementSaturation():Number
Calculates the inverse of the current color saturation
public function get complementBrightness():Number
Calculates the inverse of the current color brightness
public function get complementRGB():uint
Calculates the complementary color based on the hue, saturation and brightness
public function get color():uint
Gets the current color
public function RGB(r: int,
g: int,
b: int):uint
Sets the current color value from the given red, green, blue parts
public function get red():int
Get the red component of the current color
public function get green():int
Get the green component of the current color
public function get blue():int
Get the blue component of the current color
public function gradientByteArray(width: int,
height: int):ByteArray
Creates a byte array with color values of varying saturation and brightness based on the hue of the current color.
public function get intensity():Number
Calculates the grey scale intensity of the current color.
public function parseColor(colorString: String):void
Sets the current color value from the string containing a named, hex or decimal color.
public function toString():String
Converts the current color into a string representation.
Close