d3 / d3-color

Color spaces! RGB, HSL, Cubehelix, CIELAB, and more.
https://d3js.org/d3-color
ISC License
398 stars 91 forks source link

Allow printing formatted hsl string for css #55

Closed Kushagra8888 closed 4 years ago

Kushagra8888 commented 5 years ago

It would be nice to have a method to convert an hsl color into its css representation. A similar API is available in chroma.js

chroma.hsl(216, 1, 0.2).css('hsl')
"hsl(215.88,100%,20%)"
mbostock commented 4 years ago

I’m unsure how to expose this API.

We don’t want to change hsl.toString to return an HSL string because we want colors to return consistent formats (consistent with CSS’s computed style value) for interpolation.

We could add an hsl.hsl method that returns an HSL-format string, but this is confusing as color.rgb returns a d3.rgb instance, not an RGB-format string.

We could add an hsl.toHSLString method, which has some precedent (e.g., date.toISOString). But that suggests color.hex should have been named color.toHexString. And that’s a bit tedious.

We could have color.toString take an optional argument, similar to number.toString’s optional radix argument. Then you could say color.toString("hex") or color.toString("rgb") or color.toString("hsl").

Or we could co-opt chroma.js’s API and use color.css.