Skip to content

Commit

Permalink
Use HSLuv colorspace for color profile conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jun 24, 2021
1 parent 5d83b67 commit 1d2849b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions termenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ 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))
}
if _, ok := c.(ANSIColor); !ok {
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))
}
Expand Down

0 comments on commit 1d2849b

Please sign in to comment.