Render covers with quadrant blocks for twice the horizontal detail
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+60
-19
@@ -77,39 +77,80 @@ func TestDecodeCover(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoverViewDrawsHalfBlocks(t *testing.T) {
|
||||
func TestQuadCell(t *testing.T) {
|
||||
red, blue, black := color.RGBA{255, 0, 0, 255}, color.RGBA{0, 0, 255, 255}, color.RGBA{0, 0, 0, 255}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
px [4]color.RGBA
|
||||
wantCh rune
|
||||
wantFg color.RGBA
|
||||
wantBg color.RGBA
|
||||
sameBoth bool
|
||||
}{
|
||||
{"flat", [4]color.RGBA{red, red, red, red}, ' ', red, red, true},
|
||||
{"top half", [4]color.RGBA{red, red, blue, blue}, '▀', red, blue, false},
|
||||
{"bottom half", [4]color.RGBA{blue, blue, red, red}, '▀', blue, red, false}, // same picture, canonical form
|
||||
{"left half", [4]color.RGBA{red, blue, red, blue}, '▌', red, blue, false},
|
||||
{"one corner", [4]color.RGBA{red, blue, blue, blue}, '▘', red, blue, false},
|
||||
{"diagonal", [4]color.RGBA{red, black, black, red}, '▞', black, red, false},
|
||||
} {
|
||||
ch, fg, bg := quadCell(tc.px)
|
||||
if ch != tc.wantCh || fg != tc.wantFg || bg != tc.wantBg {
|
||||
t.Errorf("%s: %q fg=%v bg=%v, want %q fg=%v bg=%v", tc.name, ch, fg, bg, tc.wantCh, tc.wantFg, tc.wantBg)
|
||||
}
|
||||
}
|
||||
// Nearly flat noise stays a flat cell instead of a distracting pattern.
|
||||
n := color.RGBA{100, 100, 100, 255}
|
||||
m := color.RGBA{102, 100, 101, 255}
|
||||
if ch, _, _ := quadCell([4]color.RGBA{n, m, n, n}); ch != ' ' {
|
||||
t.Errorf("noise rendered as %q", ch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoverCells(t *testing.T) {
|
||||
// A 2:3 poster in a roomy box is limited by the height: 10 rows are 20
|
||||
// width units tall, so 13 columns.
|
||||
if c, r := coverCells(40, 60, 30, 10); c != 13 || r != 10 {
|
||||
t.Errorf("tall limit: %dx%d", c, r)
|
||||
}
|
||||
// In a narrow box the width limits it.
|
||||
if c, r := coverCells(40, 60, 10, 20); c != 10 || r != 7 {
|
||||
t.Errorf("narrow limit: %dx%d", c, r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoverViewDraws(t *testing.T) {
|
||||
screen := tcell.NewSimulationScreen("")
|
||||
if err := screen.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer screen.Fini()
|
||||
screen.SetSize(20, 10)
|
||||
screen.SetSize(30, 10)
|
||||
|
||||
c := newCoverView()
|
||||
c.setImage(stripes(40, 60)) // 2:3 poster
|
||||
c.SetRect(0, 0, 20, 10) // 20 columns × 20 pixel rows: a 13×20 pixel poster
|
||||
c.Draw(screen)
|
||||
c.setImage(stripes(40, 60)) // red above blue
|
||||
c.SetRect(0, 0, 30, 10) // fits 13 columns × 10 rows
|
||||
|
||||
// Row 0 holds pixel rows 0 (top) and 1: both red. The middle cell row
|
||||
// (index 4/5) crosses into blue; the last row is blue on both halves.
|
||||
c.Draw(screen)
|
||||
rgba := func(c tcell.Color) color.RGBA {
|
||||
r, g, b := c.RGB()
|
||||
return color.RGBA{uint8(r), uint8(g), uint8(b), 255}
|
||||
}
|
||||
check := func(row int, wantFg, wantBg color.RGBA) {
|
||||
t.Helper()
|
||||
ch, _, style, _ := screen.GetContent(0, row)
|
||||
fg, bg, _ := style.Decompose()
|
||||
if ch != '▀' || rgba(fg) != wantFg || rgba(bg) != wantBg {
|
||||
t.Errorf("row %d: %q fg=%v bg=%v", row, ch, rgba(fg), rgba(bg))
|
||||
}
|
||||
cell := func(x, y int) (rune, color.RGBA, color.RGBA) {
|
||||
ch, _, st, _ := screen.GetContent(x, y)
|
||||
fg, bg, _ := st.Decompose()
|
||||
return ch, rgba(fg), rgba(bg)
|
||||
}
|
||||
red, blue := color.RGBA{255, 0, 0, 255}, color.RGBA{0, 0, 255, 255}
|
||||
check(0, red, red)
|
||||
check(9, blue, blue)
|
||||
// Beyond the poster's width nothing is drawn.
|
||||
if ch, _, _, _ := screen.GetContent(19, 0); ch == '▀' {
|
||||
t.Error("poster stretched beyond its aspect ratio")
|
||||
if _, fg, bg := cell(0, 0); fg != red || bg != red {
|
||||
t.Errorf("top row = fg %v bg %v, want red", fg, bg)
|
||||
}
|
||||
if _, fg, bg := cell(12, 9); fg != blue || bg != blue {
|
||||
t.Errorf("bottom right = fg %v bg %v, want blue", fg, bg)
|
||||
}
|
||||
// Outside the poster's footprint nothing is drawn.
|
||||
if ch, _, _, _ := screen.GetContent(13, 0); ch != ' ' {
|
||||
t.Errorf("poster stretched beyond its aspect ratio: %q", ch)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user