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