From 0da4b11186c8b0c7d19cd7449f244a734ee2bba3 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 6 Oct 2022 12:52:12 -0400 Subject: [PATCH] feat: conform to the Go color interface implement RGBA with the default output --- color.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/color.go b/color.go index b134b890..ccdc43c8 100644 --- a/color.go +++ b/color.go @@ -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,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. @@ -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() +}