summaryrefslogtreecommitdiff
path: root/ui/colors.go
blob: f9b4f3fa512db8d37211bf3d8a1fb5dbe8e2ab12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ui

import (
	"hash/fnv"

	"github.com/gdamore/tcell/v2"
)

type ColorScheme int

const (
	ColorSchemeBase ColorScheme = iota
	ColorSchemeExtended
)

var colors = map[ColorScheme][]tcell.Color{
	// base 16 colors, excluding grayscale colors.
	ColorSchemeBase: {
		tcell.ColorMaroon,
		tcell.ColorGreen,
		tcell.ColorOlive,
		tcell.ColorNavy,
		tcell.ColorPurple,
		tcell.ColorTeal,
		tcell.ColorSilver,
		tcell.ColorRed,
		tcell.ColorLime,
		tcell.ColorYellow,
		tcell.ColorBlue,
		tcell.ColorFuchsia,
		tcell.ColorAqua,
	},
	// all XTerm extended colors with HSL saturation=1, light=0.5
	ColorSchemeExtended: {
		tcell.Color196, // HSL hue: 0°
		tcell.Color202, // HSL hue: 22°
		tcell.Color208, // HSL hue: 32°
		tcell.Color214, // HSL hue: 41°
		tcell.Color220, // HSL hue: 51°
		tcell.Color226, // HSL hue: 60°
		tcell.Color190, // HSL hue: 69°
		tcell.Color154, // HSL hue: 79°
		tcell.Color118, // HSL hue: 88°
		tcell.Color82,  // HSL hue: 98°
		tcell.Color46,  // HSL hue: 120°
		tcell.Color47,  // HSL hue: 142°
		tcell.Color48,  // HSL hue: 152°
		tcell.Color49,  // HSL hue: 161°
		tcell.Color50,  // HSL hue: 171°
		tcell.Color51,  // HSL hue: 180°
		tcell.Color45,  // HSL hue: 189°
		tcell.Color39,  // HSL hue: 199°
		tcell.Color33,  // HSL hue: 208°
		tcell.Color27,  // HSL hue: 218°
		tcell.Color21,  // HSL hue: 240°
		tcell.Color57,  // HSL hue: 262°
		tcell.Color93,  // HSL hue: 272°
		tcell.Color129, // HSL hue: 281°
		tcell.Color165, // HSL hue: 291°
		tcell.Color201, // HSL hue: 300°
		tcell.Color200, // HSL hue: 309°
		tcell.Color199, // HSL hue: 319°
		tcell.Color198, // HSL hue: 328°
		tcell.Color197, // HSL hue: 338°
	},
}

func IdentColor(scheme ColorScheme, ident string) tcell.Color {
	h := fnv.New32()
	_, _ = h.Write([]byte(ident))
	c := colors[scheme]
	return c[int(h.Sum32()%uint32(len(c)))]
}

func IdentString(scheme ColorScheme, ident string) StyledString {
	color := IdentColor(scheme, ident)
	style := tcell.StyleDefault.Foreground(color)
	return Styled(ident, style)
}