d3-scale
d3-scale copied to clipboard
Add invert to interpolated scales.
This PR adds an invert method (and corresponding docs & test cases) to scaleSequential... and scaleDiverging.... Rather than invert the interpolator itself (which might not be bijective), these methods expect an interpolation fraction as input and return the corresponding domain value. (Thus they only "truly" invert the scale when using the default interpolated range [0, 1].)
The motivating use case for this PR is to perform interactive filtering in response to brushes over a color gradient legend. It is relatively simple to convert pixel brush coordinates into fractional domain values, which can then be inverted. While similar functionality could be achieved by creating an additional continuous scale with a numeric pixel range, these new methods enable direct usage of the existing scale, providing a more efficient and convenient mechanism within systems such as Vega / Vega-Lite.
I realize the name "invert" might be controversial here, but I kept it for parity with other scale types (and because inversion of arbitrary interpolators is not possible). If this PR is of interest, I'd be happy to consider an alternative method name. If this PR is not of interest, it would be great to know so we can make alternate arrangements. Thanks!