def get_known_name(s: str) -> Optional[Tuple[str, ColorArg]]:
    """ Reverse translate a terminal code to a known color name, if possible.
        Returns a tuple of (codetype, knownname) on success.
        Returns None on failure.
    """

    if not s.endswith('m'):
        # All codes end with 'm', so...
        return None
    if s.startswith('\033[38;5;'):
        # Extended fore.
        name = codes_reverse['fore'].get(s, None)
        if name is None:
            num = get_code_num(s)
            return ('extended fore', num)
        else:
            return ('extended fore', name)
    elif s.startswith('\033[48;5;'):
        # Extended back.
        name = codes_reverse['back'].get(s, None)
        if name is None:
            num = get_code_num(s)
            return ('extended back', num)
        else:
            return ('extended back', name)
    elif s.startswith('\033[38;2'):
        # RGB fore.
        vals = get_code_num_rgb(s)
        if vals is not None:
            return ('rgb fore', vals)
    elif s.startswith('\033[48;2'):
        # RGB back.
        vals = get_code_num_rgb(s)
        if vals is not None:
            return ('rgb back', vals)
    elif s.startswith('\033['):
        # Fore, back, style.
        number = get_code_num(s)
        # Get code type based on number.
        if (number <= 7) or (number == 22):
            codetype = 'style'
        elif (((number >= 30) and (number < 40)) or
                ((number >= 90) and (number < 100))):
            codetype = 'fore'
        elif (((number >= 40) and (number < 50)) or
                ((number >= 100) and (number < 110))):
            codetype = 'back'
        else:
            raise InvalidEscapeCode(
                number,
                'Expecting 0-7, 22, 30-39, or 40-49 for escape code',
            )

        name = codes_reverse[codetype].get(s, None)
        if name is not None:
            return (codetype, name)

    # Not a known escape code.
    return None