Debian Codegraph @

ui/widget/color.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package widget
18
19 import (
20 "fmt"
21 "hash/fnv"
22
•23 "github.com/gdamore/tcell/v2"
24
25 "maunium.net/go/mautrix/id"
26 )
27
28 var colorNames = []string{
29 "maroon",
30 "green",
31 "olive",
32 "navy",
33 "purple",
34 "teal",
35 "silver",
36 "gray",
37 "red",
38 "lime",
39 "yellow",
40 "blue",
41 "fuchsia",
42 "aqua",
43 "white",
44 "aliceblue",
45 "antiquewhite",
46 "aquamarine",
47 "azure",
48 "beige",
49 "bisque",
50 "blanchedalmond",
51 "blueviolet",
52 "brown",
53 "burlywood",
54 "cadetblue",
55 "chartreuse",
56 "chocolate",
57 "coral",
58 "cornflowerblue",
59 "cornsilk",
60 "crimson",
61 "darkblue",
62 "darkcyan",
63 "darkgoldenrod",
64 "darkgray",
65 "darkgreen",
66 "darkkhaki",
67 "darkmagenta",
68 "darkolivegreen",
69 "darkorange",
70 "darkorchid",
71 "darkred",
72 "darksalmon",
73 "darkseagreen",
74 "darkslateblue",
75 "darkslategray",
76 "darkturquoise",
77 "darkviolet",
78 "deeppink",
79 "deepskyblue",
80 "dimgray",
81 "dodgerblue",
82 "firebrick",
83 "floralwhite",
84 "forestgreen",
85 "gainsboro",
86 "ghostwhite",
87 "gold",
88 "goldenrod",
89 "greenyellow",
90 "honeydew",
91 "hotpink",
92 "indianred",
93 "indigo",
94 "ivory",
95 "khaki",
96 "lavender",
97 "lavenderblush",
98 "lawngreen",
99 "lemonchiffon",
100 "lightblue",
101 "lightcoral",
102 "lightcyan",
103 "lightgoldenrodyellow",
104 "lightgray",
105 "lightgreen",
106 "lightpink",
107 "lightsalmon",
108 "lightseagreen",
109 "lightskyblue",
110 "lightslategray",
111 "lightsteelblue",
112 "lightyellow",
113 "limegreen",
114 "linen",
115 "mediumaquamarine",
116 "mediumblue",
117 "mediumorchid",
118 "mediumpurple",
119 "mediumseagreen",
120 "mediumslateblue",
121 "mediumspringgreen",
122 "mediumturquoise",
123 "mediumvioletred",
124 "midnightblue",
125 "mintcream",
126 "mistyrose",
127 "moccasin",
128 "navajowhite",
129 "oldlace",
130 "olivedrab",
131 "orange",
132 "orangered",
133 "orchid",
134 "palegoldenrod",
135 "palegreen",
136 "paleturquoise",
137 "palevioletred",
138 "papayawhip",
139 "peachpuff",
140 "peru",
141 "pink",
142 "plum",
143 "powderblue",
144 "rebeccapurple",
145 "rosybrown",
146 "royalblue",
147 "saddlebrown",
148 "salmon",
149 "sandybrown",
150 "seagreen",
151 "seashell",
152 "sienna",
153 "skyblue",
154 "slateblue",
155 "slategray",
156 "snow",
157 "springgreen",
158 "steelblue",
159 "tan",
160 "thistle",
161 "tomato",
162 "turquoise",
163 "violet",
164 "wheat",
165 "whitesmoke",
166 "yellowgreen",
167 "grey",
168 "dimgrey",
169 "darkgrey",
170 "darkslategrey",
171 "lightgrey",
172 "lightslategrey",
173 "slategrey",
174 }
175
176 // GetHashColorName gets a color name for the given string based on its FNV-1 hash.
177 //
178 // The array of possible color names are the alphabetically ordered color
179 // names specified in tcell.ColorNames.
180 //
181 // The algorithm to get the color is as follows:
182 //
183 // colorNames[ FNV1(string) % len(colorNames) ]
184 //
185 // With the exception of the three special cases:
186 //
187 // --> = green
188 // <-- = red
189 // --- = yellow
190 func GetHashColorName(s string) string {
191 switch s {
192 case "-->":
193 return "green"
194 case "<--":
195 return "red"
196 case "---":
197 return "yellow"
198 default:
199 h := fnv.New32a()
200 _, _ = h.Write([]byte(s))
201 return colorNames[h.Sum32()%uint32(len(colorNames))]
202 }
203 }
204
205 // GetHashColor gets the tcell Color value for the given string.
206 //
207 // GetHashColor calls GetHashColorName() and gets the Color value from the tcell.ColorNames map.
208 func GetHashColor(val interface{}) tcell.Color {
209 switch str := val.(type) {
210 case string:
211 return tcell.ColorNames[GetHashColorName(str)]
212 case *string:
213 return tcell.ColorNames[GetHashColorName(*str)]
214 case id.UserID:
215 return tcell.ColorNames[GetHashColorName(string(str))]
216 default:
217 return tcell.ColorNames["red"]
218 }
219 }
220
221 // AddColor adds tview color tags to the given string.
222 func AddColor(s, color string) string {
223 return fmt.Sprintf("[%s]%s[white]", color, s)
224 }