The colorful petals and sepals of flowers are there to attract pollinators like insects, birds and bats. They need pollinators in order to reproduce. Flowers are bright in color for a reason. This also has a scientific explanation. Brightly colored flowers are genetically created as such since they are meant to attract the birds and the bees. When bees and other pollinators see the color of the flower, this would be a go signal for them that there is nectar to gather. Pollinators have in them color vision that allows them to look for colorful flowers.
The question now is how come flowers differ in color. The reason for this is credited to co-evolution wherein species of insects happen to co-evolve with a certain flower. Various insects see varying wavelength of light or to put it simply different insects see different colors, meaning a certain insect may be able to see the color of one flower, but not the other. Insects are able to see marking and colors which are not visible to the human eye.
There is a process where a flower obtains its color. The color of a flower depends on the pigments obtained from the hereditary genome of the plant. Just like other living things a flower get its specific color from its parent. The colors red, pink, blue and purple that some flowers have are chiefly from the pigments called anthocyanins. These belong to the class of chemicals coined as flavanoids.
Humans can actually tinker what color a flower will have. But it takes an intricate procedure. A new color can emerge from extracting a specific amount of one pigment and placing it in another.
Every color and every hue that flowers have aren’t created by chance. It represents their kind.