theme.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT license that can
  3. // be found in the LICENSE file.
  4. package termui
  5. import "strings"
  6. /*
  7. // A ColorScheme represents the current look-and-feel of the dashboard.
  8. type ColorScheme struct {
  9. BodyBg Attribute
  10. BlockBg Attribute
  11. HasBorder bool
  12. BorderFg Attribute
  13. BorderBg Attribute
  14. BorderLabelTextFg Attribute
  15. BorderLabelTextBg Attribute
  16. ParTextFg Attribute
  17. ParTextBg Attribute
  18. SparklineLine Attribute
  19. SparklineTitle Attribute
  20. GaugeBar Attribute
  21. GaugePercent Attribute
  22. LineChartLine Attribute
  23. LineChartAxes Attribute
  24. ListItemFg Attribute
  25. ListItemBg Attribute
  26. BarChartBar Attribute
  27. BarChartText Attribute
  28. BarChartNum Attribute
  29. MBarChartBar Attribute
  30. MBarChartText Attribute
  31. MBarChartNum Attribute
  32. TabActiveBg Attribute
  33. }
  34. // default color scheme depends on the user's terminal setting.
  35. var themeDefault = ColorScheme{HasBorder: true}
  36. var themeHelloWorld = ColorScheme{
  37. BodyBg: ColorBlack,
  38. BlockBg: ColorBlack,
  39. HasBorder: true,
  40. BorderFg: ColorWhite,
  41. BorderBg: ColorBlack,
  42. BorderLabelTextBg: ColorBlack,
  43. BorderLabelTextFg: ColorGreen,
  44. ParTextBg: ColorBlack,
  45. ParTextFg: ColorWhite,
  46. SparklineLine: ColorMagenta,
  47. SparklineTitle: ColorWhite,
  48. GaugeBar: ColorRed,
  49. GaugePercent: ColorWhite,
  50. LineChartLine: ColorYellow | AttrBold,
  51. LineChartAxes: ColorWhite,
  52. ListItemBg: ColorBlack,
  53. ListItemFg: ColorYellow,
  54. BarChartBar: ColorRed,
  55. BarChartNum: ColorWhite,
  56. BarChartText: ColorCyan,
  57. MBarChartBar: ColorRed,
  58. MBarChartNum: ColorWhite,
  59. MBarChartText: ColorCyan,
  60. TabActiveBg: ColorMagenta,
  61. }
  62. var theme = themeDefault // global dep
  63. // Theme returns the currently used theme.
  64. func Theme() ColorScheme {
  65. return theme
  66. }
  67. // SetTheme sets a new, custom theme.
  68. func SetTheme(newTheme ColorScheme) {
  69. theme = newTheme
  70. }
  71. // UseTheme sets a predefined scheme. Currently available: "hello-world" and
  72. // "black-and-white".
  73. func UseTheme(th string) {
  74. switch th {
  75. case "helloworld":
  76. theme = themeHelloWorld
  77. default:
  78. theme = themeDefault
  79. }
  80. }
  81. */
  82. var ColorMap = map[string]Attribute{
  83. "fg": ColorWhite,
  84. "bg": ColorDefault,
  85. "border.fg": ColorWhite,
  86. "label.fg": ColorGreen,
  87. "par.fg": ColorYellow,
  88. "par.label.bg": ColorWhite,
  89. }
  90. func ThemeAttr(name string) Attribute {
  91. return lookUpAttr(ColorMap, name)
  92. }
  93. func lookUpAttr(clrmap map[string]Attribute, name string) Attribute {
  94. a, ok := clrmap[name]
  95. if ok {
  96. return a
  97. }
  98. ns := strings.Split(name, ".")
  99. for i := range ns {
  100. nn := strings.Join(ns[i:len(ns)], ".")
  101. a, ok = ColorMap[nn]
  102. if ok {
  103. break
  104. }
  105. }
  106. return a
  107. }
  108. // 0<=r,g,b <= 5
  109. func ColorRGB(r, g, b int) Attribute {
  110. within := func(n int) int {
  111. if n < 0 {
  112. return 0
  113. }
  114. if n > 5 {
  115. return 5
  116. }
  117. return n
  118. }
  119. r, b, g = within(r), within(b), within(g)
  120. return Attribute(0x0f + 36*r + 6*g + b)
  121. }