title: Given a gray value, what color provides highest visual contrast?
body: <p>Assume that I've a gray image, and I want to draw e.g. text on it. Now the image has some dark and some bright regions. So if I choose for every character a separate color, in what way do I compute such a color to gain highest contrast of the text?</p>
  
  <p>A pragmatic approach is to use yellow. (I don't know why, but its often used for subtitles in movies and documentaries)
  Furthermore I could darken the yellow in regions of bright background, and highlight it in regions of dark background. But this may provide some layer-effects.</p>
  
  <p>I know that the color space may be important. I start with an RGB gray value, but LAB, HSV, or HSL may be better suited to compute the <em>optimal</em> color.</p>
  
  <p><strong>EDIT:</strong>
  As there was the question for a useful use-case: I really do not want to paint letters of text in different color. It is about color choosing for particular glyphs on gray textured background. (E.g. an MR image.)</p>