Racism seems to have a lot to do with the need of modern European nation states to justify colonialism on moral grounds. This is a justification that premodern peoples, innocent of the peculiar notion of human rights, did not require. In the premodern world, conquest and subjugation were outcomes ordained by the gods.
It was humanism, and particularly secular humanism, that laid the basis for racism. The bestowal of rights on humans as a species forbids one group from subjugating another. In practice, this meant that technologically advanced European nations could only take advantage of their military superiority and acquire colonies on the presumption that the peoples they subjugated were not quite human and therefore not entitled to the same rights as Europeans, at least not until these putative primitives were properly civilized.
Today’s sanctimonious, antiracist “identity” politics remains locked in the same humanist frame, only with the terms inverted. Now the subhuman stigma is attached to “whites” in general and white males in particular. Thus, in its late form, humanism develops into a suicidal ideology. Perhaps this is the only way that its pernicious influence can be overcome.