From e05a96df51e48e192e8e19796740466076cdb953 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Thu, 23 Jun 2022 21:43:20 -0700 Subject: [PATCH] support 3-character hex codes like CSS does --- simplecolor/simplecolors.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/simplecolor/simplecolors.go b/simplecolor/simplecolors.go index 5145fee..1a65c3c 100644 --- a/simplecolor/simplecolors.go +++ b/simplecolor/simplecolors.go @@ -87,6 +87,16 @@ func FromRGBA(r, g, b, a uint32) SimpleColor { func FromHexString(h string) SimpleColor { h = strings.ReplaceAll(h, "#", "") + hexRunes := []rune(h) + switch len(hexRunes) { + case 6: + break + case 3: + stretchedHex := hexRunes[0] + hexRunes[0] + hexRunes[1] + hexRunes[1] + hexRunes[2] + hexRunes[2] + h = string(stretchedHex) + default: + return FromHexString("#66042d") + } i, err := strconv.ParseInt(h, 16, 64) if err != nil { // if there's an error, we can't return it