import os, sys, fcntl

colors_rgb = [
    0x00, 0x00, 0x00,	# black
    0xd8, 0x18, 0x60,	# red
    0x60, 0xff, 0x60,	# green
    0xf9, 0xfd, 0x75,	# yellow
    0x5d, 0x97, 0xfa,	# blue
    0xa7, 0x8e, 0xdb,	# magenta
    0x46, 0x95, 0xc8,	# cyan
    0xbb, 0xbb, 0xbb,	# white
    0x4d, 0x4d, 0x4d,	# darkgray
    0xf0, 0x00, 0x60,	# bright red
    0x70, 0xff, 0x70,	# bright green
    0xf9, 0xfd, 0x80,	# bright yellow
    0x67, 0x9d, 0xfa,	# bright blue
    0xad, 0x97, 0xdb,	# bright magenta
    0x5a, 0x9d, 0xc8,	# bright cyan
    0xee, 0xee, 0xee	# bright white
]

PIO_CMAP = 0x4b71

try:
    tty = os.open("/dev/tty", os.O_RDWR)
    fcntl.ioctl(tty, PIO_CMAP, bytearray(colors_rgb))
    os.close(tty)
    sys.stdout.write('\033[40m') # background = black
    sys.stdout.write('\033[37m') # foreground = white
    sys.stdout.write('\033[8]')  # store bg and fg
    sys.stdout.flush()
except:
    exit(1)

exit(0)
