Living on the Edge

Colorize Filter

Posted on February 21, 2008

I had a need to colorize the nicks for the new DjangoBot Logger. Instead of managing a collection of names and corresponding colors we decided it would be more simple to just “hash” the nickname using a colorize filter. This causes the same nickname to always appear in the color.


from django import template
register = template.Library()

def do_colorize(value):
    if not value:
        return ''

    result = sum([ord(c) for c in value])
    return "#%X" % result

register.filter('colorize', do_colorize)

If this was a critical implementation I would probably try to limit the range to make sure we don’t go too white on white. Not likely you’ll have a use for this, but if so, have at it.

One final thing, Brian Rosner has done tremendous work on implementing the DjangoBot and the Logger functionality. We plan to enhance the logger as we go, in true iterative fashion.