mirror of
https://github.com/taigrr/simplecolorpalettes.git
synced 2026-04-01 20:49:11 -07:00
add fromHex functionality
This commit is contained in:
@@ -3,6 +3,8 @@ package simplecolor
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SimpleColor int
|
type SimpleColor int
|
||||||
@@ -75,3 +77,24 @@ func (p SimplePalette) ToAnsi16() (sp SimplePalette) {
|
|||||||
|
|
||||||
return
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user