Skip to content

Commit

Permalink
close: #29 support add options with 256, rgb style
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 2, 2020
1 parent 909b6a3 commit 2a527f3
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 45 deletions.
34 changes: 32 additions & 2 deletions color_16.go
Expand Up @@ -2,13 +2,41 @@ package color

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

// Color Color16, 16 color value type
// 3(2^3=8) OR 4(2^4=16) bite color.
type Color uint8

// Opts basic color options. code: 0 - 9
type Opts []Color

// Add option value
func (o *Opts) Add(ops ...Color) {
for _, op := range ops {
if uint8(op) < 10 {
*o = append(*o, op)
}
}
}

// IsValid options
func (o Opts) IsValid() bool {
return len(o) > 0
}

// IsEmpty options
func (o Opts) IsEmpty() bool {
return len(o) == 0
}

// String options to string. eg: "1;3"
func (o Opts) String() string {
return colors2code(o...)
}

/*************************************************************
* Basic 16 color definition
*************************************************************/
Expand Down Expand Up @@ -202,12 +230,14 @@ func (c Color) Darken() Color {

// Code convert to code string. eg "35"
func (c Color) Code() string {
return fmt.Sprintf("%d", c)
// return fmt.Sprintf("%d", c)
return strconv.Itoa(int(c))
}

// String convert to code string. eg "35"
func (c Color) String() string {
return fmt.Sprintf("%d", c)
// return fmt.Sprintf("%d", c)
return strconv.Itoa(int(c))
}

// IsValid color value
Expand Down
26 changes: 23 additions & 3 deletions color_256.go
Expand Up @@ -128,9 +128,12 @@ func (c Color256) IsEmpty() bool {
// 都是由两位uint8组成, 第一位是色彩值;
// 第二位与Bit8Color不一样的是,在这里表示是否设置了值 0 未设置 ^0 已设置
type Style256 struct {
p *Printer
// p Printer

// Name of the style
Name string
// color options of the style
opts Opts
// fg and bg color
fg, bg Color256
}
Expand All @@ -155,9 +158,10 @@ func S256(fgAndBg ...uint8) *Style256 {
}

// Set fg and bg color value
func (s *Style256) Set(fgVal, bgVal uint8) *Style256 {
func (s *Style256) Set(fgVal, bgVal uint8, ops ...Color) *Style256 {
s.fg = Color256{fgVal, 1}
s.bg = Color256{bgVal, 1}
s.opts.Add(ops...)
return s
}

Expand All @@ -173,7 +177,19 @@ func (s *Style256) SetFg(fgVal uint8) *Style256 {
return s
}

// Print print message
// SetOpts set options
func (s *Style256) SetOpts(opts Opts) *Style256 {
s.opts = opts
return s
}

// AddOpts add options
func (s *Style256) AddOpts(opts ...Color) *Style256 {
s.opts.Add(opts...)
return s
}

// Print message
func (s *Style256) Print(a ...interface{}) {
doPrintV2(s.String(), fmt.Sprint(a...))
}
Expand Down Expand Up @@ -214,5 +230,9 @@ func (s *Style256) String() string {
ss = append(ss, fmt.Sprintf(TplBg256, s.bg[0]))
}

if s.opts.IsValid() {
ss = append(ss, s.opts.String())
}

return strings.Join(ss, ";")
}
49 changes: 33 additions & 16 deletions color_rgb.go
Expand Up @@ -144,11 +144,11 @@ func (c RGBColor) Code() string {

// String to color code string
func (c RGBColor) String() string {
if c[3] == AsFg { // 0 is Fg
if c[3] == AsFg {
return fmt.Sprintf(TplFgRGB, c[0], c[1], c[2])
}

if c[3] == AsBg { // 1 is Bg
if c[3] == AsBg {
return fmt.Sprintf(TplBgRGB, c[0], c[1], c[2])

}
Expand All @@ -159,19 +159,17 @@ func (c RGBColor) String() string {

// IsEmpty value
func (c RGBColor) IsEmpty() bool {
return c[3] > 1
return c[3] > AsBg
}

// IsValid value
// func (c RGBColor) IsValid() bool {
// return c[3] <= AsBg
// }

// C256 returns the closest approximate 256 (8 bit) color
func (c RGBColor) C256() Color256 {
var isBg bool
if c[3] == 0 {
isBg = false
} else if c[3] == 1 {
isBg = true
}

return C256(rgb2short(c[0], c[1], c[2]), isBg)
return C256(rgb2short(c[0], c[1], c[2]), c[3] == AsBg)
}

/*************************************************************
Expand All @@ -188,6 +186,8 @@ func (c RGBColor) C256() Color256 {
type RGBStyle struct {
// Name of the style
Name string
// color options of the style
opts Opts
// fg and bg color
fg, bg RGBColor
}
Expand Down Expand Up @@ -233,24 +233,36 @@ func RGBStyleFromString(fg string, bg ...string) *RGBStyle {
}

// Set fg and bg color
func (s *RGBStyle) Set(fg, bg RGBColor) *RGBStyle {
return s.SetFg(fg).SetBg(bg)
func (s *RGBStyle) Set(fg, bg RGBColor, opts ...Color) *RGBStyle {
return s.SetFg(fg).SetBg(bg).SetOpts(opts)
}

// SetFg set fg color
func (s *RGBStyle) SetFg(fg RGBColor) *RGBStyle {
fg[3] = 1
fg[3] = 1 // add fixed value, mark is valid
s.fg = fg
return s
}

// SetBg set bg color
func (s *RGBStyle) SetBg(bg RGBColor) *RGBStyle {
bg[3] = 1
bg[3] = 1 // add fixed value, mark is valid
s.bg = bg
return s
}

// SetOpts set options
func (s *RGBStyle) SetOpts(opts Opts) *RGBStyle {
s.opts = opts
return s
}

// AddOpts add options
func (s *RGBStyle) AddOpts(opts ...Color) *RGBStyle {
s.opts.Add(opts...)
return s
}

// Print print message
func (s *RGBStyle) Print(a ...interface{}) {
doPrintV2(s.String(), fmt.Sprint(a...))
Expand Down Expand Up @@ -284,14 +296,19 @@ func (s *RGBStyle) Code() string {
// String convert to color code string
func (s *RGBStyle) String() string {
var ss []string
if s.fg[3] == 1 { // last value ensure is enable.
// last value ensure is enable.
if s.fg[3] == 1 {
ss = append(ss, fmt.Sprintf(TplFgRGB, s.fg[0], s.fg[1], s.fg[2]))
}

if s.bg[3] == 1 {
ss = append(ss, fmt.Sprintf(TplBgRGB, s.bg[0], s.bg[1], s.bg[2]))
}

if s.opts.IsValid() {
ss = append(ss, s.opts.String())
}

return strings.Join(ss, ";")
}

Expand Down
8 changes: 8 additions & 0 deletions color_rgb_test.go
Expand Up @@ -13,6 +13,14 @@ func testRgbToC256Color(t *testing.T, name string, c RGBColor, expected uint8) {
}
}

func TestRGBStyle_SetOpts(t *testing.T) {
s := NewRGBStyle(RGB(234, 78, 23), RGB(20, 144, 234))
s.Println("rgb style message")

s.SetOpts(Opts{OpItalic, OpBold, OpUnderscore})
s.Println("RGB style message with options")
}

func TestRgbToC256(t *testing.T) {
testRgbToC256Color(t, "white", RGB(255, 255, 255), 15)
testRgbToC256Color(t, "red", RGB(255, 0, 0), 9)
Expand Down

0 comments on commit 2a527f3

Please sign in to comment.