style_color.go 625 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package ultraviolet
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. type Color struct {
  7. Red int
  8. Green int
  9. Blue int
  10. Alpha int
  11. }
  12. func ColorFromHex(value string) Color {
  13. if len(value) != 8 {
  14. return Color{}
  15. }
  16. if !strings.HasPrefix(value, "0x") {
  17. return Color{}
  18. }
  19. red, err := strconv.ParseInt(value[2:4], 16, 16)
  20. if err != nil {
  21. return Color{}
  22. }
  23. green, err := strconv.ParseInt(value[4:6], 16, 16)
  24. if err != nil {
  25. return Color{}
  26. }
  27. blue, err := strconv.ParseInt(value[6:8], 16, 16)
  28. if err != nil {
  29. return Color{}
  30. }
  31. return Color{
  32. Red: int(red),
  33. Green: int(green),
  34. Blue: int(blue),
  35. Alpha: 255,
  36. }
  37. }