From 2bcb4aa052c7affce08ce6e8175a36fd2479c635 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Fri, 15 Dec 2023 16:19:19 -0700 Subject: [PATCH] Ensure clipped is always returned and initialized --- src/toGamut.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/toGamut.js b/src/toGamut.js index c750a1f16..40012fc00 100644 --- a/src/toGamut.js +++ b/src/toGamut.js @@ -155,7 +155,7 @@ export function toGamutCSS (origin, { space = origin.space }) { return to(black, space); } - if (inGamut(origin_OKLCH, space)) { + if (inGamut(origin_OKLCH, space, { epsilon: 0 })) { return to(origin_OKLCH, space); } @@ -180,23 +180,23 @@ export function toGamutCSS (origin, { space = origin.space }) { let max = origin_OKLCH.coords[1]; let min_inGamut = true; + let clipped = clip(clone(origin_OKLCH)); let current; while ((max - min) > ε) { const chroma = (min + max) / 2; current = clone(origin_OKLCH); current.coords[1] = chroma; - if (min_inGamut && inGamut(current, space)) { + if (min_inGamut && inGamut(current, space, { epsilon: 0 })) { min = chroma; continue; } - else if (!inGamut(current, space)) { - const clipped = clip(current); + else { + clipped = clip(current); const E = deltaEOK(clipped, current); if (E < JND) { if ((JND - E < ε)) { // match found - current = clipped; break; } else { @@ -206,9 +206,8 @@ export function toGamutCSS (origin, { space = origin.space }) { } else { max = chroma; - continue; } } } - return to(current, space); + return to(clipped, space); }