Now that I'm doing it in the right order, it turns out the built-in function for picking a legible foreground color works just dandy. Pulled my custom function back out and using that.
Humans can't see blue very well.