delete debug
[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
2a6238f5 6from functools import reduce
8c840a83 7
2a6238f5
O
8grey = lambda x: colored(x, 'grey', attrs=['bold'])
9red = lambda x: colored(x, 'red', attrs=['bold'])
10green = lambda x: colored(x, 'green', attrs=['bold'])
11yellow = lambda x: colored(x, 'yellow', attrs=['bold'])
12blue = lambda x: colored(x, 'blue', attrs=['bold'])
8c840a83 13magenta = lambda x: colored(x, 'magenta', attrs=['bold'])
2a6238f5
O
14cyan = lambda x: colored(x, 'cyan', attrs=['bold'])
15white = lambda x: colored(x, 'white', attrs=['bold'])
8c840a83 16
2a6238f5
O
17on_grey = lambda x: colored(x, 'white', 'on_grey', attrs=['bold'])
18on_red = lambda x: colored(x, 'white', 'on_red', attrs=['bold'])
19on_green = lambda x: colored(x, 'white', 'on_green', attrs=['bold'])
20on_yellow = lambda x: colored(x, 'white', 'on_yellow', attrs=['bold'])
21on_blue = lambda x: colored(x, 'white', 'on_blue', attrs=['bold'])
8c840a83 22on_magenta = lambda x: colored(x, 'white', 'on_magenta', attrs=['bold'])
2a6238f5
O
23on_cyan = lambda x: colored(x, 'white', 'on_cyan', attrs=['bold'])
24on_white = lambda x: colored(x, 'white', 'on_white', attrs=['bold'])
8c840a83 25
668db21c 26colors_shuffle = [grey, red, green, yellow, blue, magenta, cyan]
2a6238f5
O
27background_shuffle = [
28 on_grey,
29 on_red,
30 on_green,
31 on_yellow,
32 on_blue,
33 on_magenta,
34 on_cyan]
668db21c 35cyc = itertools.cycle(colors_shuffle[1:])
8c840a83
O
36
37
38def order_rainbow(s):
39 """
40 Print a string with ordered color with each character
41 """
2a6238f5
O
42 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
43 return reduce(lambda x, y: x + y, c)
44
8c840a83
O
45
46def random_rainbow(s):
47 """
48 Print a string with random color with each character
49 """
668db21c 50 c = [random.choice(colors_shuffle)(i) for i in s]
2a6238f5
O
51 return reduce(lambda x, y: x + y, c)
52
8c840a83
O
53
54def Memoize(func):
55 """
56 Memoize decorator
57 """
58 cache = {}
2a6238f5 59
8c840a83
O
60 @wraps(func)
61 def wrapper(*args):
62 if args not in cache:
63 cache[args] = func(*args)
64 return cache[args]
65 return wrapper
66
2a6238f5 67
8c840a83
O
68@Memoize
69def cycle_color(s):
70 """
668db21c 71 Cycle the colors_shuffle
8c840a83
O
72 """
73 return next(cyc)(s)
74
2a6238f5 75
8c840a83
O
76def ascii_art():
77 """
78 Draw the Ascii Art
79 """
80 fi = figlet_format('Rainbow Stream', font='doom')
81 print('\n'.join(
2a6238f5
O
82 [next(cyc)(i) for i in fi.split('\n')]
83 )
8c840a83 84 )