-
Notifications
You must be signed in to change notification settings - Fork 1
/
colors.py
113 lines (105 loc) · 3.6 KB
/
colors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'''
A utility to aid bot plugins.
Accepts strings and a color code, returns a string formatted with the
requested IRC color codes.
Recognized 'default' colors:
00 White
01 Black
02 Dark blue
03 Green
04 Red
05 Dark Red
06 Purple
07 Orange
08 Yellow
09 Light Green
10 Teal
11 Cyan
12 Blue
13 Magenta
14 Dark Grey
15 Light Grey
'''
from random import choice
from types import *
RESET = u"\x0f"
COLORS = {
u"white": u"00", u"0": u"00", u"00": u"00",
u"black": u"01", u"1": u"01", u"01": u"01",
u"dark blue": u"02", u"navy": u"02", u"2": u"02", u"02": u"02",
u"green": u"03", u"3": u"03", u"03": u"03",
u"red": u"04", u"4": u"04", u"04": u"04",
u"dark red": u"05", u"brown": u"05", u"maroon": u"05",
u"5": u"05", u"05": u"05",
u"purple": u"06", u"violet": u"06", u"6": u"06", u"06": u"06",
u"orange": u"07", u"olive": u"07", u"7": u"07", u"07": u"07",
u"yellow": u"08", u"8": u"08", u"08": u"08",
u"light green": u"09", u"lime": u"09", u"9": u"09", u"09": u"09",
u"teal": u"10", u"blue cyan": u"10", u"10": u"10",
u"cyan": u"11", u"aqua": u"11", u"11": u"11",
u"blue": u"12", u"light blue": u"12", u"royal blue": u"12", u"12": u"12",
u"magenta": u"13", u"pink": u"13", u"light red": u"13", u"fuchsia": u"13",
u"13": u"13",
u"dark grey": u"14", u"dark gray": u"14", u"grey": u"14", u"gray": u"14",
u"14": u"14",
u"light grey": u"15", u"light gray": u"15", u"silver": u"15", u"15": u"15"
}
STYLES = {
u"i": "\x16", u"italic": "\x16",
u"u": "\x1F", u"underline": "\x1F",
u"b": "\x02", u"bold": "\x02"
}
def colorize(text, colors=[], styles=[]):
assert isinstance(text, basestring), u"No string provided."
assert text, u"Text is empty."
assert type(colors) is ListType, u"Colors must be in a list."
assert type(styles) is ListType, u"Styles must be in a list."
assert len(colors) < 3, u"Too many colors."
assert len(styles) < 4, u"Too many styles."
print type(text)
#text = text.encode('utf-8', 'replace')
if colors or styles:
message = text
if len(colors) == 1:
try:
message = u'\x03%s%s%s' % (COLORS[colors[0].lower()],
message,
RESET
)
except KeyError:
raise KeyError(u'Color "%s" is invalid.' % colors[0])
elif len(colors) == 2:
try:
message = u'\x03%s,%s%s\x0f' % (
COLORS[colors[0].lower()],
COLORS[colors[1].lower()],
message
)
except KeyError:
raise KeyError(u'Color pair "%s, %s" is invalid.' % (
colors[0],
colors[1]
))
if styles:
for style in styles:
try:
message = u'%s%s\x0f' % (STYLES[style.lower()], message)
except KeyError:
raise KeyError(u'Style "%s" is invalid.' % style)
return message
else:
return text
def rainbow(text):
assert isinstance(text, basestring), u"No string provided."
assert text, u"Text is empty."
rainbow = [u'black', u'red', u'navy', u'green', u'purple', u'pink']
message = u''
for c in text:
message = u'%s%s' % (
message,
colorize(c, [rainbow[choice(range(len(rainbow)))]])
)
message = u'%s%s' % (message, RESET)
return message
if __name__ == "__main__":
print __doc__.strip()