import math, re, string def wave(i): return math.cos(i / 3 * 2 * math.pi) / 2 def hsb(h, s=1.0, b=0.5): return [s * wave(float(h) - n) + b for n in range(3)] def colorizeWord(word): maxRange = float(2 ** 30) h = (hash(word.title()) % maxRange) / maxRange * 3 s = 0.7 return tuple(int(c * 255) for c in hsb(h, s)) NICKNAME_CHARS = re.escape(string.lowercase + string.uppercase + string.digits + '_-[]\\`^{}') nicknamesPattern = re.compile('([' + NICKNAME_CHARS + ']+)') def colorizeLine(f, line, highlights): for word in nicknamesPattern.split(line): if word in highlights: yield f(word, colorizeWord(word)) else: yield word yield '\n'