Skip to content

Commit

Permalink
✨ feat: Color add new method: IsBg(), IsFg(), IsOption() for check color
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 20, 2023
1 parent fe2b251 commit 9027b9d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
49 changes: 38 additions & 11 deletions color_16.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -108,7 +120,7 @@ const (
BgGray Color = 100
)

// Option settings
// Option settings. range: 0 - 9
const (
OpReset Color = iota // 0 重置所有设置
OpBold // 1 加粗
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
20 changes: 7 additions & 13 deletions color_256.go
Expand Up @@ -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
//
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions color_test.go
Expand Up @@ -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())
Expand All @@ -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())
}
Expand Down
6 changes: 4 additions & 2 deletions style.go
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 9027b9d

Please sign in to comment.