Class: Color

RS.Math.Color

Class representing an RGBA Color.

new RS.Math.Color (initial)

Creates a new Color. Accepts any arguments that RS.Math.Color#set accepts.

Name Type Description
initial RS.Math.Color | RS.Math.Spectrum | Array | Object | Number optional

initial value.

Example
let c = new RS.Math.Color();
c = new RS.Math.Color(1,0.5,0.7);
c = new RS.Math.Color([0.2,0.3,0.5]);
c = new RS.Math.Color({r: 0.1, r: 0.53, b: 0.2});
c = new RS.Math.Color(new RS.Math.Spectrum([0.2,0.7,0.5]);

Members

a Number

The alpha component of the color

b Number

The blue component of the color

g Number

The green component of the color

r Number

The red component of the color

Methods

add (rhs)RS.Math.Color

Adds another color to this color.

Name Type Description
rhs RS.Math.Color

the other color

Returns:
Type Description
RS.Math.Color this
Example
const col = new RS.Math.Color(0.2,0.2,0.3);
col.add(new RS.Math.Color(0.3,0,0.1));
// col is now {r:0.5,g:0.2,b:0.4,a:1}

cie_intensity ()Number

Returns the intensity of the RGB components, weighted according to the CIE standard.

Returns:
Type Description
Number the intensity

clone ()RS.Math.Color

returns a copy of this color.

Returns:
Type Description
RS.Math.Color

equal (rhs, tolerance)Boolean

Returns whether this color is equal to another color.

Name Type Description
rhs RS.Math.Color

the color to compare to

tolerance Number optional

if provided then this level of tolerance is used.

Returns:
Type Description
Boolean true if equal, false if not
Example
let col = new RS.Math.Color(0.01,0.05,0.03);
col.equal(col);
// returns true
col.equal(new RS.Math.Color(0.02,0.05,0.03));
// returns false
col.equal(new RS.Math.Color(0.02,0.05,0.03),0.1);
// returns true due to tolerance

equal_with_tolerance (rhs, tolerance)Boolean

Returns whether this color is equal to another color within a tolerance.

Name Type Description
rhs RS.Math.Color

the color to compare to

tolerance Number optional

if provided then this level of tolerance is used, otherwise tolerance is 10e-5

Returns:
Type Description
Boolean true if equal, false if not
Example
let col = new RS.Math.Color(0.01,0.05,0.03);
col.equal_with_tolerance(col);
// returns true
col.equal_with_tolerance(new RS.Math.Color(0.02,0.05,0.03));
// returns false
col.equal_with_tolerance(new RS.Math.Color(0.01001,0.05,0.03));
// returns true due to default tolerance
col.equal_with_tolerance(new RS.Math.Color(0.02,0.05,0.03),0.1);
// returns true due to tolerance

gamma_correct (factor)RS.Math.Color

Returns a gamma corrected copy of this color. Equivalent to this ^ (1/factor)

Name Type Description
factor Number

the gamma factor

Returns:
Type Description
RS.Math.Color the gamma corrected color

is_black ()Boolean

Checks if the color is black.

Name Type Description
tolerance. Number optional

A Number used to approximate the comparison.

Returns:
Type Description
Boolean true if black, false if not

linear_intensity ()Number

Returns the intensity of the RGB components, equally weighted.

Returns:
Type Description
Number the intensity

ntsc_intensity ()Number

Returns the intensity of the RGB components, weighted according to the NTSC standard.

Returns:
Type Description
Number the intensity

scale (scale)RS.Math.Color

Uniformly scales this color.

Name Type Description
scale Number

the scaling factor

Returns:
Type Description
RS.Math.Color this
Example
const col = new RS.Math.Color(1,0.5,0.6);
col.scale(0.5);
// col is now {r:0.5,g:0.25,z:0.3,a:1}

set (source)

Sets this color. The source may be of the following types:

In the case of an object being supplied it should have the members r, g, b, and optionally a. If a is omitted or parses as NaN then a will be set to 1. Parsing failures on r, g or b will set them to 0.

Name Type Description
source RS.Math.Color | RS.Math.Spectrum | Array | Object | Number

the object to set from or a set of numbers.

Example
const c = new RS.Math.Color();
c.set(1,0.5,0.7);
c.set([0.2,0.3,0.5]);
c.set({r: 0.1, r: 0.53, b: 0.2});
c.set(new RS.Math.Spectrum([0.2,0.7,0.5]);

subtract (rhs)RS.Math.Color

Subtracts another color from this color.

Name Type Description
rhs RS.Math.Color

the other color

Returns:
Type Description
RS.Math.Color this
Example
const col = new RS.Math.Color(0.2,0.2,0.3);
col.subtract(new RS.Math.Color(0.1,0,0.1));
// col is now {r:0.1,g:0.2,b:0.2,a:1}

tint (rhs)RS.Math.Color

Tints this color by another.

Name Type Description
rhs RS.Math.Color

the color to tint with

Returns:
Type Description
RS.Math.Color this
Example
const col = new RS.Math.Color(0.8,0.8,0.8);
col.tint(new RS.Math.Color(1,0.5,0.6));
// col is now {r:0.8,g:0.4,b:0.48,a:1}

toString ()String

Returns a string describing this Object.

Returns:
Type Description
String A String describing this Object.