diff --git a/drawing/color.go b/drawing/color.go index aa036c4..5459ec1 100644 --- a/drawing/color.go +++ b/drawing/color.go @@ -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 diff --git a/drawing/color_test.go b/drawing/color_test.go index 15c3244..c749649 100644 --- a/drawing/color_test.go +++ b/drawing/color_test.go @@ -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())