I like to make retro 8-bit art out of Perler Beads. One challenging part of this is sometimes figuring out exactly which color to use. Sometimes it can be challenging to know whether Pikachu is made from Cheddar, Yellow, Butterscotch, or a different color, for example. (in case you’re interested, it’s Cheddar)
I wanted to write a script to do this fanciness for me. I needed it to do a few different things:
- Analyze an image and create an HTML grid, with a legend, of each pixel of the image
- Determine the closest possible color match for each color, since there are only a limited number of Perler colors available
- Calculate how many of each bead is necessary
Doing #1 was pretty easy – there are existing libraries that will analyze an image, pixel by pixel, and retrieve the RGB values for each one.
The second item was unexpectedly challenging. It turns out that color distance is quite mathematically complicated and not merely an issue of linear distance. There are some very elaborate formulas that smarter folks than I have figured out. The process is essentially taking the RGB values, converting them to L*a*b* format, then using converting that into L*C*h (Light, Chroma, hue, I think?), and then doing an algebraic distance calculation on that.
Fortunately the formula is publicly shared on Wikipedia (link above) and I was able to model it in Ruby. I even managed to get my contribution accepted into the Ruby Color Gem!
The distance calculation works pretty well and is pretty darn accurate. It makes some suggestions that seem strange at first, but when it’s considered overall in the total context of the image, it looks right. It’s all contingent on having the correct RGB values for the Perler palette, though, which I have yet to nail down, exactly.
I am working on turning the script into a website and once I do I will probably make the code open source, and will link it here. For now, enjoy images of the display!