-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlegacy.go
125 lines (105 loc) · 2.1 KB
/
legacy.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
package golor
import (
"fmt"
)
// Legacy terminal supporting only 16 colors.
type Legacy struct {
}
func (t *Legacy) Colorize(s string, fg int, bg int) string {
color := ""
attr := 0
var ok bool
// fmt.Printf("Orig = %d, %d\n", fg, bg)
if fg > 15 || bg > 15 {
panic("invalid color index")
}
if fg, ok = checkBold(fg); ok {
attr = 1 // bold code
}
if bg, ok = checkBold(bg); ok {
attr = 1 // bold code
}
if fg < 0 {
fg = 39
}
if bg < 0 {
bg = 49
}
fg += 30
bg += 40
// fmt.Printf("Asked for %d;%d;%d when %s\n", attr, fg, bg, s)
color += fmt.Sprintf("\033[%d;%d;%dm", attr, fg, bg)
reset := "\033[0m"
return color + s + reset
}
// RGB returns the color code corresponding a RGB value set. Arguments
// must range from 0 to 5. Returned values range from 16 (black) to
// 231 (white).
func (t *Legacy) RGB(red, green, blue int) int {
// handle gray as special case
if red == green && green == blue && red != 0 {
return 8
}
red = convertColorBit(red)
green = convertColorBit(green)
blue = convertColorBit(blue)
bold := 0
var ok bool
if red, ok = checkBoldBit(red); ok {
bold = 1
}
if blue, ok = checkBoldBit(blue); ok {
bold = 1
}
if green, ok = checkBoldBit(green); ok {
bold = 1
}
return red + (green * 2) + (blue * 4) + (bold * 8)
}
func (t *Legacy) AssignColor(s string) int {
fg := stringId(s, 16)
// prevent black, white, gray
if fg == 0 {
fg += 1
}
if fg == 8 {
fg += 1
}
if fg == 7 || fg == 15 {
fg -= 1
}
return fg
}
func convertColorBit(bit int) int {
switch {
case bit < 2:
return 0
case bit < 4:
return 1
case bit < 6:
return 2
}
fmt.Printf("invalid bit: %v\n", bit)
panic("unreachable")
}
func checkBoldBit(bit int) (int, bool) {
switch {
case bit < 2:
return bit, false
case bit == 2:
return 1, true
}
fmt.Printf("invalid bit: %v\n", bit)
panic("unreachable")
}
func checkBold(code int) (int, bool) {
switch {
case code < 8:
return code, false
case code < 16:
return code - 8, true
}
fmt.Printf("invalid code: %v\n", code)
panic("unreachable")
}