Yes, convert the RGB to HSL (Hue, Saturation, and Luminance) and then look at the L value -- if it's above a certain threshold (i.e. bright), use a dark color, else use a light one.

I don't have the conversion handy, but a quick google search will find you what you need.

rjf&