Skip to content

Commit

Permalink
Add hexcolor support
Browse files Browse the repository at this point in the history
Add another check to the color lookup function to parse hexcode colors
into the respective color. This enables the text annotation style to
use custom colors easily: `Sprint("#6495ED{CornflowerBlue}")`.
  • Loading branch information
HeavyWombat committed Jul 12, 2019
1 parent 7ba5034 commit 943b899
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package bunt

import (
"strings"

colorful "github.com/lucasb-eyer/go-colorful"
)

Expand Down Expand Up @@ -327,9 +329,18 @@ func hexColor(scol string) colorful.Color {
}

func lookupColorByName(colorName string) *colorful.Color {
// Try to lookup a color by a supplied hexcode
if strings.HasPrefix(colorName, "#") && len(colorName) == 7 {
if color, err := colorful.Hex(colorName); err == nil {
return &color
}
}

// Try to lookup color by searching in the known colors table
if color, ok := colorByNameMap[colorName]; ok {
return &color
}

// Give up
return nil
}
17 changes: 17 additions & 0 deletions colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,21 @@ var _ = Describe("color specific tests", func() {
Expect(f1("WhiteSmoke")).To(BeEquivalentTo(f2(97))) // WhiteSmoke matches BrightWhite (#97)
})
})

Context("custom colors in text annotation", func() {
BeforeEach(func() {
ColorSetting = ON
TrueColorSetting = ON
})

AfterEach(func() {
ColorSetting = AUTO
TrueColorSetting = AUTO
})

It("should parse hexcolors in text annotations", func() {
Expect(Sprint("#6495ED{CornflowerBlue}")).To(
BeEquivalentTo(Sprint("CornflowerBlue{CornflowerBlue}")))
})
})
})
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
boldMarker = regexp.MustCompile(`\*([^*]+?)\*`)
italicMarker = regexp.MustCompile(`_([^_]+?)_`)
underlineMarker = regexp.MustCompile(`~([^~]+?)~`)
colorMarker = regexp.MustCompile(`(\w+)\{([^}]+?)\}`)
colorMarker = regexp.MustCompile(`(#?\w+)\{([^}]+?)\}`)
)

// ParseOption defines parser options
Expand Down

0 comments on commit 943b899

Please sign in to comment.