mirror of
https://github.com/taigrr/simplecolorpalettes.git
synced 2026-04-01 20:49:11 -07:00
support 3-character hex codes like CSS does
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user