RGB not bold
[rainbowstream.git] / rainbowstream / colors.py
CommitLineData
2a6238f5
O
1import random
2import itertools
8c840a83 3from functools import wraps
8c840a83
O
4from pyfiglet import figlet_format
5
6fa09c14
VNM
6
7def basic_color(code):
8 """
9 16 colors supported
10 """
11 def inner(text, bold=True):
533edcdb
VNM
12 c = code
13 if bold:
14 c = "1;%s" % c
15 return "\033[%sm%s\033[0m" % (c, text)
16 return inner
8c840a83 17
6fa09c14
VNM
18
19def RGB(code):
20 """
21 256 colors supported
22 """
93384849 23 def inner(text):
6fa09c14 24 c = code
6fa09c14
VNM
25 return "\033[38;5;%sm%s\033[0m" % (c, text)
26 return inner
27
28
29default = basic_color('39')
30black = basic_color('30')
31red = basic_color('31')
32green = basic_color('32')
33yellow = basic_color('33')
34blue = basic_color('34')
35magenta = basic_color('35')
36cyan = basic_color('36')
37grey = basic_color('90')
38light_red = basic_color('91')
39light_green = basic_color('92')
40light_yellow = basic_color('93')
41light_blue = basic_color('94')
42light_magenta = basic_color('95')
43light_cyan = basic_color('96')
44white = basic_color('97')
45
46on_default = basic_color('49')
47on_black = basic_color('40')
48on_red = basic_color('41')
49on_green = basic_color('42')
50on_yellow = basic_color('43')
51on_blue = basic_color('44')
52on_magenta = basic_color('45')
53on_cyan = basic_color('46')
54on_grey = basic_color('100')
55on_light_red = basic_color('101')
56on_light_green = basic_color('102')
57on_light_yellow = basic_color('103')
58on_light_blue = basic_color('104')
59on_light_magenta = basic_color('105')
60on_light_cyan = basic_color('106')
61on_white = basic_color('107')
533edcdb
VNM
62
63colors_shuffle = [
64 grey,
65 light_red,
66 light_green,
67 light_yellow,
68 light_blue,
69 light_magenta,
70 light_cyan]
2a6238f5
O
71background_shuffle = [
72 on_grey,
533edcdb
VNM
73 on_light_red,
74 on_light_green,
75 on_light_yellow,
76 on_light_blue,
77 on_light_magenta,
78 on_light_cyan]
668db21c 79cyc = itertools.cycle(colors_shuffle[1:])
8c840a83
O
80
81
82def order_rainbow(s):
83 """
84 Print a string with ordered color with each character
85 """
2a6238f5
O
86 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
87 return reduce(lambda x, y: x + y, c)
88
8c840a83
O
89
90def random_rainbow(s):
91 """
92 Print a string with random color with each character
93 """
668db21c 94 c = [random.choice(colors_shuffle)(i) for i in s]
2a6238f5
O
95 return reduce(lambda x, y: x + y, c)
96
8c840a83
O
97
98def Memoize(func):
99 """
100 Memoize decorator
101 """
102 cache = {}
2a6238f5 103
8c840a83
O
104 @wraps(func)
105 def wrapper(*args):
106 if args not in cache:
107 cache[args] = func(*args)
108 return cache[args]
109 return wrapper
110
2a6238f5 111
8c840a83
O
112@Memoize
113def cycle_color(s):
114 """
668db21c 115 Cycle the colors_shuffle
8c840a83
O
116 """
117 return next(cyc)(s)
118
2a6238f5 119
42fde775 120def ascii_art(text):
8c840a83
O
121 """
122 Draw the Ascii Art
123 """
42fde775 124 fi = figlet_format(text, font='doom')
8c840a83 125 print('\n'.join(
2a6238f5
O
126 [next(cyc)(i) for i in fi.split('\n')]
127 )
8c840a83 128 )