Color

util Color

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].

Constructor

new Color(objopt, colorspaceopt)

Source:
To Do:
  • implement HSV, DKL, and LMS colorspaces
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

Members

(static, readonly) COLOR_SPACE :Symbol

Source:
Properties:
Name Type Description
RGB Symbol

RGB colorspace: [r,g,b] with r,g,b in [-1, 1]

RGB255 Symbol

RGB255 colorspace: [r,g,b] with r,g,b in [0, 255]

Color spaces.

Type:
  • Symbol

(static, readonly) NAMED_COLORS :string

Source:
Properties:
Name Type Description
aliceblue string
antiquewhite string
aqua string
aquamarine string
azure string
beige string
bisque string
black string
blanchedalmond string
blue string
blueviolet string
brown string
burlywood string
cadetblue string
chartreuse string
chocolate string
coral string
cornflowerblue string
cornsilk string
crimson string
cyan string
darkblue string
darkcyan string
darkgoldenrod string
darkgray string
darkgrey string
darkgreen string
darkkhaki string
darkmagenta string
darkolivegreen string
darkorange string
darkorchid string
darkred string
darksalmon string
darkseagreen string
darkslateblue string
darkslategray string
darkslategrey string
darkturquoise string
darkviolet string
deeppink string
deepskyblue string
dimgray string
dimgrey string
dodgerblue string
firebrick string
floralwhite string
forestgreen string
fuchsia string
gainsboro string
ghostwhite string
gold string
goldenrod string
gray string
grey string
green string
greenyellow string
honeydew string
hotpink string
indianred string
indigo string
ivory string
khaki string
lavender string
lavenderblush string
lawngreen string
lemonchiffon string
lightblue string
lightcoral string
lightcyan string
lightgoldenrodyellow string
lightgray string
lightgrey string
lightgreen string
lightpink string
lightsalmon string
lightseagreen string
lightskyblue string
lightslategray string
lightslategrey string
lightsteelblue string
lightyellow string
lime string
limegreen string
linen string
magenta string
maroon string
mediumaquamarine string
mediumblue string
mediumorchid string
mediumpurple string
mediumseagreen string
mediumslateblue string
mediumspringgreen string
mediumturquoise string
mediumvioletred string
midnightblue string
mintcream string
mistyrose string
moccasin string
navajowhite string
navy string
oldlace string
olive string
olivedrab string
orange string
orangered string
orchid string
palegoldenrod string
palegreen string
paleturquoise string
palevioletred string
papayawhip string
peachpuff string
peru string
pink string
plum string
powderblue string
purple string
red string
rosybrown string
royalblue string
saddlebrown string
salmon string
sandybrown string
seagreen string
seashell string
sienna string
silver string
skyblue string
slateblue string
slategray string
slategrey string
snow string
springgreen string
steelblue string
tan string
teal string
thistle string
tomato string
turquoise string
violet string
wheat string
white string
whitesmoke string
yellow string
yellowgreen string

Named colors.

Type:
  • string

hex

Source:

Get the hexadecimal color code equivalent of this Color.

int

Source:

Get the integer code equivalent of this Color.

rgb

Source:

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

rgb255

Source:

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

rgbFull

Source:

Get the [-1,1] RGB triplet equivalent of this Color.

Methods

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

Source:

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

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

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

Source:

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

Returns:

the [0, 1] RGB equivalent

Type
Array.<number>

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

Source:

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

Returns:

the [0, 255] RGB equivalent

Type
Array.<number>

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

Source:

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

Returns:

the hexadecimal color code equivalent

Type
string

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

Source:

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

Returns:

the integer equivalent

Type
number

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

Source:

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

Returns:

the hexadecimal color code equivalent

Type
string

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

Source:

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

Returns:

the integer equivalent

Type
number

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

Source:

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

Parameters:
Name Type Description
hex string

the hexadecimal color code

Returns:

the [0,1] RGB triplet equivalent

Type
Array.<number>

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

Source:

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

Parameters:
Name Type Description
hex string

the hexadecimal color code

Returns:

the [0,255] RGB triplet equivalent

Type
Array.<number>

(static) rgb255ToHex(rgb255) → {string}

Source:

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

Parameters:
Name Type Description
rgb255 Array.<number>

the [0, 255] RGB triplet

Returns:

the hexadecimal color code equivalent

Type
string

(static) rgb255ToInt(rgb255) → {number}

Source:

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

Parameters:
Name Type Description
rgb255 Array.<number>

the [0, 255] RGB triplet

Returns:

the integer equivalent

Type
number

(static) rgbToHex(rgb) → {string}

Source:

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

Parameters:
Name Type Description
rgb Array.<number>

the [0, 1] RGB triplet

Returns:

the hexadecimal color code equivalent

Type
string

(static) rgbToInt(rgb) → {number}

Source:

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

Parameters:
Name Type Description
rgb Array.<number>

the [0, 1] RGB triplet

Returns:

the integer equivalent

Type
number

toString() → {string}

Source:

String representation of the color, i.e. the hexadecimal representation.

Returns:

the representation.

Type
string