From 0579adeb80c5dba147741c4f0be8240dbd9592b4 Mon Sep 17 00:00:00 2001 From: lorenzhohermuth Date: Thu, 15 Feb 2024 22:13:03 +0100 Subject: [PATCH] display in true colors --- cmd/draw.go | 21 ++++++++++++++++++-- cmd/palette.go | 38 ++++++++++++++++++++++++++++++++++++ internal/color/map.go | 2 +- internal/color/trueColors.go | 11 +++++++++++ main.go | 19 ------------------ 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 cmd/palette.go create mode 100644 internal/color/trueColors.go diff --git a/cmd/draw.go b/cmd/draw.go index d212b39..e413c67 100644 --- a/cmd/draw.go +++ b/cmd/draw.go @@ -1,12 +1,14 @@ /* Copyright © 2024 NAME HERE - */ package cmd import ( "fmt" + "github.com/LorenzHohermuth/cli-shapes/internal/color" + "github.com/LorenzHohermuth/cli-shapes/internal/renderer" + "github.com/LorenzHohermuth/cli-shapes/pkg/image" "github.com/spf13/cobra" ) @@ -21,12 +23,27 @@ 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") + fileFlag, _ := cmd.Flags().GetString("png") + if fileFlag != "" { + pix := image.ImageToPixles(fileFlag) + s := "" + for _, y := range pix { + for _, x := range y { + color, non := color.GetTrueColor([3]int{x.R, x.G, x.B}) + char := renderer.GetChar(x.Brightness()) + s += color + char + non + } + s += "\n" + } + fmt.Println(s) + + } }, } func init() { rootCmd.AddCommand(drawCmd) + drawCmd.Flags().String("png", "", "png image") // Here you will define your flags and configuration settings. diff --git a/cmd/palette.go b/cmd/palette.go new file mode 100644 index 0000000..c18c8f0 --- /dev/null +++ b/cmd/palette.go @@ -0,0 +1,38 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "github.com/LorenzHohermuth/cli-shapes/internal/color" + "github.com/spf13/cobra" +) + +// paletteCmd represents the palette command +var paletteCmd = &cobra.Command{ + Use: "palette", + Short: "A brief description of your command", + 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) { + color.PrintColorPallet() + }, +} + +func init() { + rootCmd.AddCommand(paletteCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // paletteCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // paletteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/internal/color/map.go b/internal/color/map.go index f9fc2e8..eaba0c5 100644 --- a/internal/color/map.go +++ b/internal/color/map.go @@ -32,7 +32,7 @@ func PrintColorPallet() { } } -func GetColor(p [3]int) (string, string) { +func GetDefaultColor(p [3]int) (string, string) { var color string = "\033[39m" var delta float64 = 255 r2 := float64(p[0]) diff --git a/internal/color/trueColors.go b/internal/color/trueColors.go new file mode 100644 index 0000000..135317d --- /dev/null +++ b/internal/color/trueColors.go @@ -0,0 +1,11 @@ +package color + +import "fmt" + +//printf "\x1b[38;2;255;100;0mTRUECOLOR\x1b[0m\n" + +func GetTrueColor(p [3]int) (string, string) { + color := fmt.Sprintf("\x1b[38;2;%d;%d;%dm", p[0], p[1], p[2]) + return color, "\x1b[0m" +} + diff --git a/main.go b/main.go index 0bc7c37..8a28d38 100644 --- a/main.go +++ b/main.go @@ -4,28 +4,9 @@ Copyright © 2024 NAME HERE package main import ( - "fmt" - "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/mushroom-mini.png") - //pix := image.ImageToPixles("/home/lorenz/Downloads/rgb.png") - pix := image.ImageToPixles("/home/lorenz/Downloads/yoshi.png") - s := "" - for _, y := range pix { - for _, x := range y { - color, non := color.GetColor([3]int{x.R, x.G, x.B}) - char := renderer.GetChar(x.Brightness()) - s += color + char + non - } - s += "\n" - } - fmt.Println(s) - color.PrintColorPallet() cmd.Execute() }