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.
ColorHSB | Color class with red, green, blue (RGB) and hue, saturation, and brightness (HSB) components. |
Implementation Details | |
norm255 | Multiplier used to normalize from a 0-255 range |
_color | The rgb color value represented by the ColorHSB object |
hsb | Calculates a RGB color value from hue, saturation and brightness components |
rgb | Shifts 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 | |
HSB | Set the ColorHSB to the RGB value corresponding to the given hue, saturation and brightness. |
hue | Calculates the hue of the current color. |
hue | Change the hue of the current color in degrees (0-360) |
saturation | Calculates the saturation of the current color. |
saturation | Change the saturation of the current color (0-1) |
brightness | Calculates the brightness of the current color. |
brightness | Change the brightness of the current color (0-1) |
Complementary methods | |
complementHue | Rotates the hue 180 degrees to find the complementary hue. |
complementSaturation | Calculates the inverse of the current color saturation |
complementBrightness | Calculates the inverse of the current color brightness |
complementRGB | Calculates the complementary color based on the hue, saturation and brightness |
RGB methods | |
color | Gets the current color |
color | Sets the current color to the given 24 bit color value |
RGB | Sets the current color value from the given red, green, blue parts |
red | Get the red component of the current color |
red | Set the red component of the current color |
green | Get the green component of the current color |
green | Set the green component of the current color |
blue | Get the blue component of the current color |
blue | Set the blue component of the current color |
Helper methods | |
gradientByteArray | Creates a byte array with color values of varying saturation and brightness based on the hue of the current color. |
intensity | Calculates the grey scale intensity of the current color. |
String conversion methods | |
parseColor | Sets the current color value from the string containing a named, hex or decimal color. |
toString | Converts the current color into a string representation. |
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.
// 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);
A ByteArray with saturation and brightness gradients
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.
A relative grey scale intensity measure of the color in the interval (0-1)
public function toString():String
Converts the current color into a string representation.
A string representing the color compatible with parseColor
Multiplier used to normalize from a 0-255 range
protected const norm255: Number
The rgb color value represented by the ColorHSB object
protected var _color: uint
Calculates a RGB color value from hue, saturation and brightness components
private function hsb( h: Number, s: Number, v: Number ):uint
Shifts red, green, blue components into a uint RGB color value
private function rgb( r: int, g: int, b: int ):uint
Creates a ColorHSB object from a RGB uint color definition if only a color value is given
public function ColorHSB( colorOrHue: Number = 0, sat: Number = NaN, bright: Number = NaN )
Set the ColorHSB to the RGB value corresponding to the given hue, saturation and brightness.
public function HSB( hue: Number, sat: Number, bright: Number ):uint
Calculates the hue of the current color.
public function get hue():Number
Calculates the saturation of the current color.
public function get saturation():Number
Calculates the brightness of the current color.
public function get brightness():Number
Rotates the hue 180 degrees to find the complementary hue.
public function get complementHue():Number
Calculates the inverse of the current color saturation
public function get complementSaturation():Number
Calculates the inverse of the current color brightness
public function get complementBrightness():Number
Calculates the complementary color based on the hue, saturation and brightness
public function get complementRGB():uint
Gets the current color
public function get color():uint
Sets the current color value from the given red, green, blue parts
public function RGB( r: int, g: int, b: int ):uint
Get the red component of the current color
public function get red():int
Get the green component of the current color
public function get green():int
Get the blue component of the current color
public function get blue():int
Creates a byte array with color values of varying saturation and brightness based on the hue of the current color.
public function gradientByteArray( width: int, height: int ):ByteArray
Calculates the grey scale intensity of the current color.
public function get intensity():Number
Sets the current color value from the string containing a named, hex or decimal color.
public function parseColor( colorString: String ):void
Converts the current color into a string representation.
public function toString():String