1package bug
2
3import (
4 "fmt"
5 "io"
6 "strings"
7
8 "github.com/MichaelMure/git-bug/util/text"
9)
10
11// Color in the form red, green, blue
12type Color struct {
13 red uint8
14 green uint8
15 blue uint8
16}
17
18type Label string
19
20func (l Label) String() string {
21 return string(l)
22}
23
24// Color from a Label in a deterministic way
25func (l Label) Color() Color {
26 label := string(l)
27 id := 0
28
29 // colors from: https://material-ui.com/style/color/
30 colors := []Color{
31 Color{red: 244, green: 67, blue: 54}, // red
32 Color{red: 233, green: 30, blue: 99}, // pink
33 Color{red: 156, green: 39, blue: 176}, // purple
34 Color{red: 103, green: 58, blue: 183}, // deepPurple
35 Color{red: 63, green: 81, blue: 181}, // indigo
36 Color{red: 33, green: 150, blue: 243}, // blue
37 Color{red: 3, green: 169, blue: 244}, // lightBlue
38 Color{red: 0, green: 188, blue: 212}, // cyan
39 Color{red: 0, green: 150, blue: 136}, // teal
40 Color{red: 76, green: 175, blue: 80}, // green
41 Color{red: 139, green: 195, blue: 74}, // lightGreen
42 Color{red: 205, green: 220, blue: 57}, // lime
43 Color{red: 255, green: 235, blue: 59}, // yellow
44 Color{red: 255, green: 193, blue: 7}, // amber
45 Color{red: 255, green: 152, blue: 0}, // orange
46 Color{red: 255, green: 87, blue: 34}, // deepOrange
47 Color{red: 121, green: 85, blue: 72}, // brown
48 Color{red: 158, green: 158, blue: 158}, // grey
49 Color{red: 96, green: 125, blue: 139}, // blueGrey
50 }
51
52 for pos, char := range label {
53 id = ((pos+1)*(id+1) + int(char)) % len(colors)
54 }
55
56 return colors[id]
57}
58
59// UnmarshalGQL implements the graphql.Unmarshaler interface
60func (l *Label) UnmarshalGQL(v interface{}) error {
61 _, ok := v.(string)
62 if !ok {
63 return fmt.Errorf("labels must be strings")
64 }
65
66 *l = v.(Label)
67
68 return nil
69}
70
71// MarshalGQL implements the graphql.Marshaler interface
72func (l Label) MarshalGQL(w io.Writer) {
73 _, _ = w.Write([]byte(`"` + l.String() + `"`))
74}
75
76func (l Label) Validate() error {
77 str := string(l)
78
79 if text.Empty(str) {
80 return fmt.Errorf("empty")
81 }
82
83 if strings.Contains(str, "\n") {
84 return fmt.Errorf("should be a single line")
85 }
86
87 if !text.Safe(str) {
88 return fmt.Errorf("not fully printable")
89 }
90
91 return nil
92}