summaryrefslogtreecommitdiff
path: root/internal/ui/theme_test.go
blob: 5fa6955a32e4c2c6d2632a73f865b5cfad0e4533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)
		}
	}
}