Copy Link
Add to Bookmark
Report

NULL mag Issue 09 17 256 colors in python

eZine's profile picture
Published in 
null magazine
 · 26 Dec 2020

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")

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT