Cooking is a form of chemistry - so there must be a scientific reason behind this.
Why do all crustaceans turn pink or red when cooked?
Crustaceans like shrimp, lobsters, crabs and crayfish have a pigment called astaxanthin in their shells.
Astaxanthin belongs to the terpines class of chemicals of which the carotenoid ¹ class is a subdivision and, in a marine environment, gets produced by an algae that is subsequently consumed by crustaceans (and other animals like salmon, red trout, red sea bream and flamingos ² )
As Astaxanthin absorbs blue light, it will appear as its opposing additive colour: a deep red. The more this deep red is diluted, it will subsequently become red, orange or yellow in colour.
While the crustaceans are alive, astaxanthin lies wrapped in the tight embrace of a protein called crustacyanin. The protein holds the pigment so tight, in fact, that it’s flattened and its light-absorption properties are changed. The astaxanthin-crustacyanin complex then winds up giving off a blue-green color. ³
This can be observed if you have aggressive live lobsters you want to cook: just put them in the sink full of water with a glass of white wine added for a few minutes and they will
get drunk instantly as they've never had alcohol in their lives relax and the blue colouring can then be clearly seen at the fronds of their carapace.
The astaxanthin-crustacyanin complex gets:
separated when a crab or lobster is cooked. Crustacyanin is not heat-stable, so introducing it to a boiling pot of water or a grill causes it to relax its bonds with astaxanthin, unravel and let the pigment’s true bold red color shine through. ³
Note ¹: Carrots have given carotenoid its name
Note ²: Eating minuscule shrimp containing this carotenoid is what turns flamingos pink: pink flamingos will be more well-fed than pale flamingos...
Note ³: Sourced here
This is most probably due to the occurrence of a specific carotenoid (Astaxanthin) in their body. This carotenoid (like many others) is
susceptible to enzymatic or nonenzymatic oxidation, which depends on the carotenoid structure, the oxygen availability, enzymes, metals, prooxidants and antioxidants, high temperature, and light exposure