-
Notifications
You must be signed in to change notification settings - Fork 57
/
color_enum.go
137 lines (122 loc) · 3.79 KB
/
color_enum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Code generated by go-enum DO NOT EDIT.
// Version: example
// Revision: example
// Build Date: example
// Built By: example
//go:build example
// +build example
package example
import (
"errors"
"fmt"
"strings"
)
const (
// ColorBlack is a Color of type Black.
ColorBlack Color = iota
// ColorWhite is a Color of type White.
ColorWhite
// ColorRed is a Color of type Red.
ColorRed
// ColorGreen is a Color of type Green.
// Green starts with 33
ColorGreen Color = iota + 30
// ColorBlue is a Color of type Blue.
ColorBlue
// ColorGrey is a Color of type Grey.
ColorGrey
// ColorYellow is a Color of type Yellow.
ColorYellow
// ColorBlueGreen is a Color of type Blue-Green.
ColorBlueGreen
// ColorRedOrange is a Color of type Red-Orange.
ColorRedOrange
// ColorYellowGreen is a Color of type Yellow_green.
ColorYellowGreen
// ColorRedOrangeBlue is a Color of type Red-Orange-Blue.
ColorRedOrangeBlue
)
var ErrInvalidColor = errors.New("not a valid Color")
const _ColorName = "BlackWhiteRedGreenBluegreyyellowblue-greenred-orangeyellow_greenred-orange-blue"
var _ColorMap = map[Color]string{
ColorBlack: _ColorName[0:5],
ColorWhite: _ColorName[5:10],
ColorRed: _ColorName[10:13],
ColorGreen: _ColorName[13:18],
ColorBlue: _ColorName[18:22],
ColorGrey: _ColorName[22:26],
ColorYellow: _ColorName[26:32],
ColorBlueGreen: _ColorName[32:42],
ColorRedOrange: _ColorName[42:52],
ColorYellowGreen: _ColorName[52:64],
ColorRedOrangeBlue: _ColorName[64:79],
}
// String implements the Stringer interface.
func (x Color) String() string {
if str, ok := _ColorMap[x]; ok {
return str
}
return fmt.Sprintf("Color(%d)", x)
}
// IsValid provides a quick way to determine if the typed value is
// part of the allowed enumerated values
func (x Color) IsValid() bool {
_, ok := _ColorMap[x]
return ok
}
var _ColorValue = map[string]Color{
_ColorName[0:5]: ColorBlack,
strings.ToLower(_ColorName[0:5]): ColorBlack,
_ColorName[5:10]: ColorWhite,
strings.ToLower(_ColorName[5:10]): ColorWhite,
_ColorName[10:13]: ColorRed,
strings.ToLower(_ColorName[10:13]): ColorRed,
_ColorName[13:18]: ColorGreen,
strings.ToLower(_ColorName[13:18]): ColorGreen,
_ColorName[18:22]: ColorBlue,
strings.ToLower(_ColorName[18:22]): ColorBlue,
_ColorName[22:26]: ColorGrey,
strings.ToLower(_ColorName[22:26]): ColorGrey,
_ColorName[26:32]: ColorYellow,
strings.ToLower(_ColorName[26:32]): ColorYellow,
_ColorName[32:42]: ColorBlueGreen,
strings.ToLower(_ColorName[32:42]): ColorBlueGreen,
_ColorName[42:52]: ColorRedOrange,
strings.ToLower(_ColorName[42:52]): ColorRedOrange,
_ColorName[52:64]: ColorYellowGreen,
strings.ToLower(_ColorName[52:64]): ColorYellowGreen,
_ColorName[64:79]: ColorRedOrangeBlue,
strings.ToLower(_ColorName[64:79]): ColorRedOrangeBlue,
}
// ParseColor attempts to convert a string to a Color.
func ParseColor(name string) (Color, error) {
if x, ok := _ColorValue[name]; ok {
return x, nil
}
return Color(0), fmt.Errorf("%s is %w", name, ErrInvalidColor)
}
// MustParseColor converts a string to a Color, and panics if is not valid.
func MustParseColor(name string) Color {
val, err := ParseColor(name)
if err != nil {
panic(err)
}
return val
}
func (x Color) Ptr() *Color {
return &x
}
// MarshalText implements the text marshaller method.
func (x Color) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
// UnmarshalText implements the text unmarshaller method.
func (x *Color) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseColor(name)
if err != nil {
return err
}
*x = tmp
return nil
}