Skip to content

Commit

Permalink
Merge pull request #51 from danbraunai/token-background-fix
Browse files Browse the repository at this point in the history
Fix white text on white background bug
  • Loading branch information
danbraunai authored Feb 8, 2023
2 parents bb760a9 + 494d81d commit db9e65e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions react/src/utils/getTokenBackgroundColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export function getTokenBackgroundColor(
negativeColor: AnyColor = "red",
positiveColor: AnyColor = "blue"
): Colord {
// 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.
if (value > 0) {
// 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.
if (value >= 0) {
return colord(positiveColor).mix(
colord("white"),
Math.min(Math.max(1 - value / max, 0), 1)
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 @@ -22,4 +22,10 @@ describe("getBackgroundColor", () => {
const hsl = res.toHsl();
expect(hsl.l).toBeCloseTo(100);
});

it("Check that 0 returns a brightness of 1", () => {
// If the brightness is <0.6, the text will be white and invisible
const res = getTokenBackgroundColor(0, 0, 1);
expect(res.brightness()).toBeCloseTo(1);
});
});

0 comments on commit db9e65e

Please sign in to comment.