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