Color
Color
color.Color
Immutable 32-bit color packed as 0xAARRGGBB (alpha in the high byte).
Table of contents
Constructors
Accessors
Methods
- equals
- isSimilarTo
- toString
- withAlpha
- withBlue
- withGreen
- withOpacity
- withRed
- fromARGB
- fromGray
- fromRGB
- fromRGBO
- parse
Constructors
constructor
• new Color(value)
Wrap an existing packed integer.
Parameters
| Name | Type | Description |
|---|---|---|
value | number | 0xAARRGGBB bitmask. |
Accessors
alpha
• get alpha(): number
Alpha channel 0–255. 0 = fully transparent, 255 = opaque.
Example
"nodejs";
const { Color } = require("color");
const color = new Color(0xff000000);
console.log(color.alpha); // 255Returns
number
blue
• get blue(): number
Blue channel 0–255.
Returns
number
green
• get green(): number
Green channel 0–255.
Returns
number
opacity
• get opacity(): number
Same alpha as alpha / 255: 0.0 transparent, 1.0 opaque.
Example
"nodejs";
const { Color } = require("color");
const color = new Color(0xff000000);
console.log(color.opacity); // 1.0Returns
number
red
• get red(): number
Red channel 0–255.
Returns
number
value
• get value(): number
Packed 0xAARRGGBB:
- bits 24–31 — alpha
- bits 16–23 — red
- bits 8–15 — green
- bits 0–7 — blue
Returns
number
Methods
equals
▸ equals(obj): boolean
Exact bitwise equality with another Color.
Parameters
| Name | Type |
|---|---|
obj | Color |
Returns
boolean
isSimilarTo
▸ isSimilarTo(other, options?): boolean
Perceptual / thresholded similarity; 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 })); // falseParameters
| Name | Type | Description |
|---|---|---|
other | Color | Color to compare. |
options? | CompareColorOptions | Tolerance knobs. |
Returns
boolean
toString
▸ toString(): string
Debug string such as Color(0xAARRGGBB).
Returns
string
withAlpha
▸ withAlpha(a): Color
Copy with a new 8-bit alpha. Out-of-range inputs are not clamped—keep a in 0–255.
Example
"nodejs";
const { Color } = require("color");
const color = new Color(0xff000000);
console.log(color.withAlpha(0x77).toString()); // 0x77000000Parameters
| Name | Type | Description |
|---|---|---|
a | number | Alpha 0–255. |
Returns
withBlue
▸ withBlue(b): Color
Copy replacing the blue channel. Values should stay in 0–255.
Parameters
| Name | Type | Description |
|---|---|---|
b | number | Blue 0–255. |
Returns
withGreen
▸ withGreen(g): Color
Copy replacing green (0–255).
Parameters
| Name | Type | Description |
|---|---|---|
g | number | Green 0–255. |
Returns
withOpacity
▸ withOpacity(opacity): Color
Copy with alpha derived from opacity (0.0–1.0).
Example
"nodejs";
const { Color } = require("color");
const color = new Color(0xff000000);
console.log(color.withOpacity(0.5).toString()); // 0x7F000000Parameters
| Name | Type | Description |
|---|---|---|
opacity | number | 0.0–1.0 alpha. |
Returns
withRed
▸ withRed(r): Color
Copy replacing red (0–255).
Parameters
| Name | Type | Description |
|---|---|---|
r | number | Red 0–255. |
Returns
fromARGB
▸ Static fromARGB(a, r, g, b): Color
Build from discrete channels. Out-of-range components are wrapped modulo 256 (implementation detail—still pass sane values).
See also
fromRGBO for a float opacity.
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 0–255. |
r | number | Red 0–255. |
g | number | Green 0–255. |
b | number | Blue 0–255. |
Returns
fromGray
▸ Static fromGray(gray): Color
Opaque gray (alpha = 255, R = G = B = gray).
Example
"nodejs";
const { Color } = require("color");
const gray = Color.fromGray(128);
console.log(gray.toString()); // Color(0xFF808080)Parameters
| Name | Type | Description |
|---|---|---|
gray | number | Luminance 0–255. |
Returns
fromRGB
▸ Static fromRGB(r, g, b): Color
Opaque RGB (alpha = 255).
Example
"nodejs";
const { Color } = require("color");
const red = Color.fromRGB(255, 0, 0);
console.log(red.toString()); // Color(0xFFFF0000)Parameters
| Name | Type | Description |
|---|---|---|
r | number | Red 0–255. |
g | number | Green 0–255. |
b | number | Blue 0–255. |
Returns
fromRGBO
▸ Static fromRGBO(r, g, b, opacity): Color
RGB channels plus floating opacity 0–1. Component wrapping matches fromARGB semantics.
See also
fromARGB for integer alpha.
Parameters
| Name | Type | Description |
|---|---|---|
r | number | Red 0–255. |
g | number | Green 0–255. |
b | number | Blue 0–255. |
opacity | number | Alpha 0.0–1.0. |
Returns
parse
▸ Static parse(color): null | Color
Parse #RRGGBB or #AARRGGBB. Returns null on invalid input.
Example
const { Color } = require("color");
const color = Color.parse("#ff0000");
console.log(color.toString());Parameters
| Name | Type | Description |
|---|---|---|
color | string | Hex color with leading #. |
Returns
null | Color
