Class: Color

util.Color(objopt, colorspaceopt)

new Color(objopt, colorspaceopt)

This class handles multiple color spaces, and offers various static methods for converting colors from one space to another.

The constructor accepts the following color representations:

  • a named color, e.g. 'aliceblue' (the colorspace must be RGB)
  • an hexadecimal string representation, e.g. '#FF0000' (the colorspace must be RGB)
  • an hexadecimal number representation, e.g. 0xFF0000 (the colorspace must be RGB)
  • a triplet of numbers, e.g. [-1, 0, 1], [0, 128, 255] (the numbers must be within the range determined by the colorspace)

Note: internally, colors are represented as a [r,g,b] triplet with r,g,b in [0,1].

Parameters:
Name Type Attributes Default Description
obj string | number | Array.<number> | undefined <optional>
'black' an object representing a color
colorspace module:util.Color#COLOR_SPACE | undefined <optional>
Color.COLOR_SPACE.RGB the colorspace of that color
Source:
To Do:
  • implement HSV, DKL, and LMS colorspaces

Members

(readonly) COLOR_SPACE :Symbol

Color spaces.
Type:
  • Symbol
Source:

(readonly) NAMED_COLORS :Symbol

Named colors.
Type:
  • Symbol
Source:

Methods

(private, static) _checkTypeAndRange(arg, rangeopt) → {boolean}

Check that the argument is an array of numbers of size 3, and, potentially, that its elements fall within the range.
Parameters:
Name Type Attributes Description
arg any the argument
range Array.<number> <optional>
the lower and higher bounds of the range
Source:
Returns:
whether the argument is an array of numbers of size 3, and, potentially, whether its elements fall within the range (if range is not undefined)
Type
boolean

(private, static) _intToRgb(hex) → {Array.<number>}

Get the [0, 1] based RGB triplet equivalent of the integer color code.

Note: this is the fast, unsafe version which does not check for argument sanity

Parameters:
Name Type Description
hex number the integer color code
Source:
Returns:
the [0, 1] RGB equivalent
Type
Array.<number>

(private, static) _intToRgb255(hex) → {Array.<number>}

Get the [0, 255] based RGB triplet equivalent of the integer color code.

Note: this is the fast, unsafe version which does not check for argument sanity

Parameters:
Name Type Description
hex number the integer color code
Source:
Returns:
the [0, 255] RGB equivalent
Type
Array.<number>

(private, static) _rgb255ToHex(rgb255) → {string}

Get the hexadecimal color code equivalent of the [0, 255] RGB triplet.

Note: this is the fast, unsafe version which does not check for argument sanity

Parameters:
Name Type Description
rgb255 Array.<number> the [0, 255] RGB triplet
Source:
Returns:
the hexadecimal color code equivalent
Type
string

(private, static) _rgb255ToInt(rgb255) → {number}

Get the integer equivalent of the [0, 255] RGB triplet.

Note: this is the fast, unsafe version which does not check for argument sanity

Parameters:
Name Type Description
rgb255 Array.<number> the [0, 255] RGB triplet
Source:
Returns:
the integer equivalent
Type
number

(private, static) _rgbToHex(rgb) → {string}

Get the hexadecimal color code equivalent of the [0, 1] RGB triplet.

Note: this is the fast, unsafe version which does not check for argument sanity

Parameters:
Name Type Description
rgb Array.<number> the [0, 1] RGB triplet
Source:
Returns:
the hexadecimal color code equivalent
Type
string

(private, static) _rgbToInt(rgb) → {number}

Get the integer equivalent of the [0, 1] RGB triplet.

Note: this is the fast, unsafe version which does not check for argument sanity

Parameters:
Name Type Description
rgb Array.<number> the [0, 1] RGB triplet
Source:
Returns:
the integer equivalent
Type
number

(static) hex() → {string}

Get the hexadecimal color code equivalent of this Color.
Source:
Returns:
the hexadecimal color code equivalent
Type
string

(static) hexToRgb(hex) → {Array.<number>}

Get the [0,1] RGB triplet equivalent of the hexadecimal color code.
Parameters:
Name Type Description
hex string the hexadecimal color code
Source:
Returns:
the [0,1] RGB triplet equivalent
Type
Array.<number>

(static) hexToRgb255(hex) → {Array.<number>}

Get the [0,255] RGB triplet equivalent of the hexadecimal color code.
Parameters:
Name Type Description
hex string the hexadecimal color code
Source:
Returns:
the [0,255] RGB triplet equivalent
Type
Array.<number>

(static) int() → {number}

Get the integer code equivalent of this Color.
Source:
Returns:
the integer code equivalent
Type
number

(static) rgb() → {Array.<number>}

Get the [0,1] RGB triplet equivalent of this Color.
Source:
Returns:
the [0,1] RGB triplet equivalent
Type
Array.<number>

(static) rgb255() → {Array.<number>}

Get the [0,255] RGB triplet equivalent of this Color.
Source:
Returns:
the [0,255] RGB triplet equivalent
Type
Array.<number>

(static) rgb255ToHex(rgb255) → {string}

Get the hexadecimal color code equivalent of the [0, 255] RGB triplet.
Parameters:
Name Type Description
rgb255 Array.<number> the [0, 255] RGB triplet
Source:
Returns:
the hexadecimal color code equivalent
Type
string

(static) rgb255ToInt(rgb255) → {number}

Get the integer equivalent of the [0, 255] RGB triplet.
Parameters:
Name Type Description
rgb255 Array.<number> the [0, 255] RGB triplet
Source:
Returns:
the integer equivalent
Type
number

(static) rgbToHex(rgb) → {string}

Get the hexadecimal color code equivalent of the [0, 1] RGB triplet.
Parameters:
Name Type Description
rgb Array.<number> the [0, 1] RGB triplet
Source:
Returns:
the hexadecimal color code equivalent
Type
string

(static) rgbToInt(rgb) → {number}

Get the integer equivalent of the [0, 1] RGB triplet.
Parameters:
Name Type Description
rgb Array.<number> the [0, 1] RGB triplet
Source:
Returns:
the integer equivalent
Type
number