summaryrefslogtreecommitdiffstats
path: root/src/tui
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2017-01-10 02:16:12 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2017-01-10 02:16:12 +0900
commitae274158de38181bca27f2ce54c8b4fc0b688eff (patch)
tree1bcecf4123c33640a0d6c57147b17920aea0ee7e /src/tui
parent340af463cd9c52c0a3a7ea5688035bba1cd29f6a (diff)
Add experimental support for 24-bit colors
Diffstat (limited to 'src/tui')
-rw-r--r--src/tui/tui.go8
-rw-r--r--src/tui/tui_test.go20
2 files changed, 28 insertions, 0 deletions
diff --git a/src/tui/tui.go b/src/tui/tui.go
index 859eed7a..11ac1e7d 100644
--- a/src/tui/tui.go
+++ b/src/tui/tui.go
@@ -1,6 +1,7 @@
package tui
import (
+ "strconv"
"time"
)
@@ -121,6 +122,13 @@ type ColorPair struct {
id int16
}
+func HexToColor(rrggbb string) Color {
+ r, _ := strconv.ParseInt(rrggbb[1:3], 16, 0)
+ g, _ := strconv.ParseInt(rrggbb[3:5], 16, 0)
+ b, _ := strconv.ParseInt(rrggbb[5:7], 16, 0)
+ return Color((1 << 24) + (r << 16) + (g << 8) + b)
+}
+
func NewColorPair(fg Color, bg Color) ColorPair {
return ColorPair{fg, bg, -1}
}
diff --git a/src/tui/tui_test.go b/src/tui/tui_test.go
new file mode 100644
index 00000000..3ba9bf35
--- /dev/null
+++ b/src/tui/tui_test.go
@@ -0,0 +1,20 @@
+package tui
+
+import "testing"
+
+func TestHexToColor(t *testing.T) {
+ assert := func(expr string, r, g, b int) {
+ color := HexToColor(expr)
+ if !color.is24() ||
+ int((color>>16)&0xff) != r ||
+ int((color>>8)&0xff) != g ||
+ int((color)&0xff) != b {
+ t.Fail()
+ }
+ }
+
+ assert("#ff0000", 255, 0, 0)
+ assert("#010203", 1, 2, 3)
+ assert("#102030", 16, 32, 48)
+ assert("#ffffff", 255, 255, 255)
+}