add testcases for color and fix color string format bug

This commit is contained in:
Yongwei Xing 2022-01-26 16:35:34 +08:00
parent 45b66f9f18
commit 2364c6e4e4
4 changed files with 43 additions and 2 deletions

View file

@ -127,7 +127,7 @@ func ParseHexColor(s string) (color.RGBA, error) {
return Black, fmt.Errorf("invalid hex color string %v", s) return Black, fmt.Errorf("invalid hex color string %v", s)
} }
if c, ok := parseHex(s); ok { if c, ok := parseHex(s[1:]); ok {
return c, nil return c, nil
} }
return Black, fmt.Errorf("invalid hex color string %v", s) return Black, fmt.Errorf("invalid hex color string %v", s)
@ -137,7 +137,7 @@ func ParseHexColor(s string) (color.RGBA, error) {
// Reference: https://stackoverflow.com/questions/54197913/parse-hex-string-to-image-color // Reference: https://stackoverflow.com/questions/54197913/parse-hex-string-to-image-color
func parseHex(s string) (color.RGBA, bool) { func parseHex(s string) (color.RGBA, bool) {
c := color.RGBA{} c := color.RGBA{}
c.A = 1 c.A = 255
ok := true ok := true
hexToByte := func(b byte) byte { hexToByte := func(b byte) byte {

30
common/color_test.go Normal file
View file

@ -0,0 +1,30 @@
package common
import (
"image/color"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseHexColor(t *testing.T) {
tests := []struct{
name string
s string
wanterr error
want color.RGBA
}{
{name: "testcase1", s: "#112233", wanterr: nil, want: color.RGBA{R: 17, G: 34, B: 51, A: 255}},
{name: "testcase2", s: "#123", wanterr: nil, want: color.RGBA{R: 17, G: 34, B: 51, A: 255}},
{name: "testcase3", s: "#000233", wanterr: nil, want: color.RGBA{R: 0, G: 2, B: 51, A: 255}},
{name: "testcase4", s: "#FFFFFFFF", wanterr: nil, want: color.RGBA{R: 255, G: 255, B: 255, A: 255}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseHexColor(tt.s)
assert.Equal(t, tt.wanterr, err)
assert.Equal(t, tt.want, got)
})
}
}

1
go.mod
View file

@ -5,5 +5,6 @@ go 1.16
require ( require (
github.com/fogleman/gg v1.3.0 github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
) )

10
go.sum
View file

@ -1,7 +1,17 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=