color from hex handles # now
This commit is contained in:
parent
7281bbdfa8
commit
dd823617d9
2 changed files with 14 additions and 4 deletions
|
@ -3,6 +3,7 @@ package drawing
|
|||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -31,7 +32,12 @@ func parseHex(hex string) uint8 {
|
|||
}
|
||||
|
||||
// ColorFromHex returns a color from a css hex code.
|
||||
//
|
||||
// NOTE: it will trim a leading '#' character if present.
|
||||
func ColorFromHex(hex string) Color {
|
||||
if strings.HasPrefix(hex, "#") {
|
||||
hex = strings.TrimPrefix(hex, "#")
|
||||
}
|
||||
var c Color
|
||||
if len(hex) == 3 {
|
||||
c.R = parseHex(string(hex[0])) * 0x11
|
||||
|
|
|
@ -9,8 +9,6 @@ import (
|
|||
)
|
||||
|
||||
func TestColorFromHex(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
|
||||
white := ColorFromHex("FFFFFF")
|
||||
testutil.AssertEqual(t, ColorWhite, white)
|
||||
|
||||
|
@ -42,9 +40,15 @@ func TestColorFromHex(t *testing.T) {
|
|||
testutil.AssertEqual(t, ColorBlue, shortBlue)
|
||||
}
|
||||
|
||||
func TestColorFromAlphaMixedRGBA(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
func TestColorFromHex_handlesHash(t *testing.T) {
|
||||
withHash := ColorFromHex("#FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, withHash)
|
||||
|
||||
withoutHash := ColorFromHex("#FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, withoutHash)
|
||||
}
|
||||
|
||||
func TestColorFromAlphaMixedRGBA(t *testing.T) {
|
||||
black := ColorFromAlphaMixedRGBA(color.Black.RGBA())
|
||||
testutil.AssertTrue(t, black.Equals(ColorBlack), black.String())
|
||||
|
||||
|
|
Loading…
Reference in a new issue