I wanted an algorithm to generate a color based on the extension of a filename (for synthesizing icons for arbitrary files.) After looking around and failing to find anything suitably interesting, I hacked up a solution that I think works pretty well.
It works like this:
That's pretty much it. It works well, it's simple, and it produces a reasonable variety of colors.
If you're so inclined you can play with the constants and color ordering to get something that produces "good" colors for certain extensions (blue for Microsoft Word extensions, for example). Obviously given the limited range, you're not gonna wind up with the best colors for everything, and there will be some similarity for otherwise-different extensions, but if your goal is just to get some good variety without hard-coding a bunch of values (or without using an icon set) this'll do it.
I also wanted a good way to isolate the file extension and -- if it's too long for my purposes -- cut it down into a reasonable-looking 3-4 character one. (So ".foobarbaz" would get cut down to something like "fbz".) I've left that in the module too, so feel free to use it if you need to.
Public domain. If your country doesn't acknowledge the concept of "public domain", use Creative Commons Zero.