TL;DW: This is “anti-fouling” that repels barnacles, algae, and mollusks by emitting “biocides”. This is desirable because they cause drag (think fuel) and structural damage. Red because traditionally some oxide of Copper.
In nature bright colours serve as a warning and to stay away. Ships are very territorial and have even been known to attack people.