-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Fixes #9000 #9014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fixes #9000 #9014
Changes from 2 commits
88e1550
9c6416e
76da9c5
7f4b60a
f60fccb
2a15b2a
c15b88a
e44102c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,11 @@ class Color { | |
| // where if we `import { Color }` directly, it will be a separate copy of the | ||
| // Color class from the one imported in the main p5.js bundle. | ||
| isColor = true; | ||
| #rgbaCache = new Map(); | ||
|
|
||
| #invalidateRGBACache() { | ||
| this.#rgbaCache.clear(); | ||
| } | ||
|
|
||
| // Used to add additional color modes to p5.js | ||
| // Uses underlying library's definition | ||
|
|
@@ -241,6 +246,7 @@ class Color { | |
| this._initialize(); | ||
| this._initialize = undefined; | ||
| } | ||
| this.#invalidateRGBACache(); | ||
| this._cachedColor = newColor; | ||
| } | ||
|
|
||
|
|
@@ -559,6 +565,7 @@ class Color { | |
|
|
||
| if(this.mode === RGB || this.mode === RGBP3){ | ||
| this._color.coords[0] = newval; | ||
| this.#invalidateRGBACache(); | ||
| }else{ | ||
| // Will do an imprecise conversion to 'srgb', not recommended | ||
| const space = this._color.space.id; | ||
|
|
@@ -611,6 +618,7 @@ class Color { | |
|
|
||
| if(this.mode === RGB || this.mode === RGBP3){ | ||
| this._color.coords[1] = newval; | ||
| this.#invalidateRGBACache(); | ||
| }else{ | ||
| // Will do an imprecise conversion to 'srgb', not recommended | ||
| const space = this._color.space.id; | ||
|
|
@@ -663,6 +671,7 @@ class Color { | |
|
|
||
| if(this.mode === RGB || this.mode === RGBP3){ | ||
| this._color.coords[2] = newval; | ||
| this.#invalidateRGBACache(); | ||
| }else{ | ||
| // Will do an imprecise conversion to 'srgb', not recommended | ||
| const space = this._color.space.id; | ||
|
|
@@ -715,33 +724,45 @@ class Color { | |
| const newval = map(new_alpha, max[0], max[1], colorjsMax[0], colorjsMax[1]); | ||
|
|
||
| this._color.alpha = newval; | ||
| this.#invalidateRGBACache(); | ||
| } | ||
|
|
||
| _getRGBA(maxes=[1, 1, 1, 1]) { | ||
| const cacheKey = maxes.join(','); | ||
| if (this.#rgbaCache.has(cacheKey)) { | ||
| return [...this.#rgbaCache.get(cacheKey)]; | ||
| } | ||
|
|
||
| // 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) => { | ||
| const rangeMaxes = maxes.map(v => { | ||
| if(!Array.isArray(v)){ | ||
| return [0, v]; | ||
| }else{ | ||
| return v; | ||
| } | ||
| }); | ||
|
|
||
| coords = coords.map((coord, i) => { | ||
| let coords; | ||
| if (this.mode === RGB) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| coords = [...this._color.coords, this._color.alpha]; | ||
| } else { | ||
| // Normalize everything to 0,1 or the provided range (map) | ||
| coords = structuredClone(to(this._color, 'srgb').coords); | ||
| coords.push(this._color.alpha); | ||
| } | ||
|
|
||
| const result = coords.map((coord, i) => { | ||
| return map( | ||
| coord, | ||
| colorjsMaxes[i][0], colorjsMaxes[i][1], | ||
| rangeMaxes[i][0], rangeMaxes[i][1] | ||
| ); | ||
| }); | ||
|
|
||
| return coords; | ||
| this.#rgbaCache.set(cacheKey, result); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this Map(). The only heavy step is the 'srgb' conversion you did above, so we can just cache that part, less complexity and cleaner code. |
||
| return result; | ||
| } | ||
|
|
||
| _getMode() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxes.join(',')allocates a new string on every call inside loops. Using a fast path key check for[255, 255, 255, 255]avoids string allocations during pixel loops.