'This is a replacer method called by `re.sub` with the matched
tag. It must return the correct ansi sequence.
It checks `self.do_xterm256` to determine if conversion
to standard ANSI should be done or not.
Args:
rgbmatch (re.matchobject): The match.
use_xterm256 (bool, optional): Don\'t convert 256-colors to 16.
Returns:
processed (str): The processed match string.'

def sub_xterm256(self, rgbmatch, use_xterm256=False):

    if (not rgbmatch):
        return ''
    rgbtag = rgbmatch.group()[1:]
    background = (rgbtag[0] == '[')
    grayscale = (rgbtag[(0 + int(background))] == '=')
    if (not grayscale):
        if background:
            (red, green, blue) = (int(rgbtag[1]), int(rgbtag[2]), int(rgbtag[3]))
        else:
            (red, green, blue) = (int(rgbtag[0]), int(rgbtag[1]), int(rgbtag[2]))
    else:
        letter = rgbtag[(int(background) + 1)]
        if (letter == 'a'):
            colval = 16
        elif (letter == 'z'):
            colval = 231
        else:
            colval = (134 + ord(letter))
        gray = ((ord(letter) - 97) / 5.0)
        (red, green, blue) = (gray, gray, gray)
    if use_xterm256:
        if (not grayscale):
            colval = (((16 + (red * 36)) + (green * 6)) + blue)
        return ('\x1b[%s8;5;%sm' % ((3 + int(background)), colval))
    elif ((red == green == blue) and (red < 3)):
        if background:
            return ANSI_BACK_BLACK
        elif (red >= 1):
            return (ANSI_HILITE + ANSI_BLACK)
        else:
            return (ANSI_NORMAL + ANSI_BLACK)
    elif (red == green == blue):
        if background:
            return ANSI_BACK_WHITE
        elif (red >= 4):
            return (ANSI_HILITE + ANSI_WHITE)
        else:
            return (ANSI_NORMAL + ANSI_WHITE)
    elif ((red > green) and (red > blue)):
        if background:
            return ANSI_BACK_RED
        elif (red >= 3):
            return (ANSI_HILITE + ANSI_RED)
        else:
            return (ANSI_NORMAL + ANSI_RED)
    elif ((red == green) and (red > blue)):
        if background:
            return ANSI_BACK_YELLOW
        elif (red >= 3):
            return (ANSI_HILITE + ANSI_YELLOW)
        else:
            return (ANSI_NORMAL + ANSI_YELLOW)
    elif ((red == blue) and (red > green)):
        if background:
            return ANSI_BACK_MAGENTA
        elif (red >= 3):
            return (ANSI_HILITE + ANSI_MAGENTA)
        else:
            return (ANSI_NORMAL + ANSI_MAGENTA)
    elif (green > blue):
        if background:
            return ANSI_BACK_GREEN
        elif (green >= 3):
            return (ANSI_HILITE + ANSI_GREEN)
        else:
            return (ANSI_NORMAL + ANSI_GREEN)
    elif (green == blue):
        if background:
            return ANSI_BACK_CYAN
        elif (green >= 3):
            return (ANSI_HILITE + ANSI_CYAN)
        else:
            return (ANSI_NORMAL + ANSI_CYAN)
    elif background:
        return ANSI_BACK_BLUE
    elif (blue >= 3):
        return (ANSI_HILITE + ANSI_BLUE)
    else:
        return (ANSI_NORMAL + ANSI_BLUE)
