Skip to content
10 changes: 10 additions & 0 deletions apps/gamut-mapping/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const methods = {
label: "Scale LH",
description: "Runs Scale, sets L, H to those of the original color, then runs Scale again.",
compute: (color) => {
if (color.inGamut("p3", { epsilon: 0 })) {
return color.to("p3");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not simply return color? There is no reason to convert it to anything, and any conversion is potentially a lossy operation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did this to match what was happening in the scale method, which returns the color converted to p3. This way, the method returns in a consistent space regardless of whether it starts in gamut or not. Otherwise, the serialized display with switch back and forth between p3 and oklch depending on whether it was in gamut.

(Perhaps the results from all methods should be serialized in a single format- currently some are oklch, and some are p3, making them harder to compare, but that's somewhat orthogonal to the question of whether the Scale LH method should return in a consistent space).

}
let [lightness] = color.to("oklch").coords;
if (lightness >= 1) {
return new Color({ space: "xyz-d65", coords: WHITES["D65"] }).to("p3");
}
else if (lightness <= 0) {
return new Color({ space: "xyz-d65", coords: [0, 0, 0] }).to("p3");
}
let mappedColor = methods.scale.compute(color);
let lch = color.to("oklch").coords;
mappedColor.set({
Expand Down