For accessibility, it’s better to do this client side, so clients can adjust it themselves to be more contrasting or whatever.
CSS function doing "here is color A, here is color B, make one that is same hue as color B but contrasting" would be much more useful
Your site's design should just have well-chosen colours.
And there's decent reason to not want to tell the server either.