diff --git a/src/color/p5.Color.js b/src/color/p5.Color.js index 2fedb173fb..af47ce43b4 100644 --- a/src/color/p5.Color.js +++ b/src/color/p5.Color.js @@ -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 @@ -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]; @@ -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], @@ -750,8 +752,6 @@ class Color { rangeMaxes[i][1] ); }); - - return coords; } _getMode() { diff --git a/test/bench/color_getrgba.bench.js b/test/bench/color_getrgba.bench.js new file mode 100644 index 0000000000..e31a7ece7e --- /dev/null +++ b/test/bench/color_getrgba.bench.js @@ -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 + ); +});