p5.colorGenerator doesn't work on newer versions of p5?

I’ve been having trouble getting the p5.ColorGenerator library to work with newer versions of p5. Here is the code that actually does something:

There is more HTML than what is below, everything else is just boilerplate.

let colorGen;

function setup() {
createCanvas(600, 200);
background(100);
colorGen = new ColorGenerator("#ffba06");

colorGen.getShades(5).forEach((shade, index) => {
fill(shade);
rect(index * 100, 0, 100, 100);
});
}

This is what the repo recommends same exact code down to the version number in the CDN link. This does work, but I’d like to use a newer version of p5 (preferably the newest), and even though the repo says “It works with p5.js 1.4 and later”, it doesn’t work. I think something must have broke with the introduction of p5 v2? That is the only thing I can think of as of now. Does anyone know of a workaround that I can try? Or, if someone has a better library that they use to generate color pallets, that would be good too.

Another related issue I’m having is when I try to use this library as a module. Here is the code:

HTML without boilerplate:

JS:

import ColorGenerator from "https://cdn.jsdelivr.net/gh/alexandru-postolache/p5.colorGenerator@2.0.0/p5.colorGenerator.mjs";

All I’m doing is importing the module, and I get this error:

Uncaught SyntaxError: The requested module './p5.colorGenerator.js' does not provide an export named 'default'

Even weirder, I get the same exact error when I use the named import syntax:

import { ColorGenerator } ... 

It still tells me that The requested module ‘./p5.colorGenerator.js’ does not provide an export named ‘default’, even though I’m using the named import syntax.

I also posted something similar on the repo as an issue.

Thanks!

Follow up:

I’m 99% sure that it has something to do with changes that were implemented in p5 v2. I’ve tried the different combinations below:

p5 v2.4.0 (latest release) - doesn’t work

p5 v2.0.0 - doesn’t work

p5 v1.11.11 (one of the last 1.x releases) - does work

p5 v1.9.4 (the one in the libraries documentation) - does work

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/2.4.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/2.0.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.11/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/p5.js"></script>

<script src="https://cdn.jsdelivr.net/gh/alexandru-postolache/p5.colorGenerator@2.0.0/p5.colorGenerator.js"></script>

I am testing this by leaving one of the p5 CDN links uncommented, and running it with the following js code:

let colorGen;
function setup() {
createCanvas(600, 200);
background(100);
circle(300, 100, 50);

colorGen = new ColorGenerator("#ffba06");
colorGen.getShades(5).forEach((shade, index) => {
fill(shade);
rect(index * 100, 0, 100, 100);
});
}

When I switch to a 2.x release of p5, I get this error message:

p5.colorGenerator.js:191 Uncaught (in promise) Error: ColorGenerator requires p5. In instance mode, pass the sketch: new ColorGenerator(color, p).

I get the same pattern as above when I use this code (instance mode):

let colorGen;
const sketch = new p5(function (p) {
p.setup = function () {

p.createCanvas(600, 200);
p.background(100);
p.circle(p.width / 2, p.height / 2, 50);

colorGen = new ColorGenerator("#ffba06", p);
colorGen.getShades(5).forEach((shade, index) => {
  p.fill(shade);
  p.rect(index * 100, 0, 100, 100);
});
};
});

I get the same error as before when I use the code right above this.

The function below in p5.colorGenerator.js seems to be the issue. But I do not yet have the expertise to solve this problem.

function resolveP5(instance) {
if (isP5Instance(instance)) return instance;
var root = typeof globalThis !== "undefined" ? globalThis : null;
if (root && root.p5 && isP5Instance(root.p5.instance)) return root.p5.instance;
if (isP5Instance(root)) return root;
throw new Error("ColorGenerator requires p5. In instance mode, pass the sketch: new ColorGenerator(color, p).");
}

Found out the culprit:

Function isP5Instance() expects to find protected property _colorMaxes.

However, p5js v2 seems to be using _renderer.states.colorMaxes instead.

So I believe you’re stuck w/ p5js v1:
https://Unpkg.com/p5@1
https://cdn.JSDelivr.net/npm/p5@1

Thanks for the information! So if I change all _colorMaxes to _renderer.states.colorMaxes, it would work? I’m assuming it would be much more complicated than that, but I thought I’d ask anyway. Based on what you know, do you think that the whole p5.colorGenerator library needs an overhaul?

I did find this article. Would going through this and making the necessary changes be enough? Also, do you know of any other libraries that do this? I know of p5.pallet, but p5.colorGenerator is much more comprehensive. Also, I’m pretty sure p5.pallet has the same version issues as p5.colorGenerator as seen in this issue.

I wouldn’t mind trying to update this library for v2 since I think it would be super useful. Although, I will say, it is one of the less popular libraries (just based on number of stars on Github and how much I’ve heard about it). Do you have any insight into how designers choose color pallets for their art? Do they just do it by hand, or am I missing something?