Skip to content

Commit

Permalink
add more tests and update some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 9, 2021
1 parent befe633 commit 8ce765b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 31 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Now, 256 colors and RGB colors have also been supported to work in Windows CMD a
- Basic colors: `Bold`, `Black`, `White`, `Gray`, `Red`, `Green`, `Yellow`, `Blue`, `Magenta`, `Cyan`
- Additional styles: `Info`, `Note`, `Light`, `Error`, `Danger`, `Notice`, `Success`, `Comment`, `Primary`, `Warning`, `Question`, `Secondary`
- Support by set `NO_COLOR` for disable color or use `FORCE_COLOR` for force open color render.
- Support Rgb, 256, 16 color conversion

## GoDoc

Expand Down Expand Up @@ -405,9 +406,26 @@ s.Println("style with options")
s.Printf("style with %s\n", "options")
```

## Color convert

Supports conversion between Rgb, 256, 16 colors, `Rgb <=> 256 <=> 16`

```go
basic := color.Red
basic.Println("basic color")

c256 := color.Red.C256()
c256.Println("256 color")
c256.C16().Println("basic color")

rgb := color.Red.RGB()
rgb.Println("rgb color")
rgb.C256().Println("256 color")
```

## Func refer

there are some useful functions reference
There are some useful functions reference

- `Disable()` disable color render
- `SetOutput(io.Writer)` custom set the colored text output writer
Expand Down
18 changes: 18 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用
- 基础色彩: `Bold` `Black` `White` `Gray` `Red` `Green` `Yellow` `Blue` `Magenta` `Cyan`
- 扩展风格: `Info` `Note` `Light` `Error` `Danger` `Notice` `Success` `Comment` `Primary` `Warning` `Question` `Secondary`
- 支持通过设置环境变量 `NO_COLOR` 来禁用色彩,或者使用 `FORCE_COLOR` 来强制使用色彩渲染.
- 支持 Rgb, 256, 16 色彩之间的互相转换
- 支持Linux、Mac,同时兼容Windows系统环境

## GoDoc
Expand Down Expand Up @@ -374,6 +375,23 @@ s.Println("style with options")
s.Printf("style with %s\n", "options")
```

## 颜色转换

支持 Rgb, 256, 16 色彩之间的互相转换 `Rgb <=> 256 <=> 16`

```go
basic := color.Red
basic.Println("basic color")

c256 := color.Red.C256()
c256.Println("256 color")
c256.C16().Println("basic color")

rgb := color.Red.RGB()
rgb.Println("rgb color")
rgb.C256().Println("256 color")
```

## 方法参考

一些有用的工具方法参考
Expand Down
37 changes: 28 additions & 9 deletions color_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package color

import (
"fmt"
"strconv"
"strings"
)

Expand Down Expand Up @@ -31,6 +32,8 @@ from wikipedia, 256 color:
const (
TplFg256 = "38;5;%d"
TplBg256 = "48;5;%d"
Fg256Pfx = "38;5;"
Bg256Pfx = "48;5;"
)

/*************************************************************
Expand Down Expand Up @@ -108,9 +111,24 @@ func (c Color256) Sprintf(format string, a ...interface{}) string {
return RenderString(c.String(), fmt.Sprintf(format, a...))
}

// C16 convert color-256 to 16 color.
func (c Color256) C16() Color {
return c.Basic()
}

// Basic convert color-256 to basic 16 color.
func (c Color256) Basic() Color {
return Color(c[0]) // TODO
}

// RGB convert color-256 to RGB color.
func (c Color256) RGB() RGBColor {
return RGBFromSlice(C256ToRgb(c[0]), c[1] == AsBg)
}

// RGBColor convert color-256 to RGB color.
func (c Color256) RGBColor() RGBColor {
return RGBFromSlice(C256ToRgb(c[0]), c[1] == AsBg)
return c.RGB()
}

// Value return color value
Expand All @@ -126,24 +144,25 @@ func (c Color256) Code() string {
// String convert to color code string. eg: "38;5;12"
func (c Color256) String() string {
if c[1] == AsFg { // 0 is Fg
return fmt.Sprintf(TplFg256, c[0])
// return fmt.Sprintf(TplFg256, c[0])
return Fg256Pfx + strconv.Itoa(int(c[0]))
}

if c[1] == AsBg { // 1 is Bg
return fmt.Sprintf(TplBg256, c[0])
// return fmt.Sprintf(TplBg256, c[0])
return Bg256Pfx + strconv.Itoa(int(c[0]))
}

// empty
return ""
return "" // empty
}

// IsFg color
func (c Color256) IsFg() bool {
return c[1] == AsFg
}

// AsFg color
func (c Color256) AsFg() Color256 {
// ToFg 256 color
func (c Color256) ToFg() Color256 {
c[1] = AsFg
return c
}
Expand All @@ -153,8 +172,8 @@ func (c Color256) IsBg() bool {
return c[1] == AsBg
}

// AsBg color
func (c Color256) AsBg() Color256 {
// ToBg 256 color
func (c Color256) ToBg() Color256 {
c[1] = AsBg
return c
}
Expand Down
11 changes: 7 additions & 4 deletions color_rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,20 @@ func (c RGBColor) IsEmpty() bool {

// C256 returns the closest approximate 256 (8 bit) color
func (c RGBColor) C256() Color256 {
return C256(Rgb2short(c[0], c[1], c[2]), c[3] == AsBg)
return C256(RgbTo256(c[0], c[1], c[2]), c[3] == AsBg)
}

// Color returns the closest approximate 16 (4 bit) color
func (c RGBColor) Color() Color {
// Basic returns the closest approximate 16 (4 bit) color
func (c RGBColor) Basic() Color {
// return Color(RgbToAnsi(c[0], c[1], c[2], c[3] == AsBg))
return Color(Rgb2basic(c[0], c[1], c[2], c[3] == AsBg))
}

// Color returns the closest approximate 16 (4 bit) color
func (c RGBColor) Color() Color { return c.Basic() }

// C16 returns the closest approximate 16 (4 bit) color
func (c RGBColor) C16() Color { return c.Color() }
func (c RGBColor) C16() Color { return c.Basic() }

/*************************************************************
* RGB Style
Expand Down
2 changes: 2 additions & 0 deletions color_rgb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ func TestRgbToC256Background(t *testing.T) {
func TestRGBColor_C16(t *testing.T) {
rgb := RGB(57, 187, 226)
assert.Equal(t, "36", rgb.C16().String())
assert.Equal(t, "36", rgb.Color().String())

rgb = RGB(57, 187, 226, true)
assert.Equal(t, "46", rgb.C16().String())
assert.Equal(t, "46", rgb.Color().String())
}
31 changes: 28 additions & 3 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ func TestSet(t *testing.T) {
num, err = Set(FgGreen)
is.Equal(0, num)
is.NoError(err)
fmt.Println("set fg is green")
fmt.Print("set fg is green")
_, err = Reset()
is.NoError(err)
fmt.Println()

if runtime.GOOS == "windows" {
fd := uintptr(syscall.Stdout)
Expand Down Expand Up @@ -441,14 +442,15 @@ func TestQuickFunc(t *testing.T) {
* test 256 color
*************************************************************/

func TestColor256(t *testing.T) {
func TestColor256_Print(t *testing.T) {
is := assert.New(t)
c := Bit8(132)
c.Print("c256 message")
is.Equal(uint8(132), c.Value())
is.Equal("38;5;132", c.String())
rgb := c.RGBColor()
rgb.Print(" => to rgb message\n")
rgb.Print(" => to rgb message")
fmt.Println()
is.Equal([]int{175, 95, 135}, rgb.Values())
is.Equal("38;2;175;95;135", rgb.String())

Expand Down Expand Up @@ -501,6 +503,29 @@ func TestColor256(t *testing.T) {
buf.Reset()
}

func TestColor256_AsBg(t *testing.T) {
is := assert.New(t)
c := C256(132)
c.Println("c256: fg-132")
is.False(c.IsBg())
is.True(c.IsFg())

c = c.ToBg()
is.True(c.IsBg())
is.False(c.IsFg())
c.Println("c256: fg-132 to bg")

c = C256(132, true)
c.Println("c256: 132 bg")
is.True(c.IsBg())
is.False(c.IsFg())

c = c.ToFg()
is.False(c.IsBg())
is.True(c.IsFg())
c.Println("c256: bg-132 to fg")
}

func TestStyle256(t *testing.T) {
is := assert.New(t)

Expand Down
32 changes: 18 additions & 14 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,6 @@ func RgbTo256Table() map[string]uint8 {
return hexTo256Table
}

/*************************************************************
* helper methods for converts
*************************************************************/

// Colors2code convert colors to code. return like "32;45;3"
func Colors2code(colors ...Color) string {
if len(colors) == 0 {
Expand All @@ -379,13 +375,18 @@ func Colors2code(colors ...Color) string {
return strings.Join(codes, ";")
}

/*************************************************************
* HEX code <=> RGB/True color code
*************************************************************/

// Hex2rgb alias of the HexToRgb()
func Hex2rgb(hex string) []int { return HexToRgb(hex) }

// HexToRGB alias of the HexToRgb()
func HexToRGB(hex string) []int { return HexToRgb(hex) }

// HexToRgb convert hex color string to RGB numbers
//
// Usage:
// rgb := HexToRgb("ccc") // rgb: [204 204 204]
// rgb := HexToRgb("aabbcc") // rgb: [170 187 204]
Expand Down Expand Up @@ -444,6 +445,10 @@ func RgbToHex(rgb []int) string {
return strings.Join(hexNodes, "")
}

/*************************************************************
* 4bit(16) color <=> RGB/True color
*************************************************************/

// Basic2hex convert basic color to hex string.
func Basic2hex(val uint8) string {
return basic2hexMap[val]
Expand Down Expand Up @@ -509,17 +514,20 @@ func RgbToAnsi(r, g, b uint8, isBg bool) uint8 {
} else {
c += compareVal(b > 0, 4, 0)
}

return base + bright + c
}

// RgbTo256 convert RGB-code to 256-code
func RgbTo256(r, g, b uint8) uint8 {
return Rgb2short(r, g, b)
}
/*************************************************************
* 8bit(256) color <=> RGB/True color
*************************************************************/

// Rgb2short convert RGB-code to 256-code
func Rgb2short(r, g, b uint8) uint8 {
return RgbTo256(r, g, b)
}

// RgbTo256 convert RGB-code to 256-code
func RgbTo256(r, g, b uint8) uint8 {
res := make([]uint8, 3)
for partI, part := range [3]uint8{r, g, b} {
i := 0
Expand Down Expand Up @@ -547,11 +555,7 @@ func Rgb2short(r, g, b uint8) uint8 {

// C256ToRgb convert an 256 color code to RGB numbers
func C256ToRgb(val uint8) (rgb []uint8) {
hex, ok := c256ToHexMap[val]
if false == ok {
return
}

hex := c256ToHexMap[val]
// convert to rgb code
rgbInts := Hex2rgb(hex)

Expand Down
2 changes: 2 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,6 @@ func TestRgbToAnsi(t *testing.T) {

func TestRgb2basic(t *testing.T) {
assert.Equal(t, uint8(31), Rgb2basic(134, 56, 56, false))
assert.Equal(t, uint8(41), Rgb2basic(134, 56, 56, true))
assert.Equal(t, uint8(46), Rgb2basic(57, 187, 226, true))
}

0 comments on commit 8ce765b

Please sign in to comment.