From c5de7795bcb4551edcde5176aaec82d41edc5d78 Mon Sep 17 00:00:00 2001 From: qeesung <1245712564@qq.com> Date: Sun, 21 Oct 2018 00:14:19 +0800 Subject: [PATCH] add colored the asicc output --- Gopkg.lock | 9 +++++++++ Gopkg.toml | 4 ++++ ascii/ascii.go | 17 +++++++++++++---- ascii/option.go | 24 +++++++++--------------- convert/convert.go | 6 ++++-- image2ascii.go | 3 +++ 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 8576533..0f22448 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,6 +1,14 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. +[[projects]] + branch = "master" + digest = "1:f8c5815a76aec3fac7f6d67911eccd3a93841294052b0e81bca1ff325a1ee62d" + name = "github.com/aybabtme/rgbterm" + packages = ["."] + pruneopts = "UT" + revision = "cc83f3b3ce5911279513a46d6d3316d67bedaa54" + [[projects]] digest = "1:0981502f9816113c9c8c4ac301583841855c8cf4da8c72f696b3ebedf6d0e4e5" name = "github.com/mattn/go-isatty" @@ -37,6 +45,7 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/aybabtme/rgbterm", "github.com/mattn/go-isatty", "github.com/nfnt/resize", "github.com/wayneashleyberry/terminal-dimensions", diff --git a/Gopkg.toml b/Gopkg.toml index 91d5f5a..d04d9d5 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -40,3 +40,7 @@ [[constraint]] name = "github.com/wayneashleyberry/terminal-dimensions" version = "1.0.0" + +[[constraint]] + branch = "master" + name = "github.com/aybabtme/rgbterm" diff --git a/ascii/ascii.go b/ascii/ascii.go index bd97f55..2b50411 100644 --- a/ascii/ascii.go +++ b/ascii/ascii.go @@ -4,6 +4,7 @@ package ascii import ( + "github.com/aybabtme/rgbterm" "image/color" "math" "reflect" @@ -14,8 +15,8 @@ func ConvertPixelToASCII(pixel color.Color, options *Options) string { defaultOptions := NewOptions() defaultOptions.mergeOptions(options) - if defaultOptions.reverse { - defaultOptions.pixels = reverse(defaultOptions.pixels) + if defaultOptions.Reverse { + defaultOptions.Pixels = reverse(defaultOptions.Pixels) } r := reflect.ValueOf(pixel).FieldByName("R").Uint() @@ -25,8 +26,11 @@ func ConvertPixelToASCII(pixel color.Color, options *Options) string { value := intensity(r, g, b, a) // Choose the char - precision := float64(255 * 3 / (len(options.pixels) - 1)) - rawChar := options.pixels[roundValue(float64(value)/precision)] + precision := float64(255 * 3 / (len(options.Pixels) - 1)) + rawChar := options.Pixels[roundValue(float64(value)/precision)] + if options.Colored { + return decorateWithColor(r, g, b, rawChar) + } return string([]byte{rawChar}) } @@ -45,3 +49,8 @@ func reverse(numbers []byte) []byte { func intensity(r, g, b, a uint64) uint64 { return (r + g + b) * a / 255 } + +func decorateWithColor(r, g, b uint64, rawChar byte) string { + coloredChar := rgbterm.FgString(string([]byte{rawChar}), uint8(r), uint8(g), uint8(b)) + return coloredChar +} diff --git a/ascii/option.go b/ascii/option.go index 860526f..71cf5da 100644 --- a/ascii/option.go +++ b/ascii/option.go @@ -2,20 +2,16 @@ package ascii // Convert options type Options struct { - pixels []byte - reverse bool - colored bool - bg bool - fg bool + Pixels []byte + Reverse bool + Colored bool } // Default options var DefaultOptions = Options{ - pixels: []byte(" .,:;i1tfLCG08@"), - reverse: false, - colored: true, - bg: false, - fg: true, + Pixels: []byte(" .,:;i1tfLCG08@"), + Reverse: false, + Colored: true, } // Create a new options @@ -25,9 +21,7 @@ func NewOptions() Options { // Merge options func (options *Options) mergeOptions(newOptions *Options) { - options.pixels = append([]byte{}, newOptions.pixels...) - options.reverse = newOptions.reverse - options.colored = newOptions.colored - options.bg = newOptions.bg - options.fg = newOptions.fg + options.Pixels = append([]byte{}, newOptions.Pixels...) + options.Reverse = newOptions.Reverse + options.Colored = newOptions.Colored } diff --git a/convert/convert.go b/convert/convert.go index e87789c..12842c6 100644 --- a/convert/convert.go +++ b/convert/convert.go @@ -15,12 +15,13 @@ type Options struct { ExpectedWidth int ExpectedHeight int FitScreen bool + Colored bool } // Convert a image to ascii matrix -func Image2ASCIIMatrix(image image.Image, options *Options) []string { +func Image2ASCIIMatrix(image image.Image, imageConvertOptions *Options) []string { // Resize the convert first - newImage := ScaleImage(image, options) + newImage := ScaleImage(image, imageConvertOptions) sz := newImage.Bounds() newWidth := sz.Max.Y newHeight := sz.Max.X @@ -30,6 +31,7 @@ func Image2ASCIIMatrix(image image.Image, options *Options) []string { pixel := color.NRGBAModel.Convert(newImage.At(j, i)) // Convert the pixel to ascii char pixelConvertOptions := ascii.NewOptions() + pixelConvertOptions.Colored = imageConvertOptions.Colored rawChar := ascii.ConvertPixelToASCII(pixel, &pixelConvertOptions) rawCharValues = append(rawCharValues, rawChar) } diff --git a/image2ascii.go b/image2ascii.go index 0d17389..b69ea38 100644 --- a/image2ascii.go +++ b/image2ascii.go @@ -14,6 +14,7 @@ var ratio float64 var expectedWidth int var expectedHeight int var fitScreen bool +var colored bool func init() { flag.StringVar(&imageFilename, "f", "", "Image filename to be convert") @@ -21,6 +22,7 @@ func init() { flag.IntVar(&expectedWidth, "w", -1, "Expected image width, -1 for image default width") flag.IntVar(&expectedHeight, "g", -1, "Expected image height, -1 for image default height") flag.BoolVar(&fitScreen, "s", true, "Fit the terminal screen, ignored when use -w, -g, -r") + flag.BoolVar(&colored, "c", true, "Colored the ascii when output to the terminal") flag.Usage = usage } @@ -37,6 +39,7 @@ func main() { ExpectedHeight: expectedHeight, ExpectedWidth: expectedWidth, FitScreen: fitScreen, + Colored: colored, } fmt.Print(convert.ImageFile2ASCIIString(imageFilename, convertOptions)) } -- 2.45.2