p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

[p5.js 2.0+ Bug Report]: paletteLerp() returns undefined outside p5.strands

Open bhabishnu opened this issue 1 month ago • 3 comments

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [x] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] WebGPU
  • [x] p5.strands
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [ ] Other (specify if possible)

p5.js version

2.4.0-rc.1

Web browser and version

Chromium

Operating system

Fedora Linux

Steps to reproduce this

A regular paletteLerp() call returns undefined on current main and 2.4.0-rc.1.

For example:

function setup() {
  createCanvas(100, 100);

  const c = paletteLerp(
    [
      [color(255, 0, 0), 0],
      [color(0, 0, 255), 1]
    ],
    0.5
  );

  console.log(c);
  background(c);
}

Expected: c is the interpolated color.

Actual: c is undefined, so the sketch renders black.

This works as expected in 2.3.4 and 1.11.13.

The regression appears to come from the p5.strands override of paletteLerp(): when no Strands context is active, it returns without falling back to the original CPU implementation.

lerpColor() has the expected fallback behavior, and paletteLerp() still works correctly inside Strands on both WebGL and WebGPU.

I couldn't find an existing issue or PR covering this regression.

bhabishnu avatar Oct 25 '26 18:10 bhabishnu

Thanks for the report, @bhabishnu, I can confirm this on current main.

The override I added in #8817 (src/strands/strands_api.js, for #8751) starts with if (!strandsContext.active) return;, so outside Strands it never reaches the CPU implementation in src/color/creating_reading.js. Unlike lerpColor, red, green, blue and random, it also never saves the original function. The existing tests only call paletteLerp inside Strands, so nothing caught it.

I'd like to fix this since it came from my change. The plan is to save const originalPaletteLerp = fn.paletteLerp; before the augmentFn call, delegate to it when Strands is inactive, and add a unit test for the non-Strands path. @davepagurek, is it okay if I take this?

LalitNarayanYadav avatar Oct 26 '26 22:10 LalitNarayanYadav

Thanks, go ahead!

Thanks for testing and catching this @bhabishnu!

davepagurek avatar Oct 26 '26 23:10 davepagurek

Sounds good, thanks! Happy to help test the PR once it’s up.

bhabishnu avatar Oct 27 '26 03:10 bhabishnu