change theme base on config
[rainbowstream.git] / rainbowstream / colors.py
... / ...
CommitLineData
1import random
2import itertools
3from functools import wraps
4from pyfiglet import figlet_format
5from .config import *
6
7
8def basic_color(code):
9 """
10 16 colors supported
11 """
12 def inner(text, bold=True):
13 c = code
14 if bold:
15 c = "1;%s" % c
16 return "\033[%sm%s\033[0m" % (c, text)
17 return inner
18
19
20def RGB(code):
21 """
22 256 colors supported
23 """
24 def inner(text):
25 c = code
26 return "\033[38;5;%sm%s\033[0m" % (c, text)
27 return inner
28
29
30default = basic_color('39')
31black = basic_color('30')
32red = basic_color('31')
33green = basic_color('32')
34yellow = basic_color('33')
35blue = basic_color('34')
36magenta = basic_color('35')
37cyan = basic_color('36')
38grey = basic_color('90')
39light_red = basic_color('91')
40light_green = basic_color('92')
41light_yellow = basic_color('93')
42light_blue = basic_color('94')
43light_magenta = basic_color('95')
44light_cyan = basic_color('96')
45white = basic_color('97')
46
47on_default = basic_color('49')
48on_black = basic_color('40')
49on_red = basic_color('41')
50on_green = basic_color('42')
51on_yellow = basic_color('43')
52on_blue = basic_color('44')
53on_magenta = basic_color('45')
54on_cyan = basic_color('46')
55on_grey = basic_color('100')
56on_light_red = basic_color('101')
57on_light_green = basic_color('102')
58on_light_yellow = basic_color('103')
59on_light_blue = basic_color('104')
60on_light_magenta = basic_color('105')
61on_light_cyan = basic_color('106')
62on_white = basic_color('107')
63
64colors_shuffle = [locals()[i.encode('utf8')] if not i.startswith('RGB_') else RGB(int(i[4:])) for i in c['CYCLE_COLOR']]
65
66background_shuffle = [
67 on_grey,
68 on_light_red,
69 on_light_green,
70 on_light_yellow,
71 on_light_blue,
72 on_light_magenta,
73 on_light_cyan]
74cyc = itertools.cycle(colors_shuffle[1:])
75
76
77def order_rainbow(s):
78 """
79 Print a string with ordered color with each character
80 """
81 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
82 return reduce(lambda x, y: x + y, c)
83
84
85def random_rainbow(s):
86 """
87 Print a string with random color with each character
88 """
89 c = [random.choice(colors_shuffle)(i) for i in s]
90 return reduce(lambda x, y: x + y, c)
91
92
93def Memoize(func):
94 """
95 Memoize decorator
96 """
97 cache = {}
98
99 @wraps(func)
100 def wrapper(*args):
101 if args not in cache:
102 cache[args] = func(*args)
103 return cache[args]
104 return wrapper
105
106
107@Memoize
108def cycle_color(s):
109 """
110 Cycle the colors_shuffle
111 """
112 return next(cyc)(s)
113
114
115def ascii_art(text):
116 """
117 Draw the Ascii Art
118 """
119 fi = figlet_format(text, font='doom')
120 print('\n'.join(
121 [next(cyc)(i) for i in fi.split('\n')]
122 )
123 )