Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions packages/ui/src/theme/color.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, test } from "bun:test"
import { fitOklch, oklchToRgb } from "./color"

describe("fitOklch", () => {
test("keeps an in-gamut color unchanged", () => {
const color = { l: 0.5, c: 0, h: 0 }

expect(fitOklch(color)).toEqual(color)
})

test("reduces an out-of-gamut color until its RGB values are in gamut", () => {
const result = fitOklch({ l: 0.5, c: 0.5, h: 0 })
const rgb = oklchToRgb(result)

expect(result.c).toBeLessThan(0.5)
expect(Math.min(rgb.r, rgb.g, rgb.b)).toBeGreaterThanOrEqual(0)
expect(Math.max(rgb.r, rgb.g, rgb.b)).toBeLessThanOrEqual(1)
})
})
6 changes: 5 additions & 1 deletion packages/ui/src/theme/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export function hexToOklch(hex: HexColor): OklchColor {
return rgbToOklch(r, g, b)
}

function isRgbInGamut(rgb: { r: number; g: number; b: number }) {
return Math.min(rgb.r, rgb.g, rgb.b) >= 0 && Math.max(rgb.r, rgb.g, rgb.b) <= 1
}

export function fitOklch(oklch: OklchColor): OklchColor {
const base = {
l: clamp(oklch.l, 0, 1),
Expand All @@ -117,7 +121,7 @@ export function fitOklch(oklch: OklchColor): OklchColor {
c *= 0.9
const next = { ...base, c }
const out = oklchToRgb(next)
if (out.r >= 0 && out.r <= 1 && out.g >= 0 && out.g <= 1 && out.b >= 0 && out.b <= 1) {
if (isRgbInGamut(out)) {
return next
}
}
Expand Down
Loading