1package hintlabel
2
3import (
4 "image/color"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8)
9
10func TestDensitySize(t *testing.T) {
11 for _, tt := range []struct {
12 name string
13 w, h, mw, mh int
14 scale, logical, want float64
15 }{
16 {"ultrawide", 5120, 2160, 990, 420, 1, 0, 17},
17 {"4k", 3840, 2160, 600, 340, 1.2, 0, 20},
18 {"unknown", 1920, 1080, 0, 0, 1, 0, 13},
19 {"bogus aspect", 1920, 1080, 100, 100, 1, 0, 13},
20 {"fallback high", 5120, 2160, 0, 0, 1, 0, 18.38477631085024},
21 {"minimum", 1920, 1080, 1000, 563, 1.2, 0, 16},
22 {"maximum", 5120, 2160, 100, 42, 1, 0, 40},
23 {"override", 5120, 2160, 990, 420, 1.2, 24, 28.8},
24 } {
25 t.Run(tt.name, func(t *testing.T) {
26 require.InDelta(t, tt.want, FontPixels(tt.w, tt.h, tt.mw, tt.mh, tt.scale, tt.logical), .00001)
27 })
28 }
29}
30
31func TestDerivedContrast(t *testing.T) {
32 for _, s := range []string{"#f9e2af", "#89b4fa", "#808080", "#000000", "#ffffff", "#1e1e2e", "#747474", "#757575", "#ff0000", "#00ff00", "#0000ff"} {
33 t.Run(s, func(t *testing.T) {
34 bg, fg := Colors(s, "", "")
35 require.GreaterOrEqual(t, contrast(bg, fg), 4.5)
36 require.Contains(t, []color.RGBA{{17, 17, 17, 255}, {245, 245, 245, 255}}, fg)
37 })
38 }
39 // Sweep the gap between near-black and near-white contrast thresholds.
40 for i := 0; i < 256; i++ {
41 c := color.RGBA{uint8(i), uint8(i), uint8(i), 255}
42 s := "#" + hexByte(c.R) + hexByte(c.G) + hexByte(c.B)
43 bg, fg := Colors(s, "", "")
44 require.GreaterOrEqual(t, contrast(bg, fg), 4.5, s)
45 }
46}
47
48func hexByte(v uint8) string {
49 const digits = "0123456789abcdef"
50 return string([]byte{digits[v>>4], digits[v&15]})
51}
52
53func TestOverridesAndTyped(t *testing.T) {
54 bg, fg := Colors("#ffffff", "#000000", "#89b4fa")
55 require.Equal(t, color.RGBA{0, 0, 0, 255}, bg)
56 require.Equal(t, color.RGBA{137, 180, 250, 255}, fg)
57 require.Equal(t, color.RGBA{62, 81, 113, 255}, mix(fg, bg))
58 fallback, _ := Colors("", "", "")
59 require.Equal(t, color.RGBA{128, 128, 128, 255}, fallback)
60}
61
62func TestStyledRasterSizeAndCache(t *testing.T) {
63 var r Raster
64 bg, fg := Colors("#f9e2af", "", "")
65 style := Style{13, bg, fg}
66 a, _, small := r.Label("fj", 0, 1, style)
67 style.FontPx = 26
68 b, _, large := r.Label("fj", 0, 1, style)
69 require.Greater(t, large, small)
70 require.NotEqual(t, a, b)
71 style.Background = color.RGBA{0, 0, 0, 255}
72 c, _, _ := r.Label("fj", 0, 1, style)
73 require.NotEqual(t, b, c)
74 require.Equal(t, byte(89), c[3], "outline has 35 percent physical-pixel alpha")
75}
76
77func TestFontPixelsClamped(t *testing.T) {
78 require.InDelta(t, 256, FontPixels(1920, 1080, 0, 0, 3, 1e9), .00001)
79 require.InDelta(t, 256, FontPixels(1920, 1080, 0, 0, 4, 72), .00001)
80}