From 74f7b434865c2d758c7b1cd5f52f4c96b76d03fb Mon Sep 17 00:00:00 2001 From: Simone Date: Sat, 15 Aug 2026 01:32:33 +0200 Subject: [PATCH] fix(web): bound OKLCH gamut mapping (#6485) Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> --- apps/web/src/themePalette.test.ts | 12 ++++++++++++ apps/web/src/themePalette.ts | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/web/src/themePalette.test.ts b/apps/web/src/themePalette.test.ts index 95ce10af9316a49c2dfccdb01d7ea42d07e7a1e0..3c5897c70152e727bcf8780f0ad873531ced9d66 100644 --- a/apps/web/src/themePalette.test.ts +++ b/apps/web/src/themePalette.test.ts @@ -246,6 +246,18 @@ describe("theme files", () => { } }); + it("gamut maps extreme finite OKLCH chroma from theme files", () => { + const theme = parseThemeFile({ + version: THEME_FILE_VERSION, + name: "Extreme chroma", + appearance: "light", + colors: { accent: "oklch(0.5 1e303 0)" }, + }); + + expect(theme.colors.accent).toBe("oklch(0.5 1e+303 0)"); + expect(themeColorToHex(theme.colors.accent)).toBe("#b5005e"); + }); + it("rejects unknown roles and invalid color values", () => { expect(() => parseThemeFile({ diff --git a/apps/web/src/themePalette.ts b/apps/web/src/themePalette.ts index 00b29dce83bafb6b1f651183a6112052a8b25fd2..7bf4f0426bd398ac6eb84d0c3c44943ee79d20d5 100644 --- a/apps/web/src/themePalette.ts +++ b/apps/web/src/themePalette.ts @@ -902,7 +902,11 @@ function mapThemeOklchToSrgbGamut(color: ThemeOklch): ThemeOklch { let low = 0; let high = color.C; - const steps = Math.max(1, Math.ceil(Math.log2(Math.max(color.C, 0.000001) / 0.000001))); + const chromaResolution = 0.000001; + const steps = Math.max( + 1, + Math.ceil(Math.log2(Math.max(color.C, chromaResolution)) - Math.log2(chromaResolution)), + ); for (let step = 0; step < steps; step += 1) { const mid = (low + high) / 2; if (isInGamut(mid)) low = mid; -- 2.54.0