Options
All
  • Public
  • Public/Protected
  • All
Menu

An immutable 32 bit color value in ARGB format.

Hierarchy

  • Color

Index

Constructors

constructor

  • new Color(value: number): Color
  • Constructs a color from an integer value.

    Parameters

    • value: number

      An integer value, formatted as 0xAARRGGBB

    Returns Color

Accessors

alpha

  • get alpha(): number
  • The alpha channel of this color in an 8 bit value.

    A value of 0 means this color is fully transparent. A value of 255 means this color is fully opaque.

    example
    "nodejs";
    const { Color } = require('color');
    const color = new Color(0xFF000000); // black
    console.log(color.alpha); // 255
    

    Returns number

blue

  • get blue(): number
  • The blue channel of this color in an 8 bit value.

    Returns number

green

  • get green(): number
  • The green channel of this color in an 8 bit value.

    Returns number

opacity

  • get opacity(): number
  • The alpha channel of this color as a floating value.

    A value of 0.0 means this color is fully transparent. A value of 1.0 means this color is fully opaque.

    example
    "nodejs";
    const { Color } = require('color');
    const color = new Color(0xFF000000); // black
    console.log(color.opacity); // 1.0
    

    Returns number

red

  • get red(): number
  • The red channel of this color in an 8 bit value.

    Returns number

value

  • get value(): number
  • A 32 bit value representing this color.

    The bits are assigned as follows:

    • Bits 24-31 are the alpha value.
    • Bits 16-23 are the red value.
    • Bits 8-15 are the green value.
    • Bits 0-7 are the blue value.

    Returns number

Methods

[custom]

  • [custom](): string
  • Returns string

equals

  • equals(obj: Color): boolean
  • Compare two colors is equal, including alpha channel.

    Parameters

    Returns boolean

    two colors are equal

isSimilarTo

  • 比较当前颜色是否与另一个颜色相似。

    see

    CompareColorOptions

    example
    "nodejs";
    const { Color } = require('color');
    const black = new Color(0xFF000000);
    const white = Color.parse('#FFFFFF');
    const black09 = Color.parse('#090909');
    console.log(black.isSimilarTo(white)) // false
    console.log(black.isSimilarTo(black09)) // true
    console.log(black.isSimilarTo(black09, { threshold: 5 })) // false
    

    Parameters

    Returns boolean

    两个颜色是否相似

toString

  • toString(): string
  • Returns string

withAlpha

  • withAlpha(a: number): Color
  • Returns a new color that matches this color with the alpha channel replaced with a (which ranges from 0 to 255).

    Out of range values will have unexpected effects.

    example
    "nodejs";
    const { Color } = require('color');
    const color = new Color(0xFF000000); // black
    console.log(color.withAlpha(0x77).toString()) // 0x77000000
    

    Parameters

    • a: number

      alpha channel

    Returns Color

    a new color

withBlue

  • withBlue(b: number): Color
  • Returns a new color that matches this color with the blue channel replaced with b (which ranges from 0 to 255).

    Out of range values will have unexpected effects.

    Parameters

    • b: number

      blue channel

    Returns Color

    a new color

withGreen

  • withGreen(g: number): Color
  • Returns a new color that matches this color with the green channel replaced with g (which ranges from 0 to 255).

    Out of range values will have unexpected effects.

    Parameters

    • g: number

      green channel

    Returns Color

    a new color

withOpacity

  • withOpacity(opacity: number): Color
  • Returns a new color that matches this color with the alpha channel replaced with the given opacity (which ranges from 0.0 to 1.0).

    Out of range values will have unexpected effects.

    example
    "nodejs";
    const { Color } = require('color');
    const color = new Color(0xFF000000); // black
    console.log(color.withOpacity(0.5).toString()) // 0x7F000000
    

    Parameters

    • opacity: number

      opacity value

    Returns Color

    a new color

withRed

  • withRed(r: number): Color
  • Returns a new color that matches this color with the red channel replaced with r (which ranges from 0 to 255).

    Out of range values will have unexpected effects.

    Parameters

    • r: number

      red channel

    Returns Color

    a new color

Static fromARGB

  • fromARGB(a: number, r: number, g: number, b: number): Color
  • Construct a color from the lower 8 bits of four integers.

    • a is the alpha value, with 0 being transparent and 255 being fully opaque.
    • r is [red], from 0 to 255.
    • g is [green], from 0 to 255.
    • b is [blue], from 0 to 255.

    Out of range values are brought into range using modulo 255.

    see

    fromRGBO which takes the alpha value as a floating point value.

    example
    "nodejs";
    const { Color } = require('color');
    const red = Color.fromARGB(255, 255, 0, 0);
    console.log(red.toString()) // Color(0xFFFF0000)
    

    Parameters

    • a: number

      alpha value

    • r: number

      red value

    • g: number

      green value

    • b: number

      blue value

    Returns Color

    a new color

Static fromGray

  • fromGray(gray: number): Color
  • Construct a color from a grayscale value. The alpha channel is set to 255, and the R, G, and B channels are set to the same value.

    example
    "nodejs";
    const { Color } = require('color');
    const gray = Color.fromGray(128);
    console.log(gray.toString()) // Color(0xFF808080)
    

    Parameters

    • gray: number

      the grayscale value

    Returns Color

    a new color

Static fromRGB

  • fromRGB(r: number, g: number, b: number): Color
  • Construct a color from RGB channels. The alpha channel is set to 255.

    example
    "nodejs";
    const { Color } = require('color');
    const red = Color.fromRGBO(255, 0, 0);
    console.log(red.toString()) // Color(0xFFFF0000)
    

    Parameters

    • r: number

      红色通道的值,范围为0-255

    • g: number

      绿色通道的值,范围为0-255

    • b: number

      蓝色通道的值,范围为0-255

    Returns Color

    新的颜色对象

Static fromRGBO

  • fromRGBO(r: number, g: number, b: number, opacity: number): Color
  • Create a color from red, green, blue, and opacity, similar to rgba() in CSS.

    • r is [red], from 0 to 255.
    • g is [green], from 0 to 255.
    • b is [blue], from 0 to 255.
    • opacity is alpha channel of this color as a double, with 0.0 being transparent and 1.0 being fully opaque.

    Out of range values are brought into range using modulo 255.

    see

    fromARGB which takes the opacity as an integer value.

    Parameters

    • r: number

      red value

    • g: number

      green value

    • b: number

      blue value

    • opacity: number

      alpha value

    Returns Color

    a new color

Static parse

  • parse(color: string): null | Color
  • Parse a color from a hex string, such as #RRGGBB or #AARRGGBB.

    example
    const { Color } = require('color');
    const color = Color.parse('#ff0000');
    console.log(color.toString());
    

    Parameters

    • color: string

      颜色字符串,格式为#RRGGBB或#AARRGGBB

    Returns null | Color

    新的颜色,或者null