fix color
[rainbowstream.git] / rainbowstream / colors.py
... / ...
CommitLineData
1import random
2import itertools
3from functools import wraps
4from termcolor import *
5from pyfiglet import figlet_format
6
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
14
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')
26light_yellow = color_code('93')
27light_blue = color_code('94')
28light_magenta = color_code('95')
29light_cyan = color_code('96')
30white = color_code('97')
31
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]
57background_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]
65cyc = itertools.cycle(colors_shuffle[1:])
66
67
68def 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
76def 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
84def 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
99def cycle_color(s):
100 """
101 Cycle the colors_shuffle
102 """
103 return next(cyc)(s)
104
105
106def 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 )