ColorDef Class

An integer representation of a color.

Colors are stored as 4 components: Red, Blue, Green, and Transparency (0=fully opaque). Each is an 8-bit integer between 0-255.

Much confusion results from attempting to interpret those 4 one-byte values as a 4 byte integer. There are generally two sources of confusion:

  1. The order the Red, Green, Blue bytes
  2. Whether to specify transparency or opacity (sometimes referred to as "alpha")

Generally, iModel.js prefers to use 0xTTBBGGRR (red in the low byte. 0==fully opaque in high byte), but this class provides methods to convert to 0xRRGGBB (see ColorDef.getRgb) and 0xAABBGGRR (red in the low byte, 0==fully transparent in high byte. see ColorDef.getAbgr).

The constructor also accepts strings in the common HTML formats.

Methods

Name Description
constructor(val?: string | ColorDefProps): ColorDef Create a new ColorDef.  
adjustForContrast(other: ColorDef, alpha?: number): ColorDef Create a new ColorDef that is adjusted from this ColorDef for maximum contrast against another color.  
clone(result?: ColorDef): ColorDef Make a copy of this ColorDef.  
equals(other: ColorDef): boolean True if the value of this ColorDef is the same as another ColorDef.  
getAbgr(): number Get the value of the color as a number in 0xAABBGGRR format (i.e.  
getAlpha(): number Get the alpha value for this ColorDef.  
getRgb(): number Get the RGB value of the color as a number in 0xRRGGBB format (i.e blue is in the low byte).  
getTransparency(): number Get the transparency value for this ColorDef (inverse of alpha).  
invert(): ColorDef Create a new ColorDef that is the inverse (all colors set to 255 - this) of this color.  
lerp(color2: ColorDef, weight: number, result?: ColorDef): ColorDef Create a ColorDef that is the linear interpolation of this ColorDef and another ColorDef, using a weighting factor.  
setAlpha(alpha: number): void Change the alpha value for this ColorDef.  
setFrom(other: ColorDef): void Set the color of this ColorDef from another ColorDef  
setTransparency(transparency: number): void Change the transparency value for this ColorDef  
toHSL(opt?: HSLColor): HSLColor Create an HSLColor from this ColorDef  
toHSV(out?: HSVColor): HSVColor Create an HSVColor from this ColorDef  
toHexString(): string Convert this ColorDef to a string in the form "#rrggbb" where values are hex digits of the respective colors  
toJSON(): ColorDefProps Convert this ColorDef to a 32 bit number representing the 0xTTBBGGRR value  
toRgbString(): string Convert this ColorDef to a string in the form "rgb(r,g,b)" where values are decimal digits of the respective colors  
from(red: number, green: number, blue: number, transparency?: number, result?: ColorDef): ColorDef Static Initialize or create a ColorDef fromn Red,Green,Blue,Transparency values.  
fromHSL(h: number, s: number, l: number, out?: ColorDef): ColorDef Static Create a ColorDef from hue, saturation, lightness values  
fromHSV(hsv: HSVColor, out?: ColorDef): ColorDef Static Create a ColorDef from an HSVColor  
fromJSON(json?: any): ColorDef Static Create a new ColorDef from a json object.  
rgb2bgr(val: number): number Static Swap the red and blue values of a 32-bit integer representing a color.  

Properties

Name Type Description
black Static ColorDef A black frozen ColorDef.  
blue Static ColorDef A blue frozen ColorDef.  
colors Accessor ReadOnly { b: number, g: number, r: number, t: number } Get the r,g,b,t values from this ColorDef.  
green Static ColorDef A green frozen ColorDef.  
isOpaque Accessor ReadOnly boolean True if this ColorDef is fully opaque  
name Accessor ReadOnly string | undefined The "known name" for this ColorDef.  
red Static ColorDef A red frozen ColorDef.  
tbgr Accessor number The color value of this ColorDef as an integer in the form 0xTTBBGGRR (red in the low byte)  
white Static ColorDef A white frozen ColorDef.  

Defined in

Last Updated: 13 June, 2024