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