change theme base on config
[rainbowstream.git] / rainbowstream / colors.py
CommitLineData
2a6238f5
O
1import random
2import itertools
8c840a83 3from functools import wraps
8c840a83 4from pyfiglet import figlet_format
632c6fa5 5from .config import *
8c840a83 6
6fa09c14
VNM
7
8def basic_color(code):
9 """
10 16 colors supported
11 """
12 def inner(text, bold=True):
533edcdb
VNM
13 c = code
14 if bold:
15 c = "1;%s" % c
16 return "\033[%sm%s\033[0m" % (c, text)
17 return inner
8c840a83 18
6fa09c14
VNM
19
20def RGB(code):
21 """
22 256 colors supported
23 """
93384849 24 def inner(text):
6fa09c14 25 c = code
6fa09c14
VNM
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')
533edcdb 63
632c6fa5
O
64colors_shuffle = [locals()[i.encode('utf8')] if not i.startswith('RGB_') else RGB(int(i[4:])) for i in c['CYCLE_COLOR']]
65
2a6238f5
O
66background_shuffle = [
67 on_grey,
533edcdb
VNM
68 on_light_red,
69 on_light_green,
70 on_light_yellow,
71 on_light_blue,
72 on_light_magenta,
73 on_light_cyan]
668db21c 74cyc = itertools.cycle(colors_shuffle[1:])
8c840a83
O
75
76
77def order_rainbow(s):
78 """
79 Print a string with ordered color with each character
80 """
2a6238f5
O
81 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
82 return reduce(lambda x, y: x + y, c)
83
8c840a83
O
84
85def random_rainbow(s):
86 """
87 Print a string with random color with each character
88 """
668db21c 89 c = [random.choice(colors_shuffle)(i) for i in s]
2a6238f5
O
90 return reduce(lambda x, y: x + y, c)
91
8c840a83
O
92
93def Memoize(func):
94 """
95 Memoize decorator
96 """
97 cache = {}
2a6238f5 98
8c840a83
O
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
2a6238f5 106
8c840a83
O
107@Memoize
108def cycle_color(s):
109 """
668db21c 110 Cycle the colors_shuffle
8c840a83
O
111 """
112 return next(cyc)(s)
113
2a6238f5 114
42fde775 115def ascii_art(text):
8c840a83
O
116 """
117 Draw the Ascii Art
118 """
42fde775 119 fi = figlet_format(text, font='doom')
8c840a83 120 print('\n'.join(
2a6238f5
O
121 [next(cyc)(i) for i in fi.split('\n')]
122 )
8c840a83 123 )