d3 / d3-color

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

Add formatHex8() to include opacity #78

Closed rben01 closed 2 years ago

rben01 commented 4 years ago

To reproduce:

console.log(d3.color("#fff1").formatRgb());  // rgba(255, 255, 255, 0.06666666666666667)
console.log(d3.color("#fff1").formatHex());  // #ffffff <-- shouldn't this be #ffffff11?

I'd expect that formatHex would preserve opacity just as formatRgb does; however, opacity is clearly ignored in the function definition https://github.com/d3/d3-color/blob/e87dc62c4bd4641534e81d8b0a42e472ab88156d/src/color.js#L266

curran commented 4 years ago

Nice catch!

danburzo commented 4 years ago

I believe formatHex() is supposed to be a widely-compatible string representation of the color. 8-char hex notation does not benefit from the same support (e.g. Edge only gained it after switching to Chromium), so maybe a formatHex8() is more suitable?

Fil commented 4 years ago

Agree with @danburzo that we should add formatHex8 and not change formatHex which is documented as "in RGB space".

jasonwilliams commented 3 years ago

shameless bump

danburzo commented 3 years ago

Another usecase for formatHex() to maintain its current implementation is that the HTML <input type='color'> only accepts 6-digit hex as a color format.

jasonwilliams commented 3 years ago

Another usecase for formatHex() to maintain its current implementation is that the HTML <input type='color'> only accepts 6-digit hex as a color format.

That’s fine, but an API that can do alpha would be useful, formatHex8 or something. I’m not too bothered if it can’t be this API as long as it’s supported somewhere.

maybe this issue should be changed to a feature request for that instead?

mootari commented 2 years ago

Edit: The answers are "yes" and "round". https://github.com/d3/d3-color/blob/7d61bbe6e426a7f3d3f4520a8b31cfc92dc69ee7/src/color.js#L279-L282