How can one explain the fact that some bacteria are green or purple in color, while most of them are colorless?

These bacteria contain a green or purple pigment.

Bacteria are unique organisms. Most of them do not have color, which is due to their living environment and nutritional characteristics.
As you know, bacteria differ from almost all other life forms in their cellular structure. They lack a nucleus in the cell, which indicates their antiquity and primitiveness compared to other organisms. Most of them live in the aquatic environment. It can be such large reservoirs as seas, oceans, lakes, or even a liquid medium inside other organisms.
Bacteria feed most often in a heterotrophic way, i.e. can absorb only existing organic substances. If bacteria adhere only to a heterotrophic type of nutrition, then they do not have color, since they do not need coloring pigments in this case. Which bacteria live inside various animals are indispensable because it is involved in the digestion and crushing of complex substances. Without them, animals, including humans, could not absorb food.
However, there is another type of bacteria that can consume both ready-made organics and photosynthesis. The color of these bacteria in green under normal living conditions is green, and they are combined into a group of cyanobacteria.
Cyanobacteria live in the seas and oceans, although they are also found in small bodies of water, and sometimes on the surface of moist soil. They have chloroplasts and can perform photosynthesis by releasing oxygen into the atmosphere.

Remember: The process of learning a person lasts a lifetime. The value of the same knowledge for different people may be different, it is determined by their individual characteristics and needs. Therefore, knowledge is always needed at any age and position.