The goog.color Namespace

.Hsl

HSL color representation. An array containing three elements [h, s, l]: h (hue) must be an integer in [0, 360], cyclic. s (saturation) must be a number in [0, 1]. l (lightness) must be a number in [0, 1].

@typedef
{Array.<number>}

.Hsv

HSV color representation. An array containing three elements [h, s, v]: h (hue) must be an integer in [0, 360], cyclic. s (saturation) must be a number in [0, 1]. v (value/brightness) must be an integer in [0, 255].

@typedef
{Array.<number>}

.Rgb

RGB color representation. An array containing three elements [r, g, b], each an integer in [0, 255], representing the red, green, and blue components of the color respectively.

@typedef
{Array.<number>}

The goog.color.alpha Namespace

… more

.blend(rgb1, rgb2, factor)

Blend two colors together, using the specified factor to indicate the weight given to the first color

rgb1 {goog.color.Rgb}
First color represented in rgb.
rgb2 {goog.color.Rgb}
Second color represented in rgb.
factor {number}
The weight to be given to rgb1 over rgb2. Values should be in the range [0, 1]. If less than 0, factor will be set to 0. If greater than 1, factor will be set to 1.
returns {!goog.color.Rgb}
Combined color represented in rgb.

.darken(rgb, factor)

Adds black to the specified color, darkening it

rgb {goog.color.Rgb}
rgb representation of the color.
factor {number}
Number in the range [0, 1]. 0 will do nothing, while 1 will return black. If less than 0, factor will be set to 0. If greater than 1, factor will be set to 1.
returns {!goog.color.Rgb}
Combined rgb color.

.hexToHsl(hex)

Converts a hex representation of a color to HSL.

hex {string}
Color to convert.
returns {!goog.color.Hsv}
hsv representation of the color.

.hexToHsv(hex)

Converts a hex representation of a color to HSV

hex {string}
Color to convert.
returns {!goog.color.Hsv}
hsv representation of the color.

.hexToRgb(hexColor)

Converts a hex representation of a color to RGB.

hexColor {string}
Color to convert.
returns {!goog.color.Rgb}
rgb representation of the color.

.hexToRgbStyle(hexColor)

Converts a hex representation of a color to RGB.

hexColor {string}
Color to convert.
returns {string}
string of the form 'rgb(R,G,B)' which can be used in styles.

.highContrast(prime, suggestions)

Find the "best" (highest-contrast) of the suggested colors for the prime color. Uses W3C formula for judging readability and visual accessibility: http://www.w3.org/TR/AERT#color-contrast

prime {goog.color.Rgb}
Color represented as a rgb array.
suggestions {Array.<goog.color.Rgb>}
Array of colors, each representing a rgb array.
returns {!goog.color.Rgb}
Highest-contrast color represented by an array..

.hslArrayToHex(hsl)

Converts from an hsl array to a hex string

hsl {goog.color.Hsl}
hsl representation of the color.
returns {string}
hex representation of the color.

.hslArrayToRgb(hsl)

Converts a color from HSL color space to RGB color space.

hsl {goog.color.Hsl}
hsl representation of the color.
returns {!goog.color.Rgb}
rgb representation of the color.

.hslDistance(hsl1, hsl2)

Calculates the Euclidean distance between two color vectors on an HSL sphere. A demo of the sphere can be found at: http://en.wikipedia.org/wiki/HSL_color_space In short, a vector for color (H, S, L) in this system can be expressed as (S*L'*cos(2*PI*H), S*L'*sin(2*PI*H), L), where L' = abs(L - 0.5), and we simply calculate the 1-2 distance using these coordinates

hsl1 {goog.color.Hsl}
First color in hsl representation.
hsl2 {goog.color.Hsl}
Second color in hsl representation.
returns {number}
Distance between the two colors, in the range [0, 1].

.hslToHex(h, s, l)

Converts from h,s,l values to a hex string

h {number}
Hue, in [0, 360].
s {number}
Saturation, in [0, 1].
l {number}
Luminosity, in [0, 1].
returns {string}
hex representation of the color.

.hslToRgb(h, s, l)

Converts a color from HSL color space to RGB color space. Modified from {@link http://www.easyrgb.com/math.html}

h {number}
Hue, in [0, 360].
s {number}
Saturation, in [0, 1].
l {number}
Luminosity, in [0, 1].
returns {!goog.color.Rgb}
rgb representation of the color.

.hsvArrayToHex(hsv)

Converts from an HSV array to a hex string

hsv {goog.color.Hsv}
hsv representation of the color.
returns {string}
hex representation of the color.

.hsvArrayToRgb(hsv)

Converts an HSV triplet to an RGB array.

hsv {goog.color.Hsv}
hsv representation of the color.
returns {!goog.color.Rgb}
rgb representation of the color.

.hsvToHex(h, s, v)

Converts from h,s,v values to a hex string

h {number}
Hue, in [0, 360].
s {number}
Saturation, in [0, 1].
v {number}
Value, in [0, 255].
returns {string}
hex representation of the color.

.hsvToRgb(h, s, brightness)

Converts an HSV triplet to an RGB array. V is brightness because b is reserved for blue in RGB.

h {number}
Hue value in [0, 360].
s {number}
Saturation value in [0, 1].
brightness {number}
brightness in [0, 255].
returns {!goog.color.Rgb}
rgb representation of the color.

.isValidColor(str)

Determines if the given string can be parsed as a color. {@see goog.color.parse}.

str {string}
Potential color string.
returns {boolean}
True if str is in a format that can be parsed to a color.

.lighten(rgb, factor)

Adds white to the specified color, lightening it

rgb {goog.color.Rgb}
rgb representation of the color.
factor {number}
Number in the range [0, 1]. 0 will do nothing, while 1 will return white. If less than 0, factor will be set to 0. If greater than 1, factor will be set to 1.
returns {!goog.color.Rgb}
Combined rgb color.

.names

A map that contains a lot of colors that are recognised by various browsers. This list is way larger than the minimal one dictated by W3C. The keys of this map are the lowercase "readable" names of the colors, while the values are the "hex" values.

.normalizeHex(hexColor)

Normalize an hex representation of a color

hexColor {string}
an hex color string.
returns {string}
hex color in the format '#rrggbb' with all lowercase literals.

.parse(str)

Parses a color out of a string.

str {string}
Color in some format.
returns {Object}
Contains two properties: 'hex', which is a string containing a hex representation of the color, as well as 'type', which is a string containing the type of color format passed in ('hex', 'rgb', 'named').

.parseRgb(str)

Parses red, green, blue components out of a valid rgb color string. Throws Error if the color string is invalid.

str {string}
RGB representation of a color. {@see goog.color.isValidRgbColor_}.
returns {!goog.color.Rgb}
rgb representation of the color.

.prependHashIfNecessaryHelper(str)

Takes a string a prepends a '#' sign if one doesn't exist. Small helper method for use by goog.color and friends.

str {string}
String to check.
returns {string}
The value passed in, prepended with a '#' if it didn't already have one.

.prependZeroIfNecessaryHelper(hex)

Takes a hex value and prepends a zero if it's a single digit. Small helper method for use by goog.color and friends.

hex {string}
Hex value to prepend if single digit.
returns {string}
hex value prepended with zero if it was single digit, otherwise the same value that was passed in.

.rgbArrayToHex(rgb)

Converts a color from RGB to hex representation.

rgb {goog.color.Rgb}
rgb representation of the color.
returns {string}
hex representation of the color.

.rgbArrayToHsl(rgb)

Converts a color from RGB color space to HSL color space.

rgb {goog.color.Rgb}
rgb representation of the color.
returns {!goog.color.Hsl}
hsl representation of the color.

.rgbArrayToHsv(rgb)

Converts from an array of RGB values to an array of HSV values.

rgb {goog.color.Rgb}
rgb representation of the color.
returns {!goog.color.Hsv}
hsv representation of the color.

.rgbToHex(r, g, b)

Converts a color from RGB to hex representation.

r {number}
Amount of red, int between 0 and 255.
g {number}
Amount of green, int between 0 and 255.
b {number}
Amount of blue, int between 0 and 255.
returns {string}
hex representation of the color.

.rgbToHsl(r, g, b)

Converts a color from RGB color space to HSL color space. Modified from {@link http://en.wikipedia.org/wiki/HLS_color_space}.

r {number}
Value of red, in [0, 255].
g {number}
Value of green, in [0, 255].
b {number}
Value of blue, in [0, 255].
returns {!goog.color.Hsl}
hsl representation of the color.

.rgbToHsv(red, green, blue)

Converts from RGB values to an array of HSV values.

red {number}
Red value in [0, 255].
green {number}
Green value in [0, 255].
blue {number}
Blue value in [0, 255].
returns {!goog.color.Hsv}
hsv representation of the color.