Skip to content

Commit 4c0f36f

Browse files
authored
Merge pull request #9014 from NalinDalal/main
Fixes #9000
2 parents e1b320b + 1acfea5 commit 4c0f36f

3 files changed

Lines changed: 96 additions & 10 deletions

File tree

‎src/color/p5.Color.js‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class Color {
319319
return this._array;
320320
}
321321

322-
lerp(color, amt, mode){
322+
lerp(color, amt, mode) {
323323
let lerpMode = mode;
324324
if (typeof amt === 'object') {
325325
// Passing in options object
@@ -726,13 +726,7 @@ class Color {
726726
}
727727

728728
_getRGBA(maxes = [1, 1, 1, 1]) {
729-
// Get colorjs maxes
730729
const colorjsMaxes = Color.#colorjsMaxes[RGB];
731-
732-
// Normalize everything to 0,1 or the provided range (map)
733-
let coords = structuredClone(to(this._color, 'srgb').coords);
734-
coords.push(this._color.alpha);
735-
736730
const rangeMaxes = maxes.map(v => {
737731
if (!Array.isArray(v)) {
738732
return [0, v];
@@ -741,7 +735,15 @@ class Color {
741735
}
742736
});
743737

744-
coords = coords.map((coord, i) => {
738+
let coords;
739+
if (this.mode === RGB) {
740+
coords = [...this._color.coords, this._color.alpha];
741+
} else {
742+
coords = structuredClone(to(this._color, 'srgb').coords);
743+
coords.push(this._color.alpha);
744+
}
745+
746+
return coords.map((coord, i) => {
745747
return map(
746748
coord,
747749
colorjsMaxes[i][0],
@@ -750,8 +752,6 @@ class Color {
750752
rangeMaxes[i][1]
751753
);
752754
});
753-
754-
return coords;
755755
}
756756

757757
_getMode() {

‎test/bench/color_getrgba.bench.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { bench, describe } from 'vitest';
2+
import p5 from '../../src/app';
3+
4+
describe('p5.Graphics.set() performance', () => {
5+
const options = { iterations: 3, time: 2000 };
6+
const W = 100;
7+
const H = 100;
8+
const FRAMES = 50;
9+
10+
bench(
11+
'set() hot loop',
12+
async () => {
13+
let myp5;
14+
new p5(function (p) {
15+
p.setup = function () {
16+
myp5 = p;
17+
};
18+
});
19+
await vi.waitFor(() => {
20+
if (myp5 === undefined) throw new Error('not ready');
21+
});
22+
23+
const buf = myp5.createGraphics(W, H);
24+
const col = myp5.color(255, 0, 0);
25+
26+
for (let f = 0; f < FRAMES; f++) {
27+
for (let y = 0; y < H; y++) {
28+
for (let x = 0; x < W; x++) {
29+
buf.set(x, y, col);
30+
}
31+
}
32+
}
33+
34+
myp5.remove();
35+
},
36+
options
37+
);
38+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { bench, describe } from 'vitest';
2+
import p5 from '../../src/app';
3+
4+
// Mirrors the minified build, where FES is excluded from the bundle.
5+
// Set inside the timed callback so the flag does not leak into other
6+
// benchmark files sharing this process.
7+
describe('p5.Graphics.set() performance (FES off)', () => {
8+
const options = { iterations: 10, time: 2000 };
9+
const W = 100;
10+
const H = 100;
11+
const FRAMES = 50;
12+
13+
bench(
14+
'set() hot loop',
15+
async () => {
16+
const previousFES = p5.disableFriendlyErrors;
17+
p5.disableFriendlyErrors = true;
18+
19+
try {
20+
let myp5;
21+
new p5(function (p) {
22+
p.setup = function () {
23+
myp5 = p;
24+
};
25+
});
26+
await vi.waitFor(() => {
27+
if (myp5 === undefined) throw new Error('not ready');
28+
});
29+
30+
const buf = myp5.createGraphics(W, H);
31+
const col = myp5.color(255, 0, 0);
32+
33+
for (let f = 0; f < FRAMES; f++) {
34+
for (let y = 0; y < H; y++) {
35+
for (let x = 0; x < W; x++) {
36+
buf.set(x, y, col);
37+
}
38+
}
39+
}
40+
41+
myp5.remove();
42+
} finally {
43+
p5.disableFriendlyErrors = previousFES;
44+
}
45+
},
46+
options
47+
);
48+
});

0 commit comments

Comments
 (0)