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
8 changes: 8 additions & 0 deletions react/src/utils/getTokenBackgroundColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export function getTokenBackgroundColor(
negativeColor: AnyColor = "red",
positiveColor: AnyColor = "blue"
): Colord {
// Zero is the neutral point and must stay white even when an observed
// range has no positive values. Dividing 0 by a zero maximum produces NaN,
// which colord represents as transparent black and makes token text
// unreadable on a white page.
if (value === 0) {
return colord("white");
}

// original_color.mix("white", x) interpolates between original_color and
// white, with x being the ratio of white. So x=0 is original_color, x=1 is
// white. Clamp at 0 to avoid negative values.
Expand Down
6 changes: 6 additions & 0 deletions react/src/utils/tests/getTokenBackgroundColor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ describe("getBackgroundColor", () => {
const res = getTokenBackgroundColor(0, 0, 1);
expect(res.brightness()).toBeCloseTo(1);
});

it("keeps zero white when the observed maximum is zero", () => {
const res = getTokenBackgroundColor(0, 0, 0);
expect(res.toRgbString()).toBe("rgb(255, 255, 255)");
expect(res.brightness()).toBeCloseTo(1);
});
});
Loading