Skip to content

Commit

Permalink
feat: complete color support
Browse files Browse the repository at this point in the history
This commit adds a new function `Complete` that replaces the old
`CompleteColor` type. It returns a `CompleteFunc` which takes
three colors and returns the appropriate one based on the
given color profile.
  • Loading branch information
aymanbagabas committed Oct 31, 2024
1 parent 4c9ba53 commit 3885a06
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"image/color"
"strconv"

"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/ansi"
"github.com/lucasb-eyer/go-colorful"
)
Expand Down Expand Up @@ -179,3 +180,47 @@ func isDarkColor(c color.Color) bool {
_, _, l := col.Hsl()
return l < 0.5
}

// CompleteFunc is a function that returns the appropriate color based on the
// given color profile.
//
// Example usage:
//
// p := colorprofile.Detect(os.Stderr, os.Environ())
// complete := lipgloss.Complete(p)
// color := complete(
// lipgloss.Color(1), // ANSI
// lipgloss.Color(124), // ANSI256
// lipgloss.Color("#ff34ac"), // TrueColor
// )
// fmt.Println("Ooh, pretty color: ", color)
//
// For more info see [Complete].
type CompleteFunc func(ansi, ansi256, truecolor color.Color) color.Color

// Complete returns a function that will return the appropriate color based on
// the given color profile.
//
// Example usage:
//
// p := colorprofile.Detect(os.Stderr, os.Environ())
// complete := lipgloss.Complete(p)
// color := complete(
// lipgloss.Color(1), // ANSI
// lipgloss.Color(124), // ANSI256
// lipgloss.Color("#ff34ac"), // TrueColor
// )
// fmt.Println("Ooh, pretty color: ", color)
func Complete(p colorprofile.Profile) CompleteFunc {
return func(ansi, ansi256, truecolor color.Color) color.Color {
switch p {

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)

Check failure on line 216 in color.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

missing cases in switch of type colorprofile.Profile: colorprofile.Ascii, colorprofile.NoTTY (exhaustive)
case colorprofile.ANSI:
return ansi
case colorprofile.ANSI256:
return ansi256
case colorprofile.TrueColor:
return truecolor
}
return noColor
}
}

0 comments on commit 3885a06

Please sign in to comment.