Skip to content

Commit 9027b9d

Browse files
committed
✨ feat: Color add new method: IsBg(), IsFg(), IsOption() for check color
1 parent fe2b251 commit 9027b9d

File tree

4 files changed

+73
-26
lines changed

4 files changed

+73
-26
lines changed

color_16.go

+38-11
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,27 @@ func (o Opts) String() string {
4141
* Basic 16 color definition
4242
*************************************************************/
4343

44-
// Base value for foreground/background color
45-
// base: fg 30~37, bg 40~47
46-
// light: fg 90~97, bg 100~107
44+
const (
45+
// OptMax max option value. range: 0 - 9
46+
OptMax = 10
47+
// DiffFgBg diff foreground and background color
48+
DiffFgBg = 10
49+
)
50+
51+
// Boundary value for foreground/background color 16
52+
//
53+
// - base: fg 30~37, bg 40~47
54+
// - light: fg 90~97, bg 100~107
4755
const (
4856
FgBase uint8 = 30
57+
FgMax uint8 = 37
4958
BgBase uint8 = 40
59+
BgMax uint8 = 47
5060

5161
HiFgBase uint8 = 90
62+
HiFgMax uint8 = 97
5263
HiBgBase uint8 = 100
64+
HiBgMax uint8 = 107
5365
)
5466

5567
// Foreground colors. basic foreground colors 30 - 37
@@ -94,7 +106,7 @@ const (
94106
BgDefault Color = 49
95107
)
96108

97-
// Extra background color 100 - 107(非标准)
109+
// Extra background color 100 - 107 (non-standard)
98110
const (
99111
BgDarkGray Color = iota + 100
100112
BgLightRed
@@ -108,7 +120,7 @@ const (
108120
BgGray Color = 100
109121
)
110122

111-
// Option settings
123+
// Option settings. range: 0 - 9
112124
const (
113125
OpReset Color = iota // 0 重置所有设置
114126
OpBold // 1 加粗
@@ -248,9 +260,9 @@ func (c Color) Println(a ...any) { doPrintlnV2(c.String(), a) }
248260
// lightCyan := Cyan.Light()
249261
// lightCyan.Print("message")
250262
func (c Color) Light() Color {
251-
val := int(c)
263+
val := uint8(c)
252264
if val >= 30 && val <= 47 {
253-
return Color(uint8(c) + 60)
265+
return Color(val + 60)
254266
}
255267

256268
// don't change
@@ -264,9 +276,9 @@ func (c Color) Light() Color {
264276
// cyan := LightCyan.Darken()
265277
// cyan.Print("message")
266278
func (c Color) Darken() Color {
267-
val := int(c)
279+
val := uint8(c)
268280
if val >= 90 && val <= 107 {
269-
return Color(uint8(c) - 60)
281+
return Color(val - 60)
270282
}
271283

272284
// don't change
@@ -324,7 +336,7 @@ func (c Color) RGB() RGBColor {
324336
return emptyRGBColor
325337
}
326338

327-
return HEX(Basic2hex(val))
339+
return HEX(Basic2hex(val), c.IsBg())
328340
}
329341

330342
// Code convert to code string. eg "35"
@@ -337,8 +349,23 @@ func (c Color) String() string {
337349
return strconv.FormatInt(int64(c), 10)
338350
}
339351

352+
// IsBg check is background color
353+
func (c Color) IsBg() bool {
354+
val := uint8(c)
355+
return val >= BgBase && val <= BgMax || val >= HiBgBase && val <= HiBgMax
356+
}
357+
358+
// IsFg check is foreground color
359+
func (c Color) IsFg() bool {
360+
val := uint8(c)
361+
return val >= FgBase && val <= FgMax || val >= HiFgBase && val <= HiFgMax
362+
}
363+
364+
// IsOption check is option code: 0-9
365+
func (c Color) IsOption() bool { return uint8(c) < OptMax }
366+
340367
// IsValid color value
341-
func (c Color) IsValid() bool { return c < 107 }
368+
func (c Color) IsValid() bool { return uint8(c) < HiBgMax }
342369

343370
/*************************************************************
344371
* basic color maps

color_256.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const (
4343
* 8bit(256) Color: Bit8Color Color256
4444
*************************************************************/
4545

46-
// Color256 256 color (8 bit), uint8 range at 0 - 255
46+
// Color256 256 color (8 bit), uint8 range at 0 - 255.
47+
// Support 256 color on windows CMD, PowerShell
4748
//
4849
// 颜色值使用10进制和16进制都可 0x98 = 152
4950
//
@@ -54,10 +55,9 @@ const (
5455
//
5556
// example:
5657
//
57-
// fg color: [152, 0]
58-
// bg color: [152, 1]
58+
// fg color: [152, 0]
59+
// bg color: [152, 1]
5960
//
60-
// NOTICE: now support 256 color on windows CMD, PowerShell
6161
// lint warn - Name starts with package name
6262
type Color256 [2]uint8
6363
type Bit8Color = Color256 // alias
@@ -164,9 +164,7 @@ func (c Color256) String() string {
164164
}
165165

166166
// IsFg color
167-
func (c Color256) IsFg() bool {
168-
return c[1] == AsFg
169-
}
167+
func (c Color256) IsFg() bool { return c[1] == AsFg }
170168

171169
// ToFg 256 color
172170
func (c Color256) ToFg() Color256 {
@@ -175,9 +173,7 @@ func (c Color256) ToFg() Color256 {
175173
}
176174

177175
// IsBg color
178-
func (c Color256) IsBg() bool {
179-
return c[1] == AsBg
180-
}
176+
func (c Color256) IsBg() bool { return c[1] == AsBg }
181177

182178
// ToBg 256 color
183179
func (c Color256) ToBg() Color256 {
@@ -186,9 +182,7 @@ func (c Color256) ToBg() Color256 {
186182
}
187183

188184
// IsEmpty value
189-
func (c Color256) IsEmpty() bool {
190-
return c[1] > 1
191-
}
185+
func (c Color256) IsEmpty() bool { return c[1] > 1 }
192186

193187
/*************************************************************
194188
* 8bit(256) Style

color_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,24 @@ func TestColor16(t *testing.T) {
334334
is.True(ok)
335335
}
336336

337+
func TestColor_check(t *testing.T) {
338+
assert.True(t, Cyan.IsValid())
339+
assert.True(t, BgCyan.IsValid())
340+
341+
// is fg
342+
assert.True(t, Cyan.IsFg())
343+
assert.False(t, BgCyan.IsFg())
344+
345+
// is bg
346+
assert.False(t, Cyan.IsBg())
347+
assert.True(t, BgCyan.IsBg())
348+
349+
// is option
350+
assert.False(t, Cyan.IsOption())
351+
assert.False(t, BgCyan.IsOption())
352+
assert.True(t, Bold.IsOption())
353+
}
354+
337355
func TestColor_convert(t *testing.T) {
338356
assert.Equal(t, "36", Cyan.ToFg().Code())
339357
assert.Equal(t, "46", Cyan.ToBg().Code())
@@ -344,6 +362,12 @@ func TestColor_convert(t *testing.T) {
344362
assert.Equal(t, "93", BgHiYellow.ToFg().Code())
345363
assert.Equal(t, "105", BgHiMagenta.ToBg().Code())
346364

365+
assert.Equal(t, "104;253;254", HiCyan.RGB().Code())
366+
assert.Equal(t, "38;2;104;253;254", HiCyan.RGB().FullCode())
367+
368+
assert.Equal(t, "104;253;254", HiCyan.ToBg().RGB().Code())
369+
assert.Equal(t, "48;2;104;253;254", HiCyan.ToBg().RGB().FullCode())
370+
347371
assert.Equal(t, "5", OpBlink.ToFg().Code())
348372
assert.Equal(t, "5", OpBlink.ToBg().Code())
349373
}

style.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func (s *Style) Add(cs ...Color) {
3737
*s = append(*s, cs...)
3838
}
3939

40-
// Render render text
40+
// Render colored text
41+
//
4142
// Usage:
4243
//
4344
// color.New(color.FgGreen).Render("text")
@@ -46,8 +47,9 @@ func (s Style) Render(a ...any) string {
4647
return RenderCode(s.String(), a...)
4748
}
4849

49-
// Renderln render text line.
50+
// Renderln render text with newline.
5051
// like Println, will add spaces for each argument
52+
//
5153
// Usage:
5254
//
5355
// color.New(color.FgGreen).Renderln("text", "more")

0 commit comments

Comments
 (0)