Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage testing and tests. #109

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add color tests.
  • Loading branch information
knz committed Nov 4, 2022
commit 70e248ae84a693c5463fcf8b065e24c3bb868fe2
56 changes: 56 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss

import (
"fmt"
"image/color"
"testing"

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 Down Expand Up @@ -272,3 +281,50 @@ func hexToByte(b byte) byte {
// Invalid, but just return 0.
return 0
}

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-8080-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-8080-0000-ffff
// rgb: #abc / #aaaa-bbbb-cccc-ffff
// adapt: #def / #dddd-eeee-ffff-ffff
}