Colors - VividD/d3 GitHub Wiki

WikiAPI ReferenceCoreColors

Constructing visualizations often involves working with colors. Even though your browser understands a lot about colors, it doesn't offer much help in manipulating colors through JavaScript. So D3 provides representations for various color spaces, including RGB, HSL, LAB and HCL, allowing specification, interpolation, conversion and manipulation (such as making colors brighter or darker).

Note: while you can work with colors directly, you might also want to take a look at D3's built-in color interpolation, such as interpolateRgb, interpolateHsl and scales. If you are looking for color palettes, see the ordinal scales reference.

RGB

# d3.rgb(r, g, b)

Constructs a new RGB color with the specified r, g and b channel values. Each channel must be specified as an integer in the range [0,255]. The channels are available as the r, g and b attributes of the returned object.

# d3.rgb(color)

Constructs a new RGB color by parsing the specified color string. If color is not a string, it is coerced to a string; thus, this constructor can also be used to create a copy of an existing color, or force the conversion of a d3.hsl color to RGB. The color string may be in a variety of formats:

  • rgb decimal - "rgb(255,255,255)"
  • hsl decimal - "hsl(120,50%,20%)"
  • rgb hexadecimal - "#ffeeaa"
  • rgb shorthand hexadecimal - "#fea"
  • named - "red", "white", "blue"

The resulting color is stored as red, green and blue integer channel values in the range [0,255]. The channels are available as the r, g and b attributes of the returned object. The list of supported named colors is specified by CSS. If the color is specified in HSL space, it is converted to RGB in a manner equivalent to hsl.rgb.

# rgb.brighter([k])

Returns a brighter copy of this color. Each channel is multiplied by 0.7 ^ -k. If the gamma value k is omitted, it defaults to 1. Channel values are capped at the maximum value of 255, and the minimum value of 30.

# rgb.darker([k])

Returns a darker copy of this color. Each channel is multiplied by 0.7 ^ k. If the gamma value k is omitted, it defaults to 1.

# rgb.hsl()

Returns the equivalent color in HSL space; see d3.hsl for details on the returned object. The conversion from HSL to RGB is described in CSS3 Color Module Level 3; this is the equivalent reverse operation.

# rgb.toString()

Converts this RGB color to a hexadecimal string, such as "#f7eaba".

HSL

# d3.hsl(h, s, l)

Constructs a new HSL color with the specified hue h, saturation s and lightness l. The hue must be a number in the range [0,360]. The saturation and lightness must be in the range [0,1] (not percentages). These values are available as the h, s and l attributes of the returned object.

# d3.hsl(color)

Constructs a new HSL color by parsing the specified color string. If color is not a string, it is coerced to a string; thus, this constructor can also be used to create a copy of an existing color, or force the conversion of a d3.rgb color to HSL. The color string may be in a variety of formats:

  • rgb decimal - "rgb(255,255,255)"
  • hsl decimal - "hsl(120,50%,20%)"
  • rgb hexadecimal - "#ffeeaa"
  • rgb shorthand hexadecimal - "#fea"
  • named - "red", "white", "blue"

The resulting color is stored as hue in the range [0,360], and saturation and lightness values in the range [0,1]. These values are available as the h, s and l attributes of the returned object. The list of supported named colors is specified by CSS. If the color is specified in RGB space, it is converted to HSL in a manner equivalent to rgb.hsl.

# hsl.brighter([k])

Returns a brighter copy of this color. The lightness channel is multiplied by 0.7 ^ -k. If the gamma value k is omitted, it defaults to 1.

# hsl.darker([k])

Returns a darker copy of this color. The lightness channel is multiplied by 0.7 ^ k. If the gamma value k is omitted, it defaults to 1.

# hsl.rgb()

Returns the equivalent color in RGB space; see d3.rgb for details on the returned object. The conversion from HSL to RGB is described in CSS3 Color Module Level 3.

# hsl.toString()

Converts this HSL color to an RGB hexadecimal string, such as "#f7eaba".

HCL

# d3.hcl(h, c, l)

# d3.hcl(color)

# hcl.brighter([k])

# hcl.darker([k])

# hcl.rgb()

# hcl.toString()

Converts this HCL color to an RGB hexadecimal string, such as "#f7eaba".

L*a*b*

# d3.lab(l, a, b)

# d3.lab(color)

# lab.brighter([k])

# lab.darker([k])

# lab.rgb()

# lab.toString()

Converts this L*a*b* color to an RGB hexadecimal string, such as "#f7eaba".

Color

A d3.color base type is provided if you want to extend D3 with additional color spaces. This type enables automatic RGB interpolation by d3.interpolate (detected via instanceof d3.color).

# d3.color()

The base constructor for color types.

# color.rgb()

Returns the RGB equivalent of this color. Must be implemented by all color spaces.

# color.toString()

Converts an RGB hexadecimal string representing this color, such as "#f7eaba".

⚠️ **GitHub.com Fallback** ⚠️