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