Skip to content

Commit daca06f

Browse files
committed
up: update some comments and add more unit tests
1 parent f30c873 commit daca06f

7 files changed

+85
-21
lines changed

color_rgb.go

+15-18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type RGBColor [4]uint8
5757
var emptyRGBColor = RGBColor{3: 99}
5858

5959
// RGB color create.
60+
//
6061
// Usage:
6162
// c := RGB(30,144,255)
6263
// c := RGB(30,144,255, true)
@@ -106,15 +107,14 @@ func HEX(hex string, isBg ...bool) RGBColor {
106107
// Hex alias of the HEX()
107108
func Hex(hex string, isBg ...bool) RGBColor { return HEX(hex, isBg...) }
108109

110+
// RGBFromHEX quick RGBColor from hex string, alias of HEX()
111+
func RGBFromHEX(hex string, isBg ...bool) RGBColor { return HEX(hex, isBg...) }
112+
109113
// HSL create RGB color from a hsl value.
110114
// more see HslToRgb()
111115
func HSL(h, s, l float64, isBg ...bool) RGBColor {
112-
if rgb := HslToRgb(h, s, l); len(rgb) > 0 {
113-
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
114-
}
115-
116-
// mark is empty
117-
return emptyRGBColor
116+
rgb := HslToRgb(h, s, l)
117+
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
118118
}
119119

120120
// Hsl alias of the HSL()
@@ -123,12 +123,8 @@ func Hsl(h, s, l float64, isBg ...bool) RGBColor { return HSL(h, s, l, isBg...)
123123
// HSLInt create RGB color from a hsl int value.
124124
// more see HslIntToRgb()
125125
func HSLInt(h, s, l int, isBg ...bool) RGBColor {
126-
if rgb := HslIntToRgb(h, s, l); len(rgb) > 0 {
127-
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
128-
}
129-
130-
// mark is empty
131-
return emptyRGBColor
126+
rgb := HslIntToRgb(h, s, l)
127+
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
132128
}
133129

134130
// HslInt alias of the HSLInt()
@@ -140,7 +136,7 @@ func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColor {
140136
}
141137

142138
// RGBFromString create RGB color from a string.
143-
// support use color name in the {namedRgbMap}
139+
// Support use color name in the {namedRgbMap}
144140
//
145141
// Usage:
146142
// c := RGBFromString("170,187,204")
@@ -278,13 +274,13 @@ func (c RGBColor) C16() Color { return c.Basic() }
278274
* RGB Style
279275
*************************************************************/
280276

281-
// RGBStyle definition.
277+
// RGBStyle supports set foreground and background color
282278
//
283-
// Foreground/Background color
284279
// All are composed of 4 digits uint8, the first three digits are the color value;
285280
// The last bit is different from RGBColor, here it indicates whether the value is set.
286-
// - 1 Has been set
287-
// - ^1 Not set
281+
//
282+
// 1 Has been set
283+
// ^1 Not set
288284
type RGBStyle struct {
289285
// Name of the style
290286
Name string
@@ -305,6 +301,7 @@ func NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle {
305301
}
306302

307303
// HEXStyle create a RGBStyle from HEX color string.
304+
//
308305
// Usage:
309306
// s := HEXStyle("aabbcc", "eee")
310307
// s.Print("message")
@@ -317,11 +314,11 @@ func HEXStyle(fg string, bg ...string) *RGBStyle {
317314
if len(fg) > 0 {
318315
s.SetFg(HEX(fg))
319316
}
320-
321317
return s
322318
}
323319

324320
// RGBStyleFromString create a RGBStyle from color value string.
321+
//
325322
// Usage:
326323
// s := RGBStyleFromString("170,187,204", "70,87,4")
327324
// s.Print("message")

color_rgb_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ func TestRGBColor(t *testing.T) {
6464
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m\n", str)
6565
}
6666

67+
func TestRGBColor_set_reset(t *testing.T) {
68+
_, err := Reset()
69+
assert.NoError(t, err)
70+
71+
c := RGBFromHEX("6256CC")
72+
73+
assert.NoError(t, c.Set())
74+
fmt.Println("a message on True color set.")
75+
assert.NoError(t, c.Reset())
76+
fmt.Println()
77+
}
78+
6779
func TestRGBFromString(t *testing.T) {
6880
forceOpenColorRender()
6981
defer resetColorRender()

color_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ func TestColor_convert(t *testing.T) {
342342
assert.Equal(t, "106", HiCyan.ToBg().Code())
343343
assert.Equal(t, "93", BgHiYellow.ToFg().Code())
344344
assert.Equal(t, "105", BgHiMagenta.ToBg().Code())
345+
346+
assert.Equal(t, "5", OpBlink.ToFg().Code())
347+
assert.Equal(t, "5", OpBlink.ToBg().Code())
345348
}
346349

347350
func TestColor_C256(t *testing.T) {
@@ -536,6 +539,25 @@ func TestColor256_Print(t *testing.T) {
536539
buf.Reset()
537540
}
538541

542+
func TestColor256_conv(t *testing.T) {
543+
is := assert.New(t)
544+
c := C256(132)
545+
546+
is.Equal("132", c.C16().Code())
547+
}
548+
549+
func TestColor256_set_reset(t *testing.T) {
550+
_, err := Reset()
551+
assert.NoError(t, err)
552+
553+
c := C256(132)
554+
555+
assert.NoError(t, c.Set())
556+
fmt.Println("a message on color 256 set.")
557+
assert.NoError(t, c.Reset())
558+
fmt.Println()
559+
}
560+
539561
func TestColor256_AsBg(t *testing.T) {
540562
is := assert.New(t)
541563
c := C256(132)

convert.go

-2
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,9 @@ func RgbToHsl(r, g, b uint8) []float64 {
756756
min, max := ps[0], ps[2]
757757
// max := math.Max(math.Max(pr, pg), pb)
758758
// min := math.Min(math.Min(pr, pg), pb)
759-
760759
mid := (max + min) / 2
761760

762761
h, s, l := mid, mid, mid
763-
764762
if max == min {
765763
h, s = 0, 0 // achromatic
766764
} else {

convert_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,10 @@ func TestHslToRgb(t *testing.T) {
4646
assert.Equal(t, []int{0, 0, 66}, hslVal)
4747

4848
hslFVal := RgbToHsl(rgbVal[0], rgbVal[1], rgbVal[2])
49-
fmt.Println(hslFVal)
49+
fmt.Println("rgb:", rgbVal, "=> hsl:", hslFVal)
50+
assert.NotEmpty(t, hslFVal)
51+
52+
hslFVal = RgbToHsl(57, 187, 226)
53+
fmt.Println("rgb: 57, 187, 226 => hsl:", hslFVal)
54+
assert.NotEmpty(t, hslFVal)
5055
}

style_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func TestStyleFunc(t *testing.T) {
154154
buf := forceOpenColorRender()
155155
defer resetColorRender()
156156

157+
Infop("color message")
158+
assert.Equal(t, "\x1b[0;32mcolor message\x1b[0m", buf.String())
159+
buf.Reset()
160+
157161
Infoln("color message")
158162
assert.Equal(t, "\x1b[0;32mcolor message\x1b[0m\n", buf.String())
159163
buf.Reset()
@@ -162,6 +166,22 @@ func TestStyleFunc(t *testing.T) {
162166
assert.Equal(t, "\x1b[0;32mcolor message\x1b[0m", buf.String())
163167
buf.Reset()
164168

169+
Successp("color message")
170+
assert.Equal(t, "\x1b[1;32mcolor message\x1b[0m", buf.String())
171+
buf.Reset()
172+
173+
Successln("color message")
174+
assert.Equal(t, "\x1b[1;32mcolor message\x1b[0m\n", buf.String())
175+
buf.Reset()
176+
177+
Successf("color %s", "message")
178+
assert.Equal(t, "\x1b[1;32mcolor message\x1b[0m", buf.String())
179+
buf.Reset()
180+
181+
Warnp("color message")
182+
assert.Equal(t, "\x1b[1;33mcolor message\x1b[0m", buf.String())
183+
buf.Reset()
184+
165185
Warnln("color message")
166186
assert.Equal(t, "\x1b[1;33mcolor message\x1b[0m\n", buf.String())
167187
buf.Reset()
@@ -170,6 +190,10 @@ func TestStyleFunc(t *testing.T) {
170190
assert.Equal(t, "\x1b[1;33mcolor message\x1b[0m", buf.String())
171191
buf.Reset()
172192

193+
Errorp("color message")
194+
assert.Equal(t, "\x1b[97;41mcolor message\x1b[0m", buf.String())
195+
buf.Reset()
196+
173197
Errorln("color message")
174198
assert.Equal(t, "\x1b[97;41mcolor message\x1b[0m\n", buf.String())
175199
buf.Reset()

utils_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,9 @@ func TestRgbToAnsi(t *testing.T) {
423423
assert.Equal(t, item.want, Rgb2ansi(r, g, b, item.isBg))
424424
}
425425
}
426+
427+
func TestEnv_COLOR_DEBUG_MODE(t *testing.T) {
428+
mockEnvValue("COLOR_DEBUG_MODE", "on", func(_ string) {
429+
debugf("print debug message")
430+
})
431+
}

0 commit comments

Comments
 (0)