commit f13aa04

delthas  ·  2022-07-25 20:32:11 +0000 UTC
parent f52114d
Add support for hex colors
1 files changed,  +43, -2
+43, -2
 1@@ -231,6 +231,40 @@ func parseColor(raw string) (fg, bg tcell.Color, n int) {
 2 	return fg, bg, n
 3 }
 4 
 5+func parseHexColorNumber(raw string) (color tcell.Color, n int) {
 6+	if len(raw) < 6 {
 7+		return
 8+	}
 9+	if raw[0] == '+' || raw[0] == '-' {
10+		return
11+	}
12+	value, err := strconv.ParseInt(raw[:6], 16, 32)
13+	if err != nil {
14+		return
15+	}
16+	return tcell.NewHexColor(int32(value)), 6
17+}
18+
19+func parseHexColor(raw string) (fg, bg tcell.Color, n int) {
20+	fg, n = parseHexColorNumber(raw)
21+	raw = raw[n:]
22+
23+	if len(raw) == 0 || raw[0] != ',' {
24+		return fg, tcell.ColorDefault, n
25+	}
26+
27+	n++
28+	bg, p := parseHexColorNumber(raw[1:])
29+	n += p
30+
31+	if bg == tcell.ColorDefault {
32+		// Lone comma, do not parse as part of a color code.
33+		return fg, tcell.ColorDefault, n - 1
34+	}
35+
36+	return fg, bg, n
37+}
38+
39 func IRCString(raw string) StyledString {
40 	var formatted strings.Builder
41 	var styles []rangedStyle
42@@ -248,8 +282,15 @@ func IRCString(raw string) StyledString {
43 		} else if r == 0x02 {
44 			lastWasBold := lastAttrs&tcell.AttrBold != 0
45 			current = last.Bold(!lastWasBold)
46-		} else if r == 0x03 {
47-			fg, bg, n := parseColor(raw[1:])
48+		} else if r == 0x03 || r == 0x04 {
49+			var fg tcell.Color
50+			var bg tcell.Color
51+			var n int
52+			if r == 0x03 {
53+				fg, bg, n = parseColor(raw[1:])
54+			} else {
55+				fg, bg, n = parseHexColor(raw[1:])
56+			}
57 			raw = raw[n:]
58 			if n == 0 {
59 				// Both `fg` and `bg` are equal to