achromatic interpolation in `d3@7`?

What is the recommended way to fix achromatic interpolation with the current version of d3?

The approach described here in the d3 docs no longer works with the current version of d3 (the version available by implicit import in Observable):

https://observablehq.com/@d3/achromatic-interpolation

You can confirm this for yourself by looking at this notebook:

https://observablehq.com/d/d94fe55f41a9f5df

If you uncomment the explicit d3@6 import, you can confirm that this used to work in d3@6 but doesn’t anymore in current d3@7.

This is a bug with how jsDelivr bundles D3.

jsDelivr captures the version of a dependency when a module is bundled. This means that you can end up with multiple versions of a dependency. When this happens, the module might not behave correctly (for example if it uses instanceof).

Here d3-interpolate 3.0.1 is loading d3-color 3.0.1, but d3 7.9.0 is loading the more recent d3-color 3.1.0. And hence the d3.hcl instance you pass into d3.interpolateHcl is not detected correctly, and hence is converted to sRGB, such that the chroma (c) channel becomes NaN instead of 0.

You can fix this by loading D3 from esm.sh instead of jsDelivr:

import * as d3 from "https://esm.sh/d3";

This problem is not unique to D3; it affects many libraries, including React. I didn’t realize until now that this was already affecting D3, so thank you for the report. I’ve done more analysis in this notebook:

https://observablehq.com/@observablehq/tracing-imports-on-js-delivr

I have an open PR to switch Observable to use esm.sh to fix this behavior:

Wow. Thank you for looking at this, taking the time to explain what is going on, and sharing the code that you used! I vaguely knew that I could end up with conflicting versions of a package via external dependencies (i.e. a imports d3@7.9.0 while b imports d3@7.8.0), but it had not occurred to me that this might occur via a package’s own internal imports. That’s a subtle and nasty bug (in jsDelivr!).