From 1d2849bf91209e37a109120228777b3fa37fd8ff Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 24 Jun 2021 03:50:39 +0200 Subject: [PATCH] Use HSLuv colorspace for color profile conversions --- color.go | 6 +++--- termenv_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/color.go b/color.go index db7980e..2019c73 100644 --- a/color.go +++ b/color.go @@ -188,7 +188,7 @@ func ansi256ToANSIColor(c ANSI256Color) ANSIColor { h, _ := colorful.Hex(ansiHex[c]) for i := 0; i <= 15; i++ { hb, _ := colorful.Hex(ansiHex[i]) - d := h.DistanceLuv(hb) + d := h.DistanceHSLuv(hb) if d < md { md = d @@ -235,8 +235,8 @@ func hexToANSI256Color(c colorful.Color) ANSI256Color { // 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.DistanceLuv(c2) - grayDist := c.DistanceLuv(g2) + colorDist := c.DistanceHSLuv(c2) + grayDist := c.DistanceHSLuv(g2) if colorDist <= grayDist { return ANSI256Color(16 + ci) diff --git a/termenv_test.go b/termenv_test.go index 37c0549..336a63e 100644 --- a/termenv_test.go +++ b/termenv_test.go @@ -108,8 +108,8 @@ func TestAscii(t *testing.T) { func TestANSIProfile(t *testing.T) { p := ANSI - c := p.Color("#abcdef") - exp := "37" + c := p.Color("#e88388") + exp := "91" if c.Sequence(false) != exp { t.Errorf("Expected %s, got %s", exp, c.Sequence(false)) } @@ -117,8 +117,8 @@ func TestANSIProfile(t *testing.T) { t.Errorf("Expected type termenv.ANSIColor, got %T", c) } - c = p.Color("139") - exp = "90" + c = p.Color("82") + exp = "92" if c.Sequence(false) != exp { t.Errorf("Expected %s, got %s", exp, c.Sequence(false)) }