I was looking through my photos and decided I wanted to make an algorithm to automatically create a palette of colours based on a photo. At some point the concept changed to creating a gradient swatch based on the most common colours within the image. This article goes through the steps I took to go from image to gradient. On this journey I sample colours from the image, convert them to a colour space that addresses perceived lightness, and write an ant colony optimization algorithm to find an optimal path through those colours using a distance calculation based on human perception. The end result being an algorithm that can take an image and provide a pleasing gradient using colours within the image.

The following is a Jupyter notebook I wrote to follow the steps.

Results

I'm pleased with the final results that come out of this algorithm. And it was interesting to learn about the colour spaces, distance functions, and write an Ant Colony Optimization algorithm. I am beginning to be curious about the performance difference between using CPython with Numpy, PyPy with pure Python modules, or re-writing this in Rust.