Skip to content

Commit

Permalink
feat: add more convert methods on Color
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 17, 2021
1 parent 5f87df4 commit 3986cfd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: true
matrix:
go: [1.14]
go: [1.16]

steps:
- name: Checkout
Expand Down
46 changes: 45 additions & 1 deletion color_16.go
Expand Up @@ -42,10 +42,12 @@ func (o Opts) String() string {
*************************************************************/

// Base value for foreground/background color
// base: fg 30~37, bg 40~47
// light: fg 90~97, bg 100~107
const (
FgBase uint8 = 30
BgBase uint8 = 40
// hi color base code

HiFgBase uint8 = 90
HiBgBase uint8 = 100
)
Expand Down Expand Up @@ -240,6 +242,7 @@ func (c Color) Println(a ...interface{}) {
}

// Light current color. eg: 36(FgCyan) -> 96(FgLightCyan).
//
// Usage:
// lightCyan := Cyan.Light()
// lightCyan.Print("message")
Expand All @@ -254,6 +257,7 @@ func (c Color) Light() Color {
}

// Darken current color. eg. 96(FgLightCyan) -> 36(FgCyan)
//
// Usage:
// cyan := LightCyan.Darken()
// cyan.Print("message")
Expand Down Expand Up @@ -291,6 +295,26 @@ func (c Color) C256() Color256 {
return Color256{val}
}

// ToFg always convert fg
func (c Color) ToFg() Color {
val := uint8(c)
// is option code, don't change
if val < 10 {
return c
}
return Color(Bg2Fg(val))
}

// ToBg always convert bg
func (c Color) ToBg() Color {
val := uint8(c)
// is option code, don't change
if val < 10 {
return c
}
return Color(Fg2Bg(val))
}

// RGB convert 16 color to 256-color code.
func (c Color) RGB() RGBColor {
val := uint8(c)
Expand Down Expand Up @@ -426,6 +450,26 @@ var (
}
)

// Bg2Fg bg color value to fg value
func Bg2Fg(val uint8) uint8 {
if val >= BgBase && val <= 47 { // is bg
val = val - 10
} else if val >= HiBgBase && val <= 107 { // is hi bg
val = val - 10
}
return val
}

// Fg2Bg fg color value to bg value
func Fg2Bg(val uint8) uint8 {
if val >= FgBase && val <= 37 { // is fg
val = val + 10
} else if val >= HiFgBase && val <= 97 { // is hi fg
val = val + 10
}
return val
}

// Basic2nameMap data
func Basic2nameMap() map[uint8]string {
return basic2nameMap
Expand Down
11 changes: 11 additions & 0 deletions color_test.go
Expand Up @@ -333,6 +333,17 @@ func TestColor16(t *testing.T) {
is.True(ok)
}

func TestColor_convert(t *testing.T) {
assert.Equal(t, "36", Cyan.ToFg().Code())
assert.Equal(t, "46", Cyan.ToBg().Code())
assert.Equal(t, "46", BgCyan.ToBg().Code())
assert.Equal(t, "36", BgCyan.ToFg().Code())

assert.Equal(t, "106", HiCyan.ToBg().Code())
assert.Equal(t, "93", BgHiYellow.ToFg().Code())
assert.Equal(t, "105", BgHiMagenta.ToBg().Code())
}

func TestColor_C256(t *testing.T) {
assert.True(t, Bold.C256().IsEmpty())

Expand Down
44 changes: 26 additions & 18 deletions convert.go
Expand Up @@ -40,14 +40,15 @@ var (
35: "c839c5", // magenta
36: "20c5c6", // cyan
37: "c7c7c7", // white
40: "000000", // black
41: "c51e14", // red
42: "1dc121", // green
43: "c7c329", // yellow
44: "0a2fc4", // blue
45: "c839c5", // magenta
46: "20c5c6", // cyan
47: "c7c7c7", // white
// - don't add bg color
// 40: "000000", // black
// 41: "c51e14", // red
// 42: "1dc121", // green
// 43: "c7c329", // yellow
// 44: "0a2fc4", // blue
// 45: "c839c5", // magenta
// 46: "20c5c6", // cyan
// 47: "c7c7c7", // white
90: "686868", // lightBlack/darkGray
91: "fd6f6b", // lightRed
92: "67f86f", // lightGreen
Expand All @@ -56,14 +57,15 @@ var (
95: "fd7cfc", // lightMagenta
96: "68fdfe", // lightCyan
97: "ffffff", // lightWhite
100: "686868", // lightBlack/darkGray
101: "fd6f6b", // lightRed
102: "67f86f", // lightGreen
103: "fffa72", // lightYellow
104: "6a76fb", // lightBlue
105: "fd7cfc", // lightMagenta
106: "68fdfe", // lightCyan
107: "ffffff", // lightWhite
// - don't add bg color
// 100: "686868", // lightBlack/darkGray
// 101: "fd6f6b", // lightRed
// 102: "67f86f", // lightGreen
// 103: "fffa72", // lightYellow
// 104: "6a76fb", // lightBlue
// 105: "fd7cfc", // lightMagenta
// 106: "68fdfe", // lightCyan
// 107: "ffffff", // lightWhite
}
// will convert data from basic2hexMap
hex2basicMap = initHex2basicMap()
Expand Down Expand Up @@ -467,12 +469,18 @@ func RgbToHex(rgb []int) string {

// Basic2hex convert basic color to hex string.
func Basic2hex(val uint8) string {
val = Fg2Bg(val)
return basic2hexMap[val]
}

// Hex2basic convert hex string to basic color code.
func Hex2basic(hex string) uint8 {
return hex2basicMap[hex]
func Hex2basic(hex string, asBg ...bool) uint8 {
val := hex2basicMap[hex]

if len(asBg) > 0 && asBg[0] {
return Fg2Bg(val)
}
return val
}

// Rgb2basic alias of the RgbToAnsi()
Expand Down
3 changes: 2 additions & 1 deletion convert_test.go
Expand Up @@ -14,7 +14,8 @@ func TestRgb2basic(t *testing.T) {
}

func TestHex2basic(t *testing.T) {
assert.Equal(t, uint8(105), Hex2basic("fd7cfc"))
assert.Equal(t, uint8(95), Hex2basic("fd7cfc"))
assert.Equal(t, uint8(105), Hex2basic("fd7cfc", true))
}

func TestHslToRgb(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions utils.go
Expand Up @@ -120,13 +120,13 @@ func Text(s string) string {
}

// Uint8sToInts convert []uint8 to []int
func Uint8sToInts(u8s []uint8 ) []int {
ints := make([]int, len(u8s))
for i, u8 := range u8s {
ints[i] = int(u8)
}
return ints
}
// func Uint8sToInts(u8s []uint8 ) []int {
// ints := make([]int, len(u8s))
// for i, u8 := range u8s {
// ints[i] = int(u8)
// }
// return ints
// }

/*************************************************************
* helper methods for print
Expand Down

0 comments on commit 3986cfd

Please sign in to comment.