Skip to content

Commit

Permalink
Return clipped value if it is within JND
Browse files Browse the repository at this point in the history
  • Loading branch information
jgerigmeyer committed Nov 17, 2023
1 parent b22d42b commit 4bf4853
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/toGamut.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,30 +179,34 @@ export function toGamutCSS (origin, { space = origin.space }) {
}
let min = 0;
let max = origin_OKLCH.coords[1];

let min_inGamut = true;
let current;
let current = clone(origin_OKLCH);
let clipped = clip(current);

let E = deltaEOK(clipped, current);
if (E < JND) {
return clipped;
}

while ((max - min) > ε) {
const chroma = (min + max) / 2;
current = clone(origin_OKLCH);
current.coords[1] = chroma;
if (min_inGamut && inGamut(current, space)) {
min = chroma;
}
else if (!inGamut(current, space)) {
const clipped = clip(current);
const E = deltaEOK(clipped, current);
clipped = clip(current);
E = deltaEOK(clipped, current);
if (E < JND) {
// Note- this implementation does not conform to the CSS Spec.
// This removes the check that (JND - E < ε) and its else
// statement. For some colors, JND - E was never less than ε, so
// the algorithm continued projecting chroma higher, until the
// while statement ended. At that point, the chroma was returned
// to `max`, which is the original chroma, so the color was
// still out of gamut.
current = clipped;
break;
if ((JND - E < ε)) {
// match found
current = clipped;
break;
}
else {
min_inGamut = false;
min = chroma;
}
}
else {
max = chroma;
Expand Down

0 comments on commit 4bf4853

Please sign in to comment.