Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatHex8() to include opacity #78

Closed
rben01 opened this issue Jun 30, 2020 · 7 comments · Fixed by #103
Closed

Add formatHex8() to include opacity #78

rben01 opened this issue Jun 30, 2020 · 7 comments · Fixed by #103
Labels
enhancement New feature or request

Comments

@rben01
Copy link

rben01 commented Jun 30, 2020

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

function rgb_formatHex() {

@curran
Copy link
Contributor

curran commented Jun 30, 2020

Nice catch!

@danburzo
Copy link
Contributor

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
Copy link
Member

Fil commented Jul 4, 2020

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

@jasonwilliams
Copy link

shameless bump

@danburzo
Copy link
Contributor

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
Copy link

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?

@Fil Fil added the enhancement New feature or request label Jun 30, 2021
@Fil Fil changed the title Is formatHex() supposed to ignore opacity? Add formatHex8() to include opacity Jul 1, 2021
@mootari
Copy link

mootari commented Feb 10, 2022

  • What happens if opacity is outside [0..1]? Should the hex value be clamped to [0..255]?
  • How should the float value be coerced? Via round() or floor()?

Edit: The answers are "yes" and "round".

d3-color/src/color.js

Lines 279 to 282 in 7d61bbe

function hex(value) {
value = Math.max(0, Math.min(255, Math.round(value) || 0));
return (value < 16 ? "0" : "") + value.toString(16);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

6 participants