From e1cc2cb21f70cd6004e84fe0bc704f2fd8bdafc5 Mon Sep 17 00:00:00 2001 From: Chris <1828125+digitallyserviced@users.noreply.github.com> Date: Sat, 26 Aug 2023 21:34:20 -0400 Subject: [PATCH] add stringable util methods --- color.go | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/color.go b/color.go index e6581b0..5035c6b 100644 --- a/color.go +++ b/color.go @@ -15,6 +15,7 @@ package tcell import ( + "fmt" ic "image/color" "strconv" ) @@ -1001,15 +1002,48 @@ func (c Color) IsRGB() bool { return c&(ColorValid|ColorIsRGB) == (ColorValid | ColorIsRGB) } +// CSS returns the CSS hex string ( #ABCDEF ) if valid +// if not a valid color returns empty string +func (c Color) CSS() string { + if !c.Valid() { + return "" + } + return fmt.Sprintf("#%06X", c.Hex()) +} + +// String implements fmt.Stringer to return either the +// W3C name if it has one or the CSS hex string '#ABCDEF' +func (c Color) String() string { + if !c.Valid() { + return "" + } + return c.Name(true) +} + +// Name returns W3C name or an empty string if no arguments +// if passed true as an argument it will falls back to +// the CSS hex string if no W3C name found '#ABCDEF' +func (c Color) Name(css ...bool) string { + for name, hex := range ColorNames { + if c == hex { + return name + } + } + if len(css) > 0 && css[0] { + return c.CSS() + } + return "" +} + // Hex returns the color's hexadecimal RGB 24-bit value with each component -// consisting of a single byte, R << 16 | G << 8 | B. If the color +// consisting of a single byte, ala R << 16 | G << 8 | B. If the color // is unknown or unset, -1 is returned. func (c Color) Hex() int32 { if !c.Valid() { return -1 } if c&ColorIsRGB != 0 { - return int32(c & 0xffffff) + return int32(c) & 0xffffff } if v, ok := ColorValues[c]; ok { return v @@ -1036,7 +1070,7 @@ func (c Color) TrueColor() Color { return ColorDefault } if c&ColorIsRGB != 0 { - return c | ColorValid + return c } return Color(c.Hex()) | ColorIsRGB | ColorValid }