You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

label.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package label
  4. import (
  5. "fmt"
  6. "regexp"
  7. "strings"
  8. )
  9. // colorPattern is a regexp which can validate label color
  10. var colorPattern = regexp.MustCompile("^#?(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})$")
  11. // Label represents label information loaded from template
  12. type Label struct {
  13. Name string `yaml:"name"`
  14. Color string `yaml:"color"`
  15. Description string `yaml:"description,omitempty"`
  16. Exclusive bool `yaml:"exclusive,omitempty"`
  17. }
  18. // NormalizeColor normalizes a color string to a 6-character hex code
  19. func NormalizeColor(color string) (string, error) {
  20. // normalize case
  21. color = strings.TrimSpace(strings.ToLower(color))
  22. // add leading hash
  23. if len(color) == 6 || len(color) == 3 {
  24. color = "#" + color
  25. }
  26. if !colorPattern.MatchString(color) {
  27. return "", fmt.Errorf("bad color code: %s", color)
  28. }
  29. // convert 3-character shorthand into 6-character version
  30. if len(color) == 4 {
  31. r := color[1]
  32. g := color[2]
  33. b := color[3]
  34. color = fmt.Sprintf("#%c%c%c%c%c%c", r, r, g, g, b, b)
  35. }
  36. return color, nil
  37. }