Debian Codegraph @

rgbterm.go

raw symbols SCIP
1 // Package rgbterm colorizes bytes and strings using RGB colors, for a
2 // full range of pretty terminal strings.
3 //
4 // Beyond the traditional boring 16 colors of your terminal lie an
5 // extended set of 256 pretty colors waiting to be used. However, they
6 // are weirdly encoded; simply asking for an RGB color is much more
7 // convenient!
8 //
9 // It's easy to use, pick an RGB code and just use it!
10 //
11 // var r, g, b uint8
12 // // pick a color
13 // r, g, b = 252, 255, 43
14 // // choose a word
15 // word := "=)"
16 // // colorize it!
17 // coloredWord := rgbterm.String(word, r, g, b)
18 //
19 // fmt.Println("Oh!", coloredWord, "hello!")
20 //
21 // Alternately, use ColorOut or one of the Interpret functions to output
22 // a string with color escape codes:
23 //
24 // fmt.Fprintln(rgbterm.ColorOut, "Let's print some {#ff0000}red{} and {#00ff00}green{} text")
25 // fmt.Fprintln(rgbterm.ColorOut, "Let's print some {#8080ff}blue,")
26 // fmt.Fprintln(rgbterm.ColorOut, "blue, blue{} text.")
27 //
28 // The RGB <-> HSL helpers were shamelessly taken from gorilla color, BSD 2 clauses
29 // licensed:
30 // https://code.google.com/p/gorilla/source/browse/?r=ef489f63418265a7249b1d53bdc358b09a4a2ea0#hg%2Fcolor
31 package rgbterm
32
33 var (
34 before = []byte("\033[")
35 after = []byte("m")
36 reset = []byte("\033[0;00m")
37 fgcolors = fgTermRGB[16:232]
38 bgcolors = bgTermRGB[16:232]
39 )
40
41 // String colorizes the input with the terminal color that matches
42 // the closest the RGB color.
43 //
44 // This is simply a helper for Bytes.
45 func String(in string, fr, fg, fb, br, bg, bb uint8) string {
46 return string(Bytes([]byte(in), fr, fg, fb, br, bg, bb))
47 }
48
49 // FgString colorizes the foreground of the input with the terminal color
50 // that matches the closest the RGB color.
51 //
52 // This is simply a helper for Bytes.
53 func FgString(in string, r, g, b uint8) string {
54 return string(FgBytes([]byte(in), r, g, b))
55 }
56
57 // BgString colorizes the background of the input with the terminal color
58 // that matches the closest the RGB color.
59 //
60 // This is simply a helper for Bytes.
61 func BgString(in string, r, g, b uint8) string {
62 return string(BgBytes([]byte(in), r, g, b))
63 }
64
65 // Bytes colorizes the input with the terminal color that matches
66 // the closest the RGB color.
67 func Bytes(in []byte, fr, fg, fb, br, bg, bb uint8) []byte {
68 return colorize(rgb(fr, fg, fb, br, bg, bb), in)
69 }
70
71 // Bytes colorizes the foreground with the terminal color that matches
72 // the closest the RGB color.
73 func FgBytes(in []byte, r, g, b uint8) []byte {
74 return colorize(color(r, g, b, true), in)
75 }
76
77 // BgBytes colorizes the background of the input with the terminal color
78 // that matches the closest the RGB color.
79 func BgBytes(in []byte, r, g, b uint8) []byte {
80 return colorize(color(r, g, b, false), in)
81 }
82
83 // Byte colorizes the input with the terminal color that matches
84 // the closest the RGB color.
85 func FgByte(in byte, r, g, b uint8) []byte {
86 return colorize(color(r, g, b, true), []byte{in})
87 }
88
89 // BgByte colorizes the background of the input with the terminal color
90 // that matches the closest the RGB color.
91 func BgByte(in byte, r, g, b uint8) []byte {
92 return colorize(color(r, g, b, false), []byte{in})
93 }
94
95 func colorize(color, in []byte) []byte {
96 return append(append(append(append(before, color...), after...), in...), reset...)
97 }
98
99 func rgb(fr, fg, fb, br, bg, bb uint8) []byte {
100 fore := append(color(fr, fg, fb, true), byte(';'))
101 back := color(br, bg, bb, false)
102 return append(fore, back...)
103 }
104
105 func color(r, g, b uint8, foreground bool) []byte {
106 // if all colors are equal, it might be in the grayscale range
107 if r == g && g == b {
108 color, ok := grayscale(r, foreground)
109 if ok {
110 return color
111 }
112 }
113
114 // the general case approximates RGB by using the closest color.
115 r6 := ((uint16(r) * 5) / 255)
116 g6 := ((uint16(g) * 5) / 255)
117 b6 := ((uint16(b) * 5) / 255)
118 i := 36*r6 + 6*g6 + b6
119 if foreground {
120 return fgcolors[i]
121 } else {
122 return bgcolors[i]
123 }
124 }
125
126 func grayscale(scale uint8, foreground bool) ([]byte, bool) {
127 var source [256][]byte
128
129 if foreground {
130 source = fgTermRGB
131 } else {
132 source = bgTermRGB
133 }
134
135 switch scale {
136 case 0x08:
137 return source[232], true
138 case 0x12:
139 return source[233], true
140 case 0x1c:
141 return source[234], true
142 case 0x26:
143 return source[235], true
144 case 0x30:
145 return source[236], true
146 case 0x3a:
147 return source[237], true
148 case 0x44:
149 return source[238], true
150 case 0x4e:
151 return source[239], true
152 case 0x58:
153 return source[240], true
154 case 0x62:
155 return source[241], true
156 case 0x6c:
157 return source[242], true
158 case 0x76:
159 return source[243], true
160 case 0x80:
161 return source[244], true
162 case 0x8a:
163 return source[245], true
164 case 0x94:
165 return source[246], true
166 case 0x9e:
167 return source[247], true
168 case 0xa8:
169 return source[248], true
170 case 0xb2:
171 return source[249], true
172 case 0xbc:
173 return source[250], true
174 case 0xc6:
175 return source[251], true
176 case 0xd0:
177 return source[252], true
178 case 0xda:
179 return source[253], true
180 case 0xe4:
181 return source[254], true
182 case 0xee:
183 return source[255], true
184 }
185 return nil, false
186 }