Skip to content

Commit

Permalink
Add color tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Sep 3, 2022
1 parent 904df5e commit b51757b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions color_test.go
@@ -1,6 +1,7 @@
package lipgloss

import (
"fmt"
"testing"

"github.com/muesli/termenv"
Expand Down Expand Up @@ -63,6 +64,10 @@ func TestHexToColor(t *testing.T) {
"#FF0000",
0xFF0000,
},
{
"#ff0000",
0xFF0000,
},
{
"#00F",
0x0000FF,
Expand All @@ -75,6 +80,10 @@ func TestHexToColor(t *testing.T) {
"invalid color",
0x0,
},
{
"",
0x0,
},
}

for i, tc := range tt {
Expand All @@ -85,3 +94,50 @@ func TestHexToColor(t *testing.T) {
}
}
}

func Example_colors() {
colors := []struct {
name string
c TerminalColor
}{
{"none", NoColor{}},
{"ansi", Color("2")},
{"rgb", Color("#abc")},
{"adapt", AdaptiveColor{"#abc", "#def"}},
}

// We'll change terminal settings, so save the current settings
// to recover them afterwards.
curProfile := ColorProfile()
darkBg := HasDarkBackground()
defer SetColorProfile(curProfile)
defer SetHasDarkBackground(darkBg)

// Force color output.
SetColorProfile(termenv.TrueColor)

for _, b := range []bool{false, true} {
fmt.Println("dark:", b)
SetHasDarkBackground(b)
for _, c := range colors {
r, g, b, a := c.c.RGBA()
fmt.Printf("%s: %v / #%04x-%04x-%04x-%04x\n", c.name, c.c.color(), r, g, b, a)
}
}

// Note: RGBA output for lipgloss.Color("12")
// (ANSI colors) does not work yet.
// See issue: https://github.com/charmbracelet/lipgloss/issues/110

// Output:
// dark: false
// none: <nil> / #0000-0000-0000-ffff
// ansi: #008000 / #0000-0000-0000-ffff
// rgb: #abc / #aaaa-bbbb-cccc-ffff
// adapt: #abc / #aaaa-bbbb-cccc-ffff
// dark: true
// none: <nil> / #0000-0000-0000-ffff
// ansi: #008000 / #0000-0000-0000-ffff
// rgb: #abc / #aaaa-bbbb-cccc-ffff
// adapt: #def / #dddd-eeee-ffff-ffff
}

0 comments on commit b51757b

Please sign in to comment.