Class: Color


Auto.js Pro 9 Docs / color / Color

Class: Color

color.Color

An immutable 32 bit color value in ARGB format.

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new Color(value)

Constructs a color from an integer value.

Parameters

NameTypeDescription
valuenumberAn integer value, formatted as 0xAARRGGBB

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

equals

equals(obj): boolean

Compare two colors is equal, including alpha channel.

Parameters

NameType
objColor

Returns

boolean

two colors are equal


isSimilarTo

isSimilarTo(other, options?): boolean

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

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

NameTypeDescription
otherColor要比较的颜色
options?CompareColorOptions比较选项

Returns

boolean

两个颜色是否相似


toString

toString(): string

Returns

string


withAlpha

withAlpha(a): 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

NameTypeDescription
anumberalpha channel

Returns

Color

a new color


withBlue

withBlue(b): 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

NameTypeDescription
bnumberblue channel

Returns

Color

a new color


withGreen

withGreen(g): 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

NameTypeDescription
gnumbergreen channel

Returns

Color

a new color


withOpacity

withOpacity(opacity): 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

NameTypeDescription
opacitynumberopacity value

Returns

Color

a new color


withRed

withRed(r): 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

NameTypeDescription
rnumberred channel

Returns

Color

a new color


fromARGB

Static fromARGB(a, r, g, b): 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

NameTypeDescription
anumberalpha value
rnumberred value
gnumbergreen value
bnumberblue value

Returns

Color

a new color


fromGray

Static fromGray(gray): 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

NameTypeDescription
graynumberthe grayscale value

Returns

Color

a new color


fromRGB

Static fromRGB(r, g, b): 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

NameTypeDescription
rnumber红色通道的值,范围为0-255
gnumber绿色通道的值,范围为0-255
bnumber蓝色通道的值,范围为0-255

Returns

Color

新的颜色对象


fromRGBO

Static fromRGBO(r, g, b, opacity): 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

NameTypeDescription
rnumberred value
gnumbergreen value
bnumberblue value
opacitynumberalpha value

Returns

Color

a new color


parse

Static parse(color): 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

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

Returns

null | Color

新的颜色,或者null