Class: Color


CloudControl 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

Name Type Description
value number An 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

Name Type
obj Color

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

Name Type Description
other Color 要比较的颜色
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

Name Type Description
a number alpha 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

Name Type Description
b number blue 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

Name Type Description
g number green 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

Name Type Description
opacity number opacity 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

Name Type Description
r number red 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

Name Type Description
a number alpha value
r number red value
g number green value
b number blue 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

Name Type Description
gray number the 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

Name Type Description
r number 红色通道的值,范围为0-255
g number 绿色通道的值,范围为0-255
b number 蓝色通道的值,范围为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

Name Type Description
r number red value
g number green value
b number blue value
opacity number alpha 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

Name Type Description
color string 颜色字符串,格式为#RRGGBB或#AARRGGBB

Returns

null | Color

新的颜色,或者null