This is Andromeda Stories (1982). It's a feature-length Japanese anime based on a manga of the same name.
From Anime-Planet:
When Prince Ithaca of Cosmoralia and Princess Lilia of Ayodoya are married, it seems as though a great new age is about to dawn on the planet of Astria. Soon, however, the planet is invaded by a mysterious force of machines that seem to be determined to destroy humanity. Astria’s (and maybe humanity’s) only hope lies with the royal couple’s twin children, Jimsa and Affle, and with a number of individuals not native to Astria that they encounter after their respective escapes from the capital – a cyborg warrior named Il, a pair of robots named Arc and Beth and the enigmatic genius known as The Elder.
The story is set within the Cosmoralian Empire on the planet Astria in the Andromeda Galaxy.
Mysterious machines arrive on the planet and begin surreptitiously taking people over in the capital city of Aranchan, with King Astralta (pictured below) and his ministers being among the first victims. It's more than just mind-control; robot spiders burrow through people's foreheads and turn them into machines internally. The victims still look human externally, albeit with marks on their foreheads where the robot spiders entered.
Andromeda Stories (1982)
Two robot spiders were sent to take over Queen Lilia (who was in labour) and her nurse, Tarama, but the robots were destroyed by the psychic powers of Lilia's unborn twin son and daughter, who're later given the names Jimsa and Affle. Twins are considered a bad omen in Cosmoralia, so when they're born, Tarama hides Affle from Lilia and presents Jimsa to her as if he were her only child. Affle is subsequently spirited out of the city, disguised as a boy and left in the care of a rural clan. The twins grow up apart, knowing nothing about one another, and are later reunited as teenagers.
The twins are the main protagonists of the story, but another important character is Il, a sword-wielding cyborg or gynoid who looks like a normal human woman externally and dresses like a samurai. She acts as a guardian to Affle but also helps Lilia and Jimsa escape Aranchan when the machines become more overt in their takeover. Near the end of the film, she sacrifices herself by detonating an explosive device within her torso and destroying a large number of the machines.
As shown in the image below, Il has white hair, Jimsa has red hair and Affle has green hair (but wears a brown wig for much of the film).
Andromeda Stories (1982)