Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 256 color support #251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func New(value ...Attribute) *Color {
return c
}

// Co256 returns a new foreground color in 256-color mode.
func Co256(n int) *Color {
return New(foreground, 5, Attribute(n))
}

// BgCo256 returns a new background color in 256-color mode.
func BgCo256(n int) *Color {
return New(background, 5, Attribute(n))
}

// RGB returns a new foreground color in 24-bit RGB.
func RGB(r, g, b int) *Color {
return New(foreground, 2, Attribute(r), Attribute(g), Attribute(b))
Expand All @@ -166,6 +176,18 @@ func BgRGB(r, g, b int) *Color {
return New(background, 2, Attribute(r), Attribute(g), Attribute(b))
}

// AddCo256 is used to chain foreground 256-color SGR parameters.
func (c *Color) AddCo256(n int) *Color {
c.params = append(c.params, foreground, 5, Attribute(n))
return c
}

// AddBgCo256 is used to chain background 256-color SGR parameters.
func (c *Color) AddBgCo256(n int) *Color {
c.params = append(c.params, background, 5, Attribute(n))
return c
}

// AddRGB is used to chain foreground RGB SGR parameters. Use as many as parameters to combine
// and create custom color objects. Example: .Add(34, 0, 12).Add(255, 128, 0).
func (c *Color) AddRGB(r, g, b int) *Color {
Expand Down
29 changes: 29 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ import (
"github.com/mattn/go-colorable"
)

func Test256Color(t *testing.T) {
rb := new(bytes.Buffer)
Output = rb
NoColor = false
testColors := make([]struct {
text string
n int
}, 256)
for i := 0; i < 256; i++ {
testColors[i].text = fmt.Sprintf("%d", i)
testColors[i].n = i
}

for _, c := range testColors {
New().AddCo256(c.n).Print(c.text)

line, _ := rb.ReadString('\n')
scannedLine := fmt.Sprintf("%q", line)
colored := fmt.Sprintf("\x1b[38;5;%dm%s\x1b[0m", c.n, c.text)
escapedForm := fmt.Sprintf("%q", colored)

fmt.Printf("%s\t: %s\n", c.text, line)

if scannedLine != escapedForm {
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
}
}
}

// Testing colors is kinda different. First we test for given colors and their
// escaped formatted results. Next we create some visual tests to be tested.
// Each visual test includes the color name to be compared.
Expand Down