add readableColor

This commit is contained in:
Timi
2026-04-12 10:40:35 +08:00
parent 1adf3eeb6d
commit 28f0eabff2
2 changed files with 52 additions and 0 deletions

View File

@@ -82,4 +82,31 @@ describe("Text", () => {
expect(Text.display(undefined)).toBe("");
});
});
describe("readableColor", () => {
it("should return #FFF for dark backgrounds", () => {
expect(Text.readableColor("#000")).toBe("#FFF");
expect(Text.readableColor("#123456")).toBe("#FFF");
});
it("should return #111 for light backgrounds", () => {
expect(Text.readableColor("#FFF")).toBe("#111");
expect(Text.readableColor("#f5f5f5")).toBe("#111");
});
it("should use threshold at luminance 186", () => {
expect(Text.readableColor("#BABABA")).toBe("#111");
expect(Text.readableColor("#B9B9B9")).toBe("#FFF");
});
it("should trim whitespace and accept mixed case hex", () => {
expect(Text.readableColor(" #AbC ")).toBe("#FFF");
});
it("should return #111 for invalid colors", () => {
expect(Text.readableColor("red")).toBe("#111");
expect(Text.readableColor("#12")).toBe("#111");
expect(Text.readableColor("#GGGGGG")).toBe("#111");
});
});
});