:set t_Co?
t_Co=256
:set background?
background=light
:set background=dark
:set background?
background=dark
:hi Normal ctermfg=grey ctermbg=234
:set background?
background=light
:hi Normal ctermfg=grey ctermbg=0
:set background?
background=dark
as you can see, the "hi" command changes background to light when ctermbg is set to 234, however, according to http://upload.wikimedia.org/wikipedia/commons/9/95/Xterm_color_chart.png color 234 is indeed a dark grey
thanks :) hope it'll get included sooner or later.
according to the source snippet posted by Christian, it seems that when "i" is true then the background is set to dark, otherwise to "light", so my script prints 1 for dark colors and 0 for light ones. I don't work with c, so I'm not sure if an array of integers is really the best option for a quick look up depending on the color index, but anyway, modifying my script shouldn't be too difficult :p
I made this just for those 256 term colors, looking at that snippet of vim source. I've no idea what happens when the gui is in use...
BTW I've to say I'm totally ignorant about luma, luminance, gamma correction etc, so i basically took a formula from someone who seemed to know what he was talking about and used it...
here's the output and the script attached. I hope I made something useful :)
1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
the formula I choosen should calculate the "perceptual" brightness, i.e. it should take into account human perception and also sRGB color space. As I said i'm no expert in this field, but after documenting a bit and doing a couple of tests I think it should give the expected result.
manual tests might be a good idea, but I think we might fall into personal opinions, with people wondering if the tests where made under daylight or in some dark room, with monitor correctly calibrated or with brightness and contrast settings completely blown up...
ok i'm exagerating a bit here, but I already wondered how really should be defined the point when a color is no longer "dark".
if someone wants to manually test the text/background combinations (and what about grey text instead of just black and white?) I suggest just to test some edge cases and then adjust that "above 50%" you suggested earlier. but maybe discussing the formula might be a better idea than doing the manual test.
for the first 16 colors, well, those might even be customized by the user to something completely different, but I've no idea if it is possible to read rgb values runtime on all shells / oses vim run on. but in the best case vim might calculate brigthness runtime for those 16 colors and lookup the pre-calculated, remaining ones.