From c9228ffa367405907265fc96228e7bf046ff7f11 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 28 May 2026 13:12:58 -0700 Subject: Fix transparency for block24 --- thumbnailer.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/thumbnailer.go b/thumbnailer.go index 6c69fc1..c0d7c9e 100644 --- a/thumbnailer.go +++ b/thumbnailer.go @@ -179,10 +179,7 @@ func printImgXterm(w io.Writer, img image.Image) { for x := 0; x < img.Bounds().Dx(); x++ { hi := img.At(x, y) if hi == color.Transparent { - if x > 0 && img.At(x-1, y) != color.Transparent { - fmt.Fprint(w, "\033[49m") - } - fmt.Fprint(w, " ") + fmt.Fprint(w, "\033[49m ") } else { lo := 9 if y+1 < img.Bounds().Dy() && img.At(x, y+1) != color.Transparent { @@ -195,6 +192,11 @@ func printImgXterm(w io.Writer, img image.Image) { } } +func isFullyTransparent(c color.Color) bool { + _, _, _, a := c.RGBA() + return a == 0 +} + func printImgBlock24(w io.Writer, img image.Image) { for y := 0; y < img.Bounds().Dy(); y += 2 { if y > 0 { @@ -202,13 +204,10 @@ func printImgBlock24(w io.Writer, img image.Image) { } for x := 0; x < img.Bounds().Dx(); x++ { hi := img.At(x, y) - if hi == color.Transparent { - if x > 0 && img.At(x-1, y) != color.Transparent { - fmt.Fprint(w, "\033[49m") - } - fmt.Fprint(w, " ") + if isFullyTransparent(hi) { + fmt.Fprint(w, "\033[49m ") } else { - if y+1 < img.Bounds().Dy() && img.At(x, y+1) != color.Transparent { + if y+1 < img.Bounds().Dy() && !isFullyTransparent(img.At(x, y+1)) { r, g, b, _ := img.At(x, y+1).RGBA() fmt.Fprintf(w, "\033[48;2;%d;%d;%dm", r>>8, g>>8, b>>8) } else { @@ -241,11 +240,12 @@ func printImgSixel(w io.Writer, img image.PalettedImage) { } } for x := 0; x < img.Bounds().Dx(); x++ { - if img.At(x, y) == color.Transparent { + c := img.ColorIndexAt(x, y) + if c == 0 { fmt.Fprint(w, "?") } else { char := '?' + 1<<(y%6) - fmt.Fprintf(w, "#%d%c", img.ColorIndexAt(x, y)-1, char) + fmt.Fprintf(w, "#%d%c", c-1, char) } } } @@ -450,7 +450,8 @@ func thumbnailer(args []string) error { func main() { flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: thumbnailer [FILE]...\n") + fmt.Fprintf(os.Stderr, "Usage: thumbnailer [OPTION]... [FILE]...\n") + flag.PrintDefaults() } flag.Parse() -- cgit v1.3.1