Skip to content

Commit

Permalink
Added --color-bg flag
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoraiz committed Sep 4, 2021
1 parent 50b6b00 commit 202179d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions aic_package/convert_gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions aic_package/convert_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions aic_package/convert_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func DefaultFlags() Flags {
SaveGifPath: "",
Negative: false,
Colored: false,
CharBackgroundColor: false,
Grayscale: false,
CustomMap: "",
FlipX: false,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions aic_package/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,6 +107,7 @@ var (
grayscale bool
negative bool
colored bool
colorBg bool
customMap string
flipX bool
flipY bool
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
negative bool
formatsTrue bool
colored bool
colorBg bool
grayscale bool
customMap string
flipX bool
Expand All @@ -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
Expand All @@ -75,6 +76,7 @@ var (
SaveGifPath: saveGifPath,
Negative: negative,
Colored: colored,
CharBackgroundColor: colorBg,
Grayscale: grayscale,
CustomMap: customMap,
FlipX: flipX,
Expand Down Expand Up @@ -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")
Expand Down
31 changes: 23 additions & 8 deletions image_manipulation/ascii_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -134,19 +134,26 @@ func ConvertToAsciiChars(imgSet [][]AsciiPixel, negative, colored, complex bool,

var char AsciiChar

char.OriginalColor = color.Sprintf("<fg="+rStr+","+gStr+","+bStr+">%v</>", chosenTable[tempInt])
char.Simple = chosenTable[tempInt]
if colorBg {
char.OriginalColor = color.Sprintf("<bg="+rStr+","+gStr+","+bStr+">%v</>", chosenTable[tempInt])
} else {
char.OriginalColor = color.Sprintf("<fg="+rStr+","+gStr+","+bStr+">%v</>", chosenTable[tempInt])
}

// If font color is not set, use a simple string. Otherwise, use True color
if fontColor != [3]int{255, 255, 255} {
fcR := strconv.Itoa(fontColor[0])
fcG := strconv.Itoa(fontColor[1])
fcB := strconv.Itoa(fontColor[2])

char.SetColor = color.Sprintf("<fg="+fcR+","+fcG+","+fcB+">%v</>", chosenTable[tempInt])
if colorBg {
char.SetColor = color.Sprintf("<bg="+fcR+","+fcG+","+fcB+">%v</>", chosenTable[tempInt])
} else {
char.SetColor = color.Sprintf("<fg="+fcR+","+fcG+","+fcB+">%v</>", chosenTable[tempInt])
}
}

char.Simple = chosenTable[tempInt]

if colored {
char.RgbValue = imgSet[i][j].rgbValue
} else {
Expand All @@ -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)

Expand Down Expand Up @@ -216,15 +223,23 @@ func ConvertToBrailleChars(imgSet [][]AsciiPixel, negative, colored bool, fontCo
var char AsciiChar

char.Simple = brailleChar
char.OriginalColor = color.Sprintf("<fg="+rStr+","+gStr+","+bStr+">%v</>", brailleChar)
if colorBg {
char.OriginalColor = color.Sprintf("<bg="+rStr+","+gStr+","+bStr+">%v</>", brailleChar)
} else {
char.OriginalColor = color.Sprintf("<fg="+rStr+","+gStr+","+bStr+">%v</>", brailleChar)
}

// If font color is not set, use a simple string. Otherwise, use True color
if fontColor != [3]int{255, 255, 255} {
fcR := strconv.Itoa(fontColor[0])
fcG := strconv.Itoa(fontColor[1])
fcB := strconv.Itoa(fontColor[2])

char.SetColor = color.Sprintf("<fg="+fcR+","+fcG+","+fcB+">%v</>", brailleChar)
if colorBg {
char.SetColor = color.Sprintf("<bg="+fcR+","+fcG+","+fcB+">%v</>", brailleChar)
} else {
char.SetColor = color.Sprintf("<fg="+fcR+","+fcG+","+fcB+">%v</>", brailleChar)
}
}

if colored {
Expand Down
2 changes: 1 addition & 1 deletion snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 202179d

Please sign in to comment.