diff --git a/imagex/colorx/colorx.go b/imagex/colorx/colorx.go index 38a9f7d..5bd6ff5 100644 --- a/imagex/colorx/colorx.go +++ b/imagex/colorx/colorx.go @@ -14,7 +14,7 @@ var errInvalidFormat = errors.New("invalid format") func ParseHexColor(s string) (c color.RGBA, err error) { c.A = 0xff - if s[0] != '#' { + if len(s) == 0 || s[0] != '#' { return c, errInvalidFormat } diff --git a/imagex/colorx/colorx_test.go b/imagex/colorx/colorx_test.go index 7159e20..49d02bd 100644 --- a/imagex/colorx/colorx_test.go +++ b/imagex/colorx/colorx_test.go @@ -2,6 +2,7 @@ package colorx import ( "fmt" + "testing" ) // ExampleParseHexColor shows how to use the ParseHexColor() function. @@ -31,3 +32,10 @@ func ExampleParseHexColor() { // #abcd = { 0 0 0 255}, invalid format // #-12 = { 0 17 34 255}, invalid format } + +func TestParseHexColor(t *testing.T) { + _, err := ParseHexColor("") + if err == nil { + t.Errorf("empty string should return error") + } +}