Bestselling Books Sorted By Color

What was I thinking?
Not much and not often. I was bored. But really, I just thought it would be cool to try and sort a list of something by an unusual property, and then the idea of the color of a book cover came to me. Just like that. An idea. There may have also been a trail of smoke leaving my ears at the time, but I wasn't in front of a mirror so I can't be sure.

It doesn't really look sorted
Well, it is. Shut up. Read the next answer.

How does it work?
Not that complicated, but in case you're bored too, I'll tell you. In Perl, every hour it goes to Amazon and reads their top 100 best-selling book data. I previously just parsed the HTML from their Best Sellers page, but that was a pain because Amazon changes their HTML slightly every few weeks (after which my ripper would fail and send me nasty emails until I fixed it). Now I'm using their Web Services, which sends me an XML file that's much easier to parse, but has a slightly different sort order. The sales rank data is updated more often than the Amazon Best Sellers page, and more susceptible to short-term sales fluctuations. Ah well.

Then it goes through each book, using PHP's Image library, grabbing each book's small thumbnail image from Amazon and figuring out its average (mean) color. I use PHP because I couldn't find a library in Perl I liked that worked with both JPEG and GIF. The quickest way I found to do this was make a system call to a PHP script and parse the output. The routine in this script is straight-forward, go through each pixel, add the red, green, and blue color components for each pixel to three running tallies, and divide each result by the total number of pixels. At the suggestion of someone who wrote in, I recently amended this to add the square of each color component. I think this gives a cleaner color distribution.

Thus armed, back in Perl, we go through each book, and sort the books by intensity (actually I just add each book's color components together). The resulting set is then divided into groups: one of 11, six of 13, and another of 11, each group representing a row in the table you saw on the previous page. Then we sort the books in each group using this as the sort value:

$foo = $red * -16 + $green + $blue * 16

It's lame, and has the effect of smearing green books all over the place, but out of all the sorts I tried, this was the best effect. At one point I tried splitting the books into rows of R, RG, G, GB, B, and RB groups (with extra blue and red rows) and then sorting each group by intensity, but that made a very jagged looking page. Part of the problem may be with the average color mechanism I used in PHP. The resulting R/G/B values may not actually represent ANY single color in the image, since it's a mean value and not a median. What I really need to try is something that arranges the colors into similar-color clusters, and picks the median color of the most populous cluster. Perhaps it should take adjacency into effect, so that if two colors are similar AND contiguous, that color has more weight in the end selection.

And then we go through each book in sorted order and write out HTML.

I think you're a twit.
Stipulated. Click on a book so I can afford some medical help.

To whom should I address my flame email?
Send it to Eric W. Lund. Actually if you have thoughts on any better way to arrange random images into a color spectrum, I'd love to hear it.

Back to Egotron home page.
Back to Book Palette.