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:

  • RGBToHSL(240, 128, 128)
  • RGBToHSL({'r':240,'g':128,'b':128}
  • RGBToHSL([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) colorToBW(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) colorToGray(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) colorToHex(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) colorToHSL(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) colorToRGB(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) colorToWB(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) hexToBW(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) hexToGray(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) hexToHSL(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) hexToRGB(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) hexToWB(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) HSLToBW(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) HSLToGray(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) HSLToHex(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) HSLToRGB(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) HSLToWB(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) nameToBW(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) nameToGray(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) nameToHex(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) nameToHSL(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) nameToRGB(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) nameToWB(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) RGBToBW(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) RGBToGray(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) RGBToHex(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) RGBToHSL(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) RGBToWB(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