feat: conform to the Go color interface

implement RGBA with the default output
This commit is contained in:
Ayman Bagabas 2022-10-06 12:52:12 -04:00
parent e5401eb8b8
commit 53fe53727e

View File

@ -1,7 +1,7 @@
package lipgloss
import (
"fmt"
"strconv"
"github.com/muesli/termenv"
)
@ -9,6 +9,7 @@ import (
// 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{}
@ -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")
@ -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.
@ -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
@ -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 {
@ -92,23 +135,18 @@ func (c CompleteColor) color(r *Renderer) termenv.Color {
}
}
<<<<<<< HEAD
func (c CompleteColor) color() termenv.Color {
return ColorProfile().Color(c.value())
}
// 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.
// CompleteAdaptiveColor specifies exact values for truecolor, ANSI256, and ANSI color
//
// Deprecated.
func (c CompleteColor) RGBA() (r, g, b, a uint32) {
return termenv.ConvertToRGB(c.color()).RGBA()
return termenv.ConvertToRGB(c.color(renderer)).RGBA()
}
// CompleteAdaptiveColor specifies exact values for truecolor, ANSI256, and ANSI color
=======
// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color
>>>>>>> f22900b20f84 (feat(renderer): use style renderer)
// profiles, with separate options for light and dark backgrounds. Automatic
// color degradation will not be performed.
type CompleteAdaptiveColor struct {
@ -122,3 +160,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()
}