From 9027b9d2a5168ea482a8a8b46711191450514aa3 Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 20 Jul 2023 16:16:27 +0800 Subject: [PATCH] :sparkles: feat: Color add new method: IsBg(), IsFg(), IsOption() for check color --- color_16.go | 49 ++++++++++++++++++++++++++++++++++++++----------- color_256.go | 20 +++++++------------- color_test.go | 24 ++++++++++++++++++++++++ style.go | 6 ++++-- 4 files changed, 73 insertions(+), 26 deletions(-) diff --git a/color_16.go b/color_16.go index 0b70efe..eda226a 100644 --- a/color_16.go +++ b/color_16.go @@ -41,15 +41,27 @@ func (o Opts) String() string { * Basic 16 color definition *************************************************************/ -// Base value for foreground/background color -// base: fg 30~37, bg 40~47 -// light: fg 90~97, bg 100~107 +const ( + // OptMax max option value. range: 0 - 9 + OptMax = 10 + // DiffFgBg diff foreground and background color + DiffFgBg = 10 +) + +// Boundary value for foreground/background color 16 +// +// - base: fg 30~37, bg 40~47 +// - light: fg 90~97, bg 100~107 const ( FgBase uint8 = 30 + FgMax uint8 = 37 BgBase uint8 = 40 + BgMax uint8 = 47 HiFgBase uint8 = 90 + HiFgMax uint8 = 97 HiBgBase uint8 = 100 + HiBgMax uint8 = 107 ) // Foreground colors. basic foreground colors 30 - 37 @@ -94,7 +106,7 @@ const ( BgDefault Color = 49 ) -// Extra background color 100 - 107(非标准) +// Extra background color 100 - 107 (non-standard) const ( BgDarkGray Color = iota + 100 BgLightRed @@ -108,7 +120,7 @@ const ( BgGray Color = 100 ) -// Option settings +// Option settings. range: 0 - 9 const ( OpReset Color = iota // 0 重置所有设置 OpBold // 1 加粗 @@ -248,9 +260,9 @@ func (c Color) Println(a ...any) { doPrintlnV2(c.String(), a) } // lightCyan := Cyan.Light() // lightCyan.Print("message") func (c Color) Light() Color { - val := int(c) + val := uint8(c) if val >= 30 && val <= 47 { - return Color(uint8(c) + 60) + return Color(val + 60) } // don't change @@ -264,9 +276,9 @@ func (c Color) Light() Color { // cyan := LightCyan.Darken() // cyan.Print("message") func (c Color) Darken() Color { - val := int(c) + val := uint8(c) if val >= 90 && val <= 107 { - return Color(uint8(c) - 60) + return Color(val - 60) } // don't change @@ -324,7 +336,7 @@ func (c Color) RGB() RGBColor { return emptyRGBColor } - return HEX(Basic2hex(val)) + return HEX(Basic2hex(val), c.IsBg()) } // Code convert to code string. eg "35" @@ -337,8 +349,23 @@ func (c Color) String() string { return strconv.FormatInt(int64(c), 10) } +// IsBg check is background color +func (c Color) IsBg() bool { + val := uint8(c) + return val >= BgBase && val <= BgMax || val >= HiBgBase && val <= HiBgMax +} + +// IsFg check is foreground color +func (c Color) IsFg() bool { + val := uint8(c) + return val >= FgBase && val <= FgMax || val >= HiFgBase && val <= HiFgMax +} + +// IsOption check is option code: 0-9 +func (c Color) IsOption() bool { return uint8(c) < OptMax } + // IsValid color value -func (c Color) IsValid() bool { return c < 107 } +func (c Color) IsValid() bool { return uint8(c) < HiBgMax } /************************************************************* * basic color maps diff --git a/color_256.go b/color_256.go index 991e604..79ae5f8 100644 --- a/color_256.go +++ b/color_256.go @@ -43,7 +43,8 @@ const ( * 8bit(256) Color: Bit8Color Color256 *************************************************************/ -// Color256 256 color (8 bit), uint8 range at 0 - 255 +// Color256 256 color (8 bit), uint8 range at 0 - 255. +// Support 256 color on windows CMD, PowerShell // // 颜色值使用10进制和16进制都可 0x98 = 152 // @@ -54,10 +55,9 @@ const ( // // example: // -// fg color: [152, 0] -// bg color: [152, 1] +// fg color: [152, 0] +// bg color: [152, 1] // -// NOTICE: now support 256 color on windows CMD, PowerShell // lint warn - Name starts with package name type Color256 [2]uint8 type Bit8Color = Color256 // alias @@ -164,9 +164,7 @@ func (c Color256) String() string { } // IsFg color -func (c Color256) IsFg() bool { - return c[1] == AsFg -} +func (c Color256) IsFg() bool { return c[1] == AsFg } // ToFg 256 color func (c Color256) ToFg() Color256 { @@ -175,9 +173,7 @@ func (c Color256) ToFg() Color256 { } // IsBg color -func (c Color256) IsBg() bool { - return c[1] == AsBg -} +func (c Color256) IsBg() bool { return c[1] == AsBg } // ToBg 256 color func (c Color256) ToBg() Color256 { @@ -186,9 +182,7 @@ func (c Color256) ToBg() Color256 { } // IsEmpty value -func (c Color256) IsEmpty() bool { - return c[1] > 1 -} +func (c Color256) IsEmpty() bool { return c[1] > 1 } /************************************************************* * 8bit(256) Style diff --git a/color_test.go b/color_test.go index 08c7abb..bea38ea 100644 --- a/color_test.go +++ b/color_test.go @@ -334,6 +334,24 @@ func TestColor16(t *testing.T) { is.True(ok) } +func TestColor_check(t *testing.T) { + assert.True(t, Cyan.IsValid()) + assert.True(t, BgCyan.IsValid()) + + // is fg + assert.True(t, Cyan.IsFg()) + assert.False(t, BgCyan.IsFg()) + + // is bg + assert.False(t, Cyan.IsBg()) + assert.True(t, BgCyan.IsBg()) + + // is option + assert.False(t, Cyan.IsOption()) + assert.False(t, BgCyan.IsOption()) + assert.True(t, Bold.IsOption()) +} + func TestColor_convert(t *testing.T) { assert.Equal(t, "36", Cyan.ToFg().Code()) assert.Equal(t, "46", Cyan.ToBg().Code()) @@ -344,6 +362,12 @@ func TestColor_convert(t *testing.T) { assert.Equal(t, "93", BgHiYellow.ToFg().Code()) assert.Equal(t, "105", BgHiMagenta.ToBg().Code()) + assert.Equal(t, "104;253;254", HiCyan.RGB().Code()) + assert.Equal(t, "38;2;104;253;254", HiCyan.RGB().FullCode()) + + assert.Equal(t, "104;253;254", HiCyan.ToBg().RGB().Code()) + assert.Equal(t, "48;2;104;253;254", HiCyan.ToBg().RGB().FullCode()) + assert.Equal(t, "5", OpBlink.ToFg().Code()) assert.Equal(t, "5", OpBlink.ToBg().Code()) } diff --git a/style.go b/style.go index a009d1d..353d39f 100644 --- a/style.go +++ b/style.go @@ -37,7 +37,8 @@ func (s *Style) Add(cs ...Color) { *s = append(*s, cs...) } -// Render render text +// Render colored text +// // Usage: // // color.New(color.FgGreen).Render("text") @@ -46,8 +47,9 @@ func (s Style) Render(a ...any) string { return RenderCode(s.String(), a...) } -// Renderln render text line. +// Renderln render text with newline. // like Println, will add spaces for each argument +// // Usage: // // color.New(color.FgGreen).Renderln("text", "more")