Skip to content

[p5.js 2.0+ Bug Report]: Inconsistent behavior with lerpColor and colorMode #9173

Description

@ChristopherPAndrews

Most appropriate sub-area of p5.js?

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

p5.js version

2.3.3

Web browser and version

Arc 1.164

Operating system

MacOSX 15.7.7

Steps to reproduce this

Steps:

  1. Create two colors
  2. Set the color mode the RGB and use lerpColor to interpolate between them
  3. Change the color mode to HSB and use lerpColor again

Snippet:

const divisions = 10;
let startColor;
let endColor;

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

    startColor = color("red");
    endColor = color("green");

    noStroke();

}

function draw() {
    background(255);
    colorMode(RGB);
    const sliceWidth = width / divisions;
    for (let i = 0; i < divisions; i++) {
        const c = lerpColor(startColor, endColor, i / divisions);
        fill(c);
        rect(i * sliceWidth, 10, sliceWidth, 100);

    }

    colorMode(HSB);
    for (let i = 0; i < divisions; i++) {
        const c = lerpColor(startColor, endColor, i / divisions);
        fill(c);
        rect(i * sliceWidth, 120, sliceWidth, 100);

    }
}

I would expect two different bands as the interpolation happens in either RGB or HSB space. This was the case in v1 and is what is stated in the documentation. However, both are interpolated in RGB space.

If I switch the color creation to happen in HSB space

colorMode(HSB);
startColor = color("red");
endColor = color("green");

the bands stay the same. It appears that despite being in HSB mode, this still creates RGB colors.

If I specify them fully

colorMode(HSB, 360, 100, 100);
    startColor = color(0, 100, 100);
    endColor = color(100, 100, 100);

then both bands interpolate in HSB. If I only switch one of the colors, then the bands stay in RGB mode.

I see #9024 which claims to have fixed this, but I still see the problem.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions