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