package svg import "encoding/xml" import "strings" import "image/color" import "github.com/mazznoer/csscolorparser" type Style map[string]string func ParseStyle(str string) (Style, error) { res := make(Style) rules := strings.Split(";", str) for _, rule := range rules { parts := strings.SplitN(rule, ":", 2) key := parts[0] value := "true" if len(parts) > 1 { value = parts[1] } res[key] = value } return res, nil } func (s *Style) UnmarshalXMLAttr(attr xml.Attr) error { style, err := ParseStyle(attr.Value) if err != nil { return err } *s = style return nil } var _ xml.UnmarshalerAttr = &Style{} func (s *Style) ToColor(key string) color.Color { name, ok := s[key] if !ok { return color.RGBA{254, 0, 0, 255} } c, err := csscolorparser.Parse(name) if err != nil { return color.RGBA{253, 0, 0, 255} } return c }