Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update image_preview.go #367

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions src/pkg/file_preview/image_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ func ConvertImageToANSI(img image.Image, defaultBGColor color.Color) string {
height := img.Bounds().Dy()
output := ""

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), BGColor)
lowerColor := termenv.RGBColor(fmt.Sprintf("#%02x%02x%02x", uint8(r>>8), uint8(g>>8), uint8(b>>8)))
upperColor := colorToTermenv(img.At(x, y), colorToHex(defaultBGColor))
lowerColor := colorToTermenv(defaultBGColor, "")

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

// Using the "▄" character which fills the lower half
Expand All @@ -44,12 +40,11 @@ func ConvertImageToANSI(img image.Image, defaultBGColor color.Color) string {
}

// colorToTermenv converts a color.Color to a termenv.RGBColor
func colorToTermenv(c color.Color, BGColor string) termenv.RGBColor {
func colorToTermenv(c color.Color, fallbackColor string) termenv.RGBColor {
r, g, b, a := c.RGBA()
if a == 0 {
return termenv.RGBColor(BGColor)
}

return termenv.RGBColor(fallbackColor)
}
return termenv.RGBColor(fmt.Sprintf("#%02x%02x%02x", uint8(r>>8), uint8(g>>8), uint8(b>>8)))
}

Expand All @@ -72,12 +67,30 @@ func ImagePreview(path string, maxWidth, maxHeight int, defaultBGColor string) (
resizedImg := resize.Thumbnail(uint(maxWidth), uint(maxHeight), img, resize.Lanczos3)

// Convert image to ANSI
ansiImage := ConvertImageToANSI(resizedImg, hexToColor(defaultBGColor))
bgColor, err := hexToColor(defaultBGColor)
if err != nil {
return "", fmt.Errorf("invalid background color: %v", err)
}
ansiImage := ConvertImageToANSI(resizedImg, bgColor)

return ansiImage, nil
}

func hexToColor(hex string) color.RGBA {
values, _ := strconv.ParseUint(string(hex[1:]), 16, 32)
return color.RGBA{R: uint8(values >> 16), G: uint8((values >> 8) & 0xFF), B: uint8(values & 0xFF), A: 255}
func hexToColor(hex string) (color.RGBA, error) {
if len(hex) != 7 || hex[0] != '#' {
return color.RGBA{}, fmt.Errorf("invalid hex color format")
}
values, err := strconv.ParseUint(string(hex[1:]), 16, 32)
if err != nil {
return color.RGBA{}, err
}
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

}