Hello,
A nice question to ask. A well flower gets their color due to various pigments and chemical present in them. The red, blue or yellow all these colors in flowers are due to presence of anthocyanins, which are special pigment in flowers. Due to various reactions the color of these pigments changes. Chlorophyll is a chemical which provide the green color in leaves and other green parts. It is mainly responsible for providing the green color.
The main purpose of the color is attracting the insects, so that the process of pollination can occur. The process of pollination is responsible for reproduction in plants.
For fruits and flowsers, the substances (pigments) responsible for color are-
1. Chlorophylls
2. Carotenoids.
Chlorophylls provide green color to plants as in green leafs . An electron excitation reaction in it do the job.
Similarly, carotenoids provide red/yellow to fruits as in carrots, mangoes, etc.