Skip to content
20 changes: 10 additions & 10 deletions src/color/p5.Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class Color {
return this._array;
}

lerp(color, amt, mode){
lerp(color, amt, mode) {
let lerpMode = mode;
if (typeof amt === 'object') {
// Passing in options object
Expand Down Expand Up @@ -726,13 +726,7 @@ class Color {
}

_getRGBA(maxes = [1, 1, 1, 1]) {
// Get colorjs maxes
const colorjsMaxes = Color.#colorjsMaxes[RGB];

// Normalize everything to 0,1 or the provided range (map)
let coords = structuredClone(to(this._color, 'srgb').coords);
coords.push(this._color.alpha);

const rangeMaxes = maxes.map(v => {
if (!Array.isArray(v)) {
return [0, v];
Expand All @@ -741,7 +735,15 @@ class Color {
}
});

coords = coords.map((coord, i) => {
let coords;
if (this.mode === RGB) {
coords = [...this._color.coords, this._color.alpha];
} else {
coords = structuredClone(to(this._color, 'srgb').coords);
coords.push(this._color.alpha);
}

return coords.map((coord, i) => {
return map(
coord,
colorjsMaxes[i][0],
Expand All @@ -750,8 +752,6 @@ class Color {
rangeMaxes[i][1]
);
});

return coords;
}

_getMode() {
Expand Down
38 changes: 38 additions & 0 deletions test/bench/color_getrgba.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { bench, describe } from 'vitest';
import p5 from '../../src/app';

describe('p5.Graphics.set() performance', () => {
const options = { iterations: 3, time: 2000 };
const W = 100;
const H = 100;
const FRAMES = 50;

bench(
'set() hot loop',
async () => {
let myp5;
new p5(function (p) {
p.setup = function () {
myp5 = p;
};
});
await vi.waitFor(() => {
if (myp5 === undefined) throw new Error('not ready');
});

const buf = myp5.createGraphics(W, H);
const col = myp5.color(255, 0, 0);

for (let f = 0; f < FRAMES; f++) {
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
buf.set(x, y, col);
}
}
}

myp5.remove();
},
options
);
});