Skip to content

Instantly share code, notes, and snippets.

@mosquito
Created October 5, 2017 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosquito/331bdd986b5d57363e88b5a0b56e6199 to your computer and use it in GitHub Desktop.
Save mosquito/331bdd986b5d57363e88b5a0b56e6199 to your computer and use it in GitHub Desktop.
Set RGB color to string in terminal
def colorize(text, fg=None, bg=None):
result = ''
def parse_hex(h):
h = h.strip("#").ljust(6, '0')
return [int(h[i+i:i+i+2], 16) for i in range(3)]
if bg:
result += '\x1b[48;2;{};{};{}m'.format(*parse_hex(bg))
if fg:
result += '\x1b[38;2;{};{};{}m'.format(*parse_hex(fg))
result += text
result += '\x1b[0m'
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment