Skip to content
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

Make Color.js proxy-friendly, resolves #305 #306

Merged
merged 3 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export default class Color {
[space, coords, alpha] = args;
}

this.#space = ColorSpace.get(space);
Object.defineProperty(this, "space", {
value: ColorSpace.get(space),
writable: false,
enumerable: true,
configurable: true, // see note in https://262.ecma-international.org/8.0/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
});
this.coords = coords? coords.slice() : [0, 0, 0];
this.alpha = alpha < 1? alpha : 1; // this also deals with NaN etc

Expand All @@ -67,22 +72,16 @@ export default class Color {
}

// Define getters and setters for each coordinate
for (let id in this.#space.coords) {
for (let id in this.space.coords) {
Object.defineProperty(this, id, {
get: () => this.get(id),
set: value => this.set(id, value)
});
}
}

#space;

get space () {
return this.#space;
}

get spaceId () {
return this.#space.id;
return this.space.id;
}

clone () {
Expand Down
138 changes: 73 additions & 65 deletions src/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ export default class ColorSpace {
this.referred = options.referred;

// Compute ancestors and store them, since they will never change
this.#path = this.#getPath().reverse();
Object.defineProperty(this, "path", {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this have some @private jsdoc to let IDEs know they should treat it as private ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn’t really need to be private, as long as it’s immutable.

value: getPath(this).reverse(),
writable: false,
enumerable: true,
configurable: true,
});

hooks.run("colorspace-init-end", this);
}
Expand Down Expand Up @@ -104,58 +109,9 @@ export default class ColorSpace {
return false;
}

#processFormat (format) {
if (format.coords && !format.coordGrammar) {
format.type ||= "function";
format.name ||= "color";

// Format has not been processed
format.coordGrammar = parseCoordGrammar(format.coords);

let coordFormats = Object.entries(this.coords).map(([id, coordMeta], i) => {
// Preferred format for each coord is the first one
let outputType = format.coordGrammar[i][0];

let fromRange = coordMeta.range || coordMeta.refRange;
let toRange = outputType.range, suffix = "";

// Non-strict equals intentional since outputType could be a string object
if (outputType == "<percentage>") {
toRange = [0, 100];
suffix = "%";
}
else if (outputType == "<angle>") {
suffix = "deg";
}

return {fromRange, toRange, suffix};
});

format.serializeCoords = (coords, precision) => {
return coords.map((c, i) => {
let {fromRange, toRange, suffix} = coordFormats[i];

if (fromRange && toRange) {
c = mapRange(fromRange, toRange, c);
}

c = toPrecision(c, precision);

if (suffix) {
c += suffix;
}

return c;
});
};
}

return format;
}

getFormat (format) {
if (typeof format === "object") {
format = this.#processFormat(format);
format = processFormat(format, this);
return format;
}

Expand All @@ -169,23 +125,16 @@ export default class ColorSpace {
}

if (ret) {
ret = this.#processFormat(ret);
ret = processFormat(ret, this);
return ret;
}

return null;
}

#path;

#getPath () {
let ret = [this];

for (let space = this; space = space.base;) {
ret.push(space);
}

return ret;
// We cannot rely on simple === because then ColorSpace objects cannot be proxied
equals (space) {
return this === space || this.id === space.id;
LeaVerou marked this conversation as resolved.
Show resolved Hide resolved
}

to (space, coords) {
Expand All @@ -204,13 +153,13 @@ export default class ColorSpace {
coords = coords.map(c => Number.isNaN(c)? 0 : c);

// Find connection space = lowest common ancestor in the base tree
let myPath = this.#path;
let otherPath = space.#path;
let myPath = this.path;
let otherPath = space.path;

let connectionSpace, connectionSpaceIndex;

for (let i=0; i < myPath.length; i++) {
if (myPath[i] === otherPath[i]) {
if (myPath[i].equals(otherPath[i])) {
connectionSpace = myPath[i];
connectionSpaceIndex = i;
}
Expand Down Expand Up @@ -397,3 +346,62 @@ export default class ColorSpace {
name: "color",
};
}

function getPath (space) {
let ret = [space];

for (let s = space; s = s.base;) {
ret.push(s);
}

return ret;
}

function processFormat (format, {coords} = {}) {
if (format.coords && !format.coordGrammar) {
format.type ||= "function";
format.name ||= "color";

// Format has not been processed
format.coordGrammar = parseCoordGrammar(format.coords);

let coordFormats = Object.entries(coords).map(([id, coordMeta], i) => {
// Preferred format for each coord is the first one
let outputType = format.coordGrammar[i][0];

let fromRange = coordMeta.range || coordMeta.refRange;
let toRange = outputType.range, suffix = "";

// Non-strict equals intentional since outputType could be a string object
if (outputType == "<percentage>") {
toRange = [0, 100];
suffix = "%";
}
else if (outputType == "<angle>") {
suffix = "deg";
}

return {fromRange, toRange, suffix};
});

format.serializeCoords = (coords, precision) => {
return coords.map((c, i) => {
let {fromRange, toRange, suffix} = coordFormats[i];

if (fromRange && toRange) {
c = mapRange(fromRange, toRange, c);
}

c = toPrecision(c, precision);

if (suffix) {
c += suffix;
}

return c;
});
};
}

return format;
}