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