diff --git a/aic_package/convert_gif.go b/aic_package/convert_gif.go index fb14f79..aa47915 100644 --- a/aic_package/convert_gif.go +++ b/aic_package/convert_gif.go @@ -105,9 +105,9 @@ func pathIsGif(gifPath, urlImgName string, pathIsURl bool, urlImgBytes []byte, l var asciiCharSet [][]imgManip.AsciiChar if braille { - asciiCharSet = imgManip.ConvertToBrailleChars(imgSet, negative, colored, fontColor, threshold) + asciiCharSet = imgManip.ConvertToBrailleChars(imgSet, negative, colored, colorBg, fontColor, threshold) } else { - asciiCharSet = imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, customMap, fontColor) + asciiCharSet = imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, colorBg, customMap, fontColor) } gifFramesSlice[i].asciiCharSet = asciiCharSet gifFramesSlice[i].delay = originalGif.Delay[i] diff --git a/aic_package/convert_image.go b/aic_package/convert_image.go index 60fe25b..c22db61 100644 --- a/aic_package/convert_image.go +++ b/aic_package/convert_image.go @@ -51,9 +51,9 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes []byt var asciiSet [][]imgManip.AsciiChar if braille { - asciiSet = imgManip.ConvertToBrailleChars(imgSet, negative, colored, fontColor, threshold) + asciiSet = imgManip.ConvertToBrailleChars(imgSet, negative, colored, colorBg, fontColor, threshold) } else { - asciiSet = imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, customMap, fontColor) + asciiSet = imgManip.ConvertToAsciiChars(imgSet, negative, colored, complex, colorBg, customMap, fontColor) } // Save ascii art as .png image before printing it, if --save-img flag is passed diff --git a/aic_package/convert_root.go b/aic_package/convert_root.go index 12c163c..b875621 100644 --- a/aic_package/convert_root.go +++ b/aic_package/convert_root.go @@ -49,6 +49,7 @@ func DefaultFlags() Flags { SaveGifPath: "", Negative: false, Colored: false, + CharBackgroundColor: false, Grayscale: false, CustomMap: "", FlipX: false, @@ -82,6 +83,7 @@ func Convert(filePath string, flags Flags) (string, error) { saveGifPath = flags.SaveGifPath negative = flags.Negative colored = flags.Colored + colorBg = flags.CharBackgroundColor grayscale = flags.Grayscale customMap = flags.CustomMap flipX = flags.FlipX diff --git a/aic_package/vars.go b/aic_package/vars.go index dd23220..b89ddfe 100644 --- a/aic_package/vars.go +++ b/aic_package/vars.go @@ -50,6 +50,10 @@ type Flags struct { // This overrides Flags.Grayscale and Flags.FontColor Colored bool + // If Flags.Colored, Flags.Grayscale or Flags.FontColor is set, use that color + // on each character's background in the terminal + CharBackgroundColor bool + // Keep grayscale colors from the original image. This uses the True color // codes for the terminal and will work on saved .png and .gif files as well // This overrides Flags.FontColor @@ -103,6 +107,7 @@ var ( grayscale bool negative bool colored bool + colorBg bool customMap string flipX bool flipY bool diff --git a/cmd/root.go b/cmd/root.go index a3bf5a2..aa0be0e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,6 +40,7 @@ var ( negative bool formatsTrue bool colored bool + colorBg bool grayscale bool customMap string flipX bool @@ -55,7 +56,7 @@ var ( rootCmd = &cobra.Command{ Use: "ascii-image-converter [image paths/urls]", Short: "Converts images and gifs into ascii art", - Version: "1.7.1", + Version: "1.8.0", Long: "This tool converts images into ascii art and prints them on the terminal.\nFurther configuration can be managed with flags.", // Not RunE since help text is getting larger and seeing it for every error impacts user experience @@ -75,6 +76,7 @@ var ( SaveGifPath: saveGifPath, Negative: negative, Colored: colored, + CharBackgroundColor: colorBg, Grayscale: grayscale, CustomMap: customMap, FlipX: flipX, @@ -124,6 +126,7 @@ func init() { // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ascii-image-converter.yaml)") rootCmd.PersistentFlags().BoolVarP(&colored, "color", "C", false, "Display ascii art with original colors\n(Inverts with --negative flag)\n(Overrides --grayscale and --font-color flags)\n") + rootCmd.PersistentFlags().BoolVar(&colorBg, "color-bg", false, "If some color flag is passed, use that color\non character background instead of foreground\n(Inverts with --negative flag)\n(Doesn't work for --save-img or --save-gif)\n") rootCmd.PersistentFlags().IntSliceVarP(&dimensions, "dimensions", "d", nil, "Set width and height for ascii art in CHARACTER length\ne.g. -d 60,30 (defaults to terminal height)\n(Overrides --width and --height flags)\n") rootCmd.PersistentFlags().IntVarP(&width, "width", "W", 0, "Set width for ascii art in CHARACTER length\nHeight is kept to aspect ratio\ne.g. -W 60\n") rootCmd.PersistentFlags().IntVarP(&height, "height", "H", 0, "Set height for ascii art in CHARACTER length\nWidth is kept to aspect ratio\ne.g. -H 60\n") diff --git a/image_manipulation/ascii_conversions.go b/image_manipulation/ascii_conversions.go index f8b2510..4643d4a 100644 --- a/image_manipulation/ascii_conversions.go +++ b/image_manipulation/ascii_conversions.go @@ -55,7 +55,7 @@ to a 2D image_conversions.AsciiChar slice If complex parameter is true, values are compared to 70 levels of color density in ASCII characters. Otherwise, values are compared to 10 levels of color density in ASCII characters. */ -func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, complex bool, customMap string, fontColor [3]int) [][]AsciiChar { +func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, complex, colorBg bool, customMap string, fontColor [3]int) [][]AsciiChar { height := len(imgSet) width := len(imgSet[0]) @@ -134,7 +134,12 @@ func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, complex bool, var char AsciiChar - char.OriginalColor = color.Sprintf("%v", chosenTable[tempInt]) + char.Simple = chosenTable[tempInt] + if colorBg { + char.OriginalColor = color.Sprintf("%v", chosenTable[tempInt]) + } else { + char.OriginalColor = color.Sprintf("%v", chosenTable[tempInt]) + } // If font color is not set, use a simple string. Otherwise, use True color if fontColor != [3]int{255, 255, 255} { @@ -142,11 +147,13 @@ func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, complex bool, fcG := strconv.Itoa(fontColor[1]) fcB := strconv.Itoa(fontColor[2]) - char.SetColor = color.Sprintf("%v", chosenTable[tempInt]) + if colorBg { + char.SetColor = color.Sprintf("%v", chosenTable[tempInt]) + } else { + char.SetColor = color.Sprintf("%v", chosenTable[tempInt]) + } } - char.Simple = chosenTable[tempInt] - if colored { char.RgbValue = imgSet[i][j].rgbValue } else { @@ -167,7 +174,7 @@ to a 2D image_conversions.AsciiChar slice Unlike ConvertToAsciiChars(), this function calculates braille characters instead of ascii */ -func ConvertToBrailleChars(imgSet [][]AsciiPixel, negative, colored bool, fontColor [3]int, threshold int) [][]AsciiChar { +func ConvertToBrailleChars(imgSet [][]AsciiPixel, negative, colored, colorBg bool, fontColor [3]int, threshold int) [][]AsciiChar { BrailleThreshold = uint32(threshold) @@ -216,7 +223,11 @@ func ConvertToBrailleChars(imgSet [][]AsciiPixel, negative, colored bool, fontCo var char AsciiChar char.Simple = brailleChar - char.OriginalColor = color.Sprintf("%v", brailleChar) + if colorBg { + char.OriginalColor = color.Sprintf("%v", brailleChar) + } else { + char.OriginalColor = color.Sprintf("%v", brailleChar) + } // If font color is not set, use a simple string. Otherwise, use True color if fontColor != [3]int{255, 255, 255} { @@ -224,7 +235,11 @@ func ConvertToBrailleChars(imgSet [][]AsciiPixel, negative, colored bool, fontCo fcG := strconv.Itoa(fontColor[1]) fcB := strconv.Itoa(fontColor[2]) - char.SetColor = color.Sprintf("%v", brailleChar) + if colorBg { + char.SetColor = color.Sprintf("%v", brailleChar) + } else { + char.SetColor = color.Sprintf("%v", brailleChar) + } } if colored { diff --git a/snapcraft.yaml b/snapcraft.yaml index 5773980..1faaee4 100644 --- a/snapcraft.yaml +++ b/snapcraft.yaml @@ -1,6 +1,6 @@ name: ascii-image-converter base: core18 -version: "1.7.1" +version: "1.8.0" summary: Convert images and gifs into ascii art description: | ascii-image-converter is a command-line tool that converts images into ascii art and prints