Skip to content

Instantly share code, notes, and snippets.

@Bengt
Forked from christian-oudard/term_colors.py
Last active August 29, 2015 14:04
Show Gist options
  • Save Bengt/4ce622847577dbed351a to your computer and use it in GitHub Desktop.
Save Bengt/4ce622847577dbed351a to your computer and use it in GitHub Desktop.

The output as an image with colors is:

image image image image image

Plaintext output:

[bengt@bengt-ssd 4ce622847577dbed351a]$ python term_colors.py 
System colors:
                
                

RGB color cube, 6x6x6:
    16 - #000000
    17 - #000033
    18 - #000066
    19 - #000099
    20 - #0000cc
    21 - #0000ff
    22 - #003300
    23 - #003333
    24 - #003366
    25 - #003399
    26 - #0033cc
    27 - #0033ff
    28 - #006600
    29 - #006633
    30 - #006666
    31 - #006699
    32 - #0066cc
    33 - #0066ff
    34 - #009900
    35 - #009933
    36 - #009966
    37 - #009999
    38 - #0099cc
    39 - #0099ff
    40 - #00cc00
    41 - #00cc33
    42 - #00cc66
    43 - #00cc99
    44 - #00cccc
    45 - #00ccff
    46 - #00ff00
    47 - #00ff33
    48 - #00ff66
    49 - #00ff99
    50 - #00ffcc
    51 - #00ffff

    52 - #330000
    53 - #330033
    54 - #330066
    55 - #330099
    56 - #3300cc
    57 - #3300ff
    58 - #333300
    59 - #333333
    60 - #333366
    61 - #333399
    62 - #3333cc
    63 - #3333ff
    64 - #336600
    65 - #336633
    66 - #336666
    67 - #336699
    68 - #3366cc
    69 - #3366ff
    70 - #339900
    71 - #339933
    72 - #339966
    73 - #339999
    74 - #3399cc
    75 - #3399ff
    76 - #33cc00
    77 - #33cc33
    78 - #33cc66
    79 - #33cc99
    80 - #33cccc
    81 - #33ccff
    82 - #33ff00
    83 - #33ff33
    84 - #33ff66
    85 - #33ff99
    86 - #33ffcc
    87 - #33ffff

    88 - #660000
    89 - #660033
    90 - #660066
    91 - #660099
    92 - #6600cc
    93 - #6600ff
    94 - #663300
    95 - #663333
    96 - #663366
    97 - #663399
    98 - #6633cc
    99 - #6633ff
   100 - #666600
   101 - #666633
   102 - #666666
   103 - #666699
   104 - #6666cc
   105 - #6666ff
   106 - #669900
   107 - #669933
   108 - #669966
   109 - #669999
   110 - #6699cc
   111 - #6699ff
   112 - #66cc00
   113 - #66cc33
   114 - #66cc66
   115 - #66cc99
   116 - #66cccc
   117 - #66ccff
   118 - #66ff00
   119 - #66ff33
   120 - #66ff66
   121 - #66ff99
   122 - #66ffcc
   123 - #66ffff

   124 - #990000
   125 - #990033
   126 - #990066
   127 - #990099
   128 - #9900cc
   129 - #9900ff
   130 - #993300
   131 - #993333
   132 - #993366
   133 - #993399
   134 - #9933cc
   135 - #9933ff
   136 - #996600
   137 - #996633
   138 - #996666
   139 - #996699
   140 - #9966cc
   141 - #9966ff
   142 - #999900
   143 - #999933
   144 - #999966
   145 - #999999
   146 - #9999cc
   147 - #9999ff
   148 - #99cc00
   149 - #99cc33
   150 - #99cc66
   151 - #99cc99
   152 - #99cccc
   153 - #99ccff
   154 - #99ff00
   155 - #99ff33
   156 - #99ff66
   157 - #99ff99
   158 - #99ffcc
   159 - #99ffff

   160 - #cc0000
   161 - #cc0033
   162 - #cc0066
   163 - #cc0099
   164 - #cc00cc
   165 - #cc00ff
   166 - #cc3300
   167 - #cc3333
   168 - #cc3366
   169 - #cc3399
   170 - #cc33cc
   171 - #cc33ff
   172 - #cc6600
   173 - #cc6633
   174 - #cc6666
   175 - #cc6699
   176 - #cc66cc
   177 - #cc66ff
   178 - #cc9900
   179 - #cc9933
   180 - #cc9966
   181 - #cc9999
   182 - #cc99cc
   183 - #cc99ff
   184 - #cccc00
   185 - #cccc33
   186 - #cccc66
   187 - #cccc99
   188 - #cccccc
   189 - #ccccff
   190 - #ccff00
   191 - #ccff33
   192 - #ccff66
   193 - #ccff99
   194 - #ccffcc
   195 - #ccffff

   196 - #ff0000
   197 - #ff0033
   198 - #ff0066
   199 - #ff0099
   200 - #ff00cc
   201 - #ff00ff
   202 - #ff3300
   203 - #ff3333
   204 - #ff3366
   205 - #ff3399
   206 - #ff33cc
   207 - #ff33ff
   208 - #ff6600
   209 - #ff6633
   210 - #ff6666
   211 - #ff6699
   212 - #ff66cc
   213 - #ff66ff
   214 - #ff9900
   215 - #ff9933
   216 - #ff9966
   217 - #ff9999
   218 - #ff99cc
   219 - #ff99ff
   220 - #ffcc00
   221 - #ffcc33
   222 - #ffcc66
   223 - #ffcc99
   224 - #ffcccc
   225 - #ffccff
   226 - #ffff00
   227 - #ffff33
   228 - #ffff66
   229 - #ffff99
   230 - #ffffcc
   231 - #ffffff


Grayscale ramp, with RGB grays:
   232 - #000000
   233 - #101010
   234 - #202020
   235 - #303030
   236 - #404040
   237 - #505050
   238 - #606060
   239 - #707070
   240 - #808080
   241 - #909090
   242 - #a0a0a0
   243 - #b0b0b0
   244 - #c0c0c0
   245 - #d0d0d0
   246 - #e0e0e0
   247 - #f0f0f0
   248 - #101010
   249 - #111111
   250 - #121212
   251 - #131313
   252 - #141414
   253 - #151515
   254 - #161616
   255 - #171717

[bengt@bengt-ssd 4ce622847577dbed351a]$ 
from __future__ import print_function
"""
Utilities for 256 color support in terminals.
Adapted from:
http://stackoverflow.com/questions/1403353/256-color-terminal-library-for-ruby
The color palette is indexed as follows:
0-15: System colors
0 black
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 light gray
8 dark gray
9 bright red
10 bright green
11 bright yellow
12 bright blue
13 bright magenta
14 bright cyan
15 white
16-231: 6x6x6 Color Cube
All combinations of red, green, and blue from 0 to 5.
232-255: Grayscale Ramp
24 shades of gray, not including black and white.
"""
SYSTEM_COLORS = {0: "black",
1: "red",
2: "green",
3: "yellow",
4: "blue",
5: "magenta",
6: "cyan",
7: "light gray",
8: "dark gray",
9: "bright red",
10: "bright green",
11: "bright yellow",
12: "bright blue",
13: "bright magenta",
14: "bright cyan",
15: "white"}
def rgb(red, green, blue):
"""
Calculate the palette index of a color in the 6x6x6 color cube.
The red, green and blue arguments may range from 0 to 5.
"""
return 16 + (red * 36) + (green * 6) + blue
def gray(value):
"""
Calculate the palette index of a color in the grayscale ramp.
The value argument may range from 0 to 23.
"""
return 232 + value
def set_color(fg=None, bg=None):
"""
Print escape codes to set the terminal color.
fg and bg are indices into the color palette for the foreground and
background colors.
"""
print(_set_color(fg, bg), end='')
def _set_color(fg=None, bg=None):
result = ''
if fg:
result += '\x1b[38;5;%dm' % fg
if bg:
result += '\x1b[48;5;%dm' % bg
return result
def reset_color():
"""
Reset terminal color to default.
"""
print(_reset_color(), end='')
def _reset_color():
return '\x1b[0m'
def print_color(*args, **kwargs):
"""
Print function, with extra arguments fg and bg to set colors.
"""
fg = kwargs.pop('fg', None)
bg = kwargs.pop('bg', None)
set_color(fg, bg)
print(*args, **kwargs)
reset_color()
def format_color(string, fg=None, bg=None):
return _set_color(fg, bg) + string + _reset_color()
def print_test_graphic():
# Print a test graphic showing all colors.
print('System colors:')
for (number, name) in SYSTEM_COLORS.items():
print_color(" ", bg=number, end='')
print(" " + str(number).rjust(3) + " - " + name, end='')
print()
print()
print('RGB color cube, 6x6x6:')
for red in range(6):
for green in range(6):
for blue in range(6):
print_color(' ', bg=rgb(red, green, blue), end='')
s = ' ' + str(rgb(red, green, blue)).rjust(3) + ' - #'
for channel in [red, green, blue]:
s += str(hex(51 * channel))[2:].ljust(2, "0")
print(s)
print()
print('Grayscale ramp, with RGB grays:')
for value in range(24):
print_color(' ', bg=gray(value), end='')
s = ' ' + str(232 + value) + ' - #'
for channel in [red, green, blue]:
s += str(hex(value))[2:].ljust(2, "0")
print(s)
def print_colors_js():
pass
if __name__ == '__main__':
print_test_graphic()
print_colors_js()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment