Represents a color.

Example

import { Color4 } from '@js-draw/math';

console.log('Red:', Color4.fromString('#f00'));
console.log('Also red:', Color4.ofRGB(1, 0, 0), Color4.red);
console.log('Mixing red and blue:', Color4.red.mix(Color4.blue, 0.5));
console.log('To string:', Color4.orange.toHexString());

Properties

a: number

Alpha/transparent component. a[0,1]{\tt a} \in [0, 1]. 0 = transparent

b: number

Blue component. b[0,1]{\tt b} \in [0, 1]

g: number

Green component. g[0,1]{\tt g} \in [0, 1]

r: number

Red component. Should be in the range [0, 1].

black: Color4 = ...
blue: Color4 = ...
clay: Color4 = ...
gray: Color4 = ...
green: Color4 = ...
orange: Color4 = ...
purple: Color4 = ...
red: Color4 = ...
transparent: Color4 = ...
white: Color4 = ...
yellow: Color4 = ...

Accessors

  • get rgb(): Vec3
  • Ignoring this color's alpha component, returns a vector with components, (rgb) \begin{pmatrix} \colorbox{#F44}{\tt r} \ \colorbox{#4F4}{\tt g} \ \colorbox{#44F}{\tt b} \end{pmatrix}

    Returns Vec3

Methods

  • Parameters

    • other: undefined | null | Color4

    Returns boolean

    true if this and other are approximately equal.

  • If fractionTo is not in the range [0,1][0, 1], it will be clamped to the nearest number in that range. For example, a.mix(b, -1) is equivalent to a.mix(b, 0).

    Parameters

    • other: Color4
    • fractionTo: number

    Returns Color4

    a color fractionTo of the way from this color to other.

    Example

    Color4.ofRGB(1, 0, 0).mix(Color4.ofRGB(0, 1, 0), 0.1) // -> Color4(0.9, 0.1, 0)
    
  • Returns the relative luminance of this color in the sRGB color space.

    Ignores the alpha component.

    Returns number

  • Returns string

    a hexadecimal color string representation of this, in the form #rrggbbaa.

    Example

    Color4.red.toHexString(); // -> #ff0000ff
    
  • Parameters

    Returns Color4

    the component-wise average of colors, or Color4.transparent if colors is empty.

  • Creates a new Color4 from a representation in HSVHSV.

    Algorithm.

    Note that hue must be given in radians. While non-standard, this is consistent with asHSV.

    hue and value should range from 0 to 1.

    Parameters

    • hue: number

      H[0,2π]H \in [0, 2\pi]

    • saturation: number

      SV[0,1]S_V \in [0, 1]

    • value: number

      V[0,1]V \in [0, 1]

    Returns Color4

  • Equivalent to ofRGB(rgb.x, rgb.y, rgb.z).

    All components should be in the range [0, 1] (0 to 1 inclusive).

    Parameters

    • rgb: Vec3
    • Optional alpha: number

    Returns Color4

  • Like fromHex, but can handle additional colors if an HTMLCanvasElement is available.

    Parameters

    • text: string

    Returns Color4

  • Create a color from red, green, blue components. The color is fully opaque (a = 1.0).

    Each component should be in the range [0, 1].

    Parameters

    • red: number
    • green: number
    • blue: number

    Returns Color4

  • Parameters

    • red: number
    • green: number
    • blue: number
    • alpha: number

    Returns Color4

Generated using TypeDoc

OpenSource licenses