How to run ipynb

  1. pip install jupyter
  • jupyter notebook my_notebook.ipynb

NOTE: cd your_work_dir and then jupyter notebook . is ok.

Import console_color library

In [1]:
# from console_color import RGB, Fore, Style, cprint, create_print
from console_color import *

Only Fore

In [2]:
cprint('red: RGB.RED', RGB.RED)
cprint('red: #FF0000', '#FF0000')  # It doesn't matter for string upper or lower, and if you don't want to add the sign of #, it's ok too.
cprint('red: (255, 0, 0)', (255, 0, 0))
red: RGB.RED
red: #FF0000
red: (255, 0, 0)

Only Background

In [3]:
cprint('red: RGB.RED', bg=RGB.RED)
cprint('red: #FF0000', bg='#ff0000')
cprint('red: (255, 0, 0)', bg=(255, 0, 0))
red: RGB.RED
red: #FF0000
red: (255, 0, 0)

Fore + Background

In [4]:
cprint('red: RGB.RED, RGB.YELLOW', RGB.RED, RGB.YELLOW)
cprint('red: FF0000, #FFFF00', 'FF0000', '#FFFF00')
cprint('red: (255, 0, 0), (255, 255, 0)', (255, 0, 0), (255, 255, 0))
red: RGB.RED, RGB.YELLOW
red: FF0000, #FFFF00
red: (255, 0, 0), (255, 255, 0)

Style

In [5]:
cprint('Italic', style=Style.ITALIC)  # The ipynb seems not to show both italic and strike well.
cprint('Italic and Bold', style=Style.BOLD + Style.ITALIC)
cprint('Strike', style=Style.STRIKE)
Italic
Italic and Bold
Strike

combine normal text

set pf = False

In [6]:
print(f"123 {cprint('Fore=Red, bg=Yellow, Style=Italic and Bold', RGB.RED, RGB.YELLOW, Style.BOLD + Style.ITALIC, False)} 456")
print(f"123 {cprint('BOLD', style=Style.BOLD, pf=False)} 456")
123 Fore=Red, bg=Yellow, Style=Italic and Bold 456
123 BOLD 456

keeping the setting

In [7]:
ry_print = create_print(fore='FF0000', bg='#FFFF00')  # ry: red and yellow
inner_ry_text = create_print('FF0000', '#FFFF00', Style.BOLD + Style.ITALIC, pf=False)
msg = "fore='FF0000', bg='#FFFF00'"
print(msg)
ry_print(msg)
print('...')
ry_print(msg)

print(f'normal text ... {inner_ry_text("fore=red, bg=yellow, style=bold+italic")} !!!')
fore='FF0000', bg='#FFFF00'
fore='FF0000', bg='#FFFF00'
...
fore='FF0000', bg='#FFFF00'
normal text ... fore=red, bg=yellow, style=bold+italic !!!

Color map

In [8]:
from itertools import permutations
row_string = ''
for count, rgb in enumerate(permutations((_ for _ in range(0, 256, 40)), 3)):
    count += 1
    fore_color = RGB.complementary_color(rgb)
    row_string += cprint(f'{RGB.tuple2hex_str(rgb)}  ', fore_color, rgb, pf=False)

    if count % 7 == 0:
        print(row_string),
        row_string = ''

print(row_string) if len(row_string) > 0 else None
#002850  #002878  #0028A0  #0028C8  #0028F0  #005028  #005078  
#0050A0  #0050C8  #0050F0  #007828  #007850  #0078A0  #0078C8  
#0078F0  #00A028  #00A050  #00A078  #00A0C8  #00A0F0  #00C828  
#00C850  #00C878  #00C8A0  #00C8F0  #00F028  #00F050  #00F078  
#00F0A0  #00F0C8  #280050  #280078  #2800A0  #2800C8  #2800F0  
#285000  #285078  #2850A0  #2850C8  #2850F0  #287800  #287850  
#2878A0  #2878C8  #2878F0  #28A000  #28A050  #28A078  #28A0C8  
#28A0F0  #28C800  #28C850  #28C878  #28C8A0  #28C8F0  #28F000  
#28F050  #28F078  #28F0A0  #28F0C8  #500028  #500078  #5000A0  
#5000C8  #5000F0  #502800  #502878  #5028A0  #5028C8  #5028F0  
#507800  #507828  #5078A0  #5078C8  #5078F0  #50A000  #50A028  
#50A078  #50A0C8  #50A0F0  #50C800  #50C828  #50C878  #50C8A0  
#50C8F0  #50F000  #50F028  #50F078  #50F0A0  #50F0C8  #780028  
#780050  #7800A0  #7800C8  #7800F0  #782800  #782850  #7828A0  
#7828C8  #7828F0  #785000  #785028  #7850A0  #7850C8  #7850F0  
#78A000  #78A028  #78A050  #78A0C8  #78A0F0  #78C800  #78C828  
#78C850  #78C8A0  #78C8F0  #78F000  #78F028  #78F050  #78F0A0  
#78F0C8  #A00028  #A00050  #A00078  #A000C8  #A000F0  #A02800  
#A02850  #A02878  #A028C8  #A028F0  #A05000  #A05028  #A05078  
#A050C8  #A050F0  #A07800  #A07828  #A07850  #A078C8  #A078F0  
#A0C800  #A0C828  #A0C850  #A0C878  #A0C8F0  #A0F000  #A0F028  
#A0F050  #A0F078  #A0F0C8  #C80028  #C80050  #C80078  #C800A0  
#C800F0  #C82800  #C82850  #C82878  #C828A0  #C828F0  #C85000  
#C85028  #C85078  #C850A0  #C850F0  #C87800  #C87828  #C87850  
#C878A0  #C878F0  #C8A000  #C8A028  #C8A050  #C8A078  #C8A0F0  
#C8F000  #C8F028  #C8F050  #C8F078  #C8F0A0  #F00028  #F00050  
#F00078  #F000A0  #F000C8  #F02800  #F02850  #F02878  #F028A0  
#F028C8  #F05000  #F05028  #F05078  #F050A0  #F050C8  #F07800  
#F07828  #F07850  #F078A0  #F078C8  #F0A000  #F0A028  #F0A050  
#F0A078  #F0A0C8  #F0C800  #F0C828  #F0C850  #F0C878  #F0C8A0  

Try by yourself

If you have no idea how to use this library, you can input by indicate, and then it will tell you what the grammar you should write.

In [9]:
text = chr(34) + input('text:') + chr(34)
in_fore = input('Fore (#XXYYRR) (r, g, b):')
if in_fore:
    in_fore = str(eval(in_fore)) if in_fore.find(',') != -1 else chr(34) + in_fore + chr(34)

in_bg = input('Background (#XXYYRR) (r, g, b):')
if in_bg:
    in_bg = str(eval(in_bg)) if in_bg.find(',') != -1 else chr(34) + in_bg + chr(34)

in_style = input('Style (BOLD, ITALIC, URL, STRIKE) sep=" ":').split(' ')
if in_style:
    in_style = '+'.join([f'Style.{_.upper()}' for _ in in_style])
print_cmd = f"cprint({text}{', fore=' + in_fore if in_fore else ''}" \
            f"{', bg=' + in_bg if in_bg else ''}" \
            f"{', style=' + in_style if in_style else ''})"
print(f'grammar: {cprint(print_cmd, RGB.GREEN, RGB.BLACK, pf=False)}')
exec(print_cmd)
grammar: cprint("Hello World", fore=(255, 0, 0), bg="ffff00", style=Style.BOLD+Style.URL)
Hello World