d3-color
Even though your browser understands a lot about colors, it doesn’t offer much help in manipulating colors through JavaScript. The d3-color module therefore provides representations for various color spaces, allowing specification, conversion and manipulation. (Also see d3-interpolate for color interpolation.)
For example, take the color named “steelblue”:
var c = d3.color("steelblue");
Let’s try converting it to HSL:
var c = d3.hsl("steelblue");
Now rotate the hue by 90°, bump up the saturation, and format as hex:
c.h += 90;
c.s += 0.2;
c + "";
In addition to the ubiquitous and machine-friendly RGB and HSL color space, d3-color supports two color spaces that are designed for humans:
Cubehelix features monotonic lightness, while Lab and HCL are perceptually uniform. Note that HCL is the cylindrical form of Lab, similar to how HSL is the cylindrical form of RGB.
Installing
If you use NPM, npm install d3-color
. Otherwise, download the latest release. The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom build using Rollup or your preferred bundler. You can also load directly from d3js.org:
<script src="https://d3js.org/d3-color.v0.3.min.js"></script>
In a vanilla environment, a d3_color
global is exported. Try d3-color in your browser.
API Reference
# d3.color(specifier)
Parses the specified CSS Color Module Level 3 specifier string, returning an RGB or HSL color. If the specifier was not valid, null is returned. Some examples:
"rgb(255,255,255)"
"hsl(120,50%,20%)"
"#ffeeaa"
"#fea"
"steelblue"
The list of supported named colors is specified by CSS.
Note: this function may also be used with instanceof
to test if an object is a color instance. The same is true of color subclasses, allowing you to test whether a color is in a particular color space.
# color.rgb()
Returns the RGB equivalent of this color. For RGB colors, that’s this
.
# color.brighter([k])
Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be. If k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.
# color.darker([k])
Returns a darker copy of this color. If k is specified, it controls how much brighter the returned color should be. If k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.
# color.displayable()
Returns true if and only if the color is displayable on standard hardware. For example, this returns false for an RGB color if any channel value is less than zero or greater than 255.
# color.toString()
Returns the RGB hexadecimal string representing this color, such as "#f7eaba"
. If this color is not displayable, a suitable displayable color is returned instead. For example, RGB channel values greater than 255 are clamped to 255.
# d3.rgb(r, g, b)
# d3.rgb(specifier)
# d3.rgb(color)
Constructs a new RGB color. The channel values are exposed as r
, g
and b
properties on the returned instance. Use the RGB color picker to explore this color space.
If r, g and b are specified, these represent the channel values of the returned color. If a CSS Color Module Level 3 specifier string is specified, it is parsed and then converted to the RGB color space. See color for examples. If a color instance is specified, it is converted to the RGB color space using color.rgb. Note that unlike color.rgb this method always returns a new instance, even if color is already an RGB color.
# d3.hsl(h, s, l)
# d3.hsl(specifier)
# d3.hsl(color)
Constructs a new HSL color. The channel values are exposed as h
, s
and l
properties on the returned instance. Use the HSL color picker to explore this color space.
If h, s and l are specified, these represent the channel values of the returned color. If a CSS Color Module Level 3 specifier string is specified, it is parsed and then converted to the HSL color space. See color for examples. If a color instance is specified, it is converted to the RGB color space using color.rgb and then converted to HSL. (Colors already in the HSL color space skip the conversion to RGB.)
# d3.lab(l, a, b)
# d3.lab(specifier)
# d3.lab(color)
Constructs a new Lab color. The channel values are exposed as l
, a
and b
properties on the returned instance. Use the Lab color picker to explore this color space.
If l, a and b are specified, these represent the channel values of the returned color. If a CSS Color Module Level 3 specifier string is specified, it is parsed and then converted to the Lab color space. See color for examples. If a color instance is specified, it is converted to the RGB color space using color.rgb and then converted to Lab. (Colors already in the Lab color space skip the conversion to RGB, and colors in the HCL color space are converted directly to Lab.)
# d3.hcl(h, c, l)
# d3.hcl(specifier)
# d3.hcl(color)
Constructs a new HCL color. The channel values are exposed as h
, c
and l
properties on the returned instance. Use the HCL color picker to explore this color space.
If h, c and l are specified, these represent the channel values of the returned color. If a CSS Color Module Level 3 specifier string is specified, it is parsed and then converted to the HCL color space. See color for examples. If a color instance is specified, it is converted to the RGB color space using color.rgb and then converted to HCL. (Colors already in the HCL color space skip the conversion to RGB, and colors in the Lab color space are converted directly to HCL.)
# d3.cubehelix(h, s, l)
# d3.cubehelix(specifier)
# d3.cubehelix(color)
Constructs a new Cubehelix color. The channel values are exposed as h
, s
and l
properties on the returned instance. Use the Cubehelix color picker to explore this color space.
If h, s and l are specified, these represent the channel values of the returned color. If a CSS Color Module Level 3 specifier string is specified, it is parsed and then converted to the Cubehelix color space. See color for examples. If a color instance is specified, it is converted to the RGB color space using color.rgb and then converted to Cubehelix. (Colors already in the Cubehelix color space skip the conversion to RGB.)