func (tw InterpretingWriter) Write(p []byte) (n int, err error) {
	return tw.p.Write(p)
}
Write writes to the underlying io.Writer while substituting the
color escapes in the input.

See Interpret for a description of color escapes.
package rgbterm
import (
	"bufio"
	"bytes"
	"io"
	"os"
	"regexp"
	"strconv"
	"strings"
)
var MaxEscapeCodeLen uint = 15
func interpret(r io.ByteReader, w io.Writer, subst func(s string) []byte) error {
	inEscape := false
	escape := &bytes.Buffer{}
	for {
		c, err := r.ReadByte()
		if err != nil {
			break
		}
		if inEscape {
			if rune(c) == '{' && escape.Len() == 0 {
				_, err = w.Write([]byte("{"))
				escape.Reset()
				inEscape = false
			} else if rune(c) == '}' {
				_, err = w.Write(subst(escape.String()))
				escape.Reset()
				inEscape = false
			} else {
				escape.WriteByte(c)
				if MaxEscapeCodeLen > 0 && uint(escape.Len()) > MaxEscapeCodeLen {
					w.Write([]byte("{"))
					_, err = w.Write(escape.Bytes())
					inEscape = false
				}
			}
		} else {
			if rune(c) == '{' {
				inEscape = true
			} else {
				_, err = w.Write([]byte{c})
			}
		}
		if err != nil {
			return err
		}
	}
	return nil
}
var colorRegex *regexp.Regexp = regexp.MustCompile(`#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})`)
func parseEscape(s string) ([]uint8, []uint8) {
	parts := strings.Split(s, ",")
	atoi := func(s []string) (r []uint8) {
		r = make([]uint8, len(s))
		for i, v := range s {
			i64, _ := strconv.ParseInt(v, 16, 0)
			r[i] = uint8(i64)
		}
		return
	}
	escapes := make([][]uint8, 2)
	for i, p := range parts {
		match := colorRegex.FindStringSubmatch(p)
		if match != nil {
			escapes[i] = atoi(match[1:])
		}
	}
	return escapes[0], escapes[1]
}
func substColor(s string) (c []byte) {
	fg, bg := parseEscape(s)
	if fg == nil && bg == nil {
		return reset
	}
	if fg != nil {
		c = append(c, color(fg[0], fg[1], fg[2], true)...)
	}
	if bg != nil {
		if len(c) > 0 {
			c = append(c, byte(';'))
		}
		c = append(c, color(bg[0], bg[1], bg[2], false)...)
	}
	c = append(before, c...)
	c = append(c, after...)
	return
}
func Interpret(r io.ByteReader, w io.Writer) error {
	return interpret(r, w, substColor)
}
func InterpretStr(s string) string {
	var out bytes.Buffer
	Interpret(bytes.NewBuffer([]byte(s)), &out)
	return out.String()
}
type InterpretingWriter struct {
	p *io.PipeWriter
}
func NewInterpretingWriter(w io.Writer) InterpretingWriter {
	pr, pw := io.Pipe()
	go Interpret(bufio.NewReader(pr), w)
	return InterpretingWriter{p: pw}
}