def get_sprite_color(sprite):
    """
    Get the most regular color of a Pokemon sprite.
    
    Parameters
    ----------
    sprite: PIL image
        Pokemon sprite (read from a png image).
    
    Returns
    -------
    color_hex: string
        String with the hexadecimal representation of the most common
        color of the Pokemon sprite.
    """

    # Make sure input sprite has P mode (8-bit pixels)
    # See https://pillow.readthedocs.io/en/5.1.x/handbook/concepts.html#modes
    if sprite.mode != 'P':
        sprite = sprite.convert('P')
        
    # Convert to RGB.
    sprite_rgb = sprite.convert('RGB')
    
    # Get the sprite's color composition.
    colors = sorted(sprite_rgb.getcolors())
    
    # Get the most common color.
    color = colors[-1][1]
    
    # If the most common color is black (probably background),
    # chose the second most common color.
    if color == (0, 0, 0):
        color = colors[-2][1]
    
    # Convert to hex.
    color_hex = f'#{color[0]:02x}{color[1]:02x}{color[2]:02x}'

    return color_hex