From 0ee74a5a90f64f7ef3bdd30423d2cd11a6ca16e5 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 30 Aug 2022 13:38:15 -0700 Subject: [PATCH] feat: non-interpolated colors, aka CompleteColor (#100) * feat: add color types for specifying colors on a per-profile basis * chore: remove go-colourful references * fix: lint (#101) Co-authored-by: Ayman Bagabas --- color.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/color.go b/color.go index d5724f84..0e7de890 100644 --- a/color.go +++ b/color.go @@ -116,7 +116,7 @@ func (n NoColor) color() termenv.Color { // 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 -// the same value that go-colorful returns on error: +// black with 100% opacity. // // Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF. func (n NoColor) RGBA() (r, g, b, a uint32) { @@ -143,11 +143,8 @@ func (c Color) color() termenv.Color { // interface. Note that on error we return black with 100% opacity, or: // // Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFF. -// -// This is inline with go-colorful's default behavior. func (c Color) RGBA() (r, g, b, a uint32) { - cf := hexToColor(c.value()) - return cf.RGBA() + return hexToColor(c.value()).RGBA() } // AdaptiveColor provides color options for light and dark backgrounds. The @@ -177,13 +174,71 @@ func (ac AdaptiveColor) color() termenv.Color { // interface. Note that on error we return black with 100% opacity, or: // // Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFF. -// -// This is inline with go-colorful's default behavior. func (ac AdaptiveColor) RGBA() (r, g, b, a uint32) { cf := hexToColor(ac.value()) return cf.RGBA() } +// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color +// profiles. Automatic color degredation will not be performed. +type CompleteColor struct { + TrueColor string + ANSI256 string + ANSI string +} + +func (c CompleteColor) value() string { + switch ColorProfile() { + case termenv.TrueColor: + return c.TrueColor + case termenv.ANSI256: + return c.ANSI256 + case termenv.ANSI: + return c.ANSI + default: + return "" + } +} + +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 +func (c CompleteColor) RGBA() (r, g, b, a uint32) { + return hexToColor(c.value()).RGBA() +} + +// CompleteColor specifies exact values for truecolor, ANSI256, and ANSI color +// profiles, with separate options for light and dark backgrounds. Automatic +// color degredation will not be performed. +type CompleteAdaptiveColor struct { + Light CompleteColor + Dark CompleteColor +} + +func (cac CompleteAdaptiveColor) value() string { + if HasDarkBackground() { + return cac.Dark.value() + } + return cac.Light.value() +} + +func (cac CompleteAdaptiveColor) color() termenv.Color { + return ColorProfile().Color(cac.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 +func (cac CompleteAdaptiveColor) RGBA() (r, g, b, a uint32) { + return hexToColor(cac.value()).RGBA() +} + // hexToColor translates a hex color string (#RRGGBB or #RGB) into a color.RGB, // which satisfies the color.Color interface. If an invalid string is passed // black with 100% opacity will be returned: or, in hex format, 0x000000FF.