Skip to content

Commit

Permalink
✨ feat: RGBColor add new method: ToFg(), ToBg() for quick convert
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 20, 2023
1 parent 74bb513 commit fe2b251
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 13 additions & 2 deletions color_rgb.go
Expand Up @@ -44,6 +44,7 @@ const (
*************************************************************/

// RGBColor definition.
// Support RGB color on Windows CMD, PowerShell
//
// The first to third digits represent the color value.
// The last digit represents the foreground(0), background(1), >1 is unset value
Expand All @@ -54,8 +55,6 @@ const (
// // 3rd: Fg=0, Bg=1, >1: unset value
// RGBColor{30,144,255, 0}
// RGBColor{30,144,255, 1}
//
// NOTICE: now support RGB color on Windows CMD, PowerShell
type RGBColor [4]uint8

// create an empty RGBColor
Expand Down Expand Up @@ -251,6 +250,18 @@ func (c RGBColor) String() string {
return ""
}

// ToBg convert to background color
func (c RGBColor) ToBg() RGBColor {
c[3] = AsBg
return c
}

// ToFg convert to foreground color
func (c RGBColor) ToFg() RGBColor {
c[3] = AsFg
return c
}

// IsEmpty value
func (c RGBColor) IsEmpty() bool {
return c[3] > AsBg
Expand Down
2 changes: 2 additions & 0 deletions color_rgb_test.go
Expand Up @@ -26,12 +26,14 @@ func TestRGBColor(t *testing.T) {
is.False(c.IsEmpty())
is.Equal("48;2;204;204;204", c.FullCode())
is.Equal("48;2;204;204;204", c.String())
is.Equal("38;2;204;204;204", c.ToFg().FullCode())

// fg
c = RGB(204, 204, 204)
is.False(c.IsEmpty())
is.Equal("38;2;204;204;204", c.FullCode())
is.Equal("38;2;204;204;204", c.String())
is.Equal("48;2;204;204;204", c.ToBg().FullCode())

// RGBColor.Sprint
str := c.Sprint("msg")
Expand Down

0 comments on commit fe2b251

Please sign in to comment.