Skip to content

Instantly share code, notes, and snippets.

@Expector-Hutch
Created February 5, 2023 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Expector-Hutch/92216cdfec10f30cd2f5005928baf50d to your computer and use it in GitHub Desktop.
Save Expector-Hutch/92216cdfec10f30cd2f5005928baf50d to your computer and use it in GitHub Desktop.
def printc(*args, fg = '', bg = '', light: bool = False, style = (), sep=' ', end='\n', file=None, flush=False):
def prints(arg): return print(arg, end='', file=file, flush=flush)
def decoder(data):
if type(data) is str:
com_table = {'black': 0, 'red': 1, 'green': 2, 'yellow': 3,
'blue': 4, 'magenta': 5, 'cyan': 6, 'white': 7}
return com_table[data]
elif type(data) is int:
return f'8;5;{data}'
elif type(data) is tuple:
if len(data) == 3:
return f'8;2;{data[0]};{data[1]};{data[2]}'
if not light or type(fg) is not str or type(bg) is not str:
if fg:
prints(f"\033[3{decoder(fg)}m")
if bg:
prints(f"\033[4{decoder(bg)}m")
else:
if fg:
prints(f"\033[9{decoder(fg)}m")
if bg:
prints(f"\033[10{decoder(bg)}m")
if type(style) is tuple:
for s in style:
if type(s) is int:
prints(f'\033[{s}m')
elif type(s) is str:
com_table = {'reset': 0, 'bold': 1, 'faint': 2, 'italic': 3,
'underline': 4, 'invert': 7, 'hide': 8, 'delete': 9}
prints(f'\033[{com_table[s]}m')
elif type(style) is str:
com_table = {'reset': 0, 'bold': 1, 'faint': 2, 'italic': 3,
'underline': 4, 'invert': 7, 'hide': 8, 'delete': 9}
prints(f'\033[{com_table[style]}m')
elif type(style) is int:
prints(f'\033[{style}m')
print(*args, sep=sep, end=end, file=file, flush=flush)
prints('\033[0m')
clear = lambda: print('\033[H\033[2J')
if __name__ == '__main__':
printc('''Copyright(c) 2022 Expector
_ _
_ __ _ __(_)_ __ | |_ ___
| '_ \| '__| | '_ \| __/ __|
| |_) | | | | | | | || (__
| .__/|_| |_|_| |_|\__\___|
|_|''', fg=(0x66, 0xcc, 0xff), bg='black', style='bold')
input('Press enter to see the palette...')
clear()
print('\n4-bit 16-color')
for color in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']:
printc(f' {color} ', bg=color, end='')
print()
for color in ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']:
printc(f' light {color} ', bg=color, light=True, end='')
print('\n8-bit 256-color')
for color in range(0, 256):
printc(f' {color} ', bg=color, end='')
print('\n24-bit true color')
for color in range(0, 77):
printc(' ', bg=(255-(color*255//76), color*510//76 if color <
38 else 510-color*510//76, color*255//76), end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment