-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththeme.go
414 lines (380 loc) · 10.7 KB
/
theme.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package charts
import (
"fmt"
"hash/crc32"
"sync"
"github.com/go-analyze/charts/chartdraw"
)
// ThemeLight is the default theme used, with series colors from echarts.
const ThemeLight = "light"
// ThemeDark is a dark alternative to the default theme 'light, with series colors from echarts'.
const ThemeDark = "dark"
// ThemeVividLight is an alternative light theme that has red, yellow, and other bright colors initially in the series.
// It can be a good option when you want the first few series items to grab the most attention.
const ThemeVividLight = "vivid-light"
// ThemeVividDark is a dark alternative to 'ThemeVividLight', with the same bright initial series colors.
const ThemeVividDark = "vivid-dark"
// ThemeGrafana is a grafana styled theme.
const ThemeGrafana = "grafana"
// ThemeAnt is an ant styled theme.
const ThemeAnt = "ant"
type ColorPalette interface {
IsDark() bool
GetAxisStrokeColor() Color
GetAxisSplitLineColor() Color
GetSeriesColor(int) Color
GetBackgroundColor() Color
GetTextColor() Color
// WithAxisColor will provide a new ColorPalette that uses the specified color for axis values.
// This includes the Axis Stroke, Split Line, and Text Color.
WithAxisColor(Color) ColorPalette
// WithTextColor will provide a new ColorPalette that uses the specified color for text.
// This is generally recommended over using the FontColor config values.
WithTextColor(Color) ColorPalette
// WithSeriesColors will provide a new ColorPalette that uses the specified series colors.
WithSeriesColors([]Color) ColorPalette
// WithBackgroundColor will provide a new ColorPalette that uses the specified color for the background.
WithBackgroundColor(Color) ColorPalette
}
type themeColorPalette struct {
name string
isDarkMode bool
axisStrokeColor Color
axisSplitLineColor Color
backgroundColor Color
textColor Color
seriesColors []Color
}
type ThemeOption struct {
IsDarkMode bool
AxisStrokeColor Color
AxisSplitLineColor Color
BackgroundColor Color
TextColor Color
SeriesColors []Color
}
var palettes = sync.Map{}
const defaultTheme = "default"
var defaultLightFontColor = Color{R: 70, G: 70, B: 70, A: 255}
var defaultDarkFontColor = Color{R: 238, G: 238, B: 238, A: 255}
var defaultGlobalMarkFillColor = ColorLightGray
func init() {
echartSeriesColors := []Color{
{ // blue
R: 84, G: 112, B: 198, A: 255,
},
{ // green
R: 145, G: 204, B: 117, A: 255,
},
ColorOrangeAlt2,
{ // red
R: 238, G: 102, B: 102, A: 255,
},
{ // aqua
R: 115, G: 192, B: 222, A: 255,
},
ColorGreenAlt3,
{ // dark orange
R: 252, G: 132, B: 82, A: 255,
},
{ // dark purple
R: 154, G: 96, B: 180, A: 255,
},
{ // light purple
R: 234, G: 124, B: 204, A: 255,
},
}
InstallTheme(
ThemeLight,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 110, G: 112, B: 121, A: 255},
AxisSplitLineColor: Color{R: 224, G: 230, B: 242, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
SeriesColors: echartSeriesColors,
},
)
InstallTheme(
ThemeDark,
ThemeOption{
IsDarkMode: true,
AxisStrokeColor: Color{R: 185, G: 184, B: 206, A: 255},
AxisSplitLineColor: Color{R: 72, G: 71, B: 83, A: 255},
BackgroundColor: ColorDarkGray,
TextColor: Color{R: 238, G: 238, B: 238, A: 255},
SeriesColors: echartSeriesColors,
},
)
vividSeriesColors := []Color{
{ // red
R: 255, G: 100, B: 100, A: 255,
},
{ // yellow
R: 255, G: 210, B: 100, A: 255,
},
{ // blue
R: 100, G: 180, B: 210, A: 255,
},
{ // green
R: 64, G: 160, B: 110, A: 255,
},
{ // purple
R: 154, G: 100, B: 180, A: 255,
},
{ // light red
R: 250, G: 128, B: 80, A: 255,
},
{ // light green
R: 90, G: 210, B: 110, A: 255,
},
{ // light purple
R: 220, G: 150, B: 210, A: 255,
},
{ // dark blue
R: 90, G: 118, B: 140, A: 255,
},
}
InstallTheme(
ThemeVividLight,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 110, G: 112, B: 121, A: 255},
AxisSplitLineColor: Color{R: 224, G: 230, B: 242, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
SeriesColors: vividSeriesColors,
},
)
InstallTheme(
ThemeVividDark,
ThemeOption{
IsDarkMode: true,
AxisStrokeColor: Color{R: 185, G: 184, B: 206, A: 255},
AxisSplitLineColor: Color{R: 72, G: 71, B: 83, A: 255},
BackgroundColor: ColorDarkGray,
TextColor: Color{R: 238, G: 238, B: 238, A: 255},
SeriesColors: vividSeriesColors,
},
)
InstallTheme(
ThemeAnt,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 110, G: 112, B: 121, A: 255},
AxisSplitLineColor: Color{R: 224, G: 230, B: 242, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
SeriesColors: []Color{
{ // light blue
R: 91, G: 143, B: 249, A: 255,
},
{ // light green
R: 90, G: 216, B: 166, A: 255,
},
{ // dark blue
R: 93, G: 112, B: 146, A: 255,
},
{ // dark yellow
R: 246, G: 189, B: 22, A: 255,
},
{ // blue
R: 111, G: 94, B: 249, A: 255,
},
{ // aqua
R: 109, G: 200, B: 236, A: 255,
},
{ // purple
R: 148, G: 95, B: 185, A: 255,
},
ColorOrangeAlt3,
},
},
)
InstallTheme(
ThemeGrafana,
ThemeOption{
IsDarkMode: true,
AxisStrokeColor: Color{R: 185, G: 184, B: 206, A: 255},
AxisSplitLineColor: Color{R: 68, G: 67, B: 67, A: 255},
BackgroundColor: Color{R: 31, G: 29, B: 29, A: 255},
TextColor: Color{R: 216, G: 217, B: 218, A: 255},
SeriesColors: []Color{
{ // dark green
R: 126, G: 178, B: 109, A: 255,
},
{ // orange
R: 234, G: 184, B: 57, A: 255,
},
{ // aqua
R: 110, G: 208, B: 224, A: 255,
},
{ // orange
R: 239, G: 132, B: 60, A: 255,
},
ColorRedAlt2,
{ // dark blue
R: 31, G: 120, B: 193, A: 255,
},
{ // dark purple
R: 112, G: 93, B: 160, A: 255,
},
ColorGreenAlt4,
},
},
)
if err := SetDefaultTheme(ThemeLight); err != nil {
panic(fmt.Errorf("could not setup default theme %s", ThemeLight))
}
}
// SetDefaultTheme sets default theme by name.
func SetDefaultTheme(name string) error {
if value, ok := palettes.Load(name); ok {
palettes.Store(defaultTheme, value)
return nil
}
return fmt.Errorf("theme not found: %s", name)
}
func getPreferredTheme(t ...ColorPalette) ColorPalette {
for _, theme := range t {
if theme != nil {
return theme
}
}
return GetDefaultTheme()
}
// GetDefaultTheme returns the default theme.
func GetDefaultTheme() ColorPalette {
return GetTheme(defaultTheme)
}
// MakeTheme constructs a one-off theme without installing it into the catalog.
func MakeTheme(opt ThemeOption) ColorPalette {
optStr := fmt.Sprintf("%v", opt)
optId := crc32.ChecksumIEEE([]byte(optStr))
return &themeColorPalette{
name: fmt.Sprintf("custom-%x", optId),
isDarkMode: opt.IsDarkMode,
axisStrokeColor: opt.AxisStrokeColor,
axisSplitLineColor: opt.AxisSplitLineColor,
backgroundColor: opt.BackgroundColor,
textColor: opt.TextColor,
seriesColors: opt.SeriesColors,
}
}
// InstallTheme adds a theme to the catalog which can later be retrieved using GetTheme.
func InstallTheme(name string, opt ThemeOption) {
cp := &themeColorPalette{
name: name,
isDarkMode: opt.IsDarkMode,
axisStrokeColor: opt.AxisStrokeColor,
axisSplitLineColor: opt.AxisSplitLineColor,
backgroundColor: opt.BackgroundColor,
textColor: opt.TextColor,
seriesColors: opt.SeriesColors,
}
palettes.Store(name, cp)
}
// GetTheme returns an installed theme by name, or the default if the theme is not installed.
func GetTheme(name string) ColorPalette {
if value, ok := palettes.Load(name); ok {
if cp, ok := value.(ColorPalette); ok {
return cp
}
}
return GetDefaultTheme()
}
func (t *themeColorPalette) String() string {
return t.name
}
func (t *themeColorPalette) IsDark() bool {
return t.isDarkMode
}
func (t *themeColorPalette) GetAxisStrokeColor() Color {
return t.axisStrokeColor
}
func (t *themeColorPalette) GetAxisSplitLineColor() Color {
return t.axisSplitLineColor
}
func (t *themeColorPalette) GetSeriesColor(index int) Color {
colors := t.seriesColors
colorCount := len(colors)
if index < colorCount {
return colors[index]
} else {
result := colors[index%colorCount]
// adjust the color shade automatically
rMax, gMax, bMax := 200, 200, 200
rMin, gMin, bMin := 0, 0, 0
// the adjustment amount and mod count must be balanced to ensure colors don't hit their limits quickly
adjustment := 40 * ((index / colorCount) % 3)
if t.IsDark() { // adjust the shade darker for dark themes
adjustment *= -1
rMax, gMax, bMax = 255, 255, 255
rMin, gMin, bMin = 40, 40, 40
}
if result.R != result.G || result.R != result.B {
// try to ensure the brightest channel maintains emphasis
if result.R >= result.G && result.R >= result.B {
rMin += 80
gMax -= 20
bMax -= 20
} else if result.G >= result.R && result.G >= result.B {
gMin += 80
rMax -= 20
bMax -= 20
} else {
bMin += 80
rMax -= 20
gMax -= 20
}
}
result.R = uint8(chartdraw.MaxInt(chartdraw.MinInt(int(result.R)+adjustment, rMax), rMin))
result.G = uint8(chartdraw.MaxInt(chartdraw.MinInt(int(result.G)+adjustment, gMax), gMin))
result.B = uint8(chartdraw.MaxInt(chartdraw.MinInt(int(result.B)+adjustment, bMax), bMin))
return result
}
}
func (t *themeColorPalette) GetBackgroundColor() Color {
return t.backgroundColor
}
func (t *themeColorPalette) GetTextColor() Color {
return t.textColor
}
func (t *themeColorPalette) WithAxisColor(c Color) ColorPalette {
copy := *t
copy.name += "-axis_mod"
copy.axisStrokeColor = c
copy.textColor = c
return ©
}
func (t *themeColorPalette) WithTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-text_mod"
copy.textColor = c
return ©
}
func (t *themeColorPalette) WithSeriesColors(colors []Color) ColorPalette {
copy := *t
if len(colors) == 0 { // ignore invalid input rather than panic later
copy.name += "-ignored_invalid_series_mod"
return ©
}
copy.name += "-series_mod"
copy.seriesColors = colors
return ©
}
func (t *themeColorPalette) WithBackgroundColor(color Color) ColorPalette {
copy := *t
copy.name += "-background_mod"
copy.backgroundColor = color
updatedDark := !isLightColor(color)
if copy.isDarkMode != updatedDark {
copy.isDarkMode = updatedDark
if copy.isDarkMode {
copy.name += "_dark"
} else {
copy.name += "_light"
}
}
return ©
}