From 924d332de2304a11cf4d80709b5e498efe68b79c Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 5 Dec 2019 12:19:17 +0100 Subject: [PATCH] Support 24bit color space in terminals with support Currently supported by xterm, Konsole, gnome Terminal, VTE, iTerm2. --- baseelement.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/baseelement.go b/baseelement.go index 4c4b996..a9d05a7 100644 --- a/baseelement.go +++ b/baseelement.go @@ -2,10 +2,13 @@ package gold import ( "errors" + "fmt" "io" + "os" "strconv" "github.com/logrusorgru/aurora" + "github.com/lucasb-eyer/go-colorful" bf "gopkg.in/russross/blackfriday.v2" ) @@ -28,11 +31,36 @@ func color(c string) (uint8, error) { return uint8(i), err } +func colorSeq(c string) (string, error) { + if len(c) == 0 { + return "", errors.New("Invalid color") + } + + col, err := colorful.Hex(c) + if err != nil { + return "", err + } + + return fmt.Sprintf("%d;%d;%dm", uint8(col.R*255), uint8(col.G*255), uint8(col.B*255)), nil +} + func renderText(w io.Writer, rules *ElementStyle, s string) { if len(s) == 0 { return } + // FIXME: ugly hack + if os.Getenv("COLORTERM") == "truecolor" { + bg, err := colorSeq(rules.BackgroundColor) + if err == nil { + s = "\x1b[48;2;" + bg + s + } + fg, err := colorSeq(rules.Color) + if err == nil { + s = "\x1b[38;2;" + fg + s + } + } + out := aurora.Reset(s) if rules != nil {