def rgb_to_megadrive_vdp(red, green, blue):
    """Convert a 24bit RGB value into the 12 bit long 3bit/colour format used by the
    megadrive VDP in the format of:

    BBB0 GGG0 RRR0

    Args:
        red, green, blue:
            8bit integer values used to represent a colour between 0-255
    Returns:
        A 12bit integer formatted for use with the megadrive VDP

    Raises:
        ValueError: If red, green or blue is not 0-255

    """
    if red > 255 or green > 255 or blue > 255:
        raise ValueError

    if red < 0 or green < 0 or blue < 0:
        raise ValueError

    # Shift the colour to strip to 3bits then shift into the required format
    blue_formatted = (blue >> 5) << 9 # 0bBBB000000000
    green_formatted = (green >> 5) << 5 # 0bGGG00000
    red_formatted = (red >> 5) << 1 # 0bRRR0

    # Mask colours together to give the formatted colour
    return blue_formatted | green_formatted | red_formatted