Its all to do with pigments that accumulate in the growing vegetable.
Sometimes they are purely decorative having been selected by humans, other times they have a particular purpose like a flavouring to stop it being eaten. Quite a few vegetables have a bitter taste which is designed to stop animals eating them.
Comments