mirror of
https://github.com/junegunn/fzf.git
synced 2024-11-10 13:25:54 +03:00
Add experimental support for 24-bit colors
This commit is contained in:
parent
340af463cd
commit
ae274158de
@ -197,7 +197,9 @@ Number of spaces for a tab character (default: 8)
|
||||
.BI "--color=" "[BASE_SCHEME][,COLOR:ANSI]"
|
||||
Color configuration. The name of the base color scheme is followed by custom
|
||||
color mappings. Ansi color code of -1 denotes terminal default
|
||||
foreground/background color.
|
||||
foreground/background color. You can also specify 24-bit color in \fB#rrggbb\fR
|
||||
format, but the support for 24-bit colors is experimental and only works when
|
||||
\fB--height\fR option is used.
|
||||
|
||||
.RS
|
||||
e.g. \fBfzf --color=bg+:24\fR
|
||||
|
@ -493,6 +493,7 @@ func dupeTheme(theme *tui.ColorTheme) *tui.ColorTheme {
|
||||
|
||||
func parseTheme(defaultTheme *tui.ColorTheme, str string) *tui.ColorTheme {
|
||||
theme := dupeTheme(defaultTheme)
|
||||
rrggbb := regexp.MustCompile("^#[0-9a-fA-F]{6}$")
|
||||
for _, str := range strings.Split(strings.ToLower(str), ",") {
|
||||
switch str {
|
||||
case "dark":
|
||||
@ -516,11 +517,17 @@ func parseTheme(defaultTheme *tui.ColorTheme, str string) *tui.ColorTheme {
|
||||
if len(pair) != 2 {
|
||||
fail()
|
||||
}
|
||||
ansi32, err := strconv.Atoi(pair[1])
|
||||
if err != nil || ansi32 < -1 || ansi32 > 255 {
|
||||
fail()
|
||||
|
||||
var ansi tui.Color
|
||||
if rrggbb.MatchString(pair[1]) {
|
||||
ansi = tui.HexToColor(pair[1])
|
||||
} else {
|
||||
ansi32, err := strconv.Atoi(pair[1])
|
||||
if err != nil || ansi32 < -1 || ansi32 > 255 {
|
||||
fail()
|
||||
}
|
||||
ansi = tui.Color(ansi32)
|
||||
}
|
||||
ansi := tui.Color(ansi32)
|
||||
switch pair[0] {
|
||||
case "fg":
|
||||
theme.Fg = ansi
|
||||
|
@ -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}
|
||||
}
|
||||
|
20
src/tui/tui_test.go
Normal file
20
src/tui/tui_test.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user