Skip to content

Commit

Permalink
chore: update some func comments, fix some code style
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 28, 2022
1 parent 2f5b36d commit f30c873
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 68 deletions.
16 changes: 5 additions & 11 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -76,7 +70,7 @@ var (
)

// TermColorLevel value on current ENV
func TermColorLevel() terminfo.ColorLevel {
func TermColorLevel() Level {
return colorLevel
}

Expand Down
22 changes: 11 additions & 11 deletions color_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
// }
11 changes: 3 additions & 8 deletions color_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -209,6 +204,7 @@ type Style256 struct {
}

// S256 create a color256 style
//
// Usage:
// s := color.S256()
// s := color.S256(132) // fg
Expand Down Expand Up @@ -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, ";")
}
21 changes: 0 additions & 21 deletions color_rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*************************************************************/
Expand Down
57 changes: 40 additions & 17 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f30c873

Please sign in to comment.