-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththeme.go
953 lines (901 loc) · 25.4 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
package charts
import (
"fmt"
"hash/crc32"
"sync"
"github.com/go-analyze/charts/chartdraw"
)
const (
// ThemeLight is the default theme used, with series colors from echarts.
ThemeLight = "light"
// ThemeDark is a dark alternative to the default theme 'light, with series colors from echarts'.
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.
ThemeVividLight = "vivid-light"
// ThemeVividDark is a dark alternative to 'ThemeVividLight', with the same bright initial series colors.
ThemeVividDark = "vivid-dark"
// ThemeGrafana is a grafana styled theme.
ThemeGrafana = "grafana"
// ThemeAnt is an ant styled theme.
ThemeAnt = "ant"
// ThemeNatureLight provides earthy color tones.
ThemeNatureLight = "nature-light"
// ThemeNatureDark provides earthy color tones with a dark background.
ThemeNatureDark = "nature-dark"
// ThemeRetro provides colors from the 50's and 60's, silver, maroon, tan, and other vintage colors.
ThemeRetro = "retro"
// ThemeOcean is a light colored theme that focuses on shades of green, blue, and other ocean colors.
ThemeOcean = "ocean"
// ThemeSlate is a dark theme with a slate background, and light pastel series colors.
ThemeSlate = "slate"
// ThemeGray is a light theme that only contains shades of gray.
ThemeGray = "gray"
// ThemeWinter is a light theme with shades of white and blue, some light purple.
ThemeWinter = "winter"
// ThemeSpring is a light theme with bright greens, yellows, and blues.
ThemeSpring = "spring"
// ThemeSummer is a light theme with red, orange, and yellow shades.
ThemeSummer = "summer"
// ThemeFall is a dark theme with shades of yellow, orange and brown.
ThemeFall = "fall"
)
// ColorPalette provides the theming for the chart.
type ColorPalette interface {
IsDark() bool
GetXAxisStrokeColor() Color
GetYAxisStrokeColor() Color
GetAxisSplitLineColor() Color
GetSeriesColor(int) Color
GetSeriesTrendColor(int) Color
GetBackgroundColor() Color
GetTitleTextColor() Color
GetMarkTextColor() Color
GetLabelTextColor() Color
GetLegendTextColor() Color
GetXAxisTextColor() Color
GetYAxisTextColor() Color
GetTitleBorderColor() Color
GetLegendBorderColor() Color
// WithXAxisColor will provide a new ColorPalette that uses the specified color for X axis. To adjust the text
// color invoke WithXAxisTextColor following this.
WithXAxisColor(Color) ColorPalette
// WithYAxisColor will provide a new ColorPalette that uses the specified color for Y axis. To adjust the text
// color invoke WithYAxisTextColor following this.
WithYAxisColor(Color) ColorPalette
// WithYAxisSeriesColor will provide a new ColorPalette that uses the specified series index color for Y axis and values.
WithYAxisSeriesColor(int) ColorPalette
// WithTitleTextColor will provide a new ColorPalette that uses the specified color for the title text.
WithTitleTextColor(Color) ColorPalette
// WithMarkTextColor will provide a new ColorPalette that uses the specified color for mark point and mark line labels.
WithMarkTextColor(Color) ColorPalette
// WithLabelTextColor will provide a new ColorPalette that uses the specified color for value labels.
WithLabelTextColor(Color) ColorPalette
// WithLegendTextColor will provide a new ColorPalette that uses the specified color for the legend labels
WithLegendTextColor(Color) ColorPalette
// WithXAxisTextColor will provide a new ColorPalette that uses the specified color for the x-axis labels.
WithXAxisTextColor(Color) ColorPalette
// WithYAxisTextColor will provide a new ColorPalette that uses the specified color for the y-axis labels.
WithYAxisTextColor(Color) ColorPalette
// WithSeriesColors will provide a new ColorPalette that uses the specified series colors. This will default the
// trend line colors to be related to the series colors provided. If you want to customize them further use
// WithSeriesTrendColors.
WithSeriesColors([]Color) ColorPalette
// WithSeriesTrendColors will provide a new ColorPalette that uses the specified series trend line colors.
WithSeriesTrendColors([]Color) ColorPalette
// WithBackgroundColor will provide a new ColorPalette that uses the specified color for the background.
WithBackgroundColor(Color) ColorPalette
// WithTitleBorderColor will provide a new ColorPalette that uses the specified color for the title border.
WithTitleBorderColor(Color) ColorPalette
// WithLegendBorderColor will provide a new ColorPalette that uses the specified color for the legend border.
WithLegendBorderColor(Color) ColorPalette
}
type themeColorPalette struct {
name string
isDarkMode bool
xaxisStrokeColor Color
yaxisStrokeColor Color
axisSplitLineColor Color
backgroundColor Color
titleTextColor Color
markTextColor Color
labelTextColor Color
legendTextColor Color
xaxisTextColor Color
yaxisTextColor Color
titleBorderColor Color
legendBorderColor Color
seriesColors []Color
seriesTrendColors []Color
}
func (t *themeColorPalette) GetTitleTextColor() Color {
return t.titleTextColor
}
func (t *themeColorPalette) GetMarkTextColor() Color {
return t.markTextColor
}
func (t *themeColorPalette) GetLabelTextColor() Color {
return t.labelTextColor
}
func (t *themeColorPalette) GetLegendTextColor() Color {
return t.legendTextColor
}
func (t *themeColorPalette) GetXAxisTextColor() Color {
return t.xaxisTextColor
}
func (t *themeColorPalette) GetYAxisTextColor() Color {
return t.yaxisTextColor
}
type ThemeOption struct {
IsDarkMode bool
AxisStrokeColor Color
XAxisStrokeColor Color
YAxisStrokeColor Color
AxisSplitLineColor Color
BackgroundColor Color
TextColor Color
TextColorTitle Color
TextColorMark Color
TextColorLabel Color
TextColorLegend Color
TextColorXAxis Color
TextColorYAxis Color
TitleBorderColor Color
LegendBorderColor Color
SeriesColors []Color
SeriesTrendColors []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,
},
},
)
natureSeriesColors := []Color{
ColorSageGreen,
{ // Terracotta
R: 242, G: 153, B: 119, A: 255,
},
{ // Sky blue alt
R: 130, G: 175, B: 222, A: 255,
},
{ // Moss green
R: 121, G: 191, B: 127, A: 255,
},
{ // Light Forest brown
R: 171, G: 136, B: 100, A: 255,
},
ColorDesertSand,
{ // Ocean blue
R: 100, G: 150, B: 180, A: 255,
},
{ // Clay red
R: 203, G: 134, B: 115, A: 255,
},
{ // Earthy olive
R: 135, G: 164, B: 112, A: 255,
},
{ // Driftwood gray-brown
R: 145, G: 133, B: 116, A: 255,
},
{ // River stone gray
R: 128, G: 146, B: 140, A: 255,
},
}
greenHeaderText := ColorGreenAlt3.WithAdjustHSL(0, 0, -0.2)
InstallTheme(
ThemeNatureLight,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 138, G: 142, B: 146, A: 255},
AxisSplitLineColor: Color{R: 200, G: 203, B: 208, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
TextColorTitle: greenHeaderText,
TextColorLegend: greenHeaderText,
SeriesColors: natureSeriesColors,
},
)
InstallTheme(
ThemeNatureDark,
ThemeOption{
IsDarkMode: true,
AxisStrokeColor: Color{R: 138, G: 142, B: 146, A: 255},
AxisSplitLineColor: Color{R: 72, G: 71, B: 83, A: 255},
BackgroundColor: ColorDarkGray,
TextColor: Color{R: 238, G: 238, B: 238, A: 255},
TextColorTitle: ColorWhite,
SeriesColors: natureSeriesColors,
},
)
InstallTheme(
ThemeRetro,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 140, G: 135, B: 130, A: 255},
AxisSplitLineColor: Color{R: 190, G: 190, B: 190, A: 255},
BackgroundColor: Color{R: 255, G: 250, B: 240, A: 255},
TextColor: Color{R: 51, G: 36, B: 16, A: 255},
SeriesColors: []Color{
ColorMaroon,
{ // sage olive green
R: 145, G: 150, B: 99, A: 255,
},
ColorTan,
{ // dark orange
R: 184, G: 90, B: 0, A: 255,
},
{ // brown
R: 101, G: 67, B: 33, A: 255,
},
ColorMustardYellow,
ColorTeal,
{ // light red
R: 200, G: 80, B: 50, A: 255,
},
{ // navy blue
R: 25, G: 42, B: 64, A: 255,
},
},
},
)
blueHeaderText := ColorBlue.WithAdjustHSL(0, 0, -0.2)
InstallTheme(
ThemeOcean,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 120, G: 130, B: 140, A: 255},
AxisSplitLineColor: Color{R: 200, G: 210, B: 220, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 0, G: 45, B: 72, A: 255},
TextColorTitle: blueHeaderText,
TextColorLegend: blueHeaderText,
SeriesColors: []Color{
ColorSkyBlue,
{ // seafoam green
R: 90, G: 210, B: 160, A: 255,
},
{ // coral pink
R: 252, G: 163, B: 148, A: 255,
},
{ // light purple
R: 180, G: 140, B: 210, A: 255,
},
ColorBlueAlt1,
{ // light teal
R: 110, G: 220, B: 210, A: 255,
},
ColorPink,
ColorPlum,
},
},
)
InstallTheme(
ThemeSlate,
ThemeOption{
IsDarkMode: true,
AxisStrokeColor: Color{R: 128, G: 129, B: 132, A: 255},
AxisSplitLineColor: Color{R: 72, G: 71, B: 73, A: 255},
BackgroundColor: Color{R: 51, G: 53, B: 60, A: 255}, // Slate with slight blue tint
TextColor: ColorWhite,
TextColorLegend: ColorSlateGray.WithAdjustHSL(0, 0, 0.2),
SeriesColors: []Color{
ColorLightCoral,
{ // pale aqua
R: 125, G: 210, B: 196, A: 255,
},
{ // pale yellow
R: 250, G: 240, B: 120, A: 255,
},
{ // pale blue
R: 170, G: 190, B: 255, A: 255,
},
{ // pale purple
R: 190, G: 164, B: 240, A: 255,
},
{ // pale salmon
R: 250, G: 160, B: 140, A: 255,
},
{ // pale sage green
R: 180, G: 200, B: 165, A: 255,
},
{ // dusty yellow
R: 238, G: 214, B: 63, A: 255,
},
{ // slate blue
R: 100, G: 130, B: 170, A: 255,
},
{ // dusty purple
R: 178, G: 132, B: 173, A: 255,
},
{ // muted green
R: 120, G: 160, B: 140, A: 255,
},
},
},
)
InstallTheme(
ThemeGray,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 142, G: 142, B: 142, A: 255},
AxisSplitLineColor: Color{R: 204, G: 204, B: 204, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
TextColorTitle: ColorBlack,
SeriesColors: []Color{
ColorDarkGray,
{R: 88, G: 88, B: 88, A: 255},
ColorGray,
{R: 160, G: 160, B: 160, A: 255},
{R: 190, G: 190, B: 190, A: 255},
ColorLightGray,
{R: 228, G: 228, B: 228, A: 255},
{R: 248, G: 248, B: 248, A: 255},
},
},
)
InstallTheme(
ThemeWinter,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 140, G: 145, B: 150, A: 255},
AxisSplitLineColor: Color{R: 200, G: 210, B: 220, A: 255},
BackgroundColor: ColorAzure,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
TextColorTitle: blueHeaderText,
TextColorLegend: blueHeaderText,
SeriesColors: []Color{
{ // Light blue
R: 150, G: 190, B: 255, A: 255,
},
ColorPlum,
{ // Frosty blue
R: 110, G: 150, B: 240, A: 255,
},
{ // Pale lavender
R: 210, G: 180, B: 230, A: 255,
},
{ // Ice blue
R: 90, G: 130, B: 210, A: 255,
},
{ // Soft purple
R: 190, G: 160, B: 220, A: 255,
},
{ // Glacier blue
R: 80, G: 110, B: 190, A: 255,
},
},
},
)
InstallTheme(
ThemeSpring,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 120, G: 130, B: 140, A: 255},
AxisSplitLineColor: Color{R: 200, G: 210, B: 220, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
TextColorTitle: greenHeaderText,
TextColorLegend: greenHeaderText,
SeriesColors: []Color{
{ // Lime green
R: 120, G: 200, B: 130, A: 255,
},
{ // Golden yellow
R: 220, G: 210, B: 100, A: 255,
},
ColorSkyBlue,
{ // Mint green
R: 150, G: 230, B: 180, A: 255,
},
{ // Sun yellow
R: 240, G: 220, B: 120, A: 255,
},
{ // Light teal
R: 110, G: 210, B: 200, A: 255,
},
},
},
)
InstallTheme(
ThemeSummer,
ThemeOption{
IsDarkMode: false,
AxisStrokeColor: Color{R: 150, G: 140, B: 130, A: 255},
AxisSplitLineColor: Color{R: 220, G: 210, B: 200, A: 255},
BackgroundColor: ColorWhite,
TextColor: Color{R: 70, G: 70, B: 70, A: 255},
TextColorTitle: ColorBlack,
SeriesColors: []Color{
ColorSalmon,
{ // Bright yellow
R: 230, G: 220, B: 110, A: 255,
},
ColorOrange,
ColorOrangeAlt1,
{ // Coral pink
R: 250, G: 160, B: 140, A: 255,
},
{ // pale yellow
R: 250, G: 240, B: 120, A: 255,
},
ColorOrangeAlt2,
},
},
)
InstallTheme(
ThemeFall,
ThemeOption{
IsDarkMode: true,
AxisStrokeColor: Color{R: 130, G: 90, B: 60, A: 255},
AxisSplitLineColor: Color{R: 160, G: 120, B: 80, A: 255},
BackgroundColor: ColorDarkGray,
TextColor: ColorWhite,
TextColorLegend: ColorLightGray,
SeriesColors: []Color{
{ // Golden yellow
R: 220, G: 190, B: 110, A: 255,
},
{ // Copper orange
R: 200, G: 130, B: 70, A: 255,
},
{ // Burnt brown
R: 180, G: 120, B: 60, A: 255,
},
{ // Amber yellow
R: 240, G: 190, B: 90, A: 255,
},
{ // Rust orange
R: 230, G: 140, B: 50, A: 255,
},
},
},
)
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 {
cp := makeColorPalette(opt)
cp.name = fmt.Sprintf("custom-%x", crc32.ChecksumIEEE([]byte(fmt.Sprintf("%v", opt))))
return cp
}
// InstallTheme adds a theme to the catalog which can later be retrieved using GetTheme.
func InstallTheme(name string, opt ThemeOption) {
cp := makeColorPalette(opt)
cp.name = name
palettes.Store(name, cp)
}
func makeColorPalette(o ThemeOption) *themeColorPalette {
if o.XAxisStrokeColor.IsZero() {
o.XAxisStrokeColor = o.AxisStrokeColor
}
if o.YAxisStrokeColor.IsZero() {
o.YAxisStrokeColor = o.AxisStrokeColor
}
if o.TextColor.IsZero() {
o.TextColor = ColorBlack
}
if o.TextColorLabel.IsZero() {
o.TextColorLabel = o.TextColor
}
if o.TextColorTitle.IsZero() {
o.TextColorTitle = o.TextColor
}
if o.TextColorMark.IsZero() {
o.TextColorMark = o.TextColor
}
if o.TextColorLegend.IsZero() {
o.TextColorLegend = o.TextColor
}
if o.TextColorXAxis.IsZero() {
o.TextColorXAxis = o.TextColor
}
if o.TextColorYAxis.IsZero() {
o.TextColorYAxis = o.TextColor
}
if o.LegendBorderColor.IsZero() {
o.LegendBorderColor = ColorBlack
}
if o.TitleBorderColor.IsZero() {
o.TitleBorderColor = ColorBlack
}
for i := len(o.SeriesTrendColors); i < len(o.SeriesColors); i++ {
o.SeriesTrendColors = append(o.SeriesTrendColors, autoSeriesTrendColor(o.SeriesColors[i]))
}
return &themeColorPalette{
isDarkMode: o.IsDarkMode,
xaxisStrokeColor: o.XAxisStrokeColor,
yaxisStrokeColor: o.YAxisStrokeColor,
axisSplitLineColor: o.AxisSplitLineColor,
backgroundColor: o.BackgroundColor,
titleBorderColor: o.TitleBorderColor,
legendBorderColor: o.LegendBorderColor,
titleTextColor: o.TextColorTitle,
markTextColor: o.TextColorMark,
labelTextColor: o.TextColorLabel,
legendTextColor: o.TextColorLegend,
xaxisTextColor: o.TextColorXAxis,
yaxisTextColor: o.TextColorYAxis,
seriesColors: o.SeriesColors,
seriesTrendColors: o.SeriesTrendColors,
}
}
func autoSeriesTrendColor(color Color) Color {
if color.IsTransparent() {
return color
}
c := color.WithAdjustHSL(0.0, 0.1, -0.1)
if c.A < 255 {
c.A += (255 - c.A) / 2
}
return c
}
// 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) GetXAxisStrokeColor() Color {
return t.xaxisStrokeColor
}
func (t *themeColorPalette) GetYAxisStrokeColor() Color {
return t.yaxisStrokeColor
}
func (t *themeColorPalette) GetAxisSplitLineColor() Color {
return t.axisSplitLineColor
}
func (t *themeColorPalette) GetSeriesColor(index int) Color {
return getSeriesColor(t.seriesColors, t.isDarkMode, index)
}
func (t *themeColorPalette) GetSeriesTrendColor(index int) Color {
return getSeriesColor(t.seriesTrendColors, t.isDarkMode, index)
}
func getSeriesColor(colors []Color, darkTheme bool, index int) Color {
colorCount := len(colors)
if index < colorCount {
return colors[index]
} else {
return adjustSeriesColor(colors[index%colorCount], index/colorCount, darkTheme)
}
}
func adjustSeriesColor(c Color, loopCount int, darkTheme bool) Color {
impact := ((loopCount - 1) % 3) + 1
satAdj := float64(impact) * -0.1
ltAdj := 0.08
if chartdraw.AbsInt(int(c.R)-int(c.G)) < 20 && chartdraw.AbsInt(int(c.R)-int(c.B)) < 20 {
ltAdj += 0.1 // more adjustment if close to gray
}
ltAdj *= float64(impact)
if darkTheme {
ltAdj *= -1
}
return c.WithAdjustHSL(0.0, satAdj, ltAdj)
}
func (t *themeColorPalette) GetBackgroundColor() Color {
return t.backgroundColor
}
func (t *themeColorPalette) GetTitleBorderColor() Color {
return t.titleBorderColor
}
func (t *themeColorPalette) GetLegendBorderColor() Color {
return t.legendBorderColor
}
func (t *themeColorPalette) WithXAxisColor(c Color) ColorPalette {
copy := *t
copy.name += "-xaxis_stroke_mod"
copy.xaxisStrokeColor = c
return ©
}
func (t *themeColorPalette) WithYAxisColor(c Color) ColorPalette {
copy := *t
copy.name += "-yaxis_stroke_mod"
copy.yaxisStrokeColor = c
return ©
}
func (t *themeColorPalette) WithYAxisSeriesColor(series int) ColorPalette {
copy := *t
copy.name += "-yaxis_mod"
seriesColor := t.GetSeriesColor(series)
copy.yaxisStrokeColor = seriesColor
copy.yaxisTextColor = seriesColor
return ©
}
func (t *themeColorPalette) WithTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-text_mod"
copy.titleTextColor = c
copy.markTextColor = c
copy.labelTextColor = c
copy.legendTextColor = c
copy.xaxisTextColor = c
copy.yaxisTextColor = c
return ©
}
func (t *themeColorPalette) WithTitleTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-title_mod"
copy.titleTextColor = c
return ©
}
func (t *themeColorPalette) WithMarkTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-mark_mod"
copy.markTextColor = c
return ©
}
func (t *themeColorPalette) WithLabelTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-label_mod"
copy.labelTextColor = c
return ©
}
func (t *themeColorPalette) WithLegendTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-legend_text_mod"
copy.legendTextColor = c
return ©
}
func (t *themeColorPalette) WithXAxisTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-xaxis_text_mod"
copy.xaxisTextColor = c
return ©
}
func (t *themeColorPalette) WithYAxisTextColor(c Color) ColorPalette {
copy := *t
copy.name += "-yaxis_text_mod"
copy.yaxisTextColor = 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
for i, c := range colors {
trendColor := autoSeriesTrendColor(c)
if i < len(copy.seriesTrendColors) {
copy.seriesTrendColors[i] = trendColor
} else {
copy.seriesTrendColors = append(copy.seriesTrendColors, trendColor)
}
}
return ©
}
func (t *themeColorPalette) WithSeriesTrendColors(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 += "-trend_mod"
copy.seriesTrendColors = 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 ©
}
func (t *themeColorPalette) WithTitleBorderColor(color Color) ColorPalette {
copy := *t
copy.name += "-title_border_mod"
copy.titleBorderColor = color
return ©
}
func (t *themeColorPalette) WithLegendBorderColor(color Color) ColorPalette {
copy := *t
copy.name += "-legend_border_mod"
copy.legendBorderColor = color
return ©
}