Skip to content

Commit

Permalink
Refactor for precalculated values
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Dec 11, 2024
1 parent 19a0613 commit bd56f18
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions src/color/p5.Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ class Color {

static colorMap = {};
static colorMaxes = {};
static #colorjsMaxes = {};

// Used to add additional color modes to p5.js
// Uses underlying library's definition
static addColorMode(mode, definition, maxes){
ColorSpace.register(definition);
Color.colorMap[mode] = definition.id;
// Get colorjs maxes
Color.#colorjsMaxes[mode] = Object.values(definition.coords).reduce((acc, v) => {
acc.push(v.refRange || v.range);
return acc;
}, []);
Color.#colorjsMaxes[mode].push([0, 1]);

if(maxes){
Color.colorMaxes[mode] = maxes;
}else{
Color.colorMaxes[mode] = Object.values(definition.coords).reduce((acc, v) => {
acc.push(v.refRange?.[1] || v.range[1]);
return acc;
}, []);

Color.colorMaxes[mode].push(1);
Color.colorMaxes[mode] = structuredClone(Color.#colorjsMaxes[mode]);
}
}

Expand All @@ -72,20 +74,15 @@ class Color {
return max;
}
});
const key = Color.colorMap[this.mode];

const colorjsMaxes = Object.values(ColorSpace.registry[key].coords).reduce((acc, v) => {
acc.push(v.refRange || v.range);
return acc;
}, []);
colorjsMaxes.push([0, 1]);
const colorjsMaxes = Color.#colorjsMaxes[this.mode];

return origin.map((channel, i) => {
const newval = map(channel, p5Maxes[i][0], p5Maxes[i][1], colorjsMaxes[i][0], colorjsMaxes[i][1]);
return newval;
});
}

// Convert from color.js color range to p5 color range
#unmapColorRange(origin){
const p5Maxes = Color.colorMaxes[this.mode].map((max) => {
if(!Array.isArray(max)){
Expand All @@ -94,12 +91,7 @@ class Color {
return max;
}
});
const key = Color.colorMap[this.mode];
const colorjsMaxes = Object.values(ColorSpace.registry[key].coords).reduce((acc, v) => {
acc.push(v.refRange || v.range);
return acc;
}, []);
colorjsMaxes.push([0, 1]);
const colorjsMaxes = Color.#colorjsMaxes[this.mode];

return origin.map((channel, i) => {
const newval = map(channel, colorjsMaxes[i][0], colorjsMaxes[i][1], p5Maxes[i][0], p5Maxes[i][1]);
Expand Down Expand Up @@ -169,16 +161,10 @@ class Color {
}

// Get coordinates mapped to current color maxes
get values() {
get _levels() {
return this.#unmapColorRange(this._array);
}

// NOTE: WebGL uses this and assumes RGB [255, 255, 255, 255]
// Consider alternative implementation
get levels() {
return this._array.map(v => v * 255);
}

lerp(color, amt, mode){
// Find the closest common ancestor color space
let spaceIndex = -1;
Expand Down Expand Up @@ -474,12 +460,7 @@ class Color {

_getRGBA(maxes=[1, 1, 1, 1]) {
// Get colorjs maxes
const key = Color.colorMap[this.mode];
const colorjsMaxes = Object.values(ColorSpace.registry[key].coords).reduce((acc, v) => {
acc.push(v.refRange || v.range);
return acc;
}, []);
colorjsMaxes.push([0, 1]);
const colorjsMaxes = Color.#colorjsMaxes[this.mode];

// Normalize everything to 0,1 or the provided range (map)
let coords = structuredClone(toGamut(this._color, 'srgb').coords);
Expand Down Expand Up @@ -550,7 +531,7 @@ class Color {
if(this.mode === HSL){
return this._color.coords[2] / 100 * Color.colorMaxes[this.mode][2];
}else{
// Will do an imprecise conversion to 'HSB', not recommended
// Will do an imprecise conversion to 'HSL', not recommended
return to(this._color, 'hsl').coords[2] / 100 * Color.colorMaxes[this.mode][2];
}
}
Expand Down

0 comments on commit bd56f18

Please sign in to comment.