Skip to content

Instantly share code, notes, and snippets.

@sooop
Last active December 27, 2020 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sooop/ff1183097bfa7d9f1663b9d6bd516193 to your computer and use it in GitHub Desktop.
Save sooop/ff1183097bfa7d9f1663b9d6bd516193 to your computer and use it in GitHub Desktop.
print colored text in python
COLORS = dict(zip(range(1, 10), 'black red green yellow blue magenta'
' cyan white reset'.split()))
class BG:
black = '\033[40m'
red = '\033[41m'
green = '\033[42m'
yellow = '\033[43m'
blue = '\033[44m'
magenta = '\033[45m'
cyan = '\033[46m'
white = '\033[47m'
reset = '\033[49m'
class FG:
black = '\033[30m'
red = '\033[31m'
green = '\033[32m'
yellow = '\033[33m'
blue = '\033[34m'
magenta = '\033[35m'
cyan = '\033[36m'
white = '\033[37m'
reset = '\033[39m'
class BRIGHT:
bright = '\033[1m'
dim = '\033[2m'
normal = '\033[22m'
class Color:
resetall = '\033[0m'
@classmethod
def colored(self, msg, foreground='white', background='black', bright=0):
xBRIGHT, xBG, xFG = '', '', ''
if bright is 1:
xBRIGHT = BRIGHT.bright
elif bright is 2:
xBRIGHT = BRIGHT.dim
else:
xBRIGHT = BRIGHT.normal
if hasattr(BG, background):
xBG = getattr(BG, background)
if hasattr(FG, foreground):
xFG = getattr(FG, foreground)
return '{}{}{}{}{}'.format(
xBRIGHT, xBG, xFG, msg, Color.resetall)
if __name__ == '__main__':
msg = input('>>> ')
colors = list(COLORS.values())
for i in range(0, len(colors), 3):
for fg in colors:
line = ' '.join([''.join(Color.colored(msg, fg, colors[i+x], y)
for y in (1, 2, 0))
for x in range(3)])
print(line)
print()
@sooop
Copy link
Author

sooop commented Oct 25, 2016

2016-10-25_111139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment