d3 / d3-scale-chromatic

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

Add discrete color schemes for arbitrary counts #19

Closed qwertystop closed 5 years ago

qwertystop commented 5 years ago

Currently, only a few interpolated color schemes have discrete equivalents, and those that do have a limited number of possible ranges. For example, schemeYlGnBu offers discrete schemes only for lengths 3 through 9, and interpolateCool has no discrete scheme.

I've got a fairly simple function which could resolve this:

  function buildDiscreteFromInterpolator(interpolator, count) {                                                  
    const discrete = []                                                                                          
    for (let i=0; i < count; i++) {                                                                                  
      discrete.push(interpolator(i/count))                                                                       
    }                                                                                                            
    return discrete                                                                                              
  }   

but from a quick look at the source, I'm not sure where to put it for an actual PR. Or, if this function is a poor fit for the job for some reason I am unaware of, possibly suggest an alternative?

mbostock commented 5 years ago

This exists already and it is called d3.quantize.