-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
93 lines (69 loc) · 1.93 KB
/
lib.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package iris
import (
"fmt"
"image/color"
)
const Reset = "\x1b[0m"
const ResetFg = "\x1b[39m"
const ResetBg = "\x1b[49m"
const (
BlackFg = "\x1b[30m"
BlackBg = "\x1b[40m"
RedFg = "\x1b[31m"
RedBg = "\x1b[41m"
GreenFg = "\x1b[32m"
GreenBg = "\x1b[42m"
YellowFg = "\x1b[33m"
YellowBg = "\x1b[43m"
BlueFg = "\x1b[34m"
BlueBg = "\x1b[44m"
MagentaFg = "\x1b[35m"
MagentaBg = "\x1b[45m"
CyanFg = "\x1b[36m"
CyanBg = "\x1b[46m"
WhiteFg = "\x1b[37m"
WhiteBg = "\x1b[47m"
)
func ushort2uchar(c uint32) uint8 {
return uint8(c / 256)
}
func AnsiFgUnchecked(f uint8) string {
return fmt.Sprintf("\x1b[%dm", f+30)
}
func AnsiBgUnchecked(f uint8) string {
return fmt.Sprintf("\x1b[%dm", f+40)
}
func AnsiFgBgUnchecked(f uint8, b uint8) string {
return fmt.Sprintf("\x1b[%d;%dm", f+30, b+40)
}
func Ansi256Fg(f uint8) string {
return fmt.Sprintf("\x1b[38;5;%dm", f)
}
func Ansi256Bg(b uint8) string {
return fmt.Sprintf("\x1b[48;5;%dm", b)
}
func Ansi256FgBg(f uint8, b uint8) string {
return fmt.Sprintf("\x1b[38;5;%dm\x1b[48;5;%dm", f, b)
}
func RgbFg(r uint8, g uint8, b uint8) string {
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
}
func ColorFg(color color.Color) string {
r, g, b, _ := color.RGBA()
return RgbFg(ushort2uchar(r), ushort2uchar(g), ushort2uchar(b))
}
func RgbBg(r uint8, g uint8, b uint8) string {
return fmt.Sprintf("\x1b[48;2;%d;%d;%dm", r, g, b)
}
func ColorBg(color color.Color) string {
r, g, b, _ := color.RGBA()
return RgbBg(ushort2uchar(r), ushort2uchar(g), ushort2uchar(b))
}
func RgbFgBg(fr uint8, fg uint8, fb uint8, br uint8, bg uint8, bb uint8) string {
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm", fr, fg, fb, br, bg, bb)
}
func ColorFgBg(fcolor color.Color, bcolor color.Color) string {
fr, fg, fb, _ := fcolor.RGBA()
br, bg, bb, _ := bcolor.RGBA()
return RgbFgBg(ushort2uchar(fr), ushort2uchar(fg), ushort2uchar(fb), ushort2uchar(br), ushort2uchar(bg), ushort2uchar(bb))
}