Skip to content

Commit

Permalink
fix some background color
Browse files Browse the repository at this point in the history
  • Loading branch information
yorukot committed Sep 11, 2024
1 parent c3e985e commit 0361e02
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/pkg/file_preview/image_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ func ConvertImageToANSI(img image.Image, defaultBGColor color.Color) string {
width := img.Bounds().Dx()
height := img.Bounds().Dy()
output := ""
BGColor :=colorToTermenv(defaultBGColor. "")

// r, g, b, _ := defaultBGColor.RGBA()

// BGColor := fmt.Sprintf("#%02x%02x%02x", uint8(r>>8), uint8(g>>8), uint8(b>>8))

for y := 0; y < height; y += 2 {
for x := 0; x < width; x++ {
upperColor := colorToTermenv(img.At(x, y), "")
lowerColor := BGColor
upperColor := colorToTermenv(img.At(x, y), colorToHex(defaultBGColor))
lowerColor := colorToTermenv(defaultBGColor, "")

if y + 1 < height {
lowerColor = colorToTermenv(img.At(x, y + 1), "")
lowerColor = colorToTermenv(img.At(x, y + 1), colorToHex(defaultBGColor))
}

// Using the "▄" character which fills the lower half
Expand All @@ -48,7 +43,7 @@ func ConvertImageToANSI(img image.Image, defaultBGColor color.Color) string {
func colorToTermenv(c color.Color, fallbackColor string) termenv.RGBColor {
r, g, b, a := c.RGBA()
if a == 0 {
return termenv.RGBColor("")
return termenv.RGBColor(fallbackColor)
}
return termenv.RGBColor(fmt.Sprintf("#%02x%02x%02x", uint8(r>>8), uint8(g>>8), uint8(b>>8)))
}
Expand Down Expand Up @@ -91,3 +86,11 @@ func hexToColor(hex string) (color.RGBA, error) {
}
return color.RGBA{R: uint8(values >> 16), G: uint8((values >> 8) & 0xFF), B: uint8(values & 0xFF), A: 255},nil
}

func colorToHex(color color.Color) (fullbackHex string) {
r, g, b, _ := color.RGBA()

fullbackHex = fmt.Sprintf("#%02x%02x%02x", uint8(r>>8), uint8(g>>8), uint8(b>>8))
return fullbackHex

}

0 comments on commit 0361e02

Please sign in to comment.