This question already has an answer here:
I understand that the function of melanin is to protect individuals from sunlight, and that people living in sun exposed areas are darker due to increased production of melanin. But why is the melanin pigment black in color? Wouldn't it have been more useful if melanin was white?
From my understanding, black absorbs more sunlight than white, so if melanin is black, it will tend to absorb more heat (which isn't a good thing in areas with a lot of sun exposure). This will lead to production of more melanin (tanning).
Instead, evolutionarily wouldn't it have made more sense if melanin were white, since not only would the pigment protect the person from sun exposure, but also prevent more sunlight from being absorbed?