mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-08-27 20:36:26 -04:00
Switch ANSI rendering to termenv to automatically convert colors between color-profiles
This commit is contained in:
+11
-77
@@ -2,15 +2,10 @@ package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
// BaseElement renders a styled primitive element.
|
||||
@@ -21,50 +16,6 @@ type BaseElement struct {
|
||||
Style StylePrimitive
|
||||
}
|
||||
|
||||
func color(c *string) (uint8, error) {
|
||||
if c == nil || len(*c) == 0 {
|
||||
return 0, errors.New("invalid color")
|
||||
}
|
||||
if (*c)[0] == '#' {
|
||||
i, err := hexToANSIColor(*c)
|
||||
return uint8(i), err
|
||||
}
|
||||
i, err := strconv.Atoi(*c)
|
||||
return uint8(i), err
|
||||
}
|
||||
|
||||
func colorSeq(fg *string, bg *string) (string, error) {
|
||||
fc := ""
|
||||
bc := ""
|
||||
if fg != nil {
|
||||
fc = *fg
|
||||
}
|
||||
if bg != nil {
|
||||
bc = *bg
|
||||
}
|
||||
|
||||
fs := ""
|
||||
bs := ""
|
||||
f, err := colorful.Hex(fc)
|
||||
if err == nil {
|
||||
fs = fmt.Sprintf("38;2;%d;%d;%d", uint8(f.R*255), uint8(f.G*255), uint8(f.B*255))
|
||||
}
|
||||
b, err := colorful.Hex(bc)
|
||||
if err == nil {
|
||||
bs = fmt.Sprintf("48;2;%d;%d;%d", uint8(b.R*255), uint8(b.G*255), uint8(b.B*255))
|
||||
}
|
||||
|
||||
if len(fs) > 0 || len(bs) > 0 {
|
||||
seq := "\x1b[" + fs
|
||||
if len(fs) > 0 {
|
||||
seq += ";"
|
||||
}
|
||||
return seq + bs + "m", nil
|
||||
}
|
||||
|
||||
return "", errors.New("invalid color")
|
||||
}
|
||||
|
||||
func formatToken(format string, token string) (string, error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
@@ -85,33 +36,16 @@ func renderText(w io.Writer, rules StylePrimitive, s string) {
|
||||
return
|
||||
}
|
||||
|
||||
truecolor := os.Getenv("COLORTERM") == "truecolor"
|
||||
// FIXME: ugly true-color ANSI support hack
|
||||
if truecolor {
|
||||
seq, err := colorSeq(rules.Color, rules.BackgroundColor)
|
||||
if err == nil {
|
||||
s = seq + s
|
||||
} else {
|
||||
truecolor = false
|
||||
}
|
||||
p := termenv.SupportedColorProfile()
|
||||
out := termenv.String(s)
|
||||
|
||||
if rules.Color != nil {
|
||||
out = out.Foreground(p.Color(*rules.Color))
|
||||
}
|
||||
if rules.BackgroundColor != nil {
|
||||
out = out.Background(p.Color(*rules.BackgroundColor))
|
||||
}
|
||||
|
||||
out := aurora.Reset(s)
|
||||
|
||||
if !truecolor {
|
||||
if rules.Color != nil {
|
||||
i, err := color(rules.Color)
|
||||
if err == nil {
|
||||
out = out.Index(i)
|
||||
}
|
||||
}
|
||||
if rules.BackgroundColor != nil {
|
||||
i, err := color(rules.BackgroundColor)
|
||||
if err == nil {
|
||||
out = out.BgIndex(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
if rules.Underline != nil && *rules.Underline {
|
||||
out = out.Underline()
|
||||
}
|
||||
@@ -122,10 +56,10 @@ func renderText(w io.Writer, rules StylePrimitive, s string) {
|
||||
out = out.Italic()
|
||||
}
|
||||
if rules.CrossedOut != nil && *rules.CrossedOut {
|
||||
out = out.CrossedOut()
|
||||
out = out.CrossOut()
|
||||
}
|
||||
if rules.Overlined != nil && *rules.Overlined {
|
||||
out = out.Overlined()
|
||||
out = out.Overline()
|
||||
}
|
||||
if rules.Inverse != nil && *rules.Inverse {
|
||||
out = out.Reverse()
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
)
|
||||
|
||||
// Chroma holds all the chroma settings.
|
||||
type Chroma struct {
|
||||
Text StylePrimitive `json:"text,omitempty"`
|
||||
@@ -229,53 +225,3 @@ func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func hexToANSIColor(h string) (int, error) {
|
||||
c, err := colorful.Hex(h)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
v2ci := func(v float64) int {
|
||||
if v < 48 {
|
||||
return 0
|
||||
}
|
||||
if v < 115 {
|
||||
return 1
|
||||
}
|
||||
return int((v - 35) / 40)
|
||||
}
|
||||
|
||||
// Calculate the nearest 0-based color index at 16..231
|
||||
r := v2ci(c.R * 255.0) // 0..5 each
|
||||
g := v2ci(c.G * 255.0)
|
||||
b := v2ci(c.B * 255.0)
|
||||
ci := 36*r + 6*g + b /* 0..215 */
|
||||
|
||||
// Calculate the represented colors back from the index
|
||||
i2cv := [6]int{0, 0x5f, 0x87, 0xaf, 0xd7, 0xff}
|
||||
cr := i2cv[r] // r/g/b, 0..255 each
|
||||
cg := i2cv[g]
|
||||
cb := i2cv[b]
|
||||
|
||||
// Calculate the nearest 0-based gray index at 232..255
|
||||
var grayIdx int
|
||||
average := (r + g + b) / 3
|
||||
if average > 238 {
|
||||
grayIdx = 23
|
||||
} else {
|
||||
grayIdx = (average - 3) / 10 // 0..23
|
||||
}
|
||||
gv := 8 + 10*grayIdx // same value for r/g/b, 0..255
|
||||
|
||||
// Return the one which is nearer to the original input rgb value
|
||||
c2 := colorful.Color{R: float64(cr) / 255.0, G: float64(cg) / 255.0, B: float64(cb) / 255.0}
|
||||
g2 := colorful.Color{R: float64(gv) / 255.0, G: float64(gv) / 255.0, B: float64(gv) / 255.0}
|
||||
colorDist := c.DistanceLab(c2)
|
||||
grayDist := c.DistanceLab(g2)
|
||||
|
||||
if colorDist <= grayDist {
|
||||
return 16 + ci, nil
|
||||
}
|
||||
return 232 + grayIdx, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user