Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Last active August 24, 2023 16:23
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mgedmin/2762225 to your computer and use it in GitHub Desktop.
Save mgedmin/2762225 to your computer and use it in GitHub Desktop.
Script to show all 256 colors supported by xterms
#!/usr/bin/python
"""Print a swatch using all 256 colors of 256-color-capable terminals."""
__author__ = "Marius Gedminas <marius@gedmin.as>"
__url__ = "https://gist.github.com/mgedmin/2762225"
__version__ = '2.0'
def hrun(start, width, padding=0):
return [None] * padding + list(range(start, start + width)) + [None] * padding
def vrun(start, width, height, padding=0):
return [hrun(s, width, padding)
for s in range(start, start + width * height, width)]
layout = [
vrun(0, 8, 2), # 16 standard xterm colors
vrun(16, 6, 6 * 6, 1), # 6x6x6 color cube
vrun(16 + 6 * 6 * 6, 8, 3), # 24 grey levels
]
def fg_seq(color):
return '\033[38;5;%dm' % color
def bg_seq(color):
return '\033[48;5;%dm' % color
reset_seq = '\033[0m'
def color_bar(seq, color, trail):
if color is None:
return '%s %s' % (reset_seq, trail)
else:
return '%s %03d%s' % (seq(color), color, trail)
def main():
for block in layout:
print("")
for row in block:
fg_bar = ''.join(color_bar(fg_seq, color, '') for color in row)
bg_bar = ''.join(color_bar(bg_seq, color, ' ') for color in row)
print('%s%s %s%s' % (fg_bar, reset_seq, bg_bar, reset_seq))
if __name__ == '__main__':
main()
@mgedmin
Copy link
Author

mgedmin commented Nov 4, 2014

Version 2.0: support Python 3 (as well as Python 2).

@Jim-Shaddix
Copy link

wow. Thats really cool!

@YuyeQingshan
Copy link

Really cool

@ivanbrennan
Copy link

This is really nicely done. I was interested in running it without Python, so I wrote a Bash translation: https://gist.github.com/ivanbrennan/8ce10a851851e5f04728d8da900ef1c5

@PeterlitsZo
Copy link

thanks, it help me a lot!!

@mgedmin
Copy link
Author

mgedmin commented Mar 19, 2020

A newer version is at https://github.com/mgedmin/scripts/blob/master/show-all-256-colors. You can give it color numbers as command-line arguments and it'll show just those colors, plus their RGB values that match my terminal.

@PeterlitsZo
Copy link

In my opinion if the new py program has its pic, it will be better~
I do like your pic that help me get the right color in my little program.

@mgedmin
Copy link
Author

mgedmin commented Mar 19, 2020

Yeah, without any arguments it prints the same diagram as before.

I use the RGB values to make Vim color schemes to have guifg=#rrggbb match my ctermfg=208 etc.

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