| 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 |
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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func FgString(in string, r, g, b uint8) string { |
| 54 |
return string(FgBytes([]byte(in), r, g, b)) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
func BgString(in string, r, g, b uint8) string { |
| 62 |
return string(BgBytes([]byte(in), r, g, b)) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 72 |
|
| 73 |
func FgBytes(in []byte, r, g, b uint8) []byte { |
| 74 |
return colorize(color(r, g, b, true), in) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
func BgBytes(in []byte, r, g, b uint8) []byte { |
| 80 |
return colorize(color(r, g, b, false), in) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func FgByte(in byte, r, g, b uint8) []byte { |
| 86 |
return colorize(color(r, g, b, true), []byte{in}) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 107 |
if r == g && g == b { |
| 108 |
color, ok := grayscale(r, foreground) |
| 109 |
if ok { |
| 110 |
return color |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 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 |
} |