Skip to content

pencil.js / color / Color

Class: Color ​

Defined in: color.ts:17

Color class

Constructors ​

Constructor ​

ts
new Color(...colorDefinition): Color;

Defined in: color.ts:45

Color constructor

Parameters ​

ParameterTypeDescription
...colorDefinitionColorDefinition[]Many types accepted (other Color instance, color name, hex string, hex number, red/green/blue/alpha value)

Returns ​

Color

Example ​

ts
new Color("indigo"); // Any valid CSS color name
new Color("#123456"); // Hex string definition
new Color("#123"); // Hex shorthand string definition, #123 <=> #112233
new Color("rgb(10, 20, 30)"); // Any valid CSS color string
new Color(0x123456); // Hex number definition
new Color(0.1, 0.2, 0.3); // Red, Green, Blue definition
// Every definition can have one more optional parameter for alpha (opacity)
new Color("violet", 0.5);

Accessors ​

alpha ​

Get Signature ​

ts
get alpha(): number;

Defined in: color.ts:99

Transparency channel value (from 0 to 1)

Returns ​

number

Set Signature ​

ts
set alpha(value): void;

Defined in: color.ts:103

Parameters ​
ParameterType
valuenumber
Returns ​

void


array ​

Get Signature ​

ts
get array(): [number, number, number];

Defined in: color.ts:111

Return an array with red, green and blue value

Example ​
ts
[0.1, 0.2, 0.3]
Returns ​

[number, number, number]


blue ​

Get Signature ​

ts
get blue(): number;

Defined in: color.ts:88

Blue channel value (from 0 to 1)

Returns ​

number

Set Signature ​

ts
set blue(value): void;

Defined in: color.ts:92

Parameters ​
ParameterType
valuenumber
Returns ​

void


green ​

Get Signature ​

ts
get green(): number;

Defined in: color.ts:77

Green channel value (from 0 to 1)

Returns ​

number

Set Signature ​

ts
set green(value): void;

Defined in: color.ts:81

Parameters ​
ParameterType
valuenumber
Returns ​

void


hex ​

Get Signature ​

ts
get hex(): string;

Defined in: color.ts:119

Return hexadecimal notation (with alpha when not opaque)

Example ​
ts
"#123456"
Returns ​

string


name ​

Get Signature ​

ts
get name(): string;

Defined in: color.ts:136

Return the closest CSS color name

Example ​
ts
"aliceblue"
Returns ​

string


red ​

Get Signature ​

ts
get red(): number;

Defined in: color.ts:66

Red channel value (from 0 to 1)

Returns ​

number

Set Signature ​

ts
set red(value): void;

Defined in: color.ts:70

Parameters ​
ParameterType
valuenumber
Returns ​

void


rgb ​

Get Signature ​

ts
get rgb(): string;

Defined in: color.ts:128

Return hexadecimal notation (alias of hex)

Example ​
ts
"#123456"
Returns ​

string

Methods ​

clone() ​

ts
clone(): Color;

Defined in: color.ts:52

Create a new copy of this color

Returns ​

Color


gray() ​

ts
gray(): this;

Defined in: color.ts:194

Change to its greyscale value (alias of grey)

Returns ​

this

Itself


grey() ​

ts
grey(): this;

Defined in: color.ts:185

Change to its greyscale value

Returns ​

this

Itself


hue() ​

ts
hue(value): this;

Defined in: color.ts:203

Change hue value (0 = red, 0.5 = cyan, 1 = red, 1.5 = cyan ...)

Parameters ​

ParameterTypeDescription
valuenumberAny value between 0 and 1

Returns ​

this

Itself


lerp() ​

ts
lerp(colorDefinition, ratio): this;

Defined in: color.ts:267

Change the color toward another color

Parameters ​

ParameterTypeDescription
colorDefinitionColorDefinitionAny other color
rationumberRatio of distance to move (0 = no change, 0.5 = equal mix, 1 = same as target color)

Returns ​

this

Itself


level() ​

ts
level(number): this;

Defined in: color.ts:248

Restrict the color space to an amount of possible value

Parameters ​

ParameterTypeDescription
numbernumberNumber of allowed value

Returns ​

this

Itself


lightness() ​

ts
lightness(value): this;

Defined in: color.ts:226

Change lightness value (0 = black, 0.5 = pure color, 1 = white)

Parameters ​

ParameterTypeDescription
valuenumberAny value between 0 and 1

Returns ​

this

Itself


mix() ​

ts
mix(colorDefinition, ratio): this;

Defined in: color.ts:279

Change the color toward another color (alias of lerp)

Parameters ​

ParameterTypeDescription
colorDefinitionColorDefinitionAny other color
rationumberRatio of distance to move (0 = no change, 0.5 = equal mix, 1 = same as target color)

Returns ​

this

Itself


reverse() ​

ts
reverse(): this;

Defined in: color.ts:238

Invert the color value

Returns ​

this

Itself


saturation() ​

ts
saturation(value): this;

Defined in: color.ts:213

Change saturation value (0 = grey, 1 = pure color)

Parameters ​

ParameterTypeDescription
valuenumberAny value between 0 and 1

Returns ​

this

Itself


set() ​

ts
set(...colorDefinition): this;

Defined in: color.ts:147

Change this values

Parameters ​

ParameterTypeDescription
...colorDefinitionColorDefinition[]Any supported color definition (see constructor)

Returns ​

this

Itself


toJSON() ​

ts
toJSON(): [number, number, number, number];

Defined in: color.ts:293

Return a json ready array [red, green, blue, alpha]

Returns ​

[number, number, number, number]


toString() ​

ts
toString(): string;

Defined in: color.ts:286

Return a CSS compatible color string

Returns ​

string


from() ​

ts
static from(...colorDefinition): Color;

Defined in: color.ts:301

Return an instance from a generic definition

Parameters ​

ParameterTypeDescription
...colorDefinition| ColorDefinition[] | [[number, number, number, number?]]Any valid color definition (see constructor)

Returns ​

Color

Released under the MIT License.