From f30c87365ad1b577a55f7b920c8e307e8b23647e Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 28 Jun 2022 10:44:35 +0800 Subject: [PATCH] chore: update some func comments, fix some code style --- color.go | 16 +++++---------- color_16.go | 22 ++++++++++---------- color_256.go | 11 +++------- color_rgb.go | 21 ------------------- convert.go | 57 ++++++++++++++++++++++++++++++++++++---------------- 5 files changed, 59 insertions(+), 68 deletions(-) diff --git a/color.go b/color.go index 6394a87..f6db6ff 100644 --- a/color.go +++ b/color.go @@ -19,22 +19,16 @@ import ( "github.com/xo/terminfo" ) -// terminal color available level alias of the terminfo.ColorLevel* -const ( - LevelNo = terminfo.ColorLevelNone // not support color. - Level16 = terminfo.ColorLevelBasic // 3/4 bit color supported - Level256 = terminfo.ColorLevelHundreds // 8 bit color supported - LevelRgb = terminfo.ColorLevelMillions // (24 bit)true color supported -) - // color render templates // // ESC 操作的表示: // "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制) const ( + // StartSet chars StartSet = "\x1b[" // ResetSet close all properties. - ResetSet = "\x1b[0m" + ResetSet = "\x1b[0m" + // SettingTpl string. SettingTpl = "\x1b[%sm" // FullColorTpl for build color code FullColorTpl = "\x1b[%sm%s\x1b[0m" @@ -63,7 +57,7 @@ var ( // output the default io.Writer message print output io.Writer = os.Stdout // mark current env, It's like in `cmd.exe` - // if not in windows, it's always is False. + // if not in windows, it's always False. isLikeInCmd bool // the color support level for current terminal // needVTP - need enable VTP, only for windows OS @@ -76,7 +70,7 @@ var ( ) // TermColorLevel value on current ENV -func TermColorLevel() terminfo.ColorLevel { +func TermColorLevel() Level { return colorLevel } diff --git a/color_16.go b/color_16.go index 898def4..3551521 100644 --- a/color_16.go +++ b/color_16.go @@ -291,7 +291,7 @@ func (c Color) C256() Color256 { // ToFg always convert fg func (c Color) ToFg() Color { val := uint8(c) - // is option code, don't change + // option code, don't change if val < 10 { return c } @@ -301,7 +301,7 @@ func (c Color) ToFg() Color { // ToBg always convert bg func (c Color) ToBg() Color { val := uint8(c) - // is option code, don't change + // option code, don't change if val < 10 { return c } @@ -406,9 +406,9 @@ var ( // TODO basic name alias // basicNameAlias = map[string]string{} // optionWithAlias = buildOpWithAlias() - // basic color name to code - name2basicMap = initName2basicMap() + // name2basicMap = initName2basicMap() + // basic2nameMap basic color code to name basic2nameMap = map[uint8]string{ 30: "black", @@ -465,13 +465,13 @@ func Basic2nameMap() map[uint8]string { return basic2nameMap } -func initName2basicMap() map[string]uint8 { - n2b := make(map[string]uint8, len(basic2nameMap)) - for u, s := range basic2nameMap { - n2b[s] = u - } - return n2b -} +// func initName2basicMap() map[string]uint8 { +// n2b := make(map[string]uint8, len(basic2nameMap)) +// for u, s := range basic2nameMap { +// n2b[s] = u +// } +// return n2b +// } // func buildOpWithAlias() map[string]uint8 { // } diff --git a/color_256.go b/color_256.go index efd6dca..c95c0f7 100644 --- a/color_256.go +++ b/color_256.go @@ -149,15 +149,12 @@ func (c Color256) FullCode() string { // String convert to color code string with prefix. eg: "38;5;12" func (c Color256) String() string { if c[1] == AsFg { // 0 is Fg - // return fmt.Sprintf(TplFg256, c[0]) return Fg256Pfx + strconv.Itoa(int(c[0])) } if c[1] == AsBg { // 1 is Bg - // return fmt.Sprintf(TplBg256, c[0]) return Bg256Pfx + strconv.Itoa(int(c[0])) } - return "" // empty } @@ -198,8 +195,6 @@ func (c Color256) IsEmpty() bool { // 都是由两位uint8组成, 第一位是色彩值; // 第二位与 Bit8Color 不一样的是,在这里表示是否设置了值 0 未设置 !=0 已设置 type Style256 struct { - // p Printer - // Name of the style Name string // color options of the style @@ -209,6 +204,7 @@ type Style256 struct { } // S256 create a color256 style +// // Usage: // s := color.S256() // s := color.S256(132) // fg @@ -293,16 +289,15 @@ func (s *Style256) Code() string { func (s *Style256) String() string { var ss []string if s.fg[1] > 0 { - ss = append(ss, fmt.Sprintf(TplFg256, s.fg[0])) + ss = append(ss, Fg256Pfx+strconv.FormatInt(int64(s.fg[0]), 10)) } if s.bg[1] > 0 { - ss = append(ss, fmt.Sprintf(TplBg256, s.bg[0])) + ss = append(ss, Bg256Pfx+strconv.FormatInt(int64(s.bg[0]), 10)) } if s.opts.IsValid() { ss = append(ss, s.opts.String()) } - return strings.Join(ss, ";") } diff --git a/color_rgb.go b/color_rgb.go index 5f16d7f..894730f 100644 --- a/color_rgb.go +++ b/color_rgb.go @@ -35,27 +35,6 @@ const ( AsBg ) -// values from https://github.com/go-terminfo/terminfo -// var ( -// RgbaBlack = image_color.RGBA{0, 0, 0, 255} -// Red = color.RGBA{205, 0, 0, 255} -// Green = color.RGBA{0, 205, 0, 255} -// Orange = color.RGBA{205, 205, 0, 255} -// Blue = color.RGBA{0, 0, 238, 255} -// Magenta = color.RGBA{205, 0, 205, 255} -// Cyan = color.RGBA{0, 205, 205, 255} -// LightGrey = color.RGBA{229, 229, 229, 255} -// -// DarkGrey = color.RGBA{127, 127, 127, 255} -// LightRed = color.RGBA{255, 0, 0, 255} -// LightGreen = color.RGBA{0, 255, 0, 255} -// Yellow = color.RGBA{255, 255, 0, 255} -// LightBlue = color.RGBA{92, 92, 255, 255} -// LightMagenta = color.RGBA{255, 0, 255, 255} -// LightCyan = color.RGBA{0, 255, 255, 255} -// White = color.RGBA{255, 255, 255, 255} -// ) - /************************************************************* * RGB Color(Bit24Color, TrueColor) *************************************************************/ diff --git a/convert.go b/convert.go index 6e3326d..232ce93 100644 --- a/convert.go +++ b/convert.go @@ -8,6 +8,27 @@ import ( "strings" ) +// values from https://github.com/go-terminfo/terminfo +// var ( +// RgbaBlack = image_color.RGBA{0, 0, 0, 255} +// Red = color.RGBA{205, 0, 0, 255} +// Green = color.RGBA{0, 205, 0, 255} +// Orange = color.RGBA{205, 205, 0, 255} +// Blue = color.RGBA{0, 0, 238, 255} +// Magenta = color.RGBA{205, 0, 205, 255} +// Cyan = color.RGBA{0, 205, 205, 255} +// LightGrey = color.RGBA{229, 229, 229, 255} +// +// DarkGrey = color.RGBA{127, 127, 127, 255} +// LightRed = color.RGBA{255, 0, 0, 255} +// LightGreen = color.RGBA{0, 255, 0, 255} +// Yellow = color.RGBA{255, 255, 0, 255} +// LightBlue = color.RGBA{92, 92, 255, 255} +// LightMagenta = color.RGBA{255, 0, 255, 255} +// LightCyan = color.RGBA{0, 255, 255, 255} +// White = color.RGBA{255, 255, 255, 255} +// ) + var ( // ---------- basic(16) <=> 256 color convert ---------- basicTo256Map = map[uint8]uint8{ @@ -32,14 +53,14 @@ var ( // ---------- basic(16) <=> RGB color convert ---------- // refer from Hyper app basic2hexMap = map[uint8]string{ - 30: "000000", // black - 31: "c51e14", // red - 32: "1dc121", // green - 33: "c7c329", // yellow - 34: "0a2fc4", // blue - 35: "c839c5", // magenta - 36: "20c5c6", // cyan - 37: "c7c7c7", // white + 30: "000000", // black + 31: "c51e14", // red + 32: "1dc121", // green + 33: "c7c329", // yellow + 34: "0a2fc4", // blue + 35: "c839c5", // magenta + 36: "20c5c6", // cyan + 37: "c7c7c7", // white // - don't add bg color // 40: "000000", // black // 41: "c51e14", // red @@ -49,14 +70,14 @@ var ( // 45: "c839c5", // magenta // 46: "20c5c6", // cyan // 47: "c7c7c7", // white - 90: "686868", // lightBlack/darkGray - 91: "fd6f6b", // lightRed - 92: "67f86f", // lightGreen - 93: "fffa72", // lightYellow - 94: "6a76fb", // lightBlue - 95: "fd7cfc", // lightMagenta - 96: "68fdfe", // lightCyan - 97: "ffffff", // lightWhite + 90: "686868", // lightBlack/darkGray + 91: "fd6f6b", // lightRed + 92: "67f86f", // lightGreen + 93: "fffa72", // lightYellow + 94: "6a76fb", // lightBlue + 95: "fd7cfc", // lightMagenta + 96: "68fdfe", // lightCyan + 97: "ffffff", // lightWhite // - don't add bg color // 100: "686868", // lightBlack/darkGray // 101: "fd6f6b", // lightRed @@ -497,7 +518,7 @@ func Rgb2basic(r, g, b uint8, isBg bool) uint8 { return RgbToAnsi(r, g, b, isBg) } -// Rgb2ansi alias of the RgbToAnsi() +// Rgb2ansi convert RGB-code to 16-code, alias of the RgbToAnsi() func Rgb2ansi(r, g, b uint8, isBg bool) uint8 { return RgbToAnsi(r, g, b, isBg) } @@ -718,7 +739,9 @@ func RgbToHslInt(r, g, b uint8) []int { } // RgbToHsl Converts an RGB color value to HSL. Conversion formula +// // adapted from http://en.wikipedia.org/wiki/HSL_color_space. +// // Assumes r, g, and b are contained in the set [0, 255] and // returns h, s, and l in the set [0, 1]. func RgbToHsl(r, g, b uint8) []float64 {