Class Color

The Color implements a a three-component color with an alpha component. The class follows the 'copy-on-write' pattern, which means that every operation such as Color.add or Color.subtract returns a copy of the color with updated values. It also implies that the values of the color cannot be directly modified.

The concept of immutability improves sharing of objects as all types in TypeScript are assigned by identity, instead of by-value (e.g. a copy) as in other programming languages like C.

Constructors

  • Parameters

    • Optionalr: number
    • Optionalg: number
    • Optionalb: number
    • Optionala: number

    Returns Color

Properties

_a: number
_b: number
_g: number
_r: number
compareEpsilon = 0.001

The default value to approximately compare colors,

Accessors

  • get a(): number
  • Returns number

  • set a(value): void
  • Parameters

    • value: number

    Returns void

  • get b(): number
  • Returns number

  • set b(value): void
  • Parameters

    • value: number

    Returns void

  • get g(): number
  • Returns number

  • set g(value): void
  • Parameters

    • value: number

    Returns void

  • get r(): number
  • Returns number

  • set r(value): void
  • Parameters

    • value: number

    Returns void

  • get black(): Color
  • Color with all elements set to 0.

    Returns Color

  • get blue(): Color
  • Pure blue color with solid alpha value.

    Returns Color

  • get cyan(): Color
  • Pure cyan color with solid alpha value.

    Returns Color

  • get green(): Color
  • Pure green color with solid alpha value.

    Returns Color

  • get lightBlue(): Color
  • Light blue color with solid alpha value.

    Returns Color

  • get magenta(): Color
  • Pure magenta color with solid alpha value.

    Returns Color

  • get red(): Color
  • Pure red color with solid alpha value.

    Returns Color

  • get white(): Color
  • Color with all elements set to 1.

    Returns Color

  • get yellow(): Color
  • Pure yellow color with solid alpha value.

    Returns Color

Methods

  • Copies all properties defined by the prototype of Color from other to this instance.

    Parameters

    Returns Color

  • Clones this instance and returns a new Color with identical values.

    Returns Color

  • Returns a copy of the current color divided by a factor.

    Parameters

    • factor: number

    Returns Color

  • Adds another color to this one and returns it.

    Parameters

    • value: number
    • addAlpha: boolean

    Returns Color

  • Divides this color by a factor.

    Parameters

    • factor: number

    Returns Color

  • Performs component-wise multiplication with another color.

    Parameters

    Returns Color

  • Performs scalar multiplication of this color and returns it.

    Parameters

    • factor: number
    • multiplyAlpha: boolean

    Returns Color

  • Sets the alpha value to the specified value.

    Parameters

    • alpha: number

    Returns Color

  • Subtracts another color from this one and returns it.

    Parameters

    Returns Color

  • Accesses color components by an index.

    Parameters

    • index: number

      Index ranging [0, 3]

    Returns number

    Value of the component at the specified index.

  • Converts the current color from linear to sRGB and returns the result.

    Returns Color

  • Converts the current color from sRGB to linear space and returns the result.

    Returns Color

  • Returns whether the provided color, given a error margin, has the same color components values as this one.

    Parameters

    • rhs: Immutable<Color>

      The other Color to compare with.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Returns whether the given color contains the same color components values as this one.

    Parameters

    Returns boolean

  • Returns a copy of the current color multiplied by the given color.

    Parameters

    Returns Color

  • Returns a copy of this color multiplied by the factor.

    Parameters

    • factor: number
    • multiplyAlpha: boolean

    Returns Color

  • Returns a copy of this color with the specified alpha value.

    Parameters

    • alpha: number

    Returns Color

  • Assigns the value to the element at the specified index.

    Parameters

    • index: number
    • value: number

    Returns void

  • Returns a copy of the current color subtracted by the given color.

    Parameters

    Returns Color

  • Returns a string representation of an object.

    Returns string

  • Converts this instance to a Vector3 with R/G/B while ignoring the alpha value.

    Returns Vector3

  • Converts a single color component from linear to SRGB.

    Parameters

    • x: number

      Component in linear space.

    Returns number

    Component in SRGB space.

  • Converts a single color component from SRGB to linear space.

    Parameters

    • x: number

      Component in SRGB space.

    Returns number

    Component in linear space.

  • Divides the given color by a factor.

    Parameters

    Returns Color

  • Compares two colors with an error margin.

    Parameters

    • lhs: Immutable<Color>

      First color to compare.

    • rhs: Immutable<Color>

      Second color to compare.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Returns whether all the colors components contains the same values.

    Parameters

    Returns boolean

  • Linearly interpolates between two values.

    Parameters

    • a: Immutable<Color>

      Starting value to interpolate from.

    • b: Immutable<Color>

      Ending value to interpolate towards.

    • t: number

      Interpolation factor in specified range.

    Returns Color

    Interpolated vector.

  • Multiply the color by the scalar.

    Parameters

    Returns Color

  • Creates a color from the specified RGBA byte values in range [0-255].

    Parameters

    • Optionalr: number
    • Optionalg: number
    • Optionalb: number
    • Optionala: number

    Returns Color

  • Creates a color from the specified Vector3 and alpha value.

    Parameters

    Returns Color