What is the best way of displaying highlighted text in a text editor?
I know of shading, which simulates what a physical highlighter does:
And 'xor' highlighting:
Are there any other methods? Which method is preferable?
If you can, you should leave it up to the user's operating system settings. OS X does a highlight effect by default, while the linux machine I'm on now (as well as Windows, if I remember right) does a bluish XOR.
As a user, it would definitely look strange to me if I was accustomed to my blue XOR effect and some application displayed a highlight effect (or vice versa). It would throw me off a second. Sure, I could figure out what was going on, but it's a visual anomaly with the rest of the system that could easily be avoided by either not overriding the effect, or inheriting from the system (if you must specify in a lower language).
Always just shade the background and allow the text to be rendered over it.
Rendering the text first and then applying a bitwise XOR highlighted area over the top might appear to work ok superficially, but you cannot guarantee that the results are as effective as leaving the anti-aliasing to the font rendering engine. Consider for example what happens if the anti aliasing does not follow a linear drop off at the edges of the text, but a curved drop-off - by XORing it you invert the curve and end up with something slightly different to how the text would be rendered straight on to the background.
Furthermore an XOR operation might turn black text white, but XORing other colours is going to generate something quite horrible.
So, no - it's not better to XOR - just use a standard background shading of suitable colour and contrast. If wish to actually invert the text colour then do that - but not by XORing it.
Inverting colors can look bad, depending on colors and also the inversion of contrast of B/W <--> W/B can be uneasy to look at.
Changing foreground only makes it hard to tell color change is due to selection.
Changing background only makes text loose emphasis relative to its background.
That is why I prefer a different approach...
If the possible foreground and background colors are known in advance change them both slightly, e.g. white on black to yellowish-white on dark grey, black on white to medium blue on light grey.
If they are not know in advance then use mathematical function e.g. slightly reduce contrast of all three channels of background and slightly reduce contrast of blue channel of foreground. This approach however won't work if the theme is low contrast.
By reduction of contrast of a single color I mean moving its value closer to the middle of the possible range.