Merge branch 'master' of github.com:DTVD/rainbowstream
[rainbowstream.git] / rainbowstream / colors.py
... / ...
CommitLineData
1import random
2import itertools
3from functools import wraps
4from termcolor import *
5from pyfiglet import figlet_format
6from functools import reduce
7
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'])
13magenta = lambda x: colored(x, 'magenta', attrs=['bold'])
14cyan = lambda x: colored(x, 'cyan', attrs=['bold'])
15white = lambda x: colored(x, 'white', attrs=['bold'])
16
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'])
22on_magenta = lambda x: colored(x, 'white', 'on_magenta', attrs=['bold'])
23on_cyan = lambda x: colored(x, 'white', 'on_cyan', attrs=['bold'])
24on_white = lambda x: colored(x, 'white', 'on_white', attrs=['bold'])
25
26colors_shuffle = [grey, red, green, yellow, blue, magenta, cyan]
27background_shuffle = [
28 on_grey,
29 on_red,
30 on_green,
31 on_yellow,
32 on_blue,
33 on_magenta,
34 on_cyan]
35cyc = itertools.cycle(colors_shuffle[1:])
36
37
38def order_rainbow(s):
39 """
40 Print a string with ordered color with each character
41 """
42 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
43 return reduce(lambda x, y: x + y, c)
44
45
46def random_rainbow(s):
47 """
48 Print a string with random color with each character
49 """
50 c = [random.choice(colors_shuffle)(i) for i in s]
51 return reduce(lambda x, y: x + y, c)
52
53
54def Memoize(func):
55 """
56 Memoize decorator
57 """
58 cache = {}
59
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
67
68@Memoize
69def cycle_color(s):
70 """
71 Cycle the colors_shuffle
72 """
73 return next(cyc)(s)
74
75
76def ascii_art():
77 """
78 Draw the Ascii Art
79 """
80 fi = figlet_format('Rainbow Stream', font='doom')
81 print('\n'.join(
82 [next(cyc)(i) for i in fi.split('\n')]
83 )
84 )