Colour Evolution

This is a page I made to give an introduction to evolutionary algorithms. These are a type of optimisation algorithm inspired by biological evolution. Here, I've used the same concepts to evolve a population of random colours towards a chosen colour.





As you can see in the demo above, a colour can be defined by red, green and blue values between 0 and 255. This means we have three variables that can change as the colours evolve, similar to genes in biological evolution. I've provided a glossary below to show some terms used in the field and what they mean in this example.

A single colour in the grid
All the colours in the grid (100 individuals)
The red, green and blue values
Mixing the genes of two colours to create a new one
Changing a gene by a random amount
A score measuring how close a colour is to the chosen colour

Here's an overview of the algorithm...

  1. Create a population of random colours
  2. Give each colour a fitness score
  3. Create a new colour using two existing colours
  4. Possibly mutate one of the genes of the new colour
  5. Replace an existing colour with the new one
  6. Go back to step 3

The colour of fitness

In order to give each colour a fitness, a distance from the chosen colour needs to be calculated. If you imagine the Red, Blue and Green values as X, Y and Z coordinates in a graph then the distance between two colours is simply the distance in 3D space. This distance is then translated into a fitness score between 0 and 100 (identical colours score 100, opposite colours score 0).

Live long and prosper

There are two points in the algorithm that add selection pressures to drive an overall change towards the chosen colour:

  1. The chance of choosing an individual for a crossover operation is proportional to its fitness
  2. The chance of choosing an individual to be replaced is proportional to 100 minus its fitness

These selections force the population to evolve. However, there is still a random element that means low scoring colours could be chosen for crossover and high scoring colours could be replaced.