Provides color manipulation API that is extensible via plugins.

Constructors

  • Creates a new black Color instance.

    Returns Color

  • Creates a new Color instance.

    Parameters

    • model: ColorModel

      The initial color model.

    • components: number[]

      The initial color components.

    Returns Color

Properties

_components: number[]

Color components of the _model.

_model: ColorModel

The current color model.

_tempComponents: null | number[] = null

Color components of the _tempModel.

_tempModel: null | ColorModel = null

The color model that was last acquired by getComponents.

_version: number = 1

Accessors

  • get version(): number
  • The color value version that is auto-incremented every time the useComponents method is called. This version can be used to track that the color value was changed, for example to invalidate caches that rely on current color value.

    Returns number

Methods

  • Creates a clone of this Color instance.

    Returns Color

    The cloned instance.

  • Returns read-only color components of this Color in a particular color model. Components are in [0, 1] range.

    Use this method to acquire color components without changing the current model of this Color instance. Usually this is required if plugin method returns a computed value.

    Note: Don't keep reference to the returned array because it is reused between getComponents invocations.

    Parameters

    • model: ColorModel

      The color model that provides color components.

    Returns readonly number[]

    Read-only color components.

  • Returns mutable color components of this Color in a particular color model. Components are in [0, 1] range.

    Use this method in plugins if you want Color instance to switch the current color model or if you want to update color components.

    Note: Don't keep reference to the returned array because it is reused between useComponents invocations.

    Parameters

    • model: ColorModel

      The color model that provides color components.

    Returns number[]

    Mutable color components.

  • Parser that transforms input values into Color instances. When another color instance is provided, it is returned as is.

    Parameters

    Returns Color

    The parsed color, or black color if value is invalid.

    clr

Plugin Methods

  • Returns opacity (alpha) component.

    Returns number

    Alpha ∈ [0, 1], 0 = transparent, 1 = opaque.

  • Sets opacity (alpha) component.

    clr().alpha(0.2).alpha(a => a * 2).alpha();
    // ⮕ 0.4

    Parameters

    • a: Applicator<number>

      Alpha ∈ [0, 1], 0 = transparent, 1 = opaque.

    Returns Color

  • Returns the list of analogous colors.

    Parameters

    • Optionaln: number

      The number of colors to pick.

    • Optionalslices: number

      The number of colors to pick.

    Returns Color[]

    The list of Color instances.

  • Returns CMYKa components as an array where cyan, magenta, yellow, and black ∈ [0, 100], and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().cmyk();
    // ⮕ [0, 0, 0, 1, 1]

    Returns CMYK

  • Sets HSLa components.

    clr().cmyk(([, , y]) => [50, 64, y, 43, 0.5]);
    

    Parameters

    • cmyk: Applicator<CMYK, [cyan: number, magenta: number, yellow: number, black: number, alpha: number]>

      The tuple of cyan, magenta, yellow, and black ∈ [0, 100], and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If a cyan, magenta, yellow, or black component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Returns color as an RGBa CSS string, or as a hex string if alpha channel is set to 1.

    clr('#abc').css();
    // ⮕ #aabbcc

    clr([26, 51, 89, 0.5]).css();
    // ⮕ rgba(26,51,89,0.50)

    Returns string

  • Sets color from CSS string.

    clr().css('hsl(290, 20%, 50% / 80%)').css();
    // ⮕ rgba(145,102,153,0.80)

    Parameters

    Returns Color

  • Returns color as a hex string. This produces a 32-bit hex string if the alpha channel isn't set to 1.

    clr([170, 186, 204, 0.87]).cssHex();
    // ⮕ #aabbccdd

    Returns string

  • Returns color as an HSLa CSS string.

    clr('#abc').cssHSL();
    // ⮕ hsla(210,25%,73%,0.5)

    Returns string

  • Returns color as an RGBa CSS string.

    clr([26, 51, 89]).cssRGB();
    // ⮕ rgb(26,51,89)

    Returns string

  • Returns HSLa components as an array where hue ∈ [0, 360], saturation and lightness ∈ [0, 100] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().hsl();
    // ⮕ [0, 0, 0, 1]

    Returns HSL

  • Sets HSLa components.

    clr().hsl(([, , l]) => [240, 100, l, 0.5]);
    

    Parameters

    • hsl: Applicator<HSL, [hue: number, saturation: number, lightness: number, alpha: number]>

      The tuple of hue ∈ [0, 360], saturation and lightness ∈ [0, 100] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If a hue, saturation or lightness component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Returns HSVa components as an array where hue ∈ [0, 360], saturation and value ∈ [0, 100] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().hsv();
    // ⮕ [0, 0, 0, 1]

    Returns HSV

  • Sets HSVa components.

    clr().hsv(([, , v]) => [240, 100, v, 0.5]);
    

    Parameters

    • hsv: Applicator<HSV, [hue: number, saturation: number, value: number, alpha: number]>

      The tuple of hue ∈ [0, 360], saturation and value ∈ [0, 100] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If a hue, saturation or value component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Returns HWBa components as an array where hue ∈ [0, 360], whiteness and blackness ∈ [0, 100] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().hwb();
    // ⮕ [0, 0, 0, 1]

    Returns HWB

  • Sets HWBa components.

    clr().hwb(([, , b]) => [240, 100, b, 0.5]);
    

    Parameters

    • hwb: Applicator<HWB, [hue: number, whiteness: number, blackness: number, alpha: number]>

      The tuple of hue ∈ [0, 360], whiteness and blackness ∈ [0, 100] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If a hue, whiteness or blackness component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Returns CIE-L*a*b* components as an array where L* ∈ [0, 100], a* and b* ∈ [-100, 100], and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().lab();
    // ⮕ [0, 0, 0, 1]

    Returns LAB

  • Sets CIE-L*a*b* components.

    Parameters

    • lab: Applicator<LAB, [lightness: number, a: number, b: number, alpha: number]>

      The tuple of L* ∈ [0, 100], a* and b* ∈ [-100, 100], and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If L*, a*, or b* component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Returns Hunter L, a, b components as an array where L ∈ [0, 100], a and b ∈ [-128, 128], and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().labh();
    // ⮕ [0, 0, 0, 1]

    Returns LABh

  • Sets Hunter L, a, b components.

    Parameters

    • labh: Applicator<LABh, [L: number, a: number, b: number, alpha: number]>

      The tuple of L ∈ [0, 100], a and b ∈ [-128, 128], and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If L, a, or b component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Mixes colors in given proportion. Alpha is left unchanged.

    Parameters

    • color: ColorLike

      The color to mix.

    • ratio: number

      The percentage ∈ [0, 1] of the mix between colors.

    Returns Color

  • Returns RGBa components as an array where red, green and blue ∈ [0, 255] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque).

    clr().rgb();
    // ⮕ [0, 0, 0, 1]

    Returns RGB

  • Sets RGBa components.

    clr().rgb(([, , B]) => [255, 255, B, 0.5]);
    

    Parameters

    • rgb: Applicator<RGB, [red: number, green: number, blue: number, alpha: number]>

      The tuple of red, green and blue ∈ [0, 255] and alpha ∈ [0, 1] (0 = transparent, 1 = opaque). If red, green or blue component is omitted it is set to 0. If alpha component is omitted it is set to 1.

    Returns Color

  • Returns 24-bit integer representing RGB components without alpha.

    clr().rgb24();
    // ⮕ 0x00_00_00

    Returns number

  • Sets RGB components from 24-bit integer representation.

    clr().rgb24(0xff_ff_ff).rgb();
    // ⮕ [255, 255, 255, 1]

    Parameters

    • rgb: Applicator<number>

      The 24-bit integer, representing color in RGB model.

    Returns Color

  • Returns 32-bit integer representing RGBa components.

    clr().rgb32();
    // ⮕ 0x00_00_00_ff

    Returns number

  • Sets RGBa components from 32-bit integer representation.

    clr().rgb32(0xaa_bb_cc_dd).rgb();
    // ⮕ [170, 187, 204, 0.86]

    Parameters

    • rgb: Applicator<number>

      The 32-bit integer, representing color in RGBa model.

    Returns Color