Skip to content

Commit fe2b251

Browse files
committed
✨ feat: RGBColor add new method: ToFg(), ToBg() for quick convert
1 parent 74bb513 commit fe2b251

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

color_rgb.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
*************************************************************/
4545

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

6160
// create an empty RGBColor
@@ -251,6 +250,18 @@ func (c RGBColor) String() string {
251250
return ""
252251
}
253252

253+
// ToBg convert to background color
254+
func (c RGBColor) ToBg() RGBColor {
255+
c[3] = AsBg
256+
return c
257+
}
258+
259+
// ToFg convert to foreground color
260+
func (c RGBColor) ToFg() RGBColor {
261+
c[3] = AsFg
262+
return c
263+
}
264+
254265
// IsEmpty value
255266
func (c RGBColor) IsEmpty() bool {
256267
return c[3] > AsBg

color_rgb_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ func TestRGBColor(t *testing.T) {
2626
is.False(c.IsEmpty())
2727
is.Equal("48;2;204;204;204", c.FullCode())
2828
is.Equal("48;2;204;204;204", c.String())
29+
is.Equal("38;2;204;204;204", c.ToFg().FullCode())
2930

3031
// fg
3132
c = RGB(204, 204, 204)
3233
is.False(c.IsEmpty())
3334
is.Equal("38;2;204;204;204", c.FullCode())
3435
is.Equal("38;2;204;204;204", c.String())
36+
is.Equal("48;2;204;204;204", c.ToBg().FullCode())
3537

3638
// RGBColor.Sprint
3739
str := c.Sprint("msg")

0 commit comments

Comments
 (0)