d3 / d3-scale-chromatic

Sequential, diverging and categorical color scales.
https://d3js.org/d3-scale-chromatic
Other
798 stars 107 forks source link

Hex code to d3 color palette conversion #42

Closed gerstej9 closed 2 years ago

gerstej9 commented 2 years ago

Hi, apologies in advance if this is the wrong place for this question. I'm currently working with a webgl scatterplot that uses d3 color palettes for shading. Each of my data points has an associated color hex code associated with it and I am hoping to find a way to utilize the hex code to find the appropriate value within a d3 color palette such as "rainbow" or "sinebow". Is that something you have attempted before or do you have any suggestions on how one might go about utilizing a hex code to get the correct shading from a d3 color palette?

Fil commented 2 years ago

Here's a quick suggestion to invert a color scale: https://observablehq.com/@recifs/invert-a-color-scale-42 It's "brute force" in the sense that we compute the distance between our color and 256 colors generated by the scale, and return the value which has the closest match (even if it's a very poor match).

gerstej9 commented 2 years ago

Thank you very much! This looks like it could be an effective solution I really appreciate it!!