An anonymous commenter suggested I use CIE L*a*b* instead of HSV. I’ll let the results speak for themselves:

color_optimization_lab.png

And that’s without any tweaks, fancy distance metrics, or pushing outside the region when the vector is too short. Whoever you were, thanks for the suggestion! Also, thanks to Madeleine Ball, whose suggestion I misunderstood, but is basically the same.

Update: with some vector extension code (slightly more contrast when colors are really close). The code is in xchat-gnome SVN for everyone to play with now.
77-225.77.png