From 16dbaa7796d6a465a85164fe434f9765b7cfc720 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Thu, 23 Jun 2022 12:08:07 -0700 Subject: [PATCH] add fromHex functionality --- simplecolor/simplecolors.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/simplecolor/simplecolors.go b/simplecolor/simplecolors.go index 6b135c5..5145fee 100644 --- a/simplecolor/simplecolors.go +++ b/simplecolor/simplecolors.go @@ -3,6 +3,8 @@ package simplecolor import ( "image/color" "sort" + "strconv" + "strings" ) type SimpleColor int @@ -75,3 +77,24 @@ func (p SimplePalette) ToAnsi16() (sp SimplePalette) { return } + +func FromRGBA(r, g, b, a uint32) SimpleColor { + c := r + c = c<<8 + g + c = c<<8 + b + return SimpleColor(c) +} + +func FromHexString(h string) SimpleColor { + h = strings.ReplaceAll(h, "#", "") + i, err := strconv.ParseInt(h, 16, 64) + if err != nil { + // if there's an error, we can't return it + // since this is a simple method. + // Instead, return a really weird color to draw attention. + // Black is the zero-value and it's actually used often, "plum" is + // (probably) not. + return FromHexString("#66042d") + } + return SimpleColor(i) +}