Created
October 5, 2017 12:35
-
-
Save mosquito/331bdd986b5d57363e88b5a0b56e6199 to your computer and use it in GitHub Desktop.
Set RGB color to string in terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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