Skip to content

Instantly share code, notes, and snippets.

@Alives
Created February 11, 2022 17:34
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 Alives/35394d142c96e3767659314666c58316 to your computer and use it in GitHub Desktop.
Save Alives/35394d142c96e3767659314666c58316 to your computer and use it in GitHub Desktop.
Generate all 256 Foreground and Background terminal colors with codes
#!/usr/bin/env python
# Original Author of the perl script: Todd Larason <jtl@molehill.org>
# $XFree86: xc/programs/xterm/vttests/256colors2.pl,v 1.2 2002/03/26 01:46:43 dickey Exp $
import sys
RESET = "\x1b[0m"
LAYERS = {38: 'foreground', 48: 'background'}
def bg(layer, color):
sys.stdout.write("%s%3d%s " % ("\x1b[%d;5;%sm" % (layer, color), color, RESET))
def fg(layer, color):
sys.stdout.write("%s %2d %s " % ("\x1b[%d;5;%sm" % (layer, color), color, RESET))
if len(sys.argv) > 1:
for color in map(int, sys.argv[1:]):
fg(38, color)
bg(48, color)
sys.stdout.write('\n')
sys.exit()
# System colors
max = 16
for layer in LAYERS.keys():
print "System colors (%s):" % LAYERS[layer]
for color in xrange(0,max):
fg(layer, color)
if color == (max - 1) or color == ((max / 2) - 1):
print
print
# Color cube 6x6x6
for layer in LAYERS.keys():
print "Color cube 6x6x6 (%s):" % LAYERS[layer]
for green in xrange(0,6):
for red in xrange(0,6):
for blue in xrange(0,6):
color = 16 + (red * 36) + (green * 6) + blue
bg(layer, color)
sys.stdout.write(" ")
print
print
# Grayscale ramp
for layer in LAYERS.keys():
print "System colors (%s):" % LAYERS[layer]
for color in xrange(232,256):
sys.stdout.write("%s %2d %s " % ("\x1b[%d;5;%sm" % (layer, color), color, RESET))
print "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment