Two ways
Untitled Trigger 006
Events
Unit - A unit Is attacked
Conditions
(Unit-type of (Attacked unit)) Equal to Castle
(Life of (Attacked unit)) Less than [value, not something like 25%]
Actions
Unit - Change ownership of (Attacked unit) to (Owner of (Attacking unit)) and Change color
Probably runs too often, but easier
2. way
Untitled Trigger 007
Events
Unit - Castle's life becomes Less than [specific value, not something like 25%]
Conditions
Actions
With this one I'm not quite sure how to refer to the attacker