parse-color.js

A simple JavaScript function for parsing colors in a variety of formats

parse-color.js is a simple JavaScript function for parsing colors in a variety of formats.

1
var color = parse_color('#000000');

It is also possible to generate a random color.

1
var random_color = parse_color();

As well as with a specific hue (0-360).

1
var random_red = parse_color(0);

Arguments

parse_color() accepts arguments in the following formats:

Strings

Note, for the following, case is ignored, for hex the hash sign is optional, and for rgb(a) spaces are optional.

  • '#000'
  • '#000000'
  • 'rgb(0, 0, 0)'
  • 'rgba(0, 0, 0, 0.5)'

Objects

  • {'h': 0, 's': 0, 'l': 0}
  • {'h': 0, 's': 0, 'v': 0}
  • {'r': 0, 'g': 0, 'b': 0}

Return

parse_color() returns an object with the following properties:

Property Type Description
hex string The hex value of the color without the hash sign.
hsl object The HSL values of the color.
hsv object The HSV values of the color.
luma number The luma (Y'709) of the color.
rgb object The RGB values of the color.