Copy Link
Add to Bookmark
Report
NULL mag Issue 09 17 256 colors in python
the following code is from shinobi. it's a script to display all full 256 ansi colors in a terminal that supports them. perhaps using 256 colors in the bbs scene is a heresy but for making CLI apps/scripts it's awesome! thank you shinobi!
#!/usr/bin/python3
import sys
printable_colours=256
sys.stdout.write(u"\u001b[2J\u001b[H")
def print_colour(i):
sys.stdout.write(u"\u001b[38;5;"+str(i)+"m"+str(i).zfill(3))
sys.stdout.write(u"\u001b[0m ")
def print_run(block_begin,block_cols):
i=block_begin
for i in range(block_begin,block_begin+block_cols,1):
if (i<printable_colours):
print_colour(i)
sys.stdout.write(" ")
def print_blocks(start,end,block_cols,block_rows,blocks_per_line):
block_length=block_cols*block_rows
for i in range(start,end+1,((blocks_per_line)*block_length)):
print("\n") # Space before each set of blocks
for row in range(0,block_rows,1):
for block in range(0,blocks_per_line,1):
print_run(i+(block*block_length),block_cols)
i+=(block_cols*1) # Prepare to print the next row
sys.stdout.write("\n")
print_run(0,16)
print_blocks(16,231,6,6,3)
print_blocks(232,255,12,2,1)
sys.stdout.write("\n")