Checks system for theme information.

        First checks for the environment variable COLORFGBG.
        Next, queries terminal, supported on Windows and xterm, perhaps others.
        See notes on get_color().

        Returns:
            str, None: 'dark', 'light', None if no information.

def get_theme():
    ''' Checks system for theme information.

        First checks for the environment variable COLORFGBG.
        Next, queries terminal, supported on Windows and xterm, perhaps others.
        See notes on get_color().

        Returns:
            str, None: 'dark', 'light', None if no information.
    '''
    theme = None
    log.debug('COLORFGBG: %s', env.COLORFGBG)
    if env.COLORFGBG:
        FG, _, BG = env.COLORFGBG.partition(';')
        theme = 'dark' if BG < '8' else 'light'  # background wins
    else:
        if os_name == 'nt':
            from .windows import get_color as _get_color  # avoid Unbound Local
            color_id = _get_color('background')
            theme = 'dark' if color_id < 8 else 'light'
        elif os_name == 'posix':
            if env.TERM in ('linux', 'fbterm'):  # default
                theme = 'dark'
            elif sys.platform.startswith('freebsd'):  # vga console :-/
                theme = 'dark'
            else:
                # try xterm - find average across rgb
                colors = get_color('background')  # bg wins
                if colors:
                    colors = tuple(int(cm[:2], 16) for cm in colors)
                    avg = sum(colors) / len(colors)
                    theme = 'dark' if avg < 128 else 'light'

    log.debug('%r', theme)
    return theme