From c70b369f31881bc122321d97141ad97b7b0d39ee Mon Sep 17 00:00:00 2001 From: lorenzhohermuth Date: Tue, 13 Feb 2024 23:06:03 +0100 Subject: [PATCH] display chars --- LICENSE | 0 cmd/draw.go | 40 +++++++++++++ cmd/root.go | 51 ++++++++++++++++ go.mod | 9 +++ go.sum | 10 ++++ internal/color/map.go | 30 ++++++++++ internal/renderer/bitmap.go | 16 +++++ internal/renderer/map.go | 114 ++++++++++++++++++++++++++++++++++++ main.go | 24 ++++++++ pkg/image/convert.go | 12 ++++ pkg/image/image.go | 49 ++++++++++++++++ 11 files changed, 355 insertions(+) create mode 100644 LICENSE create mode 100644 cmd/draw.go create mode 100644 cmd/root.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/color/map.go create mode 100644 internal/renderer/bitmap.go create mode 100644 internal/renderer/map.go create mode 100644 main.go create mode 100644 pkg/image/convert.go create mode 100644 pkg/image/image.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/cmd/draw.go b/cmd/draw.go new file mode 100644 index 0000000..d212b39 --- /dev/null +++ b/cmd/draw.go @@ -0,0 +1,40 @@ +/* +Copyright © 2024 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// drawCmd represents the draw command +var drawCmd = &cobra.Command{ + Use: "draw", + Short: "Draws a defined Image on Screen in ASCII", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("draw called") + }, +} + +func init() { + rootCmd.AddCommand(drawCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // drawCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // drawCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..becffb0 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,51 @@ +/* +Copyright © 2024 NAME HERE + +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + + + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "cli-shapes", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cli-shapes.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..21db0a2 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/LorenzHohermuth/cli-shapes + +go 1.21.6 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.8.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d0e8c2c --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/color/map.go b/internal/color/map.go new file mode 100644 index 0000000..76e9e08 --- /dev/null +++ b/internal/color/map.go @@ -0,0 +1,30 @@ +package color + +import "fmt" + +var colorForeground = map[[3]int]string { + [3]int{0, 0, 0}:"\033[30m", + [3]int{204, 0, 0}:"\033[31m", + [3]int{78, 154, 6}:"\033[32m", + [3]int{196, 160, 0}:"\033[33m", + [3]int{114, 159, 207}:"\033[34m", + [3]int{117, 80, 123}:"\033[35m", + [3]int{6, 152, 154}:"\033[36m", + [3]int{211, 215, 207}:"\033[37m", + [3]int{85, 87, 83}:"\033[90m", + [3]int{239, 41, 41}:"\033[91m", + [3]int{138, 226, 52}:"\033[92m", + [3]int{252, 233, 79}:"\033[93m", + [3]int{50, 175, 255}:"\033[94m", + [3]int{173, 127, 168}:"\033[95m", + [3]int{52, 226, 226}:"\033[96m", + [3]int{255, 255, 255}:"\033[97m", +} + +func PrintColorPallet() { + non := "\033[0m" + arr := []string{"\033[49m", "\033[40m", "\033[41m", "\033[42m", "\033[43m", "\033[44m", "\033[45m", "\033[46m", "\033[47m", "\033[100m", "\033[101m", "\033[102m", "\033[103m", "\033[104m", "\033[105m", "\033[106m", "\033[107m"} + for _, v := range arr { + fmt.Println(v, " ", non) + } +} diff --git a/internal/renderer/bitmap.go b/internal/renderer/bitmap.go new file mode 100644 index 0000000..d46048f --- /dev/null +++ b/internal/renderer/bitmap.go @@ -0,0 +1,16 @@ +package renderer + +type Bitmap struct { + Matrix [][]float64 //number between 0 and 1 +} + +func (b Bitmap) Render() (mat [][]string ) { + for _, v := range b.Matrix { + var arr []string + for _, x := range v { + arr = append(arr, GetChar(x)) + } + mat = append(mat, arr) + } + return mat +} diff --git a/internal/renderer/map.go b/internal/renderer/map.go new file mode 100644 index 0000000..04626c7 --- /dev/null +++ b/internal/renderer/map.go @@ -0,0 +1,114 @@ +package renderer + +import ( + "math" +) + +var ascii = map[string]float64{ + " ":0, + "`":0.0751, + ".":0.0829, + "-":0.0848, + "'":0.1227, + ":":0.1403, + "_":0.1559, + ",":0.185, + "^":0.2183, + "=":0.2417, + ";":0.2571, + ">":0.2852, + "<":0.2902, + "+":0.2919, + "!":0.3099, + "r":0.3192, + "c":0.3232, + "*":0.3294, + "/":0.3384, + "z":0.3609, + "?":0.3619, + "s":0.3667, + "L":0.3737, + "T":0.3747, + "v":0.3838, + ")":0.3921, + "J":0.396, + "7":0.3984, + "(":0.3993, + "|":0.4075, + "F":0.4091, + "i":0.4101, + "{":0.42, + "C":0.423, + "}":0.4247, + "f":0.4274, + "I":0.4293, + "3":0.4328, + "1":0.4382, + "t":0.4385, + "l":0.442, + "u":0.4473, + "[":0.4477, + "n":0.4503, + "e":0.4562, + "o":0.458, + "Z":0.461, + "5":0.4638, + "Y":0.4667, + "x":0.4686, + "j":0.4693, + "y":0.4703, + "a":0.4833, + "]":0.4881, + "2":0.4944, + "E":0.4953, + "S":0.4992, + "w":0.5509, + "q":0.5567, + "k":0.5569, + "P":0.5591, + "6":0.5602, + "h":0.5602, + "9":0.565, + "d":0.5776, + "4":0.5777, + "V":0.5818, + "p":0.587, + "O":0.5972, + "G":0.5999, + "b":0.6043, + "U":0.6049, + "A":0.6093, + "K":0.6099, + "X":0.6465, + "H":0.6561, + "m":0.6595, + "8":0.6631, + "R":0.6714, + "D":0.6759, + "#":0.6809, + "$":0.6816, + "B":0.6925, + "g":0.7039, + "0":0.7086, + "M":0.7235, + "N":0.7302, + "W":0.7332, + "Q":0.7602, + "%":0.7834, + "&":0.8037, + "@":0.9999, +} + +func GetChar(brightness float64) string { + var char string = " " + var delta float64 = 1 + for k, v := range ascii { + otherDelta := math.Abs(v - brightness) + if (otherDelta < delta) { + delta = otherDelta + char = k + } + } + return char +} + diff --git a/main.go b/main.go new file mode 100644 index 0000000..abddebd --- /dev/null +++ b/main.go @@ -0,0 +1,24 @@ +/* +Copyright © 2024 NAME HERE +*/ +package main + +import ( + "github.com/LorenzHohermuth/cli-shapes/cmd" + "github.com/LorenzHohermuth/cli-shapes/internal/color" + "github.com/LorenzHohermuth/cli-shapes/internal/renderer" + "github.com/LorenzHohermuth/cli-shapes/pkg/image" +) + +func main() { + pix := image.ImageToPixles("/home/lorenz/Downloads/crab.png") + s := "" + for _, y := range pix { + for _, x := range y { + s += renderer.GetChar(x.Brightness()) + } + s += "\n" + } + color.PrintColorPallet() + cmd.Execute() +} diff --git a/pkg/image/convert.go b/pkg/image/convert.go new file mode 100644 index 0000000..b5b1f02 --- /dev/null +++ b/pkg/image/convert.go @@ -0,0 +1,12 @@ +package image + +//func getBighnessMap(map [][]Pixel) [][]float64{} + +func (pix Pixel) Brightness() float64 { + R := float64(pix.R) + G := float64(pix.G) + B := float64(pix.B) + A := float64(pix.A) + brightness := (0.2126*R + 0.7152*G + 0.0722*B) /255 + return brightness * (A / 255) +} diff --git a/pkg/image/image.go b/pkg/image/image.go new file mode 100644 index 0000000..c567e1d --- /dev/null +++ b/pkg/image/image.go @@ -0,0 +1,49 @@ +package image + +import ( + "image" + "image/png" + "io" + "os" +) + +type Pixel struct { + R int + G int + B int + A int +} +func ImageToPixles(path string) [][]Pixel { + image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig) + file, err := os.Open(path) + check(err) + defer file.Close() + return getPixles(file) +} + +func getPixles(f io.Reader) [][]Pixel { + img, _, err := image.Decode(f) + check(err) + bounds := img.Bounds() + width, height := bounds.Max.X, bounds.Max.Y + + var pixels [][]Pixel + for y := 0; y < height; y++ { + var row []Pixel + for x := 0; x < width; x++ { + row = append(row, rgbaToPixel(img.At(x,y).RGBA())) + } + pixels = append(pixels, row) + } + return pixels +} + +func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) Pixel { + return Pixel{int(r / 257), int(g / 257), int(b / 257), int(a / 257)} +} + +func check(err error) { + if err != nil { + panic(err) + } +}