From ae274158de38181bca27f2ce54c8b4fc0b688eff Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 10 Jan 2017 02:16:12 +0900 Subject: Add experimental support for 24-bit colors --- src/tui/tui.go | 8 ++++++++ src/tui/tui_test.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/tui/tui_test.go (limited to 'src/tui') 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) +} -- cgit v1.2.3