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 a convenience method for hex color strings? #38

Closed
songololo opened this issue Jan 11, 2018 · 2 comments
Closed

Add a convenience method for hex color strings? #38

songololo opened this issue Jan 11, 2018 · 2 comments

Comments

@songololo
Copy link

Any possibility / support for adding a convenience method to return the colour in hex format?

@mbostock
Copy link
Member

No immediate plans to support this, but here’s one implementation:

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

d3.rgb.prototype.toHexString = function() {
  return "#" + hex(this.r) + hex(this.g) + hex(this.b);
};

@songololo
Copy link
Author

songololo commented Jan 12, 2018

Thanks, extending the prototype works fine for my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants