utils/colors

Colors provides functions for easy-to-use color calculations and conversions. Functions requiring RGB or HSL color definitions as arguments (all rgb2x and hsl2x) can be called with different types of arguments to make using them more convenient. Examples:

  • rgb2hsl(240, 128, 128)
  • rgb2hsl({'r':240,'g':128,'b':128}
  • rgb2hsl([240, 128, 128])

The universal functions color2x take even more flexible arguments. The following examples all define the same color:

  • "lightcoral"
  • "#F08080"
  • [0,0.31,0.28]
  • 240,128,128
  • {"r":240,"g":128,"b":128}
Source:

Methods

(inner) color2bw(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string either black or white at highest contrast compared to the argument from any type of valid color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or hue (0..1) or object with members r, g, b or h, s, l or array of RGB or HSL or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) color2gray(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string of the grayscaled argument from any type of valid color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or hue (0..1) or object with members r, g, b or h, s, l or array of RGB or HSL or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) color2hex(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string from any type of valid color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or hue (0..1) or object with members r, g, b or h, s, l or array of RGB or HSL or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) color2hsl(1st_value, 2nd_valueopt, 3rd_valueopt) → {object}

Source:

Returns an object containing hue ('h'), saturation ('s') and lightness ('l') from any type of valid color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or hue (0..1) or object with members r, g, b or h, s, l or array of RGB or HSL or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Object with members h, s and l as numbers (0..1)

Type
object

(inner) color2rgb(1st_value, 2nd_valueopt, 3rd_valueopt) → {object}

Source:

Returns an object containing red ('r'), green ('g') and blue ('b') from any type of valid color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or hue (0..1) or object with members r, g, b or h, s, l or array of RGB or HSL or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Object with members r, g and b as numbers (0..255)

Type
object

(inner) color2wb(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string either black or white at lowest contrast compared to the argument from any type of valid color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or hue (0..1) or object with members r, g, b or h, s, l or array of RGB or HSL or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) hex2bw(hex) → {string}

Source:

Returns a hex color string either black or white at highest contrast compared to the argument from a hex color string.

Parameters:
Name Type Description
hex string

Hex color string.

Returns:

Hex color string.

Type
string

(inner) hex2gray(hex) → {string}

Source:

Returns a hex color string of the grayscaled argument from a hex color string.

Parameters:
Name Type Description
hex string

Hex color string.

Returns:

Hex color string.

Type
string

(inner) hex2hsl(hex) → {object}

Source:

Returns an object containing hue ('h'), saturation ('s') and lightness ('l') from a hex color string.

Parameters:
Name Type Description
hex string

Hex color string.

Returns:

Object with members h, s and l as numbers (0..1)

Type
object

(inner) hex2rgb(hex) → {object}

Source:

Returns an object containing red ('r'), green ('g') and blue ('b') from a hex color string.

Parameters:
Name Type Description
hex string

Hex color string.

Returns:

Object with members r, g and b as numbers (0..255)

Type
object

(inner) hex2wb(hex) → {string}

Source:

Returns a hex color string either black or white at lowest contrast compared to the argument from a hex color string.

Parameters:
Name Type Description
hex string

Hex color string.

Returns:

Hex color string.

Type
string

(inner) hsl2bw(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string either black or white at highest contrast compared to the argument from a HSL color.

Parameters:
Name Type Attributes Description
1st_value number | array | object

hue (0..1) or object with members h, s, l or array of HSL.

2nd_value number <optional>

saturation (0..1)

3rd_value number <optional>

lightness (0..1)

Returns:

Hex color string.

Type
string

(inner) hsl2gray(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string of the grayscaled argument from a HSL color.

Parameters:
Name Type Attributes Description
1st_value number | array | object

hue (0..1) or object with members h, s, l or array of HSL.

2nd_value number <optional>

saturation (0..1)

3rd_value number <optional>

lightness (0..1)

Returns:

Hex color string.

Type
string

(inner) hsl2hex(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string from a HSL color.

Parameters:
Name Type Attributes Description
1st_value number | array | object

hue (0..1) or object with members h, s, l or array of HSL.

2nd_value number <optional>

saturation (0..1)

3rd_value number <optional>

lightness (0..1)

Returns:

Hex color string.

Type
string

(inner) hsl2rgb(1st_value, 2nd_valueopt, 3rd_valueopt) → {object}

Source:

Returns an object containing red ('r'), green ('g') and blue ('b') from a HSL color.

Parameters:
Name Type Attributes Description
1st_value number | array | object

hue (0..1) or object with members h, s, l or array of HSL.

2nd_value number <optional>

saturation (0..1)

3rd_value number <optional>

lightness (0..1)

Returns:

Object with members r, g and b as numbers (0..255)

Type
object

(inner) hsl2wb(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string either black or white at lowest contrast compared to the argument from a HSL color.

Parameters:
Name Type Attributes Description
1st_value number | array | object

hue (0..1) or object with members h, s, l or array of HSL.

2nd_value number <optional>

saturation (0..1)

3rd_value number <optional>

lightness (0..1)

Returns:

Hex color string.

Type
string

(inner) name2bw(color) → {string}

Source:

Returns a hex color string either black or white at highest contrast compared to the argument from a color name.

Parameters:
Name Type Description
color string

Color name.

Returns:

Hex color string.

Type
string

(inner) name2gray(color) → {string}

Source:

Returns a hex color string of the grayscaled argument from a color name.

Parameters:
Name Type Description
color string

Color name.

Returns:

Hex color string.

Type
string

(inner) name2hex(color) → {string}

Source:

Returns a hex color string from a color name.

Parameters:
Name Type Description
color string

Color name.

Returns:

Hex color string.

Type
string

(inner) name2hsl(color) → {object}

Source:

Returns an object containing hue ('h'), saturation ('s') and lightness ('l') from a color name.

Parameters:
Name Type Description
color string

Color name.

Returns:

Object with members h, s and l as numbers (0..1)

Type
object

(inner) name2rgb(color) → {object}

Source:

Returns an object containing red ('r'), green ('g') and blue ('b') from a color name.

Parameters:
Name Type Description
color string

Color name.

Returns:

Object with members r, g and b as numbers (0..255)

Type
object

(inner) name2wb(color) → {string}

Source:

Returns a hex color string either black or white at lowest contrast compared to the argument from a color name.

Parameters:
Name Type Description
color string

Color name.

Returns:

Hex color string.

Type
string

(inner) rgb2bw(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string either black or white at highest contrast compared to the argument from a RGB color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or object with members r, g, b or array of RGB or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) rgb2gray(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string of the grayscaled argument from a RGB color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or object with members r, g, b or array of RGB or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) rgb2hex(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string from a RGB color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or object with members r, g, b or array of RGB or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string

(inner) rgb2hsl(1st_value, 2nd_valueopt, 3rd_valueopt) → {object}

Source:

Returns an object containing hue ('h'), saturation ('s') and lightness ('l') from a RGB color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or object with members r, g, b or array of RGB or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Object with members h, s and l as numbers (0..1)

Type
object

(inner) rgb2wb(1st_value, 2nd_valueopt, 3rd_valueopt) → {string}

Source:

Returns a hex color string either black or white at lowest contrast compared to the argument from a RGB color.

Parameters:
Name Type Attributes Description
1st_value number | array | object | string

red (0..255) or object with members r, g, b or array of RGB or color name or hex string.

2nd_value number <optional>

green (0..255) or saturation (0..1)

3rd_value number <optional>

blue (0..255) or lightnes (0..1)

Returns:

Hex color string.

Type
string