From 38b276bb3d5be12a63d9bab3fe927803e2c4315c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 8 Apr 2026 22:25:36 +0300 Subject: Fix task f random theme contrast --- internal/ui/theme_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 internal/ui/theme_test.go (limited to 'internal/ui/theme_test.go') diff --git a/internal/ui/theme_test.go b/internal/ui/theme_test.go new file mode 100644 index 0000000..5fa6955 --- /dev/null +++ b/internal/ui/theme_test.go @@ -0,0 +1,35 @@ +package ui + +import ( + "strconv" + "testing" +) + +func TestContrastColorUsesHighContrastForeground(t *testing.T) { + for bg := 0; bg < 256; bg++ { + fg := contrastColor(strconv.Itoa(bg)) + if fg != "0" && fg != "15" { + t.Fatalf("contrastColor(%d) = %q, want black or white foreground", bg, fg) + } + + r, g, b := xtermRGB(bg) + var ratio float64 + if fg == "0" { + ratio = contrastRatio(r, g, b, 0, 0, 0) + } else { + ratio = contrastRatio(r, g, b, 255, 255, 255) + } + if ratio < 4.5 { + t.Fatalf("contrastColor(%d) = %q with contrast ratio %.2f, want >= 4.5", bg, fg, ratio) + } + } +} + +func TestContrastColorFallsBackForInvalidInput(t *testing.T) { + tests := []string{"not-a-color", "-1", "256"} + for _, input := range tests { + if got := contrastColor(input); got != "15" { + t.Fatalf("contrastColor(%q) = %q, want 15", input, got) + } + } +} -- cgit v1.2.3