Skip to content

Commit

Permalink
Merge pull request #367 from JassonCordones/patch-1
Browse files Browse the repository at this point in the history
Update image_preview.go
  • Loading branch information
yorukot authored Sep 11, 2024
2 parents a400c8b + 0361e02 commit 00abe49
Showing 1 changed file with 28 additions and 15 deletions.
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

}

0 comments on commit 00abe49

Please sign in to comment.