Could black-painted gunboats operating at night deter or attack submarines near the coast?
Short answerYes; that was the purpose of Motor Anti-Submarine Boats, which carried ASDIC and depth charges for coastal submarine hunting.
NotesThe exchange moves beyond paint and spotlights to the equipment that turns a patrol boat into an anti-submarine platform. The Motor Anti-Submarine Boat variant used ASDIC for detection and carried depth charges. This specialized coastal ASW mission grew from the same fast motorboat family as the gunboats.