Skip to content

Commit

Permalink
feat: conform to the Go color interface
Browse files Browse the repository at this point in the history
implement RGBA with the default output
  • Loading branch information
aymanbagabas committed Oct 6, 2022
1 parent 76b8654 commit 0da4b11
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions color.go
@@ -1,14 +1,15 @@
package lipgloss

import (
"fmt"
"strconv"

"github.com/muesli/termenv"
)

// TerminalColor is a color intended to be rendered in the terminal.
type TerminalColor interface {
color(*Renderer) termenv.Color
RGBA() (r, g, b, a uint32)
}

var noColor = NoColor{}
Expand All @@ -26,6 +27,17 @@ func (NoColor) color(*Renderer) termenv.Color {
return termenv.NoColor{}
}

// RGBA returns the RGBA value of this color. Because we have to return
// something, despite this color being the absence of color, we're returning
// black with 100% opacity.
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
//
// Deprecated
func (n NoColor) RGBA() (r, g, b, a uint32) {
return 0x0, 0x0, 0x0, 0xFFFF
}

// Color specifies a color by hex or ANSI value. For example:
//
// ansiColor := lipgloss.Color("21")
Expand All @@ -36,6 +48,16 @@ func (c Color) color(r *Renderer) termenv.Color {
return r.ColorProfile().Color(string(c))
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
//
// Deprecated
func (c Color) RGBA() (r, g, b, a uint32) {
return termenv.ConvertToRGB(c.color(renderer)).RGBA()
}

// ANSIColor is a color specified by an ANSI color value. It's merely syntactic
// sugar for the more general Color function. Invalid colors will render as
// black.
Expand All @@ -48,7 +70,18 @@ func (c Color) color(r *Renderer) termenv.Color {
type ANSIColor uint

func (ac ANSIColor) color(r *Renderer) termenv.Color {
return Color(fmt.Sprintf("%d", ac)).color(r)
return Color(strconv.FormatUint(uint64(ac), 10)).color(r)
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
//
// Deprecated
func (ac ANSIColor) RGBA() (r, g, b, a uint32) {
cf := Color(strconv.FormatUint(uint64(ac), 10))
return cf.RGBA()
}

// AdaptiveColor provides color options for light and dark backgrounds. The
Expand All @@ -70,6 +103,16 @@ func (ac AdaptiveColor) color(r *Renderer) termenv.Color {
return Color(ac.Light).color(r)
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
//
// Deprecated
func (ac AdaptiveColor) RGBA() (r, g, b, a uint32) {
return termenv.ConvertToRGB(ac.color(renderer)).RGBA()
}

// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color
// profiles. Automatic color degradation will not be performed.
type CompleteColor struct {
Expand All @@ -92,6 +135,16 @@ func (c CompleteColor) color(r *Renderer) termenv.Color {
}
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
//
// Deprecated
func (c CompleteColor) RGBA() (r, g, b, a uint32) {
return termenv.ConvertToRGB(c.color(renderer)).RGBA()
}

// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color
// profiles, with separate options for light and dark backgrounds. Automatic
// color degradation will not be performed.
Expand All @@ -106,3 +159,13 @@ func (cac CompleteAdaptiveColor) color(r *Renderer) termenv.Color {
}
return cac.Light.color(r)
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
//
// Deprecated
func (cac CompleteAdaptiveColor) RGBA() (r, g, b, a uint32) {
return termenv.ConvertToRGB(cac.color(renderer)).RGBA()
}

0 comments on commit 0da4b11

Please sign in to comment.