add 256 RGB supported
[rainbowstream.git] / rainbowstream / colors.py
CommitLineData
2a6238f5
O
1import random
2import itertools
8c840a83 3from functools import wraps
8c840a83
O
4from pyfiglet import figlet_format
5
6fa09c14
VNM
6
7def basic_color(code):
8 """
9 16 colors supported
10 """
11 def inner(text, bold=True):
533edcdb
VNM
12 c = code
13 if bold:
14 c = "1;%s" % c
15 return "\033[%sm%s\033[0m" % (c, text)
16 return inner
8c840a83 17
6fa09c14
VNM
18
19def RGB(code):
20 """
21 256 colors supported
22 """
23 def inner(text, bold=True):
24 c = code
25 if bold:
26 c = "1;%s" % c
27 return "\033[38;5;%sm%s\033[0m" % (c, text)
28 return inner
29
30
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')
47
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')
61on_light_magenta = basic_color('105')
62on_light_cyan = basic_color('106')
63on_white = basic_color('107')
533edcdb
VNM
64
65colors_shuffle = [
66 grey,
67 light_red,
68 light_green,
69 light_yellow,
70 light_blue,
71 light_magenta,
72 light_cyan]
2a6238f5
O
73background_shuffle = [
74 on_grey,
533edcdb
VNM
75 on_light_red,
76 on_light_green,
77 on_light_yellow,
78 on_light_blue,
79 on_light_magenta,
80 on_light_cyan]
668db21c 81cyc = itertools.cycle(colors_shuffle[1:])
8c840a83
O
82
83
84def order_rainbow(s):
85 """
86 Print a string with ordered color with each character
87 """
2a6238f5
O
88 c = [colors_shuffle[i % 7](s[i]) for i in xrange(len(s))]
89 return reduce(lambda x, y: x + y, c)
90
8c840a83
O
91
92def random_rainbow(s):
93 """
94 Print a string with random color with each character
95 """
668db21c 96 c = [random.choice(colors_shuffle)(i) for i in s]
2a6238f5
O
97 return reduce(lambda x, y: x + y, c)
98
8c840a83
O
99
100def Memoize(func):
101 """
102 Memoize decorator
103 """
104 cache = {}
2a6238f5 105
8c840a83
O
106 @wraps(func)
107 def wrapper(*args):
108 if args not in cache:
109 cache[args] = func(*args)
110 return cache[args]
111 return wrapper
112
2a6238f5 113
8c840a83
O
114@Memoize
115def cycle_color(s):
116 """
668db21c 117 Cycle the colors_shuffle
8c840a83
O
118 """
119 return next(cyc)(s)
120
2a6238f5 121
42fde775 122def ascii_art(text):
8c840a83
O
123 """
124 Draw the Ascii Art
125 """
42fde775 126 fi = figlet_format(text, font='doom')
8c840a83 127 print('\n'.join(
2a6238f5
O
128 [next(cyc)(i) for i in fi.split('\n')]
129 )
8c840a83 130 )