Skip to content

Instantly share code, notes, and snippets.

@sleibrock
Created August 9, 2016 21:09
Show Gist options
  • Save sleibrock/0bd00b366fe3e56b4d3c40c84b2b3a27 to your computer and use it in GitHub Desktop.
Save sleibrock/0bd00b366fe3e56b4d3c40c84b2b3a27 to your computer and use it in GitHub Desktop.
Prints all ANSI colors in your terminal
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Basic colors script
prints all ANSI colors from 0 to 256
to show you your term's color mapping
"""
from sys import argv
BEGIN = "\033["
RESET = "\033[0m"
PRINT_TEXT=False
HELP_MSG = """
Colors Demo
Displays your terminal's available colors as defined by
your terminal program. For most xterm-based terminals
you can change it through modifying the $HOME/.Xresources
and applying `xrdb -merge $HOME/.Xresources`
For Mac, ???????????????????
For Windows, ???????????????
Requires: Python
Options:
-h, --help - displays this crappy message
-t, --text - shows hexcodes of each color
--pacman - prints a Pacman!
--haskell - prints the Haskell logo
"""
pacman = [
"...xxx....",
"..xxx.x...",
".xxxxxxx..",
".xxx......",
"..xxx.....",
"...xxx....",
]
haskell = [
".x.xx.......",
".xx.xx......",
"..xx.xx.xxxx",
"...xx.xx....",
"..xx.xxxx.xx",
".xx.xx..xx..",
".x.xx....xx.",
]
def fg(i):
return "{}38;5;{}m".format(BEGIN, i)
def bg(i):
return "{}48;5;{}m".format(BEGIN,i)
def combo(c, b, s):
return "{}{}{}{}".format(bg(b), fg(c), s, RESET, RESET)
def hc(x):
return hex(x).split('x').pop()[-2:].zfill(2) if PRINT_TEXT else " "
def print_chart():
# Print a title
bc = 2
banner = combo(0, bc, " ")
print("".join([banner for x in range(16)]))
print("{}{}{}".format(banner, combo(2, 0, "Colors Demo".center(28)), banner))
print("".join([banner for x in range(16)]))
print()
print("".join([combo(256-x, x, hc(x)) for x in range(16)]))
print()
for x in range(6):
s = ""
for y in range(36):
num = 16+x+(y*6)
inv = 233
if num < 124:
inv = 255
s += combo(inv, num, hc(num))
print(s)
#print("".join([combo(256-(16+x+(y*6)), (16+x+(y*6)), hc((16+x+(y*6)))) for y in range(36)]))
print()
print("".join([combo(256-x, x, hc(x)) for x in range(232,257)]))
return None
def print_array(pic_array, color):
for row in pic_array:
print("".join([(combo(0, color, " ") if char == "x" else " ") for char in row]))
return None
if __name__ == "__main__":
argv.pop(0)
if "-h" in argv or "--help" in argv:
print(HELP_MSG)
quit()
if "--pacman" in argv:
print_array(pacman, 3)
quit()
if "--haskell" in argv:
print_array(haskell, 5)
quit()
if "-t" in argv or "--text" in argv:
PRINT_TEXT = True
print_chart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment