12345678910111213141516171819202122232425262728293031323334 |
- package vips
- import (
- "fmt"
- "regexp"
- )
- var hexColorRegex = regexp.MustCompile("^([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$")
- const (
- hexColorLongFormat = "%02x%02x%02x"
- hexColorShortFormat = "%1x%1x%1x"
- )
- type Color struct{ R, G, B uint8 }
- func ColorFromHex(hexcolor string) (Color, error) {
- c := Color{}
- if !hexColorRegex.MatchString(hexcolor) {
- return c, fmt.Errorf("Invalid hex color: %s", hexcolor)
- }
- if len(hexcolor) == 3 {
- fmt.Sscanf(hexcolor, hexColorShortFormat, &c.R, &c.G, &c.B)
- c.R *= 17
- c.G *= 17
- c.B *= 17
- } else {
- fmt.Sscanf(hexcolor, hexColorLongFormat, &c.R, &c.G, &c.B)
- }
- return c, nil
- }
|