The other week, my good friend and writer extraordinaire, Andrew, emailed me. He wanted to write a small demo in Flash, part of which would involve comparing colours against each other. You can find the finished article here and he’s done an excellent job with his demonstration. 

I found the colour matching aspect of what he was attempting quite an interesting little problem. What is the best way of finding the nearest match from a group of colours using Actionscript?

It’d be fair to say we tried a few methods, but the demo below is pretty much a fancier version of what was used in Andrew’s ‘Bug Evolver’ and it seems to work pretty well.

I’ll explain how that works on the next page, but I’d read elsewhere on the ‘net that using the Hue, Saturation and Lightness (HSL) instead of RGB colours provides better matching. Now perhaps it’s my implementation, but my demo above suggests that using RGB gives better results, as the HSL method struggles to find the best match with the very lightest and the very darkest colours.

You can switch between each method by selecting ‘Use RGB’ or ‘Use HSL’, then re-clicking on the colour spectrum (the up and down arrow keys saves having to move the mouse between clicks).

It certainly looks pretty accurate to me, but let me know if you can suggest a better method of working out the nearest colour. The important parts of the code I’ve used are on the next page…

Enjoy.

Pages: 1 2

Comments on: "Nearest Colour Matching with Flash" (2)

  1. I am sure I said right from the beginning that what we needed to do was find the square root of the combined integer variables after cubing the Euclidean Distance. But would you listen, Noooooooooooooo.

    Great idea writing this up, now other people in the future won’t have to spend weeks on end trying to find an answer on the internet only to have it handed to them in ten seconds by someone they work with. This way is much easier. Good job and awesome little program.

    I hope you don’t mind but I have copied the link to this page onto that actionscript forum. I think it is worth sharing with the world.

    • Thanks dude, and no I don’t mind at all. Lab colour looks like it’s another method that could be used, but the maths for that looks a bit complicated to me. There are some formulas for converting to and from that on EasyRGB though, which would take some of the pain away.

Leave a comment for: "Nearest Colour Matching with Flash"

Blogalism - Movie reviews and general geekiness.