def set_color(fg_col, bg_col):
global last_fg_col
global last_bg_col
if fg_col == last_fg_col and bg_col == last_bg_col:
return
last_fg_col = fg_col
last_bg_col = bg_col
if is_windows:
# convert from (rgb+2) to bgr
fg_col = rgb3_to_bgr3(fg_col-2)
bg_col = rgb3_to_bgr3(bg_col-2)
col_attr = fg_col | (bg_col << 4)
stdout_handle = ctypes.windll.kernel32.GetStdHandle(ctypes.c_ulong(-11))
ctypes.windll.kernel32.SetConsoleTextAttribute(stdout_handle, col_attr)
else:
color = str(fg_col + 28)
sys.stdout.write('\x1b['+color+'m')
color = str(bg_col + 38)
sys.stdout.write('\x1b['+color+'m')
# TODO: any other encodings to check for?
评论列表
文章目录